blob: e710b431049a50c33dca8031b0862e1a0ac92f90 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
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
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawskydc39fff2013-10-18 12:32:07 -070034/**
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
Damien Lespiauda2078c2013-02-13 15:27:27 +000055static void gen9_init_clock_gating(struct drm_device *dev)
56{
Damien Lespiauacd5c342014-03-26 16:55:46 +000057 struct drm_i915_private *dev_priv = dev->dev_private;
58
Damien Lespiau77719d22015-02-09 19:33:13 +000059 /* WaEnableLbsSlaRetryTimerDecrement:skl */
60 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
61 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
62}
Damien Lespiau91e41d12014-03-26 17:42:50 +000063
Damien Lespiau45db2192015-02-09 19:33:09 +000064static void skl_init_clock_gating(struct drm_device *dev)
Damien Lespiauda2078c2013-02-13 15:27:27 +000065{
Damien Lespiauacd5c342014-03-26 16:55:46 +000066 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau3ca5da42014-03-26 18:18:01 +000067
Damien Lespiau77719d22015-02-09 19:33:13 +000068 gen9_init_clock_gating(dev);
69
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000070 if (INTEL_REVID(dev) == SKL_REVID_A0) {
71 /*
72 * WaDisableSDEUnitClockGating:skl
Damien Lespiau9253c2e2015-02-09 19:33:10 +000073 * WaSetGAPSunitClckGateDisable:skl
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000074 */
75 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Damien Lespiau9253c2e2015-02-09 19:33:10 +000076 GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000077 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
78 }
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000079
Damien Lespiau2caa3b22015-02-09 19:33:20 +000080 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
Damien Lespiau81e231a2015-02-09 19:33:19 +000081 /* WaDisableHDCInvalidation:skl */
82 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
83 BDW_DISABLE_HDC_INVALIDATION);
84
Damien Lespiau2caa3b22015-02-09 19:33:20 +000085 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
86 I915_WRITE(FF_SLICE_CS_CHICKEN2,
87 I915_READ(FF_SLICE_CS_CHICKEN2) |
88 GEN9_TSG_BARRIER_ACK_DISABLE);
89 }
Damien Lespiau81e231a2015-02-09 19:33:19 +000090
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000091 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);
Damien Lespiauda2078c2013-02-13 15:27:27 +000095}
96
Daniel Vetterc921aba2012-04-26 23:28:17 +020097static void i915_pineview_get_mem_freq(struct drm_device *dev)
98{
Jani Nikula50227e12014-03-31 14:27:21 +030099 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200100 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{
Jani Nikula50227e12014-03-31 14:27:21 +0300138 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200139 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
Daniel Vetter20e4d402012-08-08 23:35:39 +0200164 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200165
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) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200196 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200197 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200198 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200199 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200200 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200201 }
202}
203
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300204static 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
Daniel Vetter63c62272012-04-21 23:17:55 +0200242static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300243 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
Imre Deak5209b1f2014-07-01 12:36:17 +0300266void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300267{
Imre Deak5209b1f2014-07-01 12:36:17 +0300268 struct drm_device *dev = dev_priv->dev;
269 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300270
Imre Deak5209b1f2014-07-01 12:36:17 +0300271 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 }
290
291 DRM_DEBUG_KMS("memory self-refresh is %s\n",
292 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300293}
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 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100309static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300310
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300311static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300312{
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
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200327static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300328{
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
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300344static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300345{
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
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300360/* Pineview has different values for various configs */
361static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300362 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367};
368static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300369 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300374};
375static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300376 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300381};
382static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300383 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300388};
389static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300390 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300395};
396static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300397 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300402};
403static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300404 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300409};
410static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300411 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300416};
417static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300418 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300423};
424static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300425 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300430};
431static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300432 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300437};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300438static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300439 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300444};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300445static 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};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200452static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300453 .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,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300458};
459
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300460/**
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;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300507
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
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300518 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
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100525 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000526 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300527 if (enabled)
528 return NULL;
529 enabled = crtc;
530 }
531 }
532
533 return enabled;
534}
535
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300536static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300537{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300538 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539 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");
Imre Deak5209b1f2014-07-01 12:36:17 +0300549 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300550 return;
551 }
552
553 crtc = single_enabled_crtc(dev);
554 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100555 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800556 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100557 int clock;
558
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200559 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100560 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561
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
Imre Deak5209b1f2014-07-01 12:36:17 +0300600 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300602 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300603 }
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;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300616 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300617 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);
Chris Wilson3490ea52013-01-07 10:11:40 +0000622 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623 *cursor_wm = cursor->guard_size;
624 *plane_wm = display->guard_size;
625 return false;
626 }
627
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200628 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100629 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800630 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200631 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800632 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300633
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 */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200645 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300646 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800647 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300648 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;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300702 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300703 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);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200715 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100716 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800717 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200718 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800719 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300720
Ville Syrjälä922044c2014-02-14 14:18:57 +0200721 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 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 */
Matt Roper3dd512f2015-02-27 10:12:00 -0800733 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300734 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
Gajanan Bhat0948c262014-08-07 01:58:24 +0530742static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
743 int pixel_size,
744 int *prec_mult,
745 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700747 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300748 int entries;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200749 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300750
Gajanan Bhat0948c262014-08-07 01:58:24 +0530751 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752 return false;
753
Gajanan Bhat0948c262014-08-07 01:58:24 +0530754 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
755 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300756
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530757 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700758 if (IS_CHERRYVIEW(dev))
759 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
760 DRAIN_LATENCY_PRECISION_16;
761 else
762 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
763 DRAIN_LATENCY_PRECISION_32;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530764 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300765
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530766 if (*drain_latency > DRAIN_LATENCY_MASK)
767 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300768
769 return true;
770}
771
772/*
773 * Update drain latency registers of memory arbiter
774 *
775 * Valleyview SoC has a new memory arbiter and needs drain latency registers
776 * to be programmed. Each plane has a drain latency multiplier and a drain
777 * latency value.
778 */
779
Gajanan Bhat41aad812014-07-16 18:24:03 +0530780static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300781{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700782 struct drm_device *dev = crtc->dev;
783 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
785 int pixel_size;
786 int drain_latency;
787 enum pipe pipe = intel_crtc->pipe;
788 int plane_prec, prec_mult, plane_dl;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700789 const int high_precision = IS_CHERRYVIEW(dev) ?
790 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300791
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700792 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
793 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
Gajanan Bhat0948c262014-08-07 01:58:24 +0530794 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300795
Gajanan Bhat0948c262014-08-07 01:58:24 +0530796 if (!intel_crtc_active(crtc)) {
797 I915_WRITE(VLV_DDL(pipe), plane_dl);
798 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300799 }
800
Gajanan Bhat0948c262014-08-07 01:58:24 +0530801 /* Primary plane Drain Latency */
Matt Roper59bea882015-02-27 10:12:01 -0800802 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8; /* BPP */
Gajanan Bhat0948c262014-08-07 01:58:24 +0530803 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700804 plane_prec = (prec_mult == high_precision) ?
805 DDL_PLANE_PRECISION_HIGH :
806 DDL_PLANE_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530807 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300808 }
Gajanan Bhat0948c262014-08-07 01:58:24 +0530809
810 /* Cursor Drain Latency
811 * BPP is always 4 for cursor
812 */
813 pixel_size = 4;
814
815 /* Program cursor DL only if it is enabled */
816 if (intel_crtc->cursor_base &&
817 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700818 plane_prec = (prec_mult == high_precision) ?
819 DDL_CURSOR_PRECISION_HIGH :
820 DDL_CURSOR_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530821 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
822 }
823
824 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300825}
826
827#define single_plane_enabled(mask) is_power_of_2(mask)
828
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300829static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300830{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300831 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300832 static const int sr_latency_ns = 12000;
833 struct drm_i915_private *dev_priv = dev->dev_private;
834 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
835 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000836 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300837 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300838 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839
Gajanan Bhat41aad812014-07-16 18:24:03 +0530840 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300841
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200842 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100843 &valleyview_wm_info, pessimal_latency_ns,
844 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200846 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300847
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200848 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100849 &valleyview_wm_info, pessimal_latency_ns,
850 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300851 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200852 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300853
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300854 if (single_plane_enabled(enabled) &&
855 g4x_compute_srwm(dev, ffs(enabled) - 1,
856 sr_latency_ns,
857 &valleyview_wm_info,
858 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000859 &plane_sr, &ignore_cursor_sr) &&
860 g4x_compute_srwm(dev, ffs(enabled) - 1,
861 2*sr_latency_ns,
862 &valleyview_wm_info,
863 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +0000864 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +0300865 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +0000866 } else {
Imre Deak98584252014-06-13 14:54:20 +0300867 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300868 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +0000869 plane_sr = cursor_sr = 0;
870 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300871
Ville Syrjäläa5043452014-06-28 02:04:18 +0300872 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
873 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300874 planea_wm, cursora_wm,
875 planeb_wm, cursorb_wm,
876 plane_sr, cursor_sr);
877
878 I915_WRITE(DSPFW1,
879 (plane_sr << DSPFW_SR_SHIFT) |
880 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
881 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +0300882 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300883 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +0000884 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885 (cursora_wm << DSPFW_CURSORA_SHIFT));
886 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +0000887 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
888 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +0300889
890 if (cxsr_enabled)
891 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892}
893
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300894static void cherryview_update_wm(struct drm_crtc *crtc)
895{
896 struct drm_device *dev = crtc->dev;
897 static const int sr_latency_ns = 12000;
898 struct drm_i915_private *dev_priv = dev->dev_private;
899 int planea_wm, planeb_wm, planec_wm;
900 int cursora_wm, cursorb_wm, cursorc_wm;
901 int plane_sr, cursor_sr;
902 int ignore_plane_sr, ignore_cursor_sr;
903 unsigned int enabled = 0;
904 bool cxsr_enabled;
905
906 vlv_update_drain_latency(crtc);
907
908 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100909 &valleyview_wm_info, pessimal_latency_ns,
910 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300911 &planea_wm, &cursora_wm))
912 enabled |= 1 << PIPE_A;
913
914 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100915 &valleyview_wm_info, pessimal_latency_ns,
916 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300917 &planeb_wm, &cursorb_wm))
918 enabled |= 1 << PIPE_B;
919
920 if (g4x_compute_wm0(dev, PIPE_C,
Chris Wilson5aef6002014-09-03 11:56:07 +0100921 &valleyview_wm_info, pessimal_latency_ns,
922 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300923 &planec_wm, &cursorc_wm))
924 enabled |= 1 << PIPE_C;
925
926 if (single_plane_enabled(enabled) &&
927 g4x_compute_srwm(dev, ffs(enabled) - 1,
928 sr_latency_ns,
929 &valleyview_wm_info,
930 &valleyview_cursor_wm_info,
931 &plane_sr, &ignore_cursor_sr) &&
932 g4x_compute_srwm(dev, ffs(enabled) - 1,
933 2*sr_latency_ns,
934 &valleyview_wm_info,
935 &valleyview_cursor_wm_info,
936 &ignore_plane_sr, &cursor_sr)) {
937 cxsr_enabled = true;
938 } else {
939 cxsr_enabled = false;
940 intel_set_memory_cxsr(dev_priv, false);
941 plane_sr = cursor_sr = 0;
942 }
943
944 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
945 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
946 "SR: plane=%d, cursor=%d\n",
947 planea_wm, cursora_wm,
948 planeb_wm, cursorb_wm,
949 planec_wm, cursorc_wm,
950 plane_sr, cursor_sr);
951
952 I915_WRITE(DSPFW1,
953 (plane_sr << DSPFW_SR_SHIFT) |
954 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
955 (planeb_wm << DSPFW_PLANEB_SHIFT) |
956 (planea_wm << DSPFW_PLANEA_SHIFT));
957 I915_WRITE(DSPFW2,
958 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
959 (cursora_wm << DSPFW_CURSORA_SHIFT));
960 I915_WRITE(DSPFW3,
961 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
962 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
963 I915_WRITE(DSPFW9_CHV,
964 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
965 DSPFW_CURSORC_MASK)) |
966 (planec_wm << DSPFW_PLANEC_SHIFT) |
967 (cursorc_wm << DSPFW_CURSORC_SHIFT));
968
969 if (cxsr_enabled)
970 intel_set_memory_cxsr(dev_priv, true);
971}
972
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530973static void valleyview_update_sprite_wm(struct drm_plane *plane,
974 struct drm_crtc *crtc,
975 uint32_t sprite_width,
976 uint32_t sprite_height,
977 int pixel_size,
978 bool enabled, bool scaled)
979{
980 struct drm_device *dev = crtc->dev;
981 struct drm_i915_private *dev_priv = dev->dev_private;
982 int pipe = to_intel_plane(plane)->pipe;
983 int sprite = to_intel_plane(plane)->plane;
984 int drain_latency;
985 int plane_prec;
986 int sprite_dl;
987 int prec_mult;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700988 const int high_precision = IS_CHERRYVIEW(dev) ?
989 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530990
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700991 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530992 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
993
994 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
995 &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700996 plane_prec = (prec_mult == high_precision) ?
997 DDL_SPRITE_PRECISION_HIGH(sprite) :
998 DDL_SPRITE_PRECISION_LOW(sprite);
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530999 sprite_dl |= plane_prec |
1000 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1001 }
1002
1003 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1004}
1005
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001006static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001007{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001008 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001009 static const int sr_latency_ns = 12000;
1010 struct drm_i915_private *dev_priv = dev->dev_private;
1011 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1012 int plane_sr, cursor_sr;
1013 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001014 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001015
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001016 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001017 &g4x_wm_info, pessimal_latency_ns,
1018 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001019 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001020 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001021
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001022 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001023 &g4x_wm_info, pessimal_latency_ns,
1024 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001025 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001026 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001027
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001028 if (single_plane_enabled(enabled) &&
1029 g4x_compute_srwm(dev, ffs(enabled) - 1,
1030 sr_latency_ns,
1031 &g4x_wm_info,
1032 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001033 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001034 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001035 } else {
Imre Deak98584252014-06-13 14:54:20 +03001036 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001037 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001038 plane_sr = cursor_sr = 0;
1039 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001040
Ville Syrjäläa5043452014-06-28 02:04:18 +03001041 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1042 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001043 planea_wm, cursora_wm,
1044 planeb_wm, cursorb_wm,
1045 plane_sr, cursor_sr);
1046
1047 I915_WRITE(DSPFW1,
1048 (plane_sr << DSPFW_SR_SHIFT) |
1049 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1050 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001051 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001052 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001053 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001054 (cursora_wm << DSPFW_CURSORA_SHIFT));
1055 /* HPLL off in SR has some issues on G4x... disable it */
1056 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001057 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001058 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001059
1060 if (cxsr_enabled)
1061 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001062}
1063
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001064static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001065{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001066 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001067 struct drm_i915_private *dev_priv = dev->dev_private;
1068 struct drm_crtc *crtc;
1069 int srwm = 1;
1070 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001071 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001072
1073 /* Calc sr entries for one plane configs */
1074 crtc = single_enabled_crtc(dev);
1075 if (crtc) {
1076 /* self-refresh has much higher latency */
1077 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001078 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001079 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001080 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001081 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001082 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001083 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001084 unsigned long line_time_us;
1085 int entries;
1086
Ville Syrjälä922044c2014-02-14 14:18:57 +02001087 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001088
1089 /* Use ns/us then divide to preserve precision */
1090 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1091 pixel_size * hdisplay;
1092 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1093 srwm = I965_FIFO_SIZE - entries;
1094 if (srwm < 0)
1095 srwm = 1;
1096 srwm &= 0x1ff;
1097 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1098 entries, srwm);
1099
1100 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001101 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001102 entries = DIV_ROUND_UP(entries,
1103 i965_cursor_wm_info.cacheline_size);
1104 cursor_sr = i965_cursor_wm_info.fifo_size -
1105 (entries + i965_cursor_wm_info.guard_size);
1106
1107 if (cursor_sr > i965_cursor_wm_info.max_wm)
1108 cursor_sr = i965_cursor_wm_info.max_wm;
1109
1110 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1111 "cursor %d\n", srwm, cursor_sr);
1112
Imre Deak98584252014-06-13 14:54:20 +03001113 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001114 } else {
Imre Deak98584252014-06-13 14:54:20 +03001115 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001116 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001117 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001118 }
1119
1120 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1121 srwm);
1122
1123 /* 965 has limitations... */
1124 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001125 (8 << DSPFW_CURSORB_SHIFT) |
1126 (8 << DSPFW_PLANEB_SHIFT) |
1127 (8 << DSPFW_PLANEA_SHIFT));
1128 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1129 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001130 /* update cursor SR watermark */
1131 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001132
1133 if (cxsr_enabled)
1134 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001135}
1136
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001137static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001138{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001139 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001140 struct drm_i915_private *dev_priv = dev->dev_private;
1141 const struct intel_watermark_params *wm_info;
1142 uint32_t fwater_lo;
1143 uint32_t fwater_hi;
1144 int cwm, srwm = 1;
1145 int fifo_size;
1146 int planea_wm, planeb_wm;
1147 struct drm_crtc *crtc, *enabled = NULL;
1148
1149 if (IS_I945GM(dev))
1150 wm_info = &i945_wm_info;
1151 else if (!IS_GEN2(dev))
1152 wm_info = &i915_wm_info;
1153 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001154 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001155
1156 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1157 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001158 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001159 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001160 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001161 if (IS_GEN2(dev))
1162 cpp = 4;
1163
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001164 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001165 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001166 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001167 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001168 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001169 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001170 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001171 if (planea_wm > (long)wm_info->max_wm)
1172 planea_wm = wm_info->max_wm;
1173 }
1174
1175 if (IS_GEN2(dev))
1176 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001177
1178 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1179 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001180 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001181 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001182 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001183 if (IS_GEN2(dev))
1184 cpp = 4;
1185
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001186 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001187 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001188 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001189 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001190 if (enabled == NULL)
1191 enabled = crtc;
1192 else
1193 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001194 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001195 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001196 if (planeb_wm > (long)wm_info->max_wm)
1197 planeb_wm = wm_info->max_wm;
1198 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001199
1200 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1201
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001202 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001203 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001204
Matt Roper59bea882015-02-27 10:12:01 -08001205 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001206
1207 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001208 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001209 enabled = NULL;
1210 }
1211
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001212 /*
1213 * Overlay gets an aggressive default since video jitter is bad.
1214 */
1215 cwm = 2;
1216
1217 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001218 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001219
1220 /* Calc sr entries for one plane configs */
1221 if (HAS_FW_BLC(dev) && enabled) {
1222 /* self-refresh has much higher latency */
1223 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001224 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001225 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001226 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001227 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001228 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001229 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001230 unsigned long line_time_us;
1231 int entries;
1232
Ville Syrjälä922044c2014-02-14 14:18:57 +02001233 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001234
1235 /* Use ns/us then divide to preserve precision */
1236 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1237 pixel_size * hdisplay;
1238 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1239 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1240 srwm = wm_info->fifo_size - entries;
1241 if (srwm < 0)
1242 srwm = 1;
1243
1244 if (IS_I945G(dev) || IS_I945GM(dev))
1245 I915_WRITE(FW_BLC_SELF,
1246 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1247 else if (IS_I915GM(dev))
1248 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1249 }
1250
1251 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1252 planea_wm, planeb_wm, cwm, srwm);
1253
1254 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1255 fwater_hi = (cwm & 0x1f);
1256
1257 /* Set request length to 8 cachelines per fetch */
1258 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1259 fwater_hi = fwater_hi | (1 << 8);
1260
1261 I915_WRITE(FW_BLC, fwater_lo);
1262 I915_WRITE(FW_BLC2, fwater_hi);
1263
Imre Deak5209b1f2014-07-01 12:36:17 +03001264 if (enabled)
1265 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001266}
1267
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001268static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001269{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001270 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001271 struct drm_i915_private *dev_priv = dev->dev_private;
1272 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001273 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001274 uint32_t fwater_lo;
1275 int planea_wm;
1276
1277 crtc = single_enabled_crtc(dev);
1278 if (crtc == NULL)
1279 return;
1280
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001281 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001282 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001283 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001284 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001285 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001286 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1287 fwater_lo |= (3<<8) | planea_wm;
1288
1289 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1290
1291 I915_WRITE(FW_BLC, fwater_lo);
1292}
1293
Ville Syrjälä36587292013-07-05 11:57:16 +03001294static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1295 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001296{
1297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001298 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001299
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001300 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001301
1302 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1303 * adjust the pixel_rate here. */
1304
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001305 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001306 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001307 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001308
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001309 pipe_w = intel_crtc->config->pipe_src_w;
1310 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001311 pfit_w = (pfit_size >> 16) & 0xFFFF;
1312 pfit_h = pfit_size & 0xFFFF;
1313 if (pipe_w < pfit_w)
1314 pipe_w = pfit_w;
1315 if (pipe_h < pfit_h)
1316 pipe_h = pfit_h;
1317
1318 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1319 pfit_w * pfit_h);
1320 }
1321
1322 return pixel_rate;
1323}
1324
Ville Syrjälä37126462013-08-01 16:18:55 +03001325/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001326static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001327 uint32_t latency)
1328{
1329 uint64_t ret;
1330
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001331 if (WARN(latency == 0, "Latency value missing\n"))
1332 return UINT_MAX;
1333
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001334 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1335 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1336
1337 return ret;
1338}
1339
Ville Syrjälä37126462013-08-01 16:18:55 +03001340/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001341static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001342 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1343 uint32_t latency)
1344{
1345 uint32_t ret;
1346
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001347 if (WARN(latency == 0, "Latency value missing\n"))
1348 return UINT_MAX;
1349
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001350 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1351 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1352 ret = DIV_ROUND_UP(ret, 64) + 2;
1353 return ret;
1354}
1355
Ville Syrjälä23297042013-07-05 11:57:17 +03001356static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001357 uint8_t bytes_per_pixel)
1358{
1359 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1360}
1361
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001362struct skl_pipe_wm_parameters {
1363 bool active;
1364 uint32_t pipe_htotal;
1365 uint32_t pixel_rate; /* in KHz */
1366 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1367 struct intel_plane_wm_parameters cursor;
1368};
1369
Imre Deak820c1982013-12-17 14:46:36 +02001370struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001371 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001372 uint32_t pipe_htotal;
1373 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001374 struct intel_plane_wm_parameters pri;
1375 struct intel_plane_wm_parameters spr;
1376 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001377};
1378
Imre Deak820c1982013-12-17 14:46:36 +02001379struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001380 uint16_t pri;
1381 uint16_t spr;
1382 uint16_t cur;
1383 uint16_t fbc;
1384};
1385
Ville Syrjälä240264f2013-08-07 13:29:12 +03001386/* used in computing the new watermarks state */
1387struct intel_wm_config {
1388 unsigned int num_pipes_active;
1389 bool sprites_enabled;
1390 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001391};
1392
Ville Syrjälä37126462013-08-01 16:18:55 +03001393/*
1394 * For both WM_PIPE and WM_LP.
1395 * mem_value must be in 0.1us units.
1396 */
Imre Deak820c1982013-12-17 14:46:36 +02001397static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001398 uint32_t mem_value,
1399 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001400{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001401 uint32_t method1, method2;
1402
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001403 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001404 return 0;
1405
Ville Syrjälä23297042013-07-05 11:57:17 +03001406 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001407 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001408 mem_value);
1409
1410 if (!is_lp)
1411 return method1;
1412
Ville Syrjälä23297042013-07-05 11:57:17 +03001413 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001414 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001415 params->pri.horiz_pixels,
1416 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001417 mem_value);
1418
1419 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001420}
1421
Ville Syrjälä37126462013-08-01 16:18:55 +03001422/*
1423 * For both WM_PIPE and WM_LP.
1424 * mem_value must be in 0.1us units.
1425 */
Imre Deak820c1982013-12-17 14:46:36 +02001426static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001427 uint32_t mem_value)
1428{
1429 uint32_t method1, method2;
1430
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001431 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001432 return 0;
1433
Ville Syrjälä23297042013-07-05 11:57:17 +03001434 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001435 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001436 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001437 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001438 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001439 params->spr.horiz_pixels,
1440 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001441 mem_value);
1442 return min(method1, method2);
1443}
1444
Ville Syrjälä37126462013-08-01 16:18:55 +03001445/*
1446 * For both WM_PIPE and WM_LP.
1447 * mem_value must be in 0.1us units.
1448 */
Imre Deak820c1982013-12-17 14:46:36 +02001449static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001450 uint32_t mem_value)
1451{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001452 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001453 return 0;
1454
Ville Syrjälä23297042013-07-05 11:57:17 +03001455 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001456 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001457 params->cur.horiz_pixels,
1458 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001459 mem_value);
1460}
1461
Paulo Zanonicca32e92013-05-31 11:45:06 -03001462/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001463static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001464 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001465{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001466 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001467 return 0;
1468
Ville Syrjälä23297042013-07-05 11:57:17 +03001469 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001470 params->pri.horiz_pixels,
1471 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001472}
1473
Ville Syrjälä158ae642013-08-07 13:28:19 +03001474static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1475{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001476 if (INTEL_INFO(dev)->gen >= 8)
1477 return 3072;
1478 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001479 return 768;
1480 else
1481 return 512;
1482}
1483
Ville Syrjälä4e975082014-03-07 18:32:11 +02001484static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1485 int level, bool is_sprite)
1486{
1487 if (INTEL_INFO(dev)->gen >= 8)
1488 /* BDW primary/sprite plane watermarks */
1489 return level == 0 ? 255 : 2047;
1490 else if (INTEL_INFO(dev)->gen >= 7)
1491 /* IVB/HSW primary/sprite plane watermarks */
1492 return level == 0 ? 127 : 1023;
1493 else if (!is_sprite)
1494 /* ILK/SNB primary plane watermarks */
1495 return level == 0 ? 127 : 511;
1496 else
1497 /* ILK/SNB sprite plane watermarks */
1498 return level == 0 ? 63 : 255;
1499}
1500
1501static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1502 int level)
1503{
1504 if (INTEL_INFO(dev)->gen >= 7)
1505 return level == 0 ? 63 : 255;
1506 else
1507 return level == 0 ? 31 : 63;
1508}
1509
1510static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1511{
1512 if (INTEL_INFO(dev)->gen >= 8)
1513 return 31;
1514 else
1515 return 15;
1516}
1517
Ville Syrjälä158ae642013-08-07 13:28:19 +03001518/* Calculate the maximum primary/sprite plane watermark */
1519static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1520 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001521 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001522 enum intel_ddb_partitioning ddb_partitioning,
1523 bool is_sprite)
1524{
1525 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001526
1527 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001528 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001529 return 0;
1530
1531 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001532 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001533 fifo_size /= INTEL_INFO(dev)->num_pipes;
1534
1535 /*
1536 * For some reason the non self refresh
1537 * FIFO size is only half of the self
1538 * refresh FIFO size on ILK/SNB.
1539 */
1540 if (INTEL_INFO(dev)->gen <= 6)
1541 fifo_size /= 2;
1542 }
1543
Ville Syrjälä240264f2013-08-07 13:29:12 +03001544 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001545 /* level 0 is always calculated with 1:1 split */
1546 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1547 if (is_sprite)
1548 fifo_size *= 5;
1549 fifo_size /= 6;
1550 } else {
1551 fifo_size /= 2;
1552 }
1553 }
1554
1555 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001556 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001557}
1558
1559/* Calculate the maximum cursor plane watermark */
1560static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001561 int level,
1562 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001563{
1564 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001565 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001566 return 64;
1567
1568 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001569 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001570}
1571
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001572static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001573 int level,
1574 const struct intel_wm_config *config,
1575 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001576 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001577{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001578 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1579 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1580 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001581 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001582}
1583
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001584static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1585 int level,
1586 struct ilk_wm_maximums *max)
1587{
1588 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1589 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1590 max->cur = ilk_cursor_wm_reg_max(dev, level);
1591 max->fbc = ilk_fbc_wm_reg_max(dev);
1592}
1593
Ville Syrjäläd9395652013-10-09 19:18:10 +03001594static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001595 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001596 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001597{
1598 bool ret;
1599
1600 /* already determined to be invalid? */
1601 if (!result->enable)
1602 return false;
1603
1604 result->enable = result->pri_val <= max->pri &&
1605 result->spr_val <= max->spr &&
1606 result->cur_val <= max->cur;
1607
1608 ret = result->enable;
1609
1610 /*
1611 * HACK until we can pre-compute everything,
1612 * and thus fail gracefully if LP0 watermarks
1613 * are exceeded...
1614 */
1615 if (level == 0 && !result->enable) {
1616 if (result->pri_val > max->pri)
1617 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1618 level, result->pri_val, max->pri);
1619 if (result->spr_val > max->spr)
1620 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1621 level, result->spr_val, max->spr);
1622 if (result->cur_val > max->cur)
1623 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1624 level, result->cur_val, max->cur);
1625
1626 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1627 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1628 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1629 result->enable = true;
1630 }
1631
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001632 return ret;
1633}
1634
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001635static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001636 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001637 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001638 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001639{
1640 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1641 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1642 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1643
1644 /* WM1+ latency values stored in 0.5us units */
1645 if (level > 0) {
1646 pri_latency *= 5;
1647 spr_latency *= 5;
1648 cur_latency *= 5;
1649 }
1650
1651 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1652 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1653 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1654 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1655 result->enable = true;
1656}
1657
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001658static uint32_t
1659hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001660{
1661 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001663 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001664 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001665
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001666 if (!intel_crtc_active(crtc))
1667 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001668
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001669 /* The WM are computed with base on how long it takes to fill a single
1670 * row at the given clock rate, multiplied by 8.
1671 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001672 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1673 mode->crtc_clock);
1674 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001675 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001676
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001677 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1678 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001679}
1680
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001681static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001682{
1683 struct drm_i915_private *dev_priv = dev->dev_private;
1684
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001685 if (IS_GEN9(dev)) {
1686 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001687 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001688 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001689
1690 /* read the first set of memory latencies[0:3] */
1691 val = 0; /* data0 to be programmed to 0 for first set */
1692 mutex_lock(&dev_priv->rps.hw_lock);
1693 ret = sandybridge_pcode_read(dev_priv,
1694 GEN9_PCODE_READ_MEM_LATENCY,
1695 &val);
1696 mutex_unlock(&dev_priv->rps.hw_lock);
1697
1698 if (ret) {
1699 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1700 return;
1701 }
1702
1703 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1704 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1705 GEN9_MEM_LATENCY_LEVEL_MASK;
1706 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1707 GEN9_MEM_LATENCY_LEVEL_MASK;
1708 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1709 GEN9_MEM_LATENCY_LEVEL_MASK;
1710
1711 /* read the second set of memory latencies[4:7] */
1712 val = 1; /* data0 to be programmed to 1 for second set */
1713 mutex_lock(&dev_priv->rps.hw_lock);
1714 ret = sandybridge_pcode_read(dev_priv,
1715 GEN9_PCODE_READ_MEM_LATENCY,
1716 &val);
1717 mutex_unlock(&dev_priv->rps.hw_lock);
1718 if (ret) {
1719 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1720 return;
1721 }
1722
1723 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1724 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1725 GEN9_MEM_LATENCY_LEVEL_MASK;
1726 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1727 GEN9_MEM_LATENCY_LEVEL_MASK;
1728 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1729 GEN9_MEM_LATENCY_LEVEL_MASK;
1730
Vandana Kannan367294b2014-11-04 17:06:46 +00001731 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001732 * WaWmMemoryReadLatency:skl
1733 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001734 * punit doesn't take into account the read latency so we need
1735 * to add 2us to the various latency levels we retrieve from
1736 * the punit.
1737 * - W0 is a bit special in that it's the only level that
1738 * can't be disabled if we want to have display working, so
1739 * we always add 2us there.
1740 * - For levels >=1, punit returns 0us latency when they are
1741 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001742 *
1743 * Additionally, if a level n (n > 1) has a 0us latency, all
1744 * levels m (m >= n) need to be disabled. We make sure to
1745 * sanitize the values out of the punit to satisfy this
1746 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001747 */
1748 wm[0] += 2;
1749 for (level = 1; level <= max_level; level++)
1750 if (wm[level] != 0)
1751 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001752 else {
1753 for (i = level + 1; i <= max_level; i++)
1754 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001755
Vandana Kannan4f947382014-11-04 17:06:47 +00001756 break;
1757 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001758 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001759 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1760
1761 wm[0] = (sskpd >> 56) & 0xFF;
1762 if (wm[0] == 0)
1763 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001764 wm[1] = (sskpd >> 4) & 0xFF;
1765 wm[2] = (sskpd >> 12) & 0xFF;
1766 wm[3] = (sskpd >> 20) & 0x1FF;
1767 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001768 } else if (INTEL_INFO(dev)->gen >= 6) {
1769 uint32_t sskpd = I915_READ(MCH_SSKPD);
1770
1771 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1772 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1773 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1774 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001775 } else if (INTEL_INFO(dev)->gen >= 5) {
1776 uint32_t mltr = I915_READ(MLTR_ILK);
1777
1778 /* ILK primary LP0 latency is 700 ns */
1779 wm[0] = 7;
1780 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1781 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001782 }
1783}
1784
Ville Syrjälä53615a52013-08-01 16:18:50 +03001785static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1786{
1787 /* ILK sprite LP0 latency is 1300 ns */
1788 if (INTEL_INFO(dev)->gen == 5)
1789 wm[0] = 13;
1790}
1791
1792static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1793{
1794 /* ILK cursor LP0 latency is 1300 ns */
1795 if (INTEL_INFO(dev)->gen == 5)
1796 wm[0] = 13;
1797
1798 /* WaDoubleCursorLP3Latency:ivb */
1799 if (IS_IVYBRIDGE(dev))
1800 wm[3] *= 2;
1801}
1802
Damien Lespiau546c81f2014-05-13 15:30:26 +01001803int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001804{
1805 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001806 if (IS_GEN9(dev))
1807 return 7;
1808 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001809 return 4;
1810 else if (INTEL_INFO(dev)->gen >= 6)
1811 return 3;
1812 else
1813 return 2;
1814}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001815
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001816static void intel_print_wm_latency(struct drm_device *dev,
1817 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001818 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001819{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001820 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001821
1822 for (level = 0; level <= max_level; level++) {
1823 unsigned int latency = wm[level];
1824
1825 if (latency == 0) {
1826 DRM_ERROR("%s WM%d latency not provided\n",
1827 name, level);
1828 continue;
1829 }
1830
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001831 /*
1832 * - latencies are in us on gen9.
1833 * - before then, WM1+ latency values are in 0.5us units
1834 */
1835 if (IS_GEN9(dev))
1836 latency *= 10;
1837 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001838 latency *= 5;
1839
1840 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1841 name, level, wm[level],
1842 latency / 10, latency % 10);
1843 }
1844}
1845
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001846static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1847 uint16_t wm[5], uint16_t min)
1848{
1849 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1850
1851 if (wm[0] >= min)
1852 return false;
1853
1854 wm[0] = max(wm[0], min);
1855 for (level = 1; level <= max_level; level++)
1856 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1857
1858 return true;
1859}
1860
1861static void snb_wm_latency_quirk(struct drm_device *dev)
1862{
1863 struct drm_i915_private *dev_priv = dev->dev_private;
1864 bool changed;
1865
1866 /*
1867 * The BIOS provided WM memory latency values are often
1868 * inadequate for high resolution displays. Adjust them.
1869 */
1870 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1871 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1872 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1873
1874 if (!changed)
1875 return;
1876
1877 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1878 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1879 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1880 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1881}
1882
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001883static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001884{
1885 struct drm_i915_private *dev_priv = dev->dev_private;
1886
1887 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1888
1889 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1890 sizeof(dev_priv->wm.pri_latency));
1891 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1892 sizeof(dev_priv->wm.pri_latency));
1893
1894 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1895 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001896
1897 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1898 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1899 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001900
1901 if (IS_GEN6(dev))
1902 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001903}
1904
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001905static void skl_setup_wm_latency(struct drm_device *dev)
1906{
1907 struct drm_i915_private *dev_priv = dev->dev_private;
1908
1909 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1910 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1911}
1912
Imre Deak820c1982013-12-17 14:46:36 +02001913static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001914 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001915{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001916 struct drm_device *dev = crtc->dev;
1917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1918 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001919 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001920
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001921 if (!intel_crtc_active(crtc))
1922 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001923
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001924 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001925 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001926 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roper59bea882015-02-27 10:12:01 -08001927 p->pri.bytes_per_pixel = crtc->primary->state->fb->bits_per_pixel / 8;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001928 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001929 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08001930 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001931 /* TODO: for now, assume primary and cursor planes are always enabled. */
1932 p->pri.enabled = true;
1933 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001934
Matt Roperaf2b6532014-04-01 15:22:32 -07001935 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001936 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001937
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001938 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001939 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001940 break;
1941 }
1942 }
1943}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001944
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001945static void ilk_compute_wm_config(struct drm_device *dev,
1946 struct intel_wm_config *config)
1947{
1948 struct intel_crtc *intel_crtc;
1949
1950 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01001951 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001952 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
1953
1954 if (!wm->pipe_enabled)
1955 continue;
1956
1957 config->sprites_enabled |= wm->sprites_enabled;
1958 config->sprites_scaled |= wm->sprites_scaled;
1959 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001960 }
1961}
1962
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001963/* Compute new watermarks for the pipe */
1964static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02001965 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001966 struct intel_pipe_wm *pipe_wm)
1967{
1968 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001969 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001970 int level, max_level = ilk_wm_max_level(dev);
1971 /* LP0 watermark maximums depend on this pipe alone */
1972 struct intel_wm_config config = {
1973 .num_pipes_active = 1,
1974 .sprites_enabled = params->spr.enabled,
1975 .sprites_scaled = params->spr.scaled,
1976 };
Imre Deak820c1982013-12-17 14:46:36 +02001977 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001978
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001979 pipe_wm->pipe_enabled = params->active;
1980 pipe_wm->sprites_enabled = params->spr.enabled;
1981 pipe_wm->sprites_scaled = params->spr.scaled;
1982
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02001983 /* ILK/SNB: LP2+ watermarks only w/o sprites */
1984 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
1985 max_level = 1;
1986
1987 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
1988 if (params->spr.scaled)
1989 max_level = 0;
1990
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001991 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001992
Ville Syrjäläa42a5712014-01-07 16:14:08 +02001993 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02001994 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001995
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001996 /* LP0 watermarks always use 1/2 DDB partitioning */
1997 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
1998
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001999 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002000 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2001 return false;
2002
2003 ilk_compute_wm_reg_maximums(dev, 1, &max);
2004
2005 for (level = 1; level <= max_level; level++) {
2006 struct intel_wm_level wm = {};
2007
2008 ilk_compute_wm_level(dev_priv, level, params, &wm);
2009
2010 /*
2011 * Disable any watermark level that exceeds the
2012 * register maximums since such watermarks are
2013 * always invalid.
2014 */
2015 if (!ilk_validate_wm_level(level, &max, &wm))
2016 break;
2017
2018 pipe_wm->wm[level] = wm;
2019 }
2020
2021 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002022}
2023
2024/*
2025 * Merge the watermarks from all active pipes for a specific level.
2026 */
2027static void ilk_merge_wm_level(struct drm_device *dev,
2028 int level,
2029 struct intel_wm_level *ret_wm)
2030{
2031 const struct intel_crtc *intel_crtc;
2032
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002033 ret_wm->enable = true;
2034
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002035 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002036 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2037 const struct intel_wm_level *wm = &active->wm[level];
2038
2039 if (!active->pipe_enabled)
2040 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002041
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002042 /*
2043 * The watermark values may have been used in the past,
2044 * so we must maintain them in the registers for some
2045 * time even if the level is now disabled.
2046 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002047 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002048 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002049
2050 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2051 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2052 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2053 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2054 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002055}
2056
2057/*
2058 * Merge all low power watermarks for all active pipes.
2059 */
2060static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002061 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002062 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002063 struct intel_pipe_wm *merged)
2064{
2065 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002066 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002067
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002068 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2069 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2070 config->num_pipes_active > 1)
2071 return;
2072
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002073 /* ILK: FBC WM must be disabled always */
2074 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002075
2076 /* merge each WM1+ level */
2077 for (level = 1; level <= max_level; level++) {
2078 struct intel_wm_level *wm = &merged->wm[level];
2079
2080 ilk_merge_wm_level(dev, level, wm);
2081
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002082 if (level > last_enabled_level)
2083 wm->enable = false;
2084 else if (!ilk_validate_wm_level(level, max, wm))
2085 /* make sure all following levels get disabled */
2086 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002087
2088 /*
2089 * The spec says it is preferred to disable
2090 * FBC WMs instead of disabling a WM level.
2091 */
2092 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002093 if (wm->enable)
2094 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002095 wm->fbc_val = 0;
2096 }
2097 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002098
2099 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2100 /*
2101 * FIXME this is racy. FBC might get enabled later.
2102 * What we should check here is whether FBC can be
2103 * enabled sometime later.
2104 */
2105 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2106 for (level = 2; level <= max_level; level++) {
2107 struct intel_wm_level *wm = &merged->wm[level];
2108
2109 wm->enable = false;
2110 }
2111 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002112}
2113
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002114static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2115{
2116 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2117 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2118}
2119
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002120/* The value we need to program into the WM_LPx latency field */
2121static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2122{
2123 struct drm_i915_private *dev_priv = dev->dev_private;
2124
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002125 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002126 return 2 * level;
2127 else
2128 return dev_priv->wm.pri_latency[level];
2129}
2130
Imre Deak820c1982013-12-17 14:46:36 +02002131static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002132 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002133 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002134 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002135{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002136 struct intel_crtc *intel_crtc;
2137 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002138
Ville Syrjälä0362c782013-10-09 19:17:57 +03002139 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002140 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002141
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002142 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002143 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002144 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002145
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002146 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002147
Ville Syrjälä0362c782013-10-09 19:17:57 +03002148 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002149
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002150 /*
2151 * Maintain the watermark values even if the level is
2152 * disabled. Doing otherwise could cause underruns.
2153 */
2154 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002155 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002156 (r->pri_val << WM1_LP_SR_SHIFT) |
2157 r->cur_val;
2158
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002159 if (r->enable)
2160 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2161
Ville Syrjälä416f4722013-11-02 21:07:46 -07002162 if (INTEL_INFO(dev)->gen >= 8)
2163 results->wm_lp[wm_lp - 1] |=
2164 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2165 else
2166 results->wm_lp[wm_lp - 1] |=
2167 r->fbc_val << WM1_LP_FBC_SHIFT;
2168
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002169 /*
2170 * Always set WM1S_LP_EN when spr_val != 0, even if the
2171 * level is disabled. Doing otherwise could cause underruns.
2172 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002173 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2174 WARN_ON(wm_lp != 1);
2175 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2176 } else
2177 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002178 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002179
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002180 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002181 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002182 enum pipe pipe = intel_crtc->pipe;
2183 const struct intel_wm_level *r =
2184 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002185
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002186 if (WARN_ON(!r->enable))
2187 continue;
2188
2189 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2190
2191 results->wm_pipe[pipe] =
2192 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2193 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2194 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002195 }
2196}
2197
Paulo Zanoni861f3382013-05-31 10:19:21 -03002198/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2199 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002200static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002201 struct intel_pipe_wm *r1,
2202 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002203{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002204 int level, max_level = ilk_wm_max_level(dev);
2205 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002206
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002207 for (level = 1; level <= max_level; level++) {
2208 if (r1->wm[level].enable)
2209 level1 = level;
2210 if (r2->wm[level].enable)
2211 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002212 }
2213
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002214 if (level1 == level2) {
2215 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002216 return r2;
2217 else
2218 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002219 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002220 return r1;
2221 } else {
2222 return r2;
2223 }
2224}
2225
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002226/* dirty bits used to track which watermarks need changes */
2227#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2228#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2229#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2230#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2231#define WM_DIRTY_FBC (1 << 24)
2232#define WM_DIRTY_DDB (1 << 25)
2233
Damien Lespiau055e3932014-08-18 13:49:10 +01002234static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002235 const struct ilk_wm_values *old,
2236 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002237{
2238 unsigned int dirty = 0;
2239 enum pipe pipe;
2240 int wm_lp;
2241
Damien Lespiau055e3932014-08-18 13:49:10 +01002242 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002243 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2244 dirty |= WM_DIRTY_LINETIME(pipe);
2245 /* Must disable LP1+ watermarks too */
2246 dirty |= WM_DIRTY_LP_ALL;
2247 }
2248
2249 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2250 dirty |= WM_DIRTY_PIPE(pipe);
2251 /* Must disable LP1+ watermarks too */
2252 dirty |= WM_DIRTY_LP_ALL;
2253 }
2254 }
2255
2256 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2257 dirty |= WM_DIRTY_FBC;
2258 /* Must disable LP1+ watermarks too */
2259 dirty |= WM_DIRTY_LP_ALL;
2260 }
2261
2262 if (old->partitioning != new->partitioning) {
2263 dirty |= WM_DIRTY_DDB;
2264 /* Must disable LP1+ watermarks too */
2265 dirty |= WM_DIRTY_LP_ALL;
2266 }
2267
2268 /* LP1+ watermarks already deemed dirty, no need to continue */
2269 if (dirty & WM_DIRTY_LP_ALL)
2270 return dirty;
2271
2272 /* Find the lowest numbered LP1+ watermark in need of an update... */
2273 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2274 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2275 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2276 break;
2277 }
2278
2279 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2280 for (; wm_lp <= 3; wm_lp++)
2281 dirty |= WM_DIRTY_LP(wm_lp);
2282
2283 return dirty;
2284}
2285
Ville Syrjälä8553c182013-12-05 15:51:39 +02002286static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2287 unsigned int dirty)
2288{
Imre Deak820c1982013-12-17 14:46:36 +02002289 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002290 bool changed = false;
2291
2292 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2293 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2294 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2295 changed = true;
2296 }
2297 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2298 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2299 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2300 changed = true;
2301 }
2302 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2303 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2304 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2305 changed = true;
2306 }
2307
2308 /*
2309 * Don't touch WM1S_LP_EN here.
2310 * Doing so could cause underruns.
2311 */
2312
2313 return changed;
2314}
2315
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002316/*
2317 * The spec says we shouldn't write when we don't need, because every write
2318 * causes WMs to be re-evaluated, expending some power.
2319 */
Imre Deak820c1982013-12-17 14:46:36 +02002320static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2321 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002322{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002323 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002324 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002325 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002326 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002327
Damien Lespiau055e3932014-08-18 13:49:10 +01002328 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002329 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002330 return;
2331
Ville Syrjälä8553c182013-12-05 15:51:39 +02002332 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002333
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002334 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002335 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002336 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002337 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002338 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002339 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2340
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002341 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002342 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002343 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002344 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002345 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002346 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2347
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002348 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002349 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002350 val = I915_READ(WM_MISC);
2351 if (results->partitioning == INTEL_DDB_PART_1_2)
2352 val &= ~WM_MISC_DATA_PARTITION_5_6;
2353 else
2354 val |= WM_MISC_DATA_PARTITION_5_6;
2355 I915_WRITE(WM_MISC, val);
2356 } else {
2357 val = I915_READ(DISP_ARB_CTL2);
2358 if (results->partitioning == INTEL_DDB_PART_1_2)
2359 val &= ~DISP_DATA_PARTITION_5_6;
2360 else
2361 val |= DISP_DATA_PARTITION_5_6;
2362 I915_WRITE(DISP_ARB_CTL2, val);
2363 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002364 }
2365
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002366 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002367 val = I915_READ(DISP_ARB_CTL);
2368 if (results->enable_fbc_wm)
2369 val &= ~DISP_FBC_WM_DIS;
2370 else
2371 val |= DISP_FBC_WM_DIS;
2372 I915_WRITE(DISP_ARB_CTL, val);
2373 }
2374
Imre Deak954911e2013-12-17 14:46:34 +02002375 if (dirty & WM_DIRTY_LP(1) &&
2376 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2377 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2378
2379 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002380 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2381 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2382 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2383 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2384 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002385
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002386 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002387 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002388 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002389 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002390 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002391 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002392
2393 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002394}
2395
Ville Syrjälä8553c182013-12-05 15:51:39 +02002396static bool ilk_disable_lp_wm(struct drm_device *dev)
2397{
2398 struct drm_i915_private *dev_priv = dev->dev_private;
2399
2400 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2401}
2402
Damien Lespiaub9cec072014-11-04 17:06:43 +00002403/*
2404 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2405 * different active planes.
2406 */
2407
2408#define SKL_DDB_SIZE 896 /* in blocks */
2409
2410static void
2411skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2412 struct drm_crtc *for_crtc,
2413 const struct intel_wm_config *config,
2414 const struct skl_pipe_wm_parameters *params,
2415 struct skl_ddb_entry *alloc /* out */)
2416{
2417 struct drm_crtc *crtc;
2418 unsigned int pipe_size, ddb_size;
2419 int nth_active_pipe;
2420
2421 if (!params->active) {
2422 alloc->start = 0;
2423 alloc->end = 0;
2424 return;
2425 }
2426
2427 ddb_size = SKL_DDB_SIZE;
2428
2429 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2430
2431 nth_active_pipe = 0;
2432 for_each_crtc(dev, crtc) {
2433 if (!intel_crtc_active(crtc))
2434 continue;
2435
2436 if (crtc == for_crtc)
2437 break;
2438
2439 nth_active_pipe++;
2440 }
2441
2442 pipe_size = ddb_size / config->num_pipes_active;
2443 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002444 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002445}
2446
2447static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2448{
2449 if (config->num_pipes_active == 1)
2450 return 32;
2451
2452 return 8;
2453}
2454
Damien Lespiaua269c582014-11-04 17:06:49 +00002455static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2456{
2457 entry->start = reg & 0x3ff;
2458 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002459 if (entry->end)
2460 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002461}
2462
Damien Lespiau08db6652014-11-04 17:06:52 +00002463void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2464 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002465{
Damien Lespiaua269c582014-11-04 17:06:49 +00002466 enum pipe pipe;
2467 int plane;
2468 u32 val;
2469
2470 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002471 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002472 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2473 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2474 val);
2475 }
2476
2477 val = I915_READ(CUR_BUF_CFG(pipe));
2478 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2479 }
2480}
2481
Damien Lespiaub9cec072014-11-04 17:06:43 +00002482static unsigned int
2483skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2484{
2485 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2486}
2487
2488/*
2489 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2490 * a 8192x4096@32bpp framebuffer:
2491 * 3 * 4096 * 8192 * 4 < 2^32
2492 */
2493static unsigned int
2494skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2495 const struct skl_pipe_wm_parameters *params)
2496{
2497 unsigned int total_data_rate = 0;
2498 int plane;
2499
2500 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2501 const struct intel_plane_wm_parameters *p;
2502
2503 p = &params->plane[plane];
2504 if (!p->enabled)
2505 continue;
2506
2507 total_data_rate += skl_plane_relative_data_rate(p);
2508 }
2509
2510 return total_data_rate;
2511}
2512
2513static void
2514skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2515 const struct intel_wm_config *config,
2516 const struct skl_pipe_wm_parameters *params,
2517 struct skl_ddb_allocation *ddb /* out */)
2518{
2519 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002520 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2522 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002523 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002524 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002525 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002526 unsigned int total_data_rate;
2527 int plane;
2528
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002529 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2530 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002531 if (alloc_size == 0) {
2532 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2533 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2534 return;
2535 }
2536
2537 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002538 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2539 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002540
2541 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002542 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002543
Damien Lespiau80958152015-02-09 13:35:10 +00002544 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002545 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002546 const struct intel_plane_wm_parameters *p;
2547
2548 p = &params->plane[plane];
2549 if (!p->enabled)
2550 continue;
2551
2552 minimum[plane] = 8;
2553 alloc_size -= minimum[plane];
2554 }
2555
Damien Lespiaub9cec072014-11-04 17:06:43 +00002556 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002557 * 2. Distribute the remaining space in proportion to the amount of
2558 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002559 *
2560 * FIXME: we may not allocate every single block here.
2561 */
2562 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2563
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002564 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002565 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2566 const struct intel_plane_wm_parameters *p;
2567 unsigned int data_rate;
2568 uint16_t plane_blocks;
2569
2570 p = &params->plane[plane];
2571 if (!p->enabled)
2572 continue;
2573
2574 data_rate = skl_plane_relative_data_rate(p);
2575
2576 /*
2577 * promote the expression to 64 bits to avoid overflowing, the
2578 * result is < available as data_rate / total_data_rate < 1
2579 */
Damien Lespiau80958152015-02-09 13:35:10 +00002580 plane_blocks = minimum[plane];
2581 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2582 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002583
2584 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002585 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002586
2587 start += plane_blocks;
2588 }
2589
2590}
2591
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002592static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002593{
2594 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002595 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002596}
2597
2598/*
2599 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2600 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2601 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2602 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2603*/
2604static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2605 uint32_t latency)
2606{
2607 uint32_t wm_intermediate_val, ret;
2608
2609 if (latency == 0)
2610 return UINT_MAX;
2611
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002612 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002613 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2614
2615 return ret;
2616}
2617
2618static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2619 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002620 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002621{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002622 uint32_t ret;
2623 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2624 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002625
2626 if (latency == 0)
2627 return UINT_MAX;
2628
2629 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002630
2631 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2632 tiling == I915_FORMAT_MOD_Yf_TILED) {
2633 plane_bytes_per_line *= 4;
2634 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2635 plane_blocks_per_line /= 4;
2636 } else {
2637 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2638 }
2639
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002640 wm_intermediate_val = latency * pixel_rate;
2641 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002642 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002643
2644 return ret;
2645}
2646
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002647static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2648 const struct intel_crtc *intel_crtc)
2649{
2650 struct drm_device *dev = intel_crtc->base.dev;
2651 struct drm_i915_private *dev_priv = dev->dev_private;
2652 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2653 enum pipe pipe = intel_crtc->pipe;
2654
2655 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2656 sizeof(new_ddb->plane[pipe])))
2657 return true;
2658
2659 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2660 sizeof(new_ddb->cursor[pipe])))
2661 return true;
2662
2663 return false;
2664}
2665
2666static void skl_compute_wm_global_parameters(struct drm_device *dev,
2667 struct intel_wm_config *config)
2668{
2669 struct drm_crtc *crtc;
2670 struct drm_plane *plane;
2671
2672 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2673 config->num_pipes_active += intel_crtc_active(crtc);
2674
2675 /* FIXME: I don't think we need those two global parameters on SKL */
2676 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2677 struct intel_plane *intel_plane = to_intel_plane(plane);
2678
2679 config->sprites_enabled |= intel_plane->wm.enabled;
2680 config->sprites_scaled |= intel_plane->wm.scaled;
2681 }
2682}
2683
2684static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2685 struct skl_pipe_wm_parameters *p)
2686{
2687 struct drm_device *dev = crtc->dev;
2688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2689 enum pipe pipe = intel_crtc->pipe;
2690 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002691 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002692 int i = 1; /* Index for sprite planes start */
2693
2694 p->active = intel_crtc_active(crtc);
2695 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002696 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2697 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002698
2699 /*
2700 * For now, assume primary and cursor planes are always enabled.
2701 */
2702 p->plane[0].enabled = true;
2703 p->plane[0].bytes_per_pixel =
Matt Roper59bea882015-02-27 10:12:01 -08002704 crtc->primary->state->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002705 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2706 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002707 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2708 fb = crtc->primary->state->fb;
2709 /*
2710 * Framebuffer can be NULL on plane disable, but it does not
2711 * matter for watermarks if we assume no tiling in that case.
2712 */
2713 if (fb)
2714 p->plane[0].tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002715
2716 p->cursor.enabled = true;
2717 p->cursor.bytes_per_pixel = 4;
Matt Roper3dd512f2015-02-27 10:12:00 -08002718 p->cursor.horiz_pixels = intel_crtc->base.cursor->state->crtc_w ?
2719 intel_crtc->base.cursor->state->crtc_w : 64;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002720 }
2721
2722 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2723 struct intel_plane *intel_plane = to_intel_plane(plane);
2724
Sonika Jindala712f8e2014-12-09 10:59:15 +05302725 if (intel_plane->pipe == pipe &&
2726 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002727 p->plane[i++] = intel_plane->wm;
2728 }
2729}
2730
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002731static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2732 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002733 struct intel_plane_wm_parameters *p_params,
2734 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002735 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002736 uint16_t *out_blocks, /* out */
2737 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002738{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002739 uint32_t latency = dev_priv->wm.skl_latency[level];
2740 uint32_t method1, method2;
2741 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2742 uint32_t res_blocks, res_lines;
2743 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002744
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002745 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002746 return false;
2747
2748 method1 = skl_wm_method1(p->pixel_rate,
2749 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002750 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002751 method2 = skl_wm_method2(p->pixel_rate,
2752 p->pipe_htotal,
2753 p_params->horiz_pixels,
2754 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002755 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002756 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002757
2758 plane_bytes_per_line = p_params->horiz_pixels *
2759 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002760 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002761
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002762 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2763 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2764 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2765 selected_result = max(method2, y_tile_minimum);
2766 } else {
2767 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2768 selected_result = min(method1, method2);
2769 else
2770 selected_result = method1;
2771 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002772
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002773 res_blocks = selected_result + 1;
2774 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002775
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002776 if (level >= 1 && level <= 7) {
2777 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2778 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2779 res_lines += 4;
2780 else
2781 res_blocks++;
2782 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002783
2784 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002785 return false;
2786
2787 *out_blocks = res_blocks;
2788 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002789
2790 return true;
2791}
2792
2793static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2794 struct skl_ddb_allocation *ddb,
2795 struct skl_pipe_wm_parameters *p,
2796 enum pipe pipe,
2797 int level,
2798 int num_planes,
2799 struct skl_wm_level *result)
2800{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002801 uint16_t ddb_blocks;
2802 int i;
2803
2804 for (i = 0; i < num_planes; i++) {
2805 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2806
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002807 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2808 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002809 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002810 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002811 &result->plane_res_b[i],
2812 &result->plane_res_l[i]);
2813 }
2814
2815 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002816 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2817 ddb_blocks, level,
2818 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002819 &result->cursor_res_l);
2820}
2821
Damien Lespiau407b50f2014-11-04 17:06:57 +00002822static uint32_t
2823skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2824{
2825 if (!intel_crtc_active(crtc))
2826 return 0;
2827
2828 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2829
2830}
2831
2832static void skl_compute_transition_wm(struct drm_crtc *crtc,
2833 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002834 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002835{
Damien Lespiau9414f562014-11-04 17:06:58 +00002836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2837 int i;
2838
Damien Lespiau407b50f2014-11-04 17:06:57 +00002839 if (!params->active)
2840 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002841
2842 /* Until we know more, just disable transition WMs */
2843 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2844 trans_wm->plane_en[i] = false;
2845 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002846}
2847
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002848static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2849 struct skl_ddb_allocation *ddb,
2850 struct skl_pipe_wm_parameters *params,
2851 struct skl_pipe_wm *pipe_wm)
2852{
2853 struct drm_device *dev = crtc->dev;
2854 const struct drm_i915_private *dev_priv = dev->dev_private;
2855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2856 int level, max_level = ilk_wm_max_level(dev);
2857
2858 for (level = 0; level <= max_level; level++) {
2859 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2860 level, intel_num_planes(intel_crtc),
2861 &pipe_wm->wm[level]);
2862 }
2863 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2864
Damien Lespiau9414f562014-11-04 17:06:58 +00002865 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002866}
2867
2868static void skl_compute_wm_results(struct drm_device *dev,
2869 struct skl_pipe_wm_parameters *p,
2870 struct skl_pipe_wm *p_wm,
2871 struct skl_wm_values *r,
2872 struct intel_crtc *intel_crtc)
2873{
2874 int level, max_level = ilk_wm_max_level(dev);
2875 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002876 uint32_t temp;
2877 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002878
2879 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002880 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2881 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002882
2883 temp |= p_wm->wm[level].plane_res_l[i] <<
2884 PLANE_WM_LINES_SHIFT;
2885 temp |= p_wm->wm[level].plane_res_b[i];
2886 if (p_wm->wm[level].plane_en[i])
2887 temp |= PLANE_WM_EN;
2888
2889 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002890 }
2891
2892 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002893
2894 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2895 temp |= p_wm->wm[level].cursor_res_b;
2896
2897 if (p_wm->wm[level].cursor_en)
2898 temp |= PLANE_WM_EN;
2899
2900 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002901
2902 }
2903
Damien Lespiau9414f562014-11-04 17:06:58 +00002904 /* transition WMs */
2905 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2906 temp = 0;
2907 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2908 temp |= p_wm->trans_wm.plane_res_b[i];
2909 if (p_wm->trans_wm.plane_en[i])
2910 temp |= PLANE_WM_EN;
2911
2912 r->plane_trans[pipe][i] = temp;
2913 }
2914
2915 temp = 0;
2916 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2917 temp |= p_wm->trans_wm.cursor_res_b;
2918 if (p_wm->trans_wm.cursor_en)
2919 temp |= PLANE_WM_EN;
2920
2921 r->cursor_trans[pipe] = temp;
2922
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002923 r->wm_linetime[pipe] = p_wm->linetime;
2924}
2925
Damien Lespiau16160e32014-11-04 17:06:53 +00002926static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2927 const struct skl_ddb_entry *entry)
2928{
2929 if (entry->end)
2930 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2931 else
2932 I915_WRITE(reg, 0);
2933}
2934
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002935static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2936 const struct skl_wm_values *new)
2937{
2938 struct drm_device *dev = dev_priv->dev;
2939 struct intel_crtc *crtc;
2940
2941 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
2942 int i, level, max_level = ilk_wm_max_level(dev);
2943 enum pipe pipe = crtc->pipe;
2944
Damien Lespiau5d374d92014-11-04 17:07:00 +00002945 if (!new->dirty[pipe])
2946 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002947
Damien Lespiau5d374d92014-11-04 17:07:00 +00002948 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
2949
2950 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002951 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00002952 I915_WRITE(PLANE_WM(pipe, i, level),
2953 new->plane[pipe][i][level]);
2954 I915_WRITE(CUR_WM(pipe, level),
2955 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002956 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00002957 for (i = 0; i < intel_num_planes(crtc); i++)
2958 I915_WRITE(PLANE_WM_TRANS(pipe, i),
2959 new->plane_trans[pipe][i]);
2960 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
2961
2962 for (i = 0; i < intel_num_planes(crtc); i++)
2963 skl_ddb_entry_write(dev_priv,
2964 PLANE_BUF_CFG(pipe, i),
2965 &new->ddb.plane[pipe][i]);
2966
2967 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
2968 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002969 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002970}
2971
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002972/*
2973 * When setting up a new DDB allocation arrangement, we need to correctly
2974 * sequence the times at which the new allocations for the pipes are taken into
2975 * account or we'll have pipes fetching from space previously allocated to
2976 * another pipe.
2977 *
2978 * Roughly the sequence looks like:
2979 * 1. re-allocate the pipe(s) with the allocation being reduced and not
2980 * overlapping with a previous light-up pipe (another way to put it is:
2981 * pipes with their new allocation strickly included into their old ones).
2982 * 2. re-allocate the other pipes that get their allocation reduced
2983 * 3. allocate the pipes having their allocation increased
2984 *
2985 * Steps 1. and 2. are here to take care of the following case:
2986 * - Initially DDB looks like this:
2987 * | B | C |
2988 * - enable pipe A.
2989 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
2990 * allocation
2991 * | A | B | C |
2992 *
2993 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
2994 */
2995
Damien Lespiaud21b7952014-11-04 17:07:03 +00002996static void
2997skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002998{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002999 int plane;
3000
Damien Lespiaud21b7952014-11-04 17:07:03 +00003001 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3002
Damien Lespiaudd740782015-02-28 14:54:08 +00003003 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003004 I915_WRITE(PLANE_SURF(pipe, plane),
3005 I915_READ(PLANE_SURF(pipe, plane)));
3006 }
3007 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3008}
3009
3010static bool
3011skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3012 const struct skl_ddb_allocation *new,
3013 enum pipe pipe)
3014{
3015 uint16_t old_size, new_size;
3016
3017 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3018 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3019
3020 return old_size != new_size &&
3021 new->pipe[pipe].start >= old->pipe[pipe].start &&
3022 new->pipe[pipe].end <= old->pipe[pipe].end;
3023}
3024
3025static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3026 struct skl_wm_values *new_values)
3027{
3028 struct drm_device *dev = dev_priv->dev;
3029 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3030 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3031 struct intel_crtc *crtc;
3032 enum pipe pipe;
3033
3034 new_ddb = &new_values->ddb;
3035 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3036
3037 /*
3038 * First pass: flush the pipes with the new allocation contained into
3039 * the old space.
3040 *
3041 * We'll wait for the vblank on those pipes to ensure we can safely
3042 * re-allocate the freed space without this pipe fetching from it.
3043 */
3044 for_each_intel_crtc(dev, crtc) {
3045 if (!crtc->active)
3046 continue;
3047
3048 pipe = crtc->pipe;
3049
3050 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3051 continue;
3052
Damien Lespiaud21b7952014-11-04 17:07:03 +00003053 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003054 intel_wait_for_vblank(dev, pipe);
3055
3056 reallocated[pipe] = true;
3057 }
3058
3059
3060 /*
3061 * Second pass: flush the pipes that are having their allocation
3062 * reduced, but overlapping with a previous allocation.
3063 *
3064 * Here as well we need to wait for the vblank to make sure the freed
3065 * space is not used anymore.
3066 */
3067 for_each_intel_crtc(dev, crtc) {
3068 if (!crtc->active)
3069 continue;
3070
3071 pipe = crtc->pipe;
3072
3073 if (reallocated[pipe])
3074 continue;
3075
3076 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3077 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003078 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003079 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303080 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003081 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003082 }
3083
3084 /*
3085 * Third pass: flush the pipes that got more space allocated.
3086 *
3087 * We don't need to actively wait for the update here, next vblank
3088 * will just get more DDB space with the correct WM values.
3089 */
3090 for_each_intel_crtc(dev, crtc) {
3091 if (!crtc->active)
3092 continue;
3093
3094 pipe = crtc->pipe;
3095
3096 /*
3097 * At this point, only the pipes more space than before are
3098 * left to re-allocate.
3099 */
3100 if (reallocated[pipe])
3101 continue;
3102
Damien Lespiaud21b7952014-11-04 17:07:03 +00003103 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003104 }
3105}
3106
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003107static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3108 struct skl_pipe_wm_parameters *params,
3109 struct intel_wm_config *config,
3110 struct skl_ddb_allocation *ddb, /* out */
3111 struct skl_pipe_wm *pipe_wm /* out */)
3112{
3113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3114
3115 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003116 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003117 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3118
3119 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3120 return false;
3121
3122 intel_crtc->wm.skl_active = *pipe_wm;
3123 return true;
3124}
3125
3126static void skl_update_other_pipe_wm(struct drm_device *dev,
3127 struct drm_crtc *crtc,
3128 struct intel_wm_config *config,
3129 struct skl_wm_values *r)
3130{
3131 struct intel_crtc *intel_crtc;
3132 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3133
3134 /*
3135 * If the WM update hasn't changed the allocation for this_crtc (the
3136 * crtc we are currently computing the new WM values for), other
3137 * enabled crtcs will keep the same allocation and we don't need to
3138 * recompute anything for them.
3139 */
3140 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3141 return;
3142
3143 /*
3144 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3145 * other active pipes need new DDB allocation and WM values.
3146 */
3147 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3148 base.head) {
3149 struct skl_pipe_wm_parameters params = {};
3150 struct skl_pipe_wm pipe_wm = {};
3151 bool wm_changed;
3152
3153 if (this_crtc->pipe == intel_crtc->pipe)
3154 continue;
3155
3156 if (!intel_crtc->active)
3157 continue;
3158
3159 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3160 &params, config,
3161 &r->ddb, &pipe_wm);
3162
3163 /*
3164 * If we end up re-computing the other pipe WM values, it's
3165 * because it was really needed, so we expect the WM values to
3166 * be different.
3167 */
3168 WARN_ON(!wm_changed);
3169
3170 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3171 r->dirty[intel_crtc->pipe] = true;
3172 }
3173}
3174
3175static void skl_update_wm(struct drm_crtc *crtc)
3176{
3177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3178 struct drm_device *dev = crtc->dev;
3179 struct drm_i915_private *dev_priv = dev->dev_private;
3180 struct skl_pipe_wm_parameters params = {};
3181 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3182 struct skl_pipe_wm pipe_wm = {};
3183 struct intel_wm_config config = {};
3184
3185 memset(results, 0, sizeof(*results));
3186
3187 skl_compute_wm_global_parameters(dev, &config);
3188
3189 if (!skl_update_pipe_wm(crtc, &params, &config,
3190 &results->ddb, &pipe_wm))
3191 return;
3192
3193 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3194 results->dirty[intel_crtc->pipe] = true;
3195
3196 skl_update_other_pipe_wm(dev, crtc, &config, results);
3197 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003198 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003199
3200 /* store the new configuration */
3201 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003202}
3203
3204static void
3205skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3206 uint32_t sprite_width, uint32_t sprite_height,
3207 int pixel_size, bool enabled, bool scaled)
3208{
3209 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003210 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003211
3212 intel_plane->wm.enabled = enabled;
3213 intel_plane->wm.scaled = scaled;
3214 intel_plane->wm.horiz_pixels = sprite_width;
3215 intel_plane->wm.vert_pixels = sprite_height;
3216 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003217 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3218 /*
3219 * Framebuffer can be NULL on plane disable, but it does not
3220 * matter for watermarks if we assume no tiling in that case.
3221 */
3222 if (fb)
3223 intel_plane->wm.tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003224
3225 skl_update_wm(crtc);
3226}
3227
Imre Deak820c1982013-12-17 14:46:36 +02003228static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003229{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003231 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003232 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003233 struct ilk_wm_maximums max;
3234 struct ilk_pipe_wm_parameters params = {};
3235 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003236 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003237 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003238 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003239 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003240
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003241 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003242
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003243 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3244
3245 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3246 return;
3247
3248 intel_crtc->wm.active = pipe_wm;
3249
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003250 ilk_compute_wm_config(dev, &config);
3251
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003252 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003253 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003254
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003255 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003256 if (INTEL_INFO(dev)->gen >= 7 &&
3257 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003258 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003259 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003260
Imre Deak820c1982013-12-17 14:46:36 +02003261 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003262 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003263 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003264 }
3265
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003266 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003267 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003268
Imre Deak820c1982013-12-17 14:46:36 +02003269 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003270
Imre Deak820c1982013-12-17 14:46:36 +02003271 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003272}
3273
Damien Lespiaued57cb82014-07-15 09:21:24 +02003274static void
3275ilk_update_sprite_wm(struct drm_plane *plane,
3276 struct drm_crtc *crtc,
3277 uint32_t sprite_width, uint32_t sprite_height,
3278 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003279{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003280 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003281 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003282
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003283 intel_plane->wm.enabled = enabled;
3284 intel_plane->wm.scaled = scaled;
3285 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003286 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003287 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003288
Ville Syrjälä8553c182013-12-05 15:51:39 +02003289 /*
3290 * IVB workaround: must disable low power watermarks for at least
3291 * one frame before enabling scaling. LP watermarks can be re-enabled
3292 * when scaling is disabled.
3293 *
3294 * WaCxSRDisabledForSpriteScaling:ivb
3295 */
3296 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3297 intel_wait_for_vblank(dev, intel_plane->pipe);
3298
Imre Deak820c1982013-12-17 14:46:36 +02003299 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003300}
3301
Pradeep Bhat30789992014-11-04 17:06:45 +00003302static void skl_pipe_wm_active_state(uint32_t val,
3303 struct skl_pipe_wm *active,
3304 bool is_transwm,
3305 bool is_cursor,
3306 int i,
3307 int level)
3308{
3309 bool is_enabled = (val & PLANE_WM_EN) != 0;
3310
3311 if (!is_transwm) {
3312 if (!is_cursor) {
3313 active->wm[level].plane_en[i] = is_enabled;
3314 active->wm[level].plane_res_b[i] =
3315 val & PLANE_WM_BLOCKS_MASK;
3316 active->wm[level].plane_res_l[i] =
3317 (val >> PLANE_WM_LINES_SHIFT) &
3318 PLANE_WM_LINES_MASK;
3319 } else {
3320 active->wm[level].cursor_en = is_enabled;
3321 active->wm[level].cursor_res_b =
3322 val & PLANE_WM_BLOCKS_MASK;
3323 active->wm[level].cursor_res_l =
3324 (val >> PLANE_WM_LINES_SHIFT) &
3325 PLANE_WM_LINES_MASK;
3326 }
3327 } else {
3328 if (!is_cursor) {
3329 active->trans_wm.plane_en[i] = is_enabled;
3330 active->trans_wm.plane_res_b[i] =
3331 val & PLANE_WM_BLOCKS_MASK;
3332 active->trans_wm.plane_res_l[i] =
3333 (val >> PLANE_WM_LINES_SHIFT) &
3334 PLANE_WM_LINES_MASK;
3335 } else {
3336 active->trans_wm.cursor_en = is_enabled;
3337 active->trans_wm.cursor_res_b =
3338 val & PLANE_WM_BLOCKS_MASK;
3339 active->trans_wm.cursor_res_l =
3340 (val >> PLANE_WM_LINES_SHIFT) &
3341 PLANE_WM_LINES_MASK;
3342 }
3343 }
3344}
3345
3346static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3347{
3348 struct drm_device *dev = crtc->dev;
3349 struct drm_i915_private *dev_priv = dev->dev_private;
3350 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3352 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3353 enum pipe pipe = intel_crtc->pipe;
3354 int level, i, max_level;
3355 uint32_t temp;
3356
3357 max_level = ilk_wm_max_level(dev);
3358
3359 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3360
3361 for (level = 0; level <= max_level; level++) {
3362 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3363 hw->plane[pipe][i][level] =
3364 I915_READ(PLANE_WM(pipe, i, level));
3365 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3366 }
3367
3368 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3369 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3370 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3371
3372 if (!intel_crtc_active(crtc))
3373 return;
3374
3375 hw->dirty[pipe] = true;
3376
3377 active->linetime = hw->wm_linetime[pipe];
3378
3379 for (level = 0; level <= max_level; level++) {
3380 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3381 temp = hw->plane[pipe][i][level];
3382 skl_pipe_wm_active_state(temp, active, false,
3383 false, i, level);
3384 }
3385 temp = hw->cursor[pipe][level];
3386 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3387 }
3388
3389 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3390 temp = hw->plane_trans[pipe][i];
3391 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3392 }
3393
3394 temp = hw->cursor_trans[pipe];
3395 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3396}
3397
3398void skl_wm_get_hw_state(struct drm_device *dev)
3399{
Damien Lespiaua269c582014-11-04 17:06:49 +00003400 struct drm_i915_private *dev_priv = dev->dev_private;
3401 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003402 struct drm_crtc *crtc;
3403
Damien Lespiaua269c582014-11-04 17:06:49 +00003404 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003405 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3406 skl_pipe_wm_get_hw_state(crtc);
3407}
3408
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003409static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3410{
3411 struct drm_device *dev = crtc->dev;
3412 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003413 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3415 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3416 enum pipe pipe = intel_crtc->pipe;
3417 static const unsigned int wm0_pipe_reg[] = {
3418 [PIPE_A] = WM0_PIPEA_ILK,
3419 [PIPE_B] = WM0_PIPEB_ILK,
3420 [PIPE_C] = WM0_PIPEC_IVB,
3421 };
3422
3423 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003424 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003425 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003426
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003427 active->pipe_enabled = intel_crtc_active(crtc);
3428
3429 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003430 u32 tmp = hw->wm_pipe[pipe];
3431
3432 /*
3433 * For active pipes LP0 watermark is marked as
3434 * enabled, and LP1+ watermaks as disabled since
3435 * we can't really reverse compute them in case
3436 * multiple pipes are active.
3437 */
3438 active->wm[0].enable = true;
3439 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3440 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3441 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3442 active->linetime = hw->wm_linetime[pipe];
3443 } else {
3444 int level, max_level = ilk_wm_max_level(dev);
3445
3446 /*
3447 * For inactive pipes, all watermark levels
3448 * should be marked as enabled but zeroed,
3449 * which is what we'd compute them to.
3450 */
3451 for (level = 0; level <= max_level; level++)
3452 active->wm[level].enable = true;
3453 }
3454}
3455
3456void ilk_wm_get_hw_state(struct drm_device *dev)
3457{
3458 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003459 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003460 struct drm_crtc *crtc;
3461
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003462 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003463 ilk_pipe_wm_get_hw_state(crtc);
3464
3465 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3466 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3467 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3468
3469 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003470 if (INTEL_INFO(dev)->gen >= 7) {
3471 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3472 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3473 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003474
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003475 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003476 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3477 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3478 else if (IS_IVYBRIDGE(dev))
3479 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3480 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003481
3482 hw->enable_fbc_wm =
3483 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3484}
3485
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003486/**
3487 * intel_update_watermarks - update FIFO watermark values based on current modes
3488 *
3489 * Calculate watermark values for the various WM regs based on current mode
3490 * and plane configuration.
3491 *
3492 * There are several cases to deal with here:
3493 * - normal (i.e. non-self-refresh)
3494 * - self-refresh (SR) mode
3495 * - lines are large relative to FIFO size (buffer can hold up to 2)
3496 * - lines are small relative to FIFO size (buffer can hold more than 2
3497 * lines), so need to account for TLB latency
3498 *
3499 * The normal calculation is:
3500 * watermark = dotclock * bytes per pixel * latency
3501 * where latency is platform & configuration dependent (we assume pessimal
3502 * values here).
3503 *
3504 * The SR calculation is:
3505 * watermark = (trunc(latency/line time)+1) * surface width *
3506 * bytes per pixel
3507 * where
3508 * line time = htotal / dotclock
3509 * surface width = hdisplay for normal plane and 64 for cursor
3510 * and latency is assumed to be high, as above.
3511 *
3512 * The final value programmed to the register should always be rounded up,
3513 * and include an extra 2 entries to account for clock crossings.
3514 *
3515 * We don't use the sprite, so we can ignore that. And on Crestline we have
3516 * to set the non-SR watermarks to 8.
3517 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003518void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003519{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003520 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003521
3522 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003523 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003524}
3525
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003526void intel_update_sprite_watermarks(struct drm_plane *plane,
3527 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003528 uint32_t sprite_width,
3529 uint32_t sprite_height,
3530 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003531 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003532{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003533 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003534
3535 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003536 dev_priv->display.update_sprite_wm(plane, crtc,
3537 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003538 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003539}
3540
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003541static struct drm_i915_gem_object *
3542intel_alloc_context_page(struct drm_device *dev)
3543{
3544 struct drm_i915_gem_object *ctx;
3545 int ret;
3546
3547 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3548
3549 ctx = i915_gem_alloc_object(dev, 4096);
3550 if (!ctx) {
3551 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3552 return NULL;
3553 }
3554
Daniel Vetterc69766f2014-02-14 14:01:17 +01003555 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003556 if (ret) {
3557 DRM_ERROR("failed to pin power context: %d\n", ret);
3558 goto err_unref;
3559 }
3560
3561 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3562 if (ret) {
3563 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3564 goto err_unpin;
3565 }
3566
3567 return ctx;
3568
3569err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003570 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003571err_unref:
3572 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003573 return NULL;
3574}
3575
Daniel Vetter92703882012-08-09 16:46:01 +02003576/**
3577 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003578 */
3579DEFINE_SPINLOCK(mchdev_lock);
3580
3581/* Global for IPS driver to get at the current i915 device. Protected by
3582 * mchdev_lock. */
3583static struct drm_i915_private *i915_mch_dev;
3584
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003585bool ironlake_set_drps(struct drm_device *dev, u8 val)
3586{
3587 struct drm_i915_private *dev_priv = dev->dev_private;
3588 u16 rgvswctl;
3589
Daniel Vetter92703882012-08-09 16:46:01 +02003590 assert_spin_locked(&mchdev_lock);
3591
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003592 rgvswctl = I915_READ16(MEMSWCTL);
3593 if (rgvswctl & MEMCTL_CMD_STS) {
3594 DRM_DEBUG("gpu busy, RCS change rejected\n");
3595 return false; /* still busy with another command */
3596 }
3597
3598 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3599 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3600 I915_WRITE16(MEMSWCTL, rgvswctl);
3601 POSTING_READ16(MEMSWCTL);
3602
3603 rgvswctl |= MEMCTL_CMD_STS;
3604 I915_WRITE16(MEMSWCTL, rgvswctl);
3605
3606 return true;
3607}
3608
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003609static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003610{
3611 struct drm_i915_private *dev_priv = dev->dev_private;
3612 u32 rgvmodectl = I915_READ(MEMMODECTL);
3613 u8 fmax, fmin, fstart, vstart;
3614
Daniel Vetter92703882012-08-09 16:46:01 +02003615 spin_lock_irq(&mchdev_lock);
3616
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003617 /* Enable temp reporting */
3618 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3619 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3620
3621 /* 100ms RC evaluation intervals */
3622 I915_WRITE(RCUPEI, 100000);
3623 I915_WRITE(RCDNEI, 100000);
3624
3625 /* Set max/min thresholds to 90ms and 80ms respectively */
3626 I915_WRITE(RCBMAXAVG, 90000);
3627 I915_WRITE(RCBMINAVG, 80000);
3628
3629 I915_WRITE(MEMIHYST, 1);
3630
3631 /* Set up min, max, and cur for interrupt handling */
3632 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3633 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3634 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3635 MEMMODE_FSTART_SHIFT;
3636
3637 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3638 PXVFREQ_PX_SHIFT;
3639
Daniel Vetter20e4d402012-08-08 23:35:39 +02003640 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3641 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003642
Daniel Vetter20e4d402012-08-08 23:35:39 +02003643 dev_priv->ips.max_delay = fstart;
3644 dev_priv->ips.min_delay = fmin;
3645 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003646
3647 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3648 fmax, fmin, fstart);
3649
3650 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3651
3652 /*
3653 * Interrupts will be enabled in ironlake_irq_postinstall
3654 */
3655
3656 I915_WRITE(VIDSTART, vstart);
3657 POSTING_READ(VIDSTART);
3658
3659 rgvmodectl |= MEMMODE_SWMODE_EN;
3660 I915_WRITE(MEMMODECTL, rgvmodectl);
3661
Daniel Vetter92703882012-08-09 16:46:01 +02003662 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003663 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003664 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003665
3666 ironlake_set_drps(dev, fstart);
3667
Daniel Vetter20e4d402012-08-08 23:35:39 +02003668 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003669 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003670 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3671 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003672 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003673
3674 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003675}
3676
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003677static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003678{
3679 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003680 u16 rgvswctl;
3681
3682 spin_lock_irq(&mchdev_lock);
3683
3684 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003685
3686 /* Ack interrupts, disable EFC interrupt */
3687 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3688 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3689 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3690 I915_WRITE(DEIIR, DE_PCU_EVENT);
3691 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3692
3693 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003694 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003695 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003696 rgvswctl |= MEMCTL_CMD_STS;
3697 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003698 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003699
Daniel Vetter92703882012-08-09 16:46:01 +02003700 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003701}
3702
Daniel Vetteracbe9472012-07-26 11:50:05 +02003703/* There's a funny hw issue where the hw returns all 0 when reading from
3704 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3705 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3706 * all limits and the gpu stuck at whatever frequency it is at atm).
3707 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003708static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003709{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003710 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003711
Daniel Vetter20b46e52012-07-26 11:16:14 +02003712 /* Only set the down limit when we've reached the lowest level to avoid
3713 * getting more interrupts, otherwise leave this clear. This prevents a
3714 * race in the hw when coming out of rc6: There's a tiny window where
3715 * the hw runs at the minimal clock before selecting the desired
3716 * frequency, if the down threshold expires in that window we will not
3717 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003718 limits = dev_priv->rps.max_freq_softlimit << 24;
3719 if (val <= dev_priv->rps.min_freq_softlimit)
3720 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003721
3722 return limits;
3723}
3724
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003725static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3726{
3727 int new_power;
3728
3729 new_power = dev_priv->rps.power;
3730 switch (dev_priv->rps.power) {
3731 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003732 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003733 new_power = BETWEEN;
3734 break;
3735
3736 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003737 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003738 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003739 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003740 new_power = HIGH_POWER;
3741 break;
3742
3743 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003744 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003745 new_power = BETWEEN;
3746 break;
3747 }
3748 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003749 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003750 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003751 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003752 new_power = HIGH_POWER;
3753 if (new_power == dev_priv->rps.power)
3754 return;
3755
3756 /* Note the units here are not exactly 1us, but 1280ns. */
3757 switch (new_power) {
3758 case LOW_POWER:
3759 /* Upclock if more than 95% busy over 16ms */
3760 I915_WRITE(GEN6_RP_UP_EI, 12500);
3761 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3762
3763 /* Downclock if less than 85% busy over 32ms */
3764 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3765 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3766
3767 I915_WRITE(GEN6_RP_CONTROL,
3768 GEN6_RP_MEDIA_TURBO |
3769 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3770 GEN6_RP_MEDIA_IS_GFX |
3771 GEN6_RP_ENABLE |
3772 GEN6_RP_UP_BUSY_AVG |
3773 GEN6_RP_DOWN_IDLE_AVG);
3774 break;
3775
3776 case BETWEEN:
3777 /* Upclock if more than 90% busy over 13ms */
3778 I915_WRITE(GEN6_RP_UP_EI, 10250);
3779 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3780
3781 /* Downclock if less than 75% busy over 32ms */
3782 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3783 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3784
3785 I915_WRITE(GEN6_RP_CONTROL,
3786 GEN6_RP_MEDIA_TURBO |
3787 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3788 GEN6_RP_MEDIA_IS_GFX |
3789 GEN6_RP_ENABLE |
3790 GEN6_RP_UP_BUSY_AVG |
3791 GEN6_RP_DOWN_IDLE_AVG);
3792 break;
3793
3794 case HIGH_POWER:
3795 /* Upclock if more than 85% busy over 10ms */
3796 I915_WRITE(GEN6_RP_UP_EI, 8000);
3797 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3798
3799 /* Downclock if less than 60% busy over 32ms */
3800 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3801 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3802
3803 I915_WRITE(GEN6_RP_CONTROL,
3804 GEN6_RP_MEDIA_TURBO |
3805 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3806 GEN6_RP_MEDIA_IS_GFX |
3807 GEN6_RP_ENABLE |
3808 GEN6_RP_UP_BUSY_AVG |
3809 GEN6_RP_DOWN_IDLE_AVG);
3810 break;
3811 }
3812
3813 dev_priv->rps.power = new_power;
3814 dev_priv->rps.last_adj = 0;
3815}
3816
Chris Wilson2876ce72014-03-28 08:03:34 +00003817static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3818{
3819 u32 mask = 0;
3820
3821 if (val > dev_priv->rps.min_freq_softlimit)
3822 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3823 if (val < dev_priv->rps.max_freq_softlimit)
3824 mask |= GEN6_PM_RP_UP_THRESHOLD;
3825
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003826 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3827 mask &= dev_priv->pm_rps_events;
3828
Imre Deak59d02a12014-12-19 19:33:26 +02003829 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003830}
3831
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003832/* gen6_set_rps is called to update the frequency request, but should also be
3833 * called when the range (min_delay and max_delay) is modified so that we can
3834 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003835static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003836{
3837 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003838
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003839 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003840 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3841 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003842
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003843 /* min/max delay may still have been modified so be sure to
3844 * write the limits value.
3845 */
3846 if (val != dev_priv->rps.cur_freq) {
3847 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003848
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003849 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003850 I915_WRITE(GEN6_RPNSWREQ,
3851 HSW_FREQUENCY(val));
3852 else
3853 I915_WRITE(GEN6_RPNSWREQ,
3854 GEN6_FREQUENCY(val) |
3855 GEN6_OFFSET(0) |
3856 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003857 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003858
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003859 /* Make sure we continue to get interrupts
3860 * until we hit the minimum or maximum frequencies.
3861 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003862 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003863 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003864
Ben Widawskyd5570a72012-09-07 19:43:41 -07003865 POSTING_READ(GEN6_RPNSWREQ);
3866
Ben Widawskyb39fb292014-03-19 18:31:11 -07003867 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003868 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003869}
3870
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003871static void valleyview_set_rps(struct drm_device *dev, u8 val)
3872{
3873 struct drm_i915_private *dev_priv = dev->dev_private;
3874
3875 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3876 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3877 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3878
3879 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3880 "Odd GPU freq value\n"))
3881 val &= ~1;
3882
3883 if (val != dev_priv->rps.cur_freq)
3884 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3885
3886 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3887
3888 dev_priv->rps.cur_freq = val;
3889 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3890}
3891
Deepak S76c3552f2014-01-30 23:08:16 +05303892/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3893 *
3894 * * If Gfx is Idle, then
3895 * 1. Mask Turbo interrupts
3896 * 2. Bring up Gfx clock
3897 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3898 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3899 * 5. Unmask Turbo interrupts
3900*/
3901static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3902{
Deepak S5549d252014-06-28 11:26:11 +05303903 struct drm_device *dev = dev_priv->dev;
3904
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003905 /* CHV and latest VLV don't need to force the gfx clock */
3906 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303907 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3908 return;
3909 }
3910
Deepak S76c3552f2014-01-30 23:08:16 +05303911 /*
3912 * When we are idle. Drop to min voltage state.
3913 */
3914
Ben Widawskyb39fb292014-03-19 18:31:11 -07003915 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303916 return;
3917
3918 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003919 I915_WRITE(GEN6_PMINTRMSK,
3920 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303921
Imre Deak650ad972014-04-18 16:35:02 +03003922 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303923
Ben Widawskyb39fb292014-03-19 18:31:11 -07003924 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303925
3926 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003927 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303928
3929 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003930 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303931 DRM_ERROR("timed out waiting for Punit\n");
3932
Imre Deak650ad972014-04-18 16:35:02 +03003933 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303934
Chris Wilson2876ce72014-03-28 08:03:34 +00003935 I915_WRITE(GEN6_PMINTRMSK,
3936 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303937}
3938
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003939void gen6_rps_idle(struct drm_i915_private *dev_priv)
3940{
Damien Lespiau691bb712013-12-12 14:36:36 +00003941 struct drm_device *dev = dev_priv->dev;
3942
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003943 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003944 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003945 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303946 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003947 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003948 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003949 dev_priv->rps.last_adj = 0;
3950 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003951 mutex_unlock(&dev_priv->rps.hw_lock);
3952}
3953
3954void gen6_rps_boost(struct drm_i915_private *dev_priv)
3955{
3956 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003957 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003958 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003959 dev_priv->rps.last_adj = 0;
3960 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003961 mutex_unlock(&dev_priv->rps.hw_lock);
3962}
3963
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003964void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003965{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003966 if (IS_VALLEYVIEW(dev))
3967 valleyview_set_rps(dev, val);
3968 else
3969 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003970}
3971
Zhe Wang20e49362014-11-04 17:07:05 +00003972static void gen9_disable_rps(struct drm_device *dev)
3973{
3974 struct drm_i915_private *dev_priv = dev->dev_private;
3975
3976 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00003977 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00003978}
3979
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003980static void gen6_disable_rps(struct drm_device *dev)
3981{
3982 struct drm_i915_private *dev_priv = dev->dev_private;
3983
3984 I915_WRITE(GEN6_RC_CONTROL, 0);
3985 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003986}
3987
Deepak S38807742014-05-23 21:00:15 +05303988static void cherryview_disable_rps(struct drm_device *dev)
3989{
3990 struct drm_i915_private *dev_priv = dev->dev_private;
3991
3992 I915_WRITE(GEN6_RC_CONTROL, 0);
3993}
3994
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003995static void valleyview_disable_rps(struct drm_device *dev)
3996{
3997 struct drm_i915_private *dev_priv = dev->dev_private;
3998
Deepak S98a2e5f2014-08-18 10:35:27 -07003999 /* we're doing forcewake before Disabling RC6,
4000 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004001 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004002
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004003 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004004
Mika Kuoppala59bad942015-01-16 11:34:40 +02004005 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004006}
4007
Ben Widawskydc39fff2013-10-18 12:32:07 -07004008static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4009{
Imre Deak91ca6892014-04-14 20:24:25 +03004010 if (IS_VALLEYVIEW(dev)) {
4011 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4012 mode = GEN6_RC_CTL_RC6_ENABLE;
4013 else
4014 mode = 0;
4015 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004016 if (HAS_RC6p(dev))
4017 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4018 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4019 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4020 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4021
4022 else
4023 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4024 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004025}
4026
Imre Deake6069ca2014-04-18 16:01:02 +03004027static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004028{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004029 /* No RC6 before Ironlake */
4030 if (INTEL_INFO(dev)->gen < 5)
4031 return 0;
4032
Imre Deake6069ca2014-04-18 16:01:02 +03004033 /* RC6 is only on Ironlake mobile not on desktop */
4034 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4035 return 0;
4036
Daniel Vetter456470e2012-08-08 23:35:40 +02004037 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004038 if (enable_rc6 >= 0) {
4039 int mask;
4040
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004041 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004042 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4043 INTEL_RC6pp_ENABLE;
4044 else
4045 mask = INTEL_RC6_ENABLE;
4046
4047 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004048 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4049 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004050
4051 return enable_rc6 & mask;
4052 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004053
Chris Wilson6567d742012-11-10 10:00:06 +00004054 /* Disable RC6 on Ironlake */
4055 if (INTEL_INFO(dev)->gen == 5)
4056 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004057
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004058 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004059 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004060
4061 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004062}
4063
Imre Deake6069ca2014-04-18 16:01:02 +03004064int intel_enable_rc6(const struct drm_device *dev)
4065{
4066 return i915.enable_rc6;
4067}
4068
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004069static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004070{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004071 struct drm_i915_private *dev_priv = dev->dev_private;
4072 uint32_t rp_state_cap;
4073 u32 ddcc_status = 0;
4074 int ret;
4075
4076 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004077 /* All of these values are in units of 50MHz */
4078 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004079 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004080 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004081 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004082 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004083 /* hw_max = RP0 until we check for overclocking */
4084 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4085
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004086 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4087 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4088 ret = sandybridge_pcode_read(dev_priv,
4089 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4090 &ddcc_status);
4091 if (0 == ret)
4092 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004093 clamp_t(u8,
4094 ((ddcc_status >> 8) & 0xff),
4095 dev_priv->rps.min_freq,
4096 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004097 }
4098
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004099 /* Preserve min/max settings in case of re-init */
4100 if (dev_priv->rps.max_freq_softlimit == 0)
4101 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4102
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004103 if (dev_priv->rps.min_freq_softlimit == 0) {
4104 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4105 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004106 /* max(RPe, 450 MHz) */
4107 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004108 else
4109 dev_priv->rps.min_freq_softlimit =
4110 dev_priv->rps.min_freq;
4111 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004112}
4113
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004114/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004115static void gen9_enable_rps(struct drm_device *dev)
4116{
4117 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004118
4119 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4120
Damien Lespiauba1c5542015-01-16 18:07:26 +00004121 gen6_init_rps_frequencies(dev);
4122
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004123 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4124 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4125
4126 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4127 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4128 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4129 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4130 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4131 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4132 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4133 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4134 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4135 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4136 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4137 GEN6_RP_DOWN_IDLE_AVG);
4138
4139 gen6_enable_rps_interrupts(dev);
4140
4141 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4142}
4143
4144static void gen9_enable_rc6(struct drm_device *dev)
4145{
4146 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004147 struct intel_engine_cs *ring;
4148 uint32_t rc6_mask = 0;
4149 int unused;
4150
4151 /* 1a: Software RC state - RC0 */
4152 I915_WRITE(GEN6_RC_STATE, 0);
4153
4154 /* 1b: Get forcewake during program sequence. Although the driver
4155 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004156 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004157
4158 /* 2a: Disable RC states. */
4159 I915_WRITE(GEN6_RC_CONTROL, 0);
4160
4161 /* 2b: Program RC6 thresholds.*/
4162 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4163 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4164 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4165 for_each_ring(ring, dev_priv, unused)
4166 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4167 I915_WRITE(GEN6_RC_SLEEP, 0);
4168 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4169
Zhe Wang38c23522015-01-20 12:23:04 +00004170 /* 2c: Program Coarse Power Gating Policies. */
4171 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4172 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4173
Zhe Wang20e49362014-11-04 17:07:05 +00004174 /* 3a: Enable RC6 */
4175 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4176 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4177 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4178 "on" : "off");
4179 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4180 GEN6_RC_CTL_EI_MODE(1) |
4181 rc6_mask);
4182
Zhe Wang38c23522015-01-20 12:23:04 +00004183 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4184 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4185
Mika Kuoppala59bad942015-01-16 11:34:40 +02004186 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004187
4188}
4189
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004190static void gen8_enable_rps(struct drm_device *dev)
4191{
4192 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004193 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004194 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004195 int unused;
4196
4197 /* 1a: Software RC state - RC0 */
4198 I915_WRITE(GEN6_RC_STATE, 0);
4199
4200 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4201 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004202 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004203
4204 /* 2a: Disable RC states. */
4205 I915_WRITE(GEN6_RC_CONTROL, 0);
4206
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004207 /* Initialize rps frequencies */
4208 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004209
4210 /* 2b: Program RC6 thresholds.*/
4211 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4212 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4213 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4214 for_each_ring(ring, dev_priv, unused)
4215 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4216 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004217 if (IS_BROADWELL(dev))
4218 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4219 else
4220 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004221
4222 /* 3: Enable RC6 */
4223 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4224 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004225 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004226 if (IS_BROADWELL(dev))
4227 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4228 GEN7_RC_CTL_TO_MODE |
4229 rc6_mask);
4230 else
4231 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4232 GEN6_RC_CTL_EI_MODE(1) |
4233 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004234
4235 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004236 I915_WRITE(GEN6_RPNSWREQ,
4237 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4238 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4239 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004240 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4241 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004242
Daniel Vetter7526ed72014-09-29 15:07:19 +02004243 /* Docs recommend 900MHz, and 300 MHz respectively */
4244 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4245 dev_priv->rps.max_freq_softlimit << 24 |
4246 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004247
Daniel Vetter7526ed72014-09-29 15:07:19 +02004248 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4249 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4250 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4251 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004252
Daniel Vetter7526ed72014-09-29 15:07:19 +02004253 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004254
4255 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004256 I915_WRITE(GEN6_RP_CONTROL,
4257 GEN6_RP_MEDIA_TURBO |
4258 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4259 GEN6_RP_MEDIA_IS_GFX |
4260 GEN6_RP_ENABLE |
4261 GEN6_RP_UP_BUSY_AVG |
4262 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004263
Daniel Vetter7526ed72014-09-29 15:07:19 +02004264 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004265
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004266 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4267 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004268
Mika Kuoppala59bad942015-01-16 11:34:40 +02004269 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004270}
4271
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004272static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004273{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004274 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004275 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004276 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004277 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004278 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004279 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004280
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004281 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004282
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004283 /* Here begins a magic sequence of register writes to enable
4284 * auto-downclocking.
4285 *
4286 * Perhaps there might be some value in exposing these to
4287 * userspace...
4288 */
4289 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004290
4291 /* Clear the DBG now so we don't confuse earlier errors */
4292 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4293 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4294 I915_WRITE(GTFIFODBG, gtfifodbg);
4295 }
4296
Mika Kuoppala59bad942015-01-16 11:34:40 +02004297 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004298
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004299 /* Initialize rps frequencies */
4300 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004301
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004302 /* disable the counters and set deterministic thresholds */
4303 I915_WRITE(GEN6_RC_CONTROL, 0);
4304
4305 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4306 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4307 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4308 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4309 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4310
Chris Wilsonb4519512012-05-11 14:29:30 +01004311 for_each_ring(ring, dev_priv, i)
4312 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004313
4314 I915_WRITE(GEN6_RC_SLEEP, 0);
4315 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004316 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004317 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4318 else
4319 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004320 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004321 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4322
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004323 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004324 rc6_mode = intel_enable_rc6(dev_priv->dev);
4325 if (rc6_mode & INTEL_RC6_ENABLE)
4326 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4327
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004328 /* We don't use those on Haswell */
4329 if (!IS_HASWELL(dev)) {
4330 if (rc6_mode & INTEL_RC6p_ENABLE)
4331 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004332
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004333 if (rc6_mode & INTEL_RC6pp_ENABLE)
4334 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4335 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004336
Ben Widawskydc39fff2013-10-18 12:32:07 -07004337 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004338
4339 I915_WRITE(GEN6_RC_CONTROL,
4340 rc6_mask |
4341 GEN6_RC_CTL_EI_MODE(1) |
4342 GEN6_RC_CTL_HW_ENABLE);
4343
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004344 /* Power down if completely idle for over 50ms */
4345 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004346 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004347
Ben Widawsky42c05262012-09-26 10:34:00 -07004348 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004349 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004350 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004351
4352 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4353 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4354 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004355 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004356 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004357 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004358 }
4359
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004360 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004361 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004362
Ben Widawsky31643d52012-09-26 10:34:01 -07004363 rc6vids = 0;
4364 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4365 if (IS_GEN6(dev) && ret) {
4366 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4367 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4368 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4369 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4370 rc6vids &= 0xffff00;
4371 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4372 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4373 if (ret)
4374 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4375 }
4376
Mika Kuoppala59bad942015-01-16 11:34:40 +02004377 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004378}
4379
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004380static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004381{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004382 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004383 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004384 unsigned int gpu_freq;
4385 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004386 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004387 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004388
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004389 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004390
Ben Widawskyeda79642013-10-07 17:15:48 -03004391 policy = cpufreq_cpu_get(0);
4392 if (policy) {
4393 max_ia_freq = policy->cpuinfo.max_freq;
4394 cpufreq_cpu_put(policy);
4395 } else {
4396 /*
4397 * Default to measured freq if none found, PCU will ensure we
4398 * don't go over
4399 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004400 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004401 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004402
4403 /* Convert from kHz to MHz */
4404 max_ia_freq /= 1000;
4405
Ben Widawsky153b4b952013-10-22 22:05:09 -07004406 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004407 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4408 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004409
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004410 /*
4411 * For each potential GPU frequency, load a ring frequency we'd like
4412 * to use for memory access. We do this by specifying the IA frequency
4413 * the PCU should use as a reference to determine the ring frequency.
4414 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004415 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004416 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004417 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004418 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004419
Ben Widawsky46c764d2013-11-02 21:07:49 -07004420 if (INTEL_INFO(dev)->gen >= 8) {
4421 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4422 ring_freq = max(min_ring_freq, gpu_freq);
4423 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004424 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004425 ring_freq = max(min_ring_freq, ring_freq);
4426 /* leave ia_freq as the default, chosen by cpufreq */
4427 } else {
4428 /* On older processors, there is no separate ring
4429 * clock domain, so in order to boost the bandwidth
4430 * of the ring, we need to upclock the CPU (ia_freq).
4431 *
4432 * For GPU frequencies less than 750MHz,
4433 * just use the lowest ring freq.
4434 */
4435 if (gpu_freq < min_freq)
4436 ia_freq = 800;
4437 else
4438 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4439 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4440 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004441
Ben Widawsky42c05262012-09-26 10:34:00 -07004442 sandybridge_pcode_write(dev_priv,
4443 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004444 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4445 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4446 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004447 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004448}
4449
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004450void gen6_update_ring_freq(struct drm_device *dev)
4451{
4452 struct drm_i915_private *dev_priv = dev->dev_private;
4453
4454 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4455 return;
4456
4457 mutex_lock(&dev_priv->rps.hw_lock);
4458 __gen6_update_ring_freq(dev);
4459 mutex_unlock(&dev_priv->rps.hw_lock);
4460}
4461
Ville Syrjälä03af2042014-06-28 02:03:53 +03004462static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304463{
Deepak S095acd52015-01-17 11:05:59 +05304464 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304465 u32 val, rp0;
4466
Deepak S095acd52015-01-17 11:05:59 +05304467 if (dev->pdev->revision >= 0x20) {
4468 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304469
Deepak S095acd52015-01-17 11:05:59 +05304470 switch (INTEL_INFO(dev)->eu_total) {
4471 case 8:
4472 /* (2 * 4) config */
4473 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4474 break;
4475 case 12:
4476 /* (2 * 6) config */
4477 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4478 break;
4479 case 16:
4480 /* (2 * 8) config */
4481 default:
4482 /* Setting (2 * 8) Min RP0 for any other combination */
4483 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4484 break;
4485 }
4486 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4487 } else {
4488 /* For pre-production hardware */
4489 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4490 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4491 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4492 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304493 return rp0;
4494}
4495
4496static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4497{
4498 u32 val, rpe;
4499
4500 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4501 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4502
4503 return rpe;
4504}
4505
Deepak S7707df42014-07-12 18:46:14 +05304506static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4507{
Deepak S095acd52015-01-17 11:05:59 +05304508 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304509 u32 val, rp1;
4510
Deepak S095acd52015-01-17 11:05:59 +05304511 if (dev->pdev->revision >= 0x20) {
4512 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4513 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4514 } else {
4515 /* For pre-production hardware */
4516 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4517 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4518 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4519 }
Deepak S7707df42014-07-12 18:46:14 +05304520 return rp1;
4521}
4522
Ville Syrjälä03af2042014-06-28 02:03:53 +03004523static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304524{
Deepak S095acd52015-01-17 11:05:59 +05304525 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304526 u32 val, rpn;
4527
Deepak S095acd52015-01-17 11:05:59 +05304528 if (dev->pdev->revision >= 0x20) {
4529 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4530 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4531 FB_GFX_FREQ_FUSE_MASK);
4532 } else { /* For pre-production hardware */
4533 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4534 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4535 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4536 }
4537
Deepak S2b6b3a02014-05-27 15:59:30 +05304538 return rpn;
4539}
4540
Deepak Sf8f2b002014-07-10 13:16:21 +05304541static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4542{
4543 u32 val, rp1;
4544
4545 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4546
4547 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4548
4549 return rp1;
4550}
4551
Ville Syrjälä03af2042014-06-28 02:03:53 +03004552static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004553{
4554 u32 val, rp0;
4555
Jani Nikula64936252013-05-22 15:36:20 +03004556 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004557
4558 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4559 /* Clamp to max */
4560 rp0 = min_t(u32, rp0, 0xea);
4561
4562 return rp0;
4563}
4564
4565static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4566{
4567 u32 val, rpe;
4568
Jani Nikula64936252013-05-22 15:36:20 +03004569 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004570 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004571 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004572 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4573
4574 return rpe;
4575}
4576
Ville Syrjälä03af2042014-06-28 02:03:53 +03004577static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004578{
Jani Nikula64936252013-05-22 15:36:20 +03004579 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004580}
4581
Imre Deakae484342014-03-31 15:10:44 +03004582/* Check that the pctx buffer wasn't move under us. */
4583static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4584{
4585 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4586
4587 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4588 dev_priv->vlv_pctx->stolen->start);
4589}
4590
Deepak S38807742014-05-23 21:00:15 +05304591
4592/* Check that the pcbr address is not empty. */
4593static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4594{
4595 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4596
4597 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4598}
4599
4600static void cherryview_setup_pctx(struct drm_device *dev)
4601{
4602 struct drm_i915_private *dev_priv = dev->dev_private;
4603 unsigned long pctx_paddr, paddr;
4604 struct i915_gtt *gtt = &dev_priv->gtt;
4605 u32 pcbr;
4606 int pctx_size = 32*1024;
4607
4608 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4609
4610 pcbr = I915_READ(VLV_PCBR);
4611 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004612 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304613 paddr = (dev_priv->mm.stolen_base +
4614 (gtt->stolen_size - pctx_size));
4615
4616 pctx_paddr = (paddr & (~4095));
4617 I915_WRITE(VLV_PCBR, pctx_paddr);
4618 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004619
4620 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304621}
4622
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004623static void valleyview_setup_pctx(struct drm_device *dev)
4624{
4625 struct drm_i915_private *dev_priv = dev->dev_private;
4626 struct drm_i915_gem_object *pctx;
4627 unsigned long pctx_paddr;
4628 u32 pcbr;
4629 int pctx_size = 24*1024;
4630
Imre Deak17b0c1f2014-02-11 21:39:06 +02004631 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4632
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004633 pcbr = I915_READ(VLV_PCBR);
4634 if (pcbr) {
4635 /* BIOS set it up already, grab the pre-alloc'd space */
4636 int pcbr_offset;
4637
4638 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4639 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4640 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004641 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004642 pctx_size);
4643 goto out;
4644 }
4645
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004646 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4647
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004648 /*
4649 * From the Gunit register HAS:
4650 * The Gfx driver is expected to program this register and ensure
4651 * proper allocation within Gfx stolen memory. For example, this
4652 * register should be programmed such than the PCBR range does not
4653 * overlap with other ranges, such as the frame buffer, protected
4654 * memory, or any other relevant ranges.
4655 */
4656 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4657 if (!pctx) {
4658 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4659 return;
4660 }
4661
4662 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4663 I915_WRITE(VLV_PCBR, pctx_paddr);
4664
4665out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004666 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004667 dev_priv->vlv_pctx = pctx;
4668}
4669
Imre Deakae484342014-03-31 15:10:44 +03004670static void valleyview_cleanup_pctx(struct drm_device *dev)
4671{
4672 struct drm_i915_private *dev_priv = dev->dev_private;
4673
4674 if (WARN_ON(!dev_priv->vlv_pctx))
4675 return;
4676
4677 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4678 dev_priv->vlv_pctx = NULL;
4679}
4680
Imre Deak4e805192014-04-14 20:24:41 +03004681static void valleyview_init_gt_powersave(struct drm_device *dev)
4682{
4683 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004684 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004685
4686 valleyview_setup_pctx(dev);
4687
4688 mutex_lock(&dev_priv->rps.hw_lock);
4689
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004690 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4691 switch ((val >> 6) & 3) {
4692 case 0:
4693 case 1:
4694 dev_priv->mem_freq = 800;
4695 break;
4696 case 2:
4697 dev_priv->mem_freq = 1066;
4698 break;
4699 case 3:
4700 dev_priv->mem_freq = 1333;
4701 break;
4702 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004703 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004704
Imre Deak4e805192014-04-14 20:24:41 +03004705 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4706 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4707 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004708 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004709 dev_priv->rps.max_freq);
4710
4711 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4712 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004713 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004714 dev_priv->rps.efficient_freq);
4715
Deepak Sf8f2b002014-07-10 13:16:21 +05304716 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4717 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004718 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304719 dev_priv->rps.rp1_freq);
4720
Imre Deak4e805192014-04-14 20:24:41 +03004721 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4722 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004723 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004724 dev_priv->rps.min_freq);
4725
4726 /* Preserve min/max settings in case of re-init */
4727 if (dev_priv->rps.max_freq_softlimit == 0)
4728 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4729
4730 if (dev_priv->rps.min_freq_softlimit == 0)
4731 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4732
4733 mutex_unlock(&dev_priv->rps.hw_lock);
4734}
4735
Deepak S38807742014-05-23 21:00:15 +05304736static void cherryview_init_gt_powersave(struct drm_device *dev)
4737{
Deepak S2b6b3a02014-05-27 15:59:30 +05304738 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004739 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304740
Deepak S38807742014-05-23 21:00:15 +05304741 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304742
4743 mutex_lock(&dev_priv->rps.hw_lock);
4744
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004745 mutex_lock(&dev_priv->dpio_lock);
4746 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4747 mutex_unlock(&dev_priv->dpio_lock);
4748
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004749 switch ((val >> 2) & 0x7) {
4750 case 0:
4751 case 1:
4752 dev_priv->rps.cz_freq = 200;
4753 dev_priv->mem_freq = 1600;
4754 break;
4755 case 2:
4756 dev_priv->rps.cz_freq = 267;
4757 dev_priv->mem_freq = 1600;
4758 break;
4759 case 3:
4760 dev_priv->rps.cz_freq = 333;
4761 dev_priv->mem_freq = 2000;
4762 break;
4763 case 4:
4764 dev_priv->rps.cz_freq = 320;
4765 dev_priv->mem_freq = 1600;
4766 break;
4767 case 5:
4768 dev_priv->rps.cz_freq = 400;
4769 dev_priv->mem_freq = 1600;
4770 break;
4771 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004772 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004773
Deepak S2b6b3a02014-05-27 15:59:30 +05304774 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4775 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4776 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004777 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304778 dev_priv->rps.max_freq);
4779
4780 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4781 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004782 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304783 dev_priv->rps.efficient_freq);
4784
Deepak S7707df42014-07-12 18:46:14 +05304785 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4786 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004787 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304788 dev_priv->rps.rp1_freq);
4789
Deepak S2b6b3a02014-05-27 15:59:30 +05304790 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4791 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004792 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304793 dev_priv->rps.min_freq);
4794
Ville Syrjälä1c147622014-08-18 14:42:43 +03004795 WARN_ONCE((dev_priv->rps.max_freq |
4796 dev_priv->rps.efficient_freq |
4797 dev_priv->rps.rp1_freq |
4798 dev_priv->rps.min_freq) & 1,
4799 "Odd GPU freq values\n");
4800
Deepak S2b6b3a02014-05-27 15:59:30 +05304801 /* Preserve min/max settings in case of re-init */
4802 if (dev_priv->rps.max_freq_softlimit == 0)
4803 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4804
4805 if (dev_priv->rps.min_freq_softlimit == 0)
4806 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4807
4808 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304809}
4810
Imre Deak4e805192014-04-14 20:24:41 +03004811static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4812{
4813 valleyview_cleanup_pctx(dev);
4814}
4815
Deepak S38807742014-05-23 21:00:15 +05304816static void cherryview_enable_rps(struct drm_device *dev)
4817{
4818 struct drm_i915_private *dev_priv = dev->dev_private;
4819 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304820 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304821 int i;
4822
4823 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4824
4825 gtfifodbg = I915_READ(GTFIFODBG);
4826 if (gtfifodbg) {
4827 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4828 gtfifodbg);
4829 I915_WRITE(GTFIFODBG, gtfifodbg);
4830 }
4831
4832 cherryview_check_pctx(dev_priv);
4833
4834 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4835 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004836 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304837
Ville Syrjälä160614a2015-01-19 13:50:47 +02004838 /* Disable RC states. */
4839 I915_WRITE(GEN6_RC_CONTROL, 0);
4840
Deepak S38807742014-05-23 21:00:15 +05304841 /* 2a: Program RC6 thresholds.*/
4842 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4843 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4844 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4845
4846 for_each_ring(ring, dev_priv, i)
4847 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4848 I915_WRITE(GEN6_RC_SLEEP, 0);
4849
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004850 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4851 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304852
4853 /* allows RC6 residency counter to work */
4854 I915_WRITE(VLV_COUNTER_CONTROL,
4855 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4856 VLV_MEDIA_RC6_COUNT_EN |
4857 VLV_RENDER_RC6_COUNT_EN));
4858
4859 /* For now we assume BIOS is allocating and populating the PCBR */
4860 pcbr = I915_READ(VLV_PCBR);
4861
Deepak S38807742014-05-23 21:00:15 +05304862 /* 3: Enable RC6 */
4863 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4864 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004865 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304866
4867 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4868
Deepak S2b6b3a02014-05-27 15:59:30 +05304869 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004870 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304871 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4872 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4873 I915_WRITE(GEN6_RP_UP_EI, 66000);
4874 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4875
4876 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4877
4878 /* 5: Enable RPS */
4879 I915_WRITE(GEN6_RP_CONTROL,
4880 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004881 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304882 GEN6_RP_ENABLE |
4883 GEN6_RP_UP_BUSY_AVG |
4884 GEN6_RP_DOWN_IDLE_AVG);
4885
4886 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4887
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004888 /* RPS code assumes GPLL is used */
4889 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4890
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004891 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304892 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4893
4894 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4895 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004896 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304897 dev_priv->rps.cur_freq);
4898
4899 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004900 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304901 dev_priv->rps.efficient_freq);
4902
4903 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4904
Mika Kuoppala59bad942015-01-16 11:34:40 +02004905 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304906}
4907
Jesse Barnes0a073b82013-04-17 15:54:58 -07004908static void valleyview_enable_rps(struct drm_device *dev)
4909{
4910 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004911 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004912 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004913 int i;
4914
4915 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4916
Imre Deakae484342014-03-31 15:10:44 +03004917 valleyview_check_pctx(dev_priv);
4918
Jesse Barnes0a073b82013-04-17 15:54:58 -07004919 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004920 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4921 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004922 I915_WRITE(GTFIFODBG, gtfifodbg);
4923 }
4924
Deepak Sc8d9a592013-11-23 14:55:42 +05304925 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004926 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004927
Ville Syrjälä160614a2015-01-19 13:50:47 +02004928 /* Disable RC states. */
4929 I915_WRITE(GEN6_RC_CONTROL, 0);
4930
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004931 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004932 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4933 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4934 I915_WRITE(GEN6_RP_UP_EI, 66000);
4935 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4936
4937 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4938
4939 I915_WRITE(GEN6_RP_CONTROL,
4940 GEN6_RP_MEDIA_TURBO |
4941 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4942 GEN6_RP_MEDIA_IS_GFX |
4943 GEN6_RP_ENABLE |
4944 GEN6_RP_UP_BUSY_AVG |
4945 GEN6_RP_DOWN_IDLE_CONT);
4946
4947 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4948 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4949 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4950
4951 for_each_ring(ring, dev_priv, i)
4952 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4953
Jesse Barnes2f0aa302013-11-15 09:32:11 -08004954 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004955
4956 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004957 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004958 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4959 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004960 VLV_MEDIA_RC6_COUNT_EN |
4961 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004962
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004963 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004964 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004965
4966 intel_print_rc6_info(dev, rc6_mode);
4967
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004968 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004969
Jani Nikula64936252013-05-22 15:36:20 +03004970 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004971
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004972 /* RPS code assumes GPLL is used */
4973 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4974
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004975 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07004976 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4977
Ben Widawskyb39fb292014-03-19 18:31:11 -07004978 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004979 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004980 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004981 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004982
Ville Syrjälä73008b92013-06-25 19:21:01 +03004983 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004984 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004985 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004986
Ben Widawskyb39fb292014-03-19 18:31:11 -07004987 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004988
Mika Kuoppala59bad942015-01-16 11:34:40 +02004989 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004990}
4991
Daniel Vetter930ebb42012-06-29 23:32:16 +02004992void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004993{
4994 struct drm_i915_private *dev_priv = dev->dev_private;
4995
Daniel Vetter3e373942012-11-02 19:55:04 +01004996 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004997 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004998 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4999 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005000 }
5001
Daniel Vetter3e373942012-11-02 19:55:04 +01005002 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08005003 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01005004 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
5005 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005006 }
5007}
5008
Daniel Vetter930ebb42012-06-29 23:32:16 +02005009static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005010{
5011 struct drm_i915_private *dev_priv = dev->dev_private;
5012
5013 if (I915_READ(PWRCTXA)) {
5014 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
5015 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
5016 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
5017 50);
5018
5019 I915_WRITE(PWRCTXA, 0);
5020 POSTING_READ(PWRCTXA);
5021
5022 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
5023 POSTING_READ(RSTDBYCTL);
5024 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005025}
5026
5027static int ironlake_setup_rc6(struct drm_device *dev)
5028{
5029 struct drm_i915_private *dev_priv = dev->dev_private;
5030
Daniel Vetter3e373942012-11-02 19:55:04 +01005031 if (dev_priv->ips.renderctx == NULL)
5032 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
5033 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005034 return -ENOMEM;
5035
Daniel Vetter3e373942012-11-02 19:55:04 +01005036 if (dev_priv->ips.pwrctx == NULL)
5037 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
5038 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005039 ironlake_teardown_rc6(dev);
5040 return -ENOMEM;
5041 }
5042
5043 return 0;
5044}
5045
Daniel Vetter930ebb42012-06-29 23:32:16 +02005046static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005047{
5048 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005049 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00005050 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005051 int ret;
5052
5053 /* rc6 disabled by default due to repeated reports of hanging during
5054 * boot and resume.
5055 */
5056 if (!intel_enable_rc6(dev))
5057 return;
5058
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005059 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5060
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005061 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005062 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005063 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005064
Chris Wilson3e960502012-11-27 16:22:54 +00005065 was_interruptible = dev_priv->mm.interruptible;
5066 dev_priv->mm.interruptible = false;
5067
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005068 /*
5069 * GPU can automatically power down the render unit if given a page
5070 * to save state.
5071 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02005072 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005073 if (ret) {
5074 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00005075 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005076 return;
5077 }
5078
Daniel Vetter6d90c952012-04-26 23:28:05 +02005079 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
5080 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005081 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02005082 MI_MM_SPACE_GTT |
5083 MI_SAVE_EXT_STATE_EN |
5084 MI_RESTORE_EXT_STATE_EN |
5085 MI_RESTORE_INHIBIT);
5086 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
5087 intel_ring_emit(ring, MI_NOOP);
5088 intel_ring_emit(ring, MI_FLUSH);
5089 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005090
5091 /*
5092 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
5093 * does an implicit flush, combined with MI_FLUSH above, it should be
5094 * safe to assume that renderctx is valid
5095 */
Chris Wilson3e960502012-11-27 16:22:54 +00005096 ret = intel_ring_idle(ring);
5097 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005098 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02005099 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005100 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005101 return;
5102 }
5103
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005104 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005105 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005106
Imre Deak91ca6892014-04-14 20:24:25 +03005107 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005108}
5109
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005110static unsigned long intel_pxfreq(u32 vidfreq)
5111{
5112 unsigned long freq;
5113 int div = (vidfreq & 0x3f0000) >> 16;
5114 int post = (vidfreq & 0x3000) >> 12;
5115 int pre = (vidfreq & 0x7);
5116
5117 if (!pre)
5118 return 0;
5119
5120 freq = ((div * 133333) / ((1<<post) * pre));
5121
5122 return freq;
5123}
5124
Daniel Vettereb48eb02012-04-26 23:28:12 +02005125static const struct cparams {
5126 u16 i;
5127 u16 t;
5128 u16 m;
5129 u16 c;
5130} cparams[] = {
5131 { 1, 1333, 301, 28664 },
5132 { 1, 1066, 294, 24460 },
5133 { 1, 800, 294, 25192 },
5134 { 0, 1333, 276, 27605 },
5135 { 0, 1066, 276, 27605 },
5136 { 0, 800, 231, 23784 },
5137};
5138
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005139static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005140{
5141 u64 total_count, diff, ret;
5142 u32 count1, count2, count3, m = 0, c = 0;
5143 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5144 int i;
5145
Daniel Vetter02d71952012-08-09 16:44:54 +02005146 assert_spin_locked(&mchdev_lock);
5147
Daniel Vetter20e4d402012-08-08 23:35:39 +02005148 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005149
5150 /* Prevent division-by-zero if we are asking too fast.
5151 * Also, we don't get interesting results if we are polling
5152 * faster than once in 10ms, so just return the saved value
5153 * in such cases.
5154 */
5155 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005156 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005157
5158 count1 = I915_READ(DMIEC);
5159 count2 = I915_READ(DDREC);
5160 count3 = I915_READ(CSIEC);
5161
5162 total_count = count1 + count2 + count3;
5163
5164 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005165 if (total_count < dev_priv->ips.last_count1) {
5166 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005167 diff += total_count;
5168 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005169 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005170 }
5171
5172 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005173 if (cparams[i].i == dev_priv->ips.c_m &&
5174 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005175 m = cparams[i].m;
5176 c = cparams[i].c;
5177 break;
5178 }
5179 }
5180
5181 diff = div_u64(diff, diff1);
5182 ret = ((m * diff) + c);
5183 ret = div_u64(ret, 10);
5184
Daniel Vetter20e4d402012-08-08 23:35:39 +02005185 dev_priv->ips.last_count1 = total_count;
5186 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005187
Daniel Vetter20e4d402012-08-08 23:35:39 +02005188 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005189
5190 return ret;
5191}
5192
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005193unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5194{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005195 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005196 unsigned long val;
5197
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005198 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005199 return 0;
5200
5201 spin_lock_irq(&mchdev_lock);
5202
5203 val = __i915_chipset_val(dev_priv);
5204
5205 spin_unlock_irq(&mchdev_lock);
5206
5207 return val;
5208}
5209
Daniel Vettereb48eb02012-04-26 23:28:12 +02005210unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5211{
5212 unsigned long m, x, b;
5213 u32 tsfs;
5214
5215 tsfs = I915_READ(TSFS);
5216
5217 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5218 x = I915_READ8(TR1);
5219
5220 b = tsfs & TSFS_INTR_MASK;
5221
5222 return ((m * x) / 127) - b;
5223}
5224
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005225static int _pxvid_to_vd(u8 pxvid)
5226{
5227 if (pxvid == 0)
5228 return 0;
5229
5230 if (pxvid >= 8 && pxvid < 31)
5231 pxvid = 31;
5232
5233 return (pxvid + 2) * 125;
5234}
5235
5236static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005237{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005238 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005239 const int vd = _pxvid_to_vd(pxvid);
5240 const int vm = vd - 1125;
5241
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005242 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005243 return vm > 0 ? vm : 0;
5244
5245 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005246}
5247
Daniel Vetter02d71952012-08-09 16:44:54 +02005248static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005249{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005250 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005251 u32 count;
5252
Daniel Vetter02d71952012-08-09 16:44:54 +02005253 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005254
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005255 now = ktime_get_raw_ns();
5256 diffms = now - dev_priv->ips.last_time2;
5257 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005258
5259 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005260 if (!diffms)
5261 return;
5262
5263 count = I915_READ(GFXEC);
5264
Daniel Vetter20e4d402012-08-08 23:35:39 +02005265 if (count < dev_priv->ips.last_count2) {
5266 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005267 diff += count;
5268 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005269 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005270 }
5271
Daniel Vetter20e4d402012-08-08 23:35:39 +02005272 dev_priv->ips.last_count2 = count;
5273 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005274
5275 /* More magic constants... */
5276 diff = diff * 1181;
5277 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005278 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005279}
5280
Daniel Vetter02d71952012-08-09 16:44:54 +02005281void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5282{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005283 struct drm_device *dev = dev_priv->dev;
5284
5285 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005286 return;
5287
Daniel Vetter92703882012-08-09 16:46:01 +02005288 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005289
5290 __i915_update_gfx_val(dev_priv);
5291
Daniel Vetter92703882012-08-09 16:46:01 +02005292 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005293}
5294
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005295static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005296{
5297 unsigned long t, corr, state1, corr2, state2;
5298 u32 pxvid, ext_v;
5299
Daniel Vetter02d71952012-08-09 16:44:54 +02005300 assert_spin_locked(&mchdev_lock);
5301
Ben Widawskyb39fb292014-03-19 18:31:11 -07005302 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005303 pxvid = (pxvid >> 24) & 0x7f;
5304 ext_v = pvid_to_extvid(dev_priv, pxvid);
5305
5306 state1 = ext_v;
5307
5308 t = i915_mch_val(dev_priv);
5309
5310 /* Revel in the empirically derived constants */
5311
5312 /* Correction factor in 1/100000 units */
5313 if (t > 80)
5314 corr = ((t * 2349) + 135940);
5315 else if (t >= 50)
5316 corr = ((t * 964) + 29317);
5317 else /* < 50 */
5318 corr = ((t * 301) + 1004);
5319
5320 corr = corr * ((150142 * state1) / 10000 - 78642);
5321 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005322 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005323
5324 state2 = (corr2 * state1) / 10000;
5325 state2 /= 100; /* convert to mW */
5326
Daniel Vetter02d71952012-08-09 16:44:54 +02005327 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005328
Daniel Vetter20e4d402012-08-08 23:35:39 +02005329 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005330}
5331
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005332unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5333{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005334 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005335 unsigned long val;
5336
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005337 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005338 return 0;
5339
5340 spin_lock_irq(&mchdev_lock);
5341
5342 val = __i915_gfx_val(dev_priv);
5343
5344 spin_unlock_irq(&mchdev_lock);
5345
5346 return val;
5347}
5348
Daniel Vettereb48eb02012-04-26 23:28:12 +02005349/**
5350 * i915_read_mch_val - return value for IPS use
5351 *
5352 * Calculate and return a value for the IPS driver to use when deciding whether
5353 * we have thermal and power headroom to increase CPU or GPU power budget.
5354 */
5355unsigned long i915_read_mch_val(void)
5356{
5357 struct drm_i915_private *dev_priv;
5358 unsigned long chipset_val, graphics_val, ret = 0;
5359
Daniel Vetter92703882012-08-09 16:46:01 +02005360 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005361 if (!i915_mch_dev)
5362 goto out_unlock;
5363 dev_priv = i915_mch_dev;
5364
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005365 chipset_val = __i915_chipset_val(dev_priv);
5366 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005367
5368 ret = chipset_val + graphics_val;
5369
5370out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005371 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005372
5373 return ret;
5374}
5375EXPORT_SYMBOL_GPL(i915_read_mch_val);
5376
5377/**
5378 * i915_gpu_raise - raise GPU frequency limit
5379 *
5380 * Raise the limit; IPS indicates we have thermal headroom.
5381 */
5382bool i915_gpu_raise(void)
5383{
5384 struct drm_i915_private *dev_priv;
5385 bool ret = true;
5386
Daniel Vetter92703882012-08-09 16:46:01 +02005387 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005388 if (!i915_mch_dev) {
5389 ret = false;
5390 goto out_unlock;
5391 }
5392 dev_priv = i915_mch_dev;
5393
Daniel Vetter20e4d402012-08-08 23:35:39 +02005394 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5395 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005396
5397out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005398 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005399
5400 return ret;
5401}
5402EXPORT_SYMBOL_GPL(i915_gpu_raise);
5403
5404/**
5405 * i915_gpu_lower - lower GPU frequency limit
5406 *
5407 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5408 * frequency maximum.
5409 */
5410bool i915_gpu_lower(void)
5411{
5412 struct drm_i915_private *dev_priv;
5413 bool ret = true;
5414
Daniel Vetter92703882012-08-09 16:46:01 +02005415 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005416 if (!i915_mch_dev) {
5417 ret = false;
5418 goto out_unlock;
5419 }
5420 dev_priv = i915_mch_dev;
5421
Daniel Vetter20e4d402012-08-08 23:35:39 +02005422 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5423 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005424
5425out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005426 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005427
5428 return ret;
5429}
5430EXPORT_SYMBOL_GPL(i915_gpu_lower);
5431
5432/**
5433 * i915_gpu_busy - indicate GPU business to IPS
5434 *
5435 * Tell the IPS driver whether or not the GPU is busy.
5436 */
5437bool i915_gpu_busy(void)
5438{
5439 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005440 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005441 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005442 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005443
Daniel Vetter92703882012-08-09 16:46:01 +02005444 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005445 if (!i915_mch_dev)
5446 goto out_unlock;
5447 dev_priv = i915_mch_dev;
5448
Chris Wilsonf047e392012-07-21 12:31:41 +01005449 for_each_ring(ring, dev_priv, i)
5450 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005451
5452out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005453 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005454
5455 return ret;
5456}
5457EXPORT_SYMBOL_GPL(i915_gpu_busy);
5458
5459/**
5460 * i915_gpu_turbo_disable - disable graphics turbo
5461 *
5462 * Disable graphics turbo by resetting the max frequency and setting the
5463 * current frequency to the default.
5464 */
5465bool i915_gpu_turbo_disable(void)
5466{
5467 struct drm_i915_private *dev_priv;
5468 bool ret = true;
5469
Daniel Vetter92703882012-08-09 16:46:01 +02005470 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005471 if (!i915_mch_dev) {
5472 ret = false;
5473 goto out_unlock;
5474 }
5475 dev_priv = i915_mch_dev;
5476
Daniel Vetter20e4d402012-08-08 23:35:39 +02005477 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005478
Daniel Vetter20e4d402012-08-08 23:35:39 +02005479 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005480 ret = false;
5481
5482out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005483 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005484
5485 return ret;
5486}
5487EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5488
5489/**
5490 * Tells the intel_ips driver that the i915 driver is now loaded, if
5491 * IPS got loaded first.
5492 *
5493 * This awkward dance is so that neither module has to depend on the
5494 * other in order for IPS to do the appropriate communication of
5495 * GPU turbo limits to i915.
5496 */
5497static void
5498ips_ping_for_i915_load(void)
5499{
5500 void (*link)(void);
5501
5502 link = symbol_get(ips_link_to_i915_driver);
5503 if (link) {
5504 link();
5505 symbol_put(ips_link_to_i915_driver);
5506 }
5507}
5508
5509void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5510{
Daniel Vetter02d71952012-08-09 16:44:54 +02005511 /* We only register the i915 ips part with intel-ips once everything is
5512 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005513 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005514 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005515 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005516
5517 ips_ping_for_i915_load();
5518}
5519
5520void intel_gpu_ips_teardown(void)
5521{
Daniel Vetter92703882012-08-09 16:46:01 +02005522 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005523 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005524 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005525}
Deepak S76c3552f2014-01-30 23:08:16 +05305526
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005527static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005528{
5529 struct drm_i915_private *dev_priv = dev->dev_private;
5530 u32 lcfuse;
5531 u8 pxw[16];
5532 int i;
5533
5534 /* Disable to program */
5535 I915_WRITE(ECR, 0);
5536 POSTING_READ(ECR);
5537
5538 /* Program energy weights for various events */
5539 I915_WRITE(SDEW, 0x15040d00);
5540 I915_WRITE(CSIEW0, 0x007f0000);
5541 I915_WRITE(CSIEW1, 0x1e220004);
5542 I915_WRITE(CSIEW2, 0x04000004);
5543
5544 for (i = 0; i < 5; i++)
5545 I915_WRITE(PEW + (i * 4), 0);
5546 for (i = 0; i < 3; i++)
5547 I915_WRITE(DEW + (i * 4), 0);
5548
5549 /* Program P-state weights to account for frequency power adjustment */
5550 for (i = 0; i < 16; i++) {
5551 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5552 unsigned long freq = intel_pxfreq(pxvidfreq);
5553 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5554 PXVFREQ_PX_SHIFT;
5555 unsigned long val;
5556
5557 val = vid * vid;
5558 val *= (freq / 1000);
5559 val *= 255;
5560 val /= (127*127*900);
5561 if (val > 0xff)
5562 DRM_ERROR("bad pxval: %ld\n", val);
5563 pxw[i] = val;
5564 }
5565 /* Render standby states get 0 weight */
5566 pxw[14] = 0;
5567 pxw[15] = 0;
5568
5569 for (i = 0; i < 4; i++) {
5570 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5571 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5572 I915_WRITE(PXW + (i * 4), val);
5573 }
5574
5575 /* Adjust magic regs to magic values (more experimental results) */
5576 I915_WRITE(OGW0, 0);
5577 I915_WRITE(OGW1, 0);
5578 I915_WRITE(EG0, 0x00007f00);
5579 I915_WRITE(EG1, 0x0000000e);
5580 I915_WRITE(EG2, 0x000e0000);
5581 I915_WRITE(EG3, 0x68000300);
5582 I915_WRITE(EG4, 0x42000000);
5583 I915_WRITE(EG5, 0x00140031);
5584 I915_WRITE(EG6, 0);
5585 I915_WRITE(EG7, 0);
5586
5587 for (i = 0; i < 8; i++)
5588 I915_WRITE(PXWL + (i * 4), 0);
5589
5590 /* Enable PMON + select events */
5591 I915_WRITE(ECR, 0x80000019);
5592
5593 lcfuse = I915_READ(LCFUSE02);
5594
Daniel Vetter20e4d402012-08-08 23:35:39 +02005595 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005596}
5597
Imre Deakae484342014-03-31 15:10:44 +03005598void intel_init_gt_powersave(struct drm_device *dev)
5599{
Imre Deake6069ca2014-04-18 16:01:02 +03005600 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5601
Deepak S38807742014-05-23 21:00:15 +05305602 if (IS_CHERRYVIEW(dev))
5603 cherryview_init_gt_powersave(dev);
5604 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005605 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005606}
5607
5608void intel_cleanup_gt_powersave(struct drm_device *dev)
5609{
Deepak S38807742014-05-23 21:00:15 +05305610 if (IS_CHERRYVIEW(dev))
5611 return;
5612 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005613 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005614}
5615
Imre Deakdbea3ce2014-12-15 18:59:28 +02005616static void gen6_suspend_rps(struct drm_device *dev)
5617{
5618 struct drm_i915_private *dev_priv = dev->dev_private;
5619
5620 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5621
5622 /*
5623 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5624 * is added for it.
5625 */
5626 if (INTEL_INFO(dev)->gen < 9)
5627 gen6_disable_rps_interrupts(dev);
5628}
5629
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005630/**
5631 * intel_suspend_gt_powersave - suspend PM work and helper threads
5632 * @dev: drm device
5633 *
5634 * We don't want to disable RC6 or other features here, we just want
5635 * to make sure any work we've queued has finished and won't bother
5636 * us while we're suspended.
5637 */
5638void intel_suspend_gt_powersave(struct drm_device *dev)
5639{
5640 struct drm_i915_private *dev_priv = dev->dev_private;
5641
Imre Deakd4d70aa2014-11-19 15:30:04 +02005642 if (INTEL_INFO(dev)->gen < 6)
5643 return;
5644
Imre Deakdbea3ce2014-12-15 18:59:28 +02005645 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305646
5647 /* Force GPU to min freq during suspend */
5648 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005649}
5650
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005651void intel_disable_gt_powersave(struct drm_device *dev)
5652{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005653 struct drm_i915_private *dev_priv = dev->dev_private;
5654
Daniel Vetter930ebb42012-06-29 23:32:16 +02005655 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005656 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005657 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305658 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005659 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005660
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005661 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005662 if (INTEL_INFO(dev)->gen >= 9)
5663 gen9_disable_rps(dev);
5664 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305665 cherryview_disable_rps(dev);
5666 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005667 valleyview_disable_rps(dev);
5668 else
5669 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005670
Chris Wilsonc0951f02013-10-10 21:58:50 +01005671 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005672 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005673 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005674}
5675
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005676static void intel_gen6_powersave_work(struct work_struct *work)
5677{
5678 struct drm_i915_private *dev_priv =
5679 container_of(work, struct drm_i915_private,
5680 rps.delayed_resume_work.work);
5681 struct drm_device *dev = dev_priv->dev;
5682
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005683 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005684
Imre Deak3cc134e2014-11-19 15:30:03 +02005685 /*
5686 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5687 * added for it.
5688 */
5689 if (INTEL_INFO(dev)->gen < 9)
5690 gen6_reset_rps_interrupts(dev);
5691
Deepak S38807742014-05-23 21:00:15 +05305692 if (IS_CHERRYVIEW(dev)) {
5693 cherryview_enable_rps(dev);
5694 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005695 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005696 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005697 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005698 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005699 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005700 } else if (IS_BROADWELL(dev)) {
5701 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005702 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005703 } else {
5704 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005705 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005706 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005707 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005708
5709 if (INTEL_INFO(dev)->gen < 9)
5710 gen6_enable_rps_interrupts(dev);
5711
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005712 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005713
5714 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005715}
5716
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005717void intel_enable_gt_powersave(struct drm_device *dev)
5718{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005719 struct drm_i915_private *dev_priv = dev->dev_private;
5720
Yu Zhangf61018b2015-02-10 19:05:52 +08005721 /* Powersaving is controlled by the host when inside a VM */
5722 if (intel_vgpu_active(dev))
5723 return;
5724
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005725 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005726 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005727 ironlake_enable_drps(dev);
5728 ironlake_enable_rc6(dev);
5729 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005730 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305731 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005732 /*
5733 * PCU communication is slow and this doesn't need to be
5734 * done at any specific time, so do this out of our fast path
5735 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005736 *
5737 * We depend on the HW RC6 power context save/restore
5738 * mechanism when entering D3 through runtime PM suspend. So
5739 * disable RPM until RPS/RC6 is properly setup. We can only
5740 * get here via the driver load/system resume/runtime resume
5741 * paths, so the _noresume version is enough (and in case of
5742 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005743 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005744 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5745 round_jiffies_up_relative(HZ)))
5746 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005747 }
5748}
5749
Imre Deakc6df39b2014-04-14 20:24:29 +03005750void intel_reset_gt_powersave(struct drm_device *dev)
5751{
5752 struct drm_i915_private *dev_priv = dev->dev_private;
5753
Imre Deakdbea3ce2014-12-15 18:59:28 +02005754 if (INTEL_INFO(dev)->gen < 6)
5755 return;
5756
5757 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005758 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005759}
5760
Daniel Vetter3107bd42012-10-31 22:52:31 +01005761static void ibx_init_clock_gating(struct drm_device *dev)
5762{
5763 struct drm_i915_private *dev_priv = dev->dev_private;
5764
5765 /*
5766 * On Ibex Peak and Cougar Point, we need to disable clock
5767 * gating for the panel power sequencer or it will fail to
5768 * start up when no ports are active.
5769 */
5770 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5771}
5772
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005773static void g4x_disable_trickle_feed(struct drm_device *dev)
5774{
5775 struct drm_i915_private *dev_priv = dev->dev_private;
5776 int pipe;
5777
Damien Lespiau055e3932014-08-18 13:49:10 +01005778 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005779 I915_WRITE(DSPCNTR(pipe),
5780 I915_READ(DSPCNTR(pipe)) |
5781 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005782 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005783 }
5784}
5785
Ville Syrjälä017636c2013-12-05 15:51:37 +02005786static void ilk_init_lp_watermarks(struct drm_device *dev)
5787{
5788 struct drm_i915_private *dev_priv = dev->dev_private;
5789
5790 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5791 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5792 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5793
5794 /*
5795 * Don't touch WM1S_LP_EN here.
5796 * Doing so could cause underruns.
5797 */
5798}
5799
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005800static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005801{
5802 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005803 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005804
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005805 /*
5806 * Required for FBC
5807 * WaFbcDisableDpfcClockGating:ilk
5808 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005809 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5810 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5811 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005812
5813 I915_WRITE(PCH_3DCGDIS0,
5814 MARIUNIT_CLOCK_GATE_DISABLE |
5815 SVSMUNIT_CLOCK_GATE_DISABLE);
5816 I915_WRITE(PCH_3DCGDIS1,
5817 VFMUNIT_CLOCK_GATE_DISABLE);
5818
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005819 /*
5820 * According to the spec the following bits should be set in
5821 * order to enable memory self-refresh
5822 * The bit 22/21 of 0x42004
5823 * The bit 5 of 0x42020
5824 * The bit 15 of 0x45000
5825 */
5826 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5827 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5828 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005829 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005830 I915_WRITE(DISP_ARB_CTL,
5831 (I915_READ(DISP_ARB_CTL) |
5832 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005833
5834 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005835
5836 /*
5837 * Based on the document from hardware guys the following bits
5838 * should be set unconditionally in order to enable FBC.
5839 * The bit 22 of 0x42000
5840 * The bit 22 of 0x42004
5841 * The bit 7,8,9 of 0x42020.
5842 */
5843 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005844 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005845 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5846 I915_READ(ILK_DISPLAY_CHICKEN1) |
5847 ILK_FBCQ_DIS);
5848 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5849 I915_READ(ILK_DISPLAY_CHICKEN2) |
5850 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005851 }
5852
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005853 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5854
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005855 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5856 I915_READ(ILK_DISPLAY_CHICKEN2) |
5857 ILK_ELPIN_409_SELECT);
5858 I915_WRITE(_3D_CHICKEN2,
5859 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5860 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005861
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005862 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005863 I915_WRITE(CACHE_MODE_0,
5864 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005865
Akash Goel4e046322014-04-04 17:14:38 +05305866 /* WaDisable_RenderCache_OperationalFlush:ilk */
5867 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5868
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005869 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005870
Daniel Vetter3107bd42012-10-31 22:52:31 +01005871 ibx_init_clock_gating(dev);
5872}
5873
5874static void cpt_init_clock_gating(struct drm_device *dev)
5875{
5876 struct drm_i915_private *dev_priv = dev->dev_private;
5877 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005878 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005879
5880 /*
5881 * On Ibex Peak and Cougar Point, we need to disable clock
5882 * gating for the panel power sequencer or it will fail to
5883 * start up when no ports are active.
5884 */
Jesse Barnescd664072013-10-02 10:34:19 -07005885 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5886 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5887 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005888 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5889 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005890 /* The below fixes the weird display corruption, a few pixels shifted
5891 * downward, on (only) LVDS of some HP laptops with IVY.
5892 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005893 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005894 val = I915_READ(TRANS_CHICKEN2(pipe));
5895 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5896 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005897 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005898 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005899 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5900 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5901 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005902 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5903 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005904 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005905 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005906 I915_WRITE(TRANS_CHICKEN1(pipe),
5907 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5908 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005909}
5910
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005911static void gen6_check_mch_setup(struct drm_device *dev)
5912{
5913 struct drm_i915_private *dev_priv = dev->dev_private;
5914 uint32_t tmp;
5915
5916 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005917 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5918 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5919 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005920}
5921
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005922static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005923{
5924 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005925 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005926
Damien Lespiau231e54f2012-10-19 17:55:41 +01005927 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005928
5929 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5930 I915_READ(ILK_DISPLAY_CHICKEN2) |
5931 ILK_ELPIN_409_SELECT);
5932
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005933 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005934 I915_WRITE(_3D_CHICKEN,
5935 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5936
Akash Goel4e046322014-04-04 17:14:38 +05305937 /* WaDisable_RenderCache_OperationalFlush:snb */
5938 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5939
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005940 /*
5941 * BSpec recoomends 8x4 when MSAA is used,
5942 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005943 *
5944 * Note that PS/WM thread counts depend on the WIZ hashing
5945 * disable bit, which we don't touch here, but it's good
5946 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005947 */
5948 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005949 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005950
Ville Syrjälä017636c2013-12-05 15:51:37 +02005951 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005952
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005953 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005954 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005955
5956 I915_WRITE(GEN6_UCGCTL1,
5957 I915_READ(GEN6_UCGCTL1) |
5958 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5959 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5960
5961 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5962 * gating disable must be set. Failure to set it results in
5963 * flickering pixels due to Z write ordering failures after
5964 * some amount of runtime in the Mesa "fire" demo, and Unigine
5965 * Sanctuary and Tropics, and apparently anything else with
5966 * alpha test or pixel discard.
5967 *
5968 * According to the spec, bit 11 (RCCUNIT) must also be set,
5969 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005970 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005971 * WaDisableRCCUnitClockGating:snb
5972 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005973 */
5974 I915_WRITE(GEN6_UCGCTL2,
5975 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5976 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5977
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005978 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005979 I915_WRITE(_3D_CHICKEN3,
5980 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005981
5982 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005983 * Bspec says:
5984 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5985 * 3DSTATE_SF number of SF output attributes is more than 16."
5986 */
5987 I915_WRITE(_3D_CHICKEN3,
5988 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5989
5990 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005991 * According to the spec the following bits should be
5992 * set in order to enable memory self-refresh and fbc:
5993 * The bit21 and bit22 of 0x42000
5994 * The bit21 and bit22 of 0x42004
5995 * The bit5 and bit7 of 0x42020
5996 * The bit14 of 0x70180
5997 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005998 *
5999 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006000 */
6001 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6002 I915_READ(ILK_DISPLAY_CHICKEN1) |
6003 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6004 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6005 I915_READ(ILK_DISPLAY_CHICKEN2) |
6006 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006007 I915_WRITE(ILK_DSPCLK_GATE_D,
6008 I915_READ(ILK_DSPCLK_GATE_D) |
6009 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6010 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006011
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006012 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006013
Daniel Vetter3107bd42012-10-31 22:52:31 +01006014 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006015
6016 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006017}
6018
6019static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6020{
6021 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6022
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006023 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006024 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006025 *
6026 * This actually overrides the dispatch
6027 * mode for all thread types.
6028 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006029 reg &= ~GEN7_FF_SCHED_MASK;
6030 reg |= GEN7_FF_TS_SCHED_HW;
6031 reg |= GEN7_FF_VS_SCHED_HW;
6032 reg |= GEN7_FF_DS_SCHED_HW;
6033
6034 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6035}
6036
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006037static void lpt_init_clock_gating(struct drm_device *dev)
6038{
6039 struct drm_i915_private *dev_priv = dev->dev_private;
6040
6041 /*
6042 * TODO: this bit should only be enabled when really needed, then
6043 * disabled when not needed anymore in order to save power.
6044 */
6045 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6046 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6047 I915_READ(SOUTH_DSPCLK_GATE_D) |
6048 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006049
6050 /* WADPOClockGatingDisable:hsw */
6051 I915_WRITE(_TRANSA_CHICKEN1,
6052 I915_READ(_TRANSA_CHICKEN1) |
6053 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006054}
6055
Imre Deak7d708ee2013-04-17 14:04:50 +03006056static void lpt_suspend_hw(struct drm_device *dev)
6057{
6058 struct drm_i915_private *dev_priv = dev->dev_private;
6059
6060 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6061 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6062
6063 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6064 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6065 }
6066}
6067
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006068static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006069{
6070 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006071 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006072
6073 I915_WRITE(WM3_LP_ILK, 0);
6074 I915_WRITE(WM2_LP_ILK, 0);
6075 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006076
Ben Widawskyab57fff2013-12-12 15:28:04 -08006077 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006078 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006079
Ben Widawskyab57fff2013-12-12 15:28:04 -08006080 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006081 I915_WRITE(CHICKEN_PAR1_1,
6082 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6083
Ben Widawskyab57fff2013-12-12 15:28:04 -08006084 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006085 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006086 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006087 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006088 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006089 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006090
Ben Widawskyab57fff2013-12-12 15:28:04 -08006091 /* WaVSRefCountFullforceMissDisable:bdw */
6092 /* WaDSRefCountFullforceMissDisable:bdw */
6093 I915_WRITE(GEN7_FF_THREAD_MODE,
6094 I915_READ(GEN7_FF_THREAD_MODE) &
6095 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006096
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006097 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6098 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006099
6100 /* WaDisableSDEUnitClockGating:bdw */
6101 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6102 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006103
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006104 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006105}
6106
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006107static void haswell_init_clock_gating(struct drm_device *dev)
6108{
6109 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006110
Ville Syrjälä017636c2013-12-05 15:51:37 +02006111 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006112
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006113 /* L3 caching of data atomics doesn't work -- disable it. */
6114 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6115 I915_WRITE(HSW_ROW_CHICKEN3,
6116 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6117
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006118 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006119 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6120 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6121 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6122
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006123 /* WaVSRefCountFullforceMissDisable:hsw */
6124 I915_WRITE(GEN7_FF_THREAD_MODE,
6125 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006126
Akash Goel4e046322014-04-04 17:14:38 +05306127 /* WaDisable_RenderCache_OperationalFlush:hsw */
6128 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6129
Chia-I Wufe27c602014-01-28 13:29:33 +08006130 /* enable HiZ Raw Stall Optimization */
6131 I915_WRITE(CACHE_MODE_0_GEN7,
6132 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6133
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006134 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006135 I915_WRITE(CACHE_MODE_1,
6136 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006137
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006138 /*
6139 * BSpec recommends 8x4 when MSAA is used,
6140 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006141 *
6142 * Note that PS/WM thread counts depend on the WIZ hashing
6143 * disable bit, which we don't touch here, but it's good
6144 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006145 */
6146 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006147 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006148
Kenneth Graunke94411592014-12-31 16:23:00 -08006149 /* WaSampleCChickenBitEnable:hsw */
6150 I915_WRITE(HALF_SLICE_CHICKEN3,
6151 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6152
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006153 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006154 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6155
Paulo Zanoni90a88642013-05-03 17:23:45 -03006156 /* WaRsPkgCStateDisplayPMReq:hsw */
6157 I915_WRITE(CHICKEN_PAR1_1,
6158 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006159
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006160 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006161}
6162
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006163static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006164{
6165 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006166 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006167
Ville Syrjälä017636c2013-12-05 15:51:37 +02006168 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006169
Damien Lespiau231e54f2012-10-19 17:55:41 +01006170 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006171
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006172 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006173 I915_WRITE(_3D_CHICKEN3,
6174 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6175
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006176 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006177 I915_WRITE(IVB_CHICKEN3,
6178 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6179 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6180
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006181 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006182 if (IS_IVB_GT1(dev))
6183 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6184 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006185
Akash Goel4e046322014-04-04 17:14:38 +05306186 /* WaDisable_RenderCache_OperationalFlush:ivb */
6187 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6188
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006189 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006190 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6191 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6192
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006193 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006194 I915_WRITE(GEN7_L3CNTLREG1,
6195 GEN7_WA_FOR_GEN7_L3_CONTROL);
6196 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006197 GEN7_WA_L3_CHICKEN_MODE);
6198 if (IS_IVB_GT1(dev))
6199 I915_WRITE(GEN7_ROW_CHICKEN2,
6200 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006201 else {
6202 /* must write both registers */
6203 I915_WRITE(GEN7_ROW_CHICKEN2,
6204 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006205 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6206 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006207 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006208
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006209 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006210 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6211 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6212
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006213 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006214 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006215 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006216 */
6217 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006218 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006219
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006220 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006221 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6222 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6223 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6224
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006225 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006226
6227 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006228
Chris Wilson22721342014-03-04 09:41:43 +00006229 if (0) { /* causes HiZ corruption on ivb:gt1 */
6230 /* enable HiZ Raw Stall Optimization */
6231 I915_WRITE(CACHE_MODE_0_GEN7,
6232 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6233 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006234
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006235 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006236 I915_WRITE(CACHE_MODE_1,
6237 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006238
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006239 /*
6240 * BSpec recommends 8x4 when MSAA is used,
6241 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006242 *
6243 * Note that PS/WM thread counts depend on the WIZ hashing
6244 * disable bit, which we don't touch here, but it's good
6245 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006246 */
6247 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006248 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006249
Ben Widawsky20848222012-05-04 18:58:59 -07006250 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6251 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6252 snpcr |= GEN6_MBC_SNPCR_MED;
6253 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006254
Ben Widawskyab5c6082013-04-05 13:12:41 -07006255 if (!HAS_PCH_NOP(dev))
6256 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006257
6258 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006259}
6260
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006261static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006262{
6263 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006264
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03006265 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006266
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006267 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006268 I915_WRITE(_3D_CHICKEN3,
6269 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6270
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006271 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006272 I915_WRITE(IVB_CHICKEN3,
6273 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6274 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6275
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006276 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006277 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006278 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006279 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6280 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006281
Akash Goel4e046322014-04-04 17:14:38 +05306282 /* WaDisable_RenderCache_OperationalFlush:vlv */
6283 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6284
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006285 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006286 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6287 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6288
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006289 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006290 I915_WRITE(GEN7_ROW_CHICKEN2,
6291 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6292
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006293 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006294 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6295 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6296 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6297
Ville Syrjälä46680e02014-01-22 21:33:01 +02006298 gen7_setup_fixed_func_scheduler(dev_priv);
6299
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006300 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006301 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006302 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006303 */
6304 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006305 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006306
Akash Goelc98f5062014-03-24 23:00:07 +05306307 /* WaDisableL3Bank2xClockGate:vlv
6308 * Disabling L3 clock gating- MMIO 940c[25] = 1
6309 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6310 I915_WRITE(GEN7_UCGCTL4,
6311 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006312
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006313 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006314
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006315 /*
6316 * BSpec says this must be set, even though
6317 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6318 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006319 I915_WRITE(CACHE_MODE_1,
6320 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006321
6322 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006323 * BSpec recommends 8x4 when MSAA is used,
6324 * however in practice 16x4 seems fastest.
6325 *
6326 * Note that PS/WM thread counts depend on the WIZ hashing
6327 * disable bit, which we don't touch here, but it's good
6328 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6329 */
6330 I915_WRITE(GEN7_GT_MODE,
6331 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6332
6333 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006334 * WaIncreaseL3CreditsForVLVB0:vlv
6335 * This is the hardware default actually.
6336 */
6337 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6338
6339 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006340 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006341 * Disable clock gating on th GCFG unit to prevent a delay
6342 * in the reporting of vblank events.
6343 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006344 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006345}
6346
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006347static void cherryview_init_clock_gating(struct drm_device *dev)
6348{
6349 struct drm_i915_private *dev_priv = dev->dev_private;
6350
6351 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6352
6353 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006354
Ville Syrjälä232ce332014-04-09 13:28:35 +03006355 /* WaVSRefCountFullforceMissDisable:chv */
6356 /* WaDSRefCountFullforceMissDisable:chv */
6357 I915_WRITE(GEN7_FF_THREAD_MODE,
6358 I915_READ(GEN7_FF_THREAD_MODE) &
6359 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006360
6361 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6362 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6363 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006364
6365 /* WaDisableCSUnitClockGating:chv */
6366 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6367 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006368
6369 /* WaDisableSDEUnitClockGating:chv */
6370 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6371 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006372}
6373
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006374static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006375{
6376 struct drm_i915_private *dev_priv = dev->dev_private;
6377 uint32_t dspclk_gate;
6378
6379 I915_WRITE(RENCLK_GATE_D1, 0);
6380 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6381 GS_UNIT_CLOCK_GATE_DISABLE |
6382 CL_UNIT_CLOCK_GATE_DISABLE);
6383 I915_WRITE(RAMCLK_GATE_D, 0);
6384 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6385 OVRUNIT_CLOCK_GATE_DISABLE |
6386 OVCUNIT_CLOCK_GATE_DISABLE;
6387 if (IS_GM45(dev))
6388 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6389 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006390
6391 /* WaDisableRenderCachePipelinedFlush */
6392 I915_WRITE(CACHE_MODE_0,
6393 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006394
Akash Goel4e046322014-04-04 17:14:38 +05306395 /* WaDisable_RenderCache_OperationalFlush:g4x */
6396 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6397
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006398 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006399}
6400
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006401static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006402{
6403 struct drm_i915_private *dev_priv = dev->dev_private;
6404
6405 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6406 I915_WRITE(RENCLK_GATE_D2, 0);
6407 I915_WRITE(DSPCLK_GATE_D, 0);
6408 I915_WRITE(RAMCLK_GATE_D, 0);
6409 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006410 I915_WRITE(MI_ARB_STATE,
6411 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306412
6413 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6414 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006415}
6416
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006417static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006418{
6419 struct drm_i915_private *dev_priv = dev->dev_private;
6420
6421 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6422 I965_RCC_CLOCK_GATE_DISABLE |
6423 I965_RCPB_CLOCK_GATE_DISABLE |
6424 I965_ISC_CLOCK_GATE_DISABLE |
6425 I965_FBC_CLOCK_GATE_DISABLE);
6426 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006427 I915_WRITE(MI_ARB_STATE,
6428 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306429
6430 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6431 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006432}
6433
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006434static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006435{
6436 struct drm_i915_private *dev_priv = dev->dev_private;
6437 u32 dstate = I915_READ(D_STATE);
6438
6439 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6440 DSTATE_DOT_CLOCK_GATING;
6441 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006442
6443 if (IS_PINEVIEW(dev))
6444 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006445
6446 /* IIR "flip pending" means done if this bit is set */
6447 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006448
6449 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006450 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006451
6452 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6453 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006454
6455 I915_WRITE(MI_ARB_STATE,
6456 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006457}
6458
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006459static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006460{
6461 struct drm_i915_private *dev_priv = dev->dev_private;
6462
6463 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006464
6465 /* interrupts should cause a wake up from C3 */
6466 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6467 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006468
6469 I915_WRITE(MEM_MODE,
6470 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006471}
6472
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006473static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006474{
6475 struct drm_i915_private *dev_priv = dev->dev_private;
6476
6477 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006478
6479 I915_WRITE(MEM_MODE,
6480 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6481 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006482}
6483
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006484void intel_init_clock_gating(struct drm_device *dev)
6485{
6486 struct drm_i915_private *dev_priv = dev->dev_private;
6487
Damien Lespiauc57e3552015-02-09 19:33:05 +00006488 if (dev_priv->display.init_clock_gating)
6489 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006490}
6491
Imre Deak7d708ee2013-04-17 14:04:50 +03006492void intel_suspend_hw(struct drm_device *dev)
6493{
6494 if (HAS_PCH_LPT(dev))
6495 lpt_suspend_hw(dev);
6496}
6497
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006498/* Set up chip specific power management-related functions */
6499void intel_init_pm(struct drm_device *dev)
6500{
6501 struct drm_i915_private *dev_priv = dev->dev_private;
6502
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006503 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006504
Daniel Vetterc921aba2012-04-26 23:28:17 +02006505 /* For cxsr */
6506 if (IS_PINEVIEW(dev))
6507 i915_pineview_get_mem_freq(dev);
6508 else if (IS_GEN5(dev))
6509 i915_ironlake_get_mem_freq(dev);
6510
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006511 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006512 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006513 skl_setup_wm_latency(dev);
6514
Damien Lespiau45db2192015-02-09 19:33:09 +00006515 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006516 dev_priv->display.update_wm = skl_update_wm;
6517 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306518 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006519 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006520
Ville Syrjäläbd602542014-01-07 16:14:10 +02006521 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6522 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6523 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6524 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6525 dev_priv->display.update_wm = ilk_update_wm;
6526 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6527 } else {
6528 DRM_DEBUG_KMS("Failed to read display plane latency. "
6529 "Disable CxSR\n");
6530 }
6531
6532 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006533 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006534 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006535 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006536 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006537 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006538 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006539 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006540 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006541 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006542 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03006543 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306544 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006545 dev_priv->display.init_clock_gating =
6546 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006547 } else if (IS_VALLEYVIEW(dev)) {
6548 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306549 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006550 dev_priv->display.init_clock_gating =
6551 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006552 } else if (IS_PINEVIEW(dev)) {
6553 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6554 dev_priv->is_ddr3,
6555 dev_priv->fsb_freq,
6556 dev_priv->mem_freq)) {
6557 DRM_INFO("failed to find known CxSR latency "
6558 "(found ddr%s fsb freq %d, mem freq %d), "
6559 "disabling CxSR\n",
6560 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6561 dev_priv->fsb_freq, dev_priv->mem_freq);
6562 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006563 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006564 dev_priv->display.update_wm = NULL;
6565 } else
6566 dev_priv->display.update_wm = pineview_update_wm;
6567 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6568 } else if (IS_G4X(dev)) {
6569 dev_priv->display.update_wm = g4x_update_wm;
6570 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6571 } else if (IS_GEN4(dev)) {
6572 dev_priv->display.update_wm = i965_update_wm;
6573 if (IS_CRESTLINE(dev))
6574 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6575 else if (IS_BROADWATER(dev))
6576 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6577 } else if (IS_GEN3(dev)) {
6578 dev_priv->display.update_wm = i9xx_update_wm;
6579 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6580 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006581 } else if (IS_GEN2(dev)) {
6582 if (INTEL_INFO(dev)->num_pipes == 1) {
6583 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006584 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006585 } else {
6586 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006587 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006588 }
6589
6590 if (IS_I85X(dev) || IS_I865G(dev))
6591 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6592 else
6593 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6594 } else {
6595 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006596 }
6597}
6598
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006599int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006600{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006601 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006602
6603 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6604 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6605 return -EAGAIN;
6606 }
6607
6608 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006609 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006610 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6611
6612 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6613 500)) {
6614 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6615 return -ETIMEDOUT;
6616 }
6617
6618 *val = I915_READ(GEN6_PCODE_DATA);
6619 I915_WRITE(GEN6_PCODE_DATA, 0);
6620
6621 return 0;
6622}
6623
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006624int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006625{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006626 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006627
6628 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6629 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6630 return -EAGAIN;
6631 }
6632
6633 I915_WRITE(GEN6_PCODE_DATA, val);
6634 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6635
6636 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6637 500)) {
6638 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6639 return -ETIMEDOUT;
6640 }
6641
6642 I915_WRITE(GEN6_PCODE_DATA, 0);
6643
6644 return 0;
6645}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006646
Ville Syrjälädd06f882014-11-10 22:55:12 +02006647static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006648{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006649 switch (czclk_freq) {
6650 case 200:
6651 return 10;
6652 case 267:
6653 return 12;
6654 case 320:
6655 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006656 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006657 case 400:
6658 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006659 default:
6660 return -1;
6661 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006662}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006663
Ville Syrjälädd06f882014-11-10 22:55:12 +02006664static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6665{
6666 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6667
6668 div = vlv_gpu_freq_div(czclk_freq);
6669 if (div < 0)
6670 return div;
6671
6672 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006673}
6674
Fengguang Wub55dd642014-07-12 11:21:39 +02006675static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006676{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006677 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006678
Ville Syrjälädd06f882014-11-10 22:55:12 +02006679 mul = vlv_gpu_freq_div(czclk_freq);
6680 if (mul < 0)
6681 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006682
Ville Syrjälädd06f882014-11-10 22:55:12 +02006683 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006684}
6685
Fengguang Wub55dd642014-07-12 11:21:39 +02006686static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306687{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006688 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306689
Ville Syrjälädd06f882014-11-10 22:55:12 +02006690 div = vlv_gpu_freq_div(czclk_freq) / 2;
6691 if (div < 0)
6692 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306693
Ville Syrjälädd06f882014-11-10 22:55:12 +02006694 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306695}
6696
Fengguang Wub55dd642014-07-12 11:21:39 +02006697static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306698{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006699 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306700
Ville Syrjälädd06f882014-11-10 22:55:12 +02006701 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6702 if (mul < 0)
6703 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306704
Ville Syrjälä1c147622014-08-18 14:42:43 +03006705 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006706 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306707}
6708
Ville Syrjälä616bc822015-01-23 21:04:25 +02006709int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6710{
6711 if (IS_CHERRYVIEW(dev_priv->dev))
6712 return chv_gpu_freq(dev_priv, val);
6713 else if (IS_VALLEYVIEW(dev_priv->dev))
6714 return byt_gpu_freq(dev_priv, val);
6715 else
6716 return val * GT_FREQUENCY_MULTIPLIER;
6717}
6718
Ville Syrjälä616bc822015-01-23 21:04:25 +02006719int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6720{
Deepak S22b1b2f2014-07-12 14:54:33 +05306721 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006722 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306723 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006724 return byt_freq_opcode(dev_priv, val);
6725 else
6726 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306727}
6728
Daniel Vetterf742a552013-12-06 10:17:53 +01006729void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006730{
6731 struct drm_i915_private *dev_priv = dev->dev_private;
6732
Daniel Vetterf742a552013-12-06 10:17:53 +01006733 mutex_init(&dev_priv->rps.hw_lock);
6734
Chris Wilson907b28c2013-07-19 20:36:52 +01006735 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6736 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006737
Paulo Zanoni33688d92014-03-07 20:08:19 -03006738 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006739}