blob: c198dbad3e8286f5b202228206891f4ef58d995e [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;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200758
759 *prec_mult = IS_CHERRYVIEW(dev) ? 16 : 64;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530760 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300761
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200762 if (*drain_latency > DRAIN_LATENCY_MASK) {
763 *prec_mult /= 2;
764 *drain_latency = (64 * (*prec_mult) * 4) / entries;
765 }
766
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530767 if (*drain_latency > DRAIN_LATENCY_MASK)
768 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300769
770 return true;
771}
772
773/*
774 * Update drain latency registers of memory arbiter
775 *
776 * Valleyview SoC has a new memory arbiter and needs drain latency registers
777 * to be programmed. Each plane has a drain latency multiplier and a drain
778 * latency value.
779 */
780
Gajanan Bhat41aad812014-07-16 18:24:03 +0530781static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300782{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700783 struct drm_device *dev = crtc->dev;
784 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
786 int pixel_size;
787 int drain_latency;
788 enum pipe pipe = intel_crtc->pipe;
789 int plane_prec, prec_mult, plane_dl;
Ville Syrjälä12030512015-03-05 21:19:42 +0200790 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 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;
Ville Syrjälä12030512015-03-05 21:19:42 +0200988 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530989
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700990 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530991 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
992
993 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
994 &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700995 plane_prec = (prec_mult == high_precision) ?
996 DDL_SPRITE_PRECISION_HIGH(sprite) :
997 DDL_SPRITE_PRECISION_LOW(sprite);
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530998 sprite_dl |= plane_prec |
999 (drain_latency << DDL_SPRITE_SHIFT(sprite));
1000 }
1001
1002 I915_WRITE(VLV_DDL(pipe), sprite_dl);
1003}
1004
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001005static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001006{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001007 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001008 static const int sr_latency_ns = 12000;
1009 struct drm_i915_private *dev_priv = dev->dev_private;
1010 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1011 int plane_sr, cursor_sr;
1012 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001013 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001014
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001015 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001016 &g4x_wm_info, pessimal_latency_ns,
1017 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001018 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001019 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001020
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001021 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001022 &g4x_wm_info, pessimal_latency_ns,
1023 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001024 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001025 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001026
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001027 if (single_plane_enabled(enabled) &&
1028 g4x_compute_srwm(dev, ffs(enabled) - 1,
1029 sr_latency_ns,
1030 &g4x_wm_info,
1031 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001032 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001033 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001034 } else {
Imre Deak98584252014-06-13 14:54:20 +03001035 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001036 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001037 plane_sr = cursor_sr = 0;
1038 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001039
Ville Syrjäläa5043452014-06-28 02:04:18 +03001040 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1041 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001042 planea_wm, cursora_wm,
1043 planeb_wm, cursorb_wm,
1044 plane_sr, cursor_sr);
1045
1046 I915_WRITE(DSPFW1,
1047 (plane_sr << DSPFW_SR_SHIFT) |
1048 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1049 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001050 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001051 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001052 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001053 (cursora_wm << DSPFW_CURSORA_SHIFT));
1054 /* HPLL off in SR has some issues on G4x... disable it */
1055 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001056 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001057 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001058
1059 if (cxsr_enabled)
1060 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001061}
1062
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001063static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001064{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001065 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001066 struct drm_i915_private *dev_priv = dev->dev_private;
1067 struct drm_crtc *crtc;
1068 int srwm = 1;
1069 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001070 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001071
1072 /* Calc sr entries for one plane configs */
1073 crtc = single_enabled_crtc(dev);
1074 if (crtc) {
1075 /* self-refresh has much higher latency */
1076 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001077 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001078 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001079 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001080 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001081 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001082 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001083 unsigned long line_time_us;
1084 int entries;
1085
Ville Syrjälä922044c2014-02-14 14:18:57 +02001086 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001087
1088 /* Use ns/us then divide to preserve precision */
1089 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1090 pixel_size * hdisplay;
1091 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1092 srwm = I965_FIFO_SIZE - entries;
1093 if (srwm < 0)
1094 srwm = 1;
1095 srwm &= 0x1ff;
1096 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1097 entries, srwm);
1098
1099 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001100 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001101 entries = DIV_ROUND_UP(entries,
1102 i965_cursor_wm_info.cacheline_size);
1103 cursor_sr = i965_cursor_wm_info.fifo_size -
1104 (entries + i965_cursor_wm_info.guard_size);
1105
1106 if (cursor_sr > i965_cursor_wm_info.max_wm)
1107 cursor_sr = i965_cursor_wm_info.max_wm;
1108
1109 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1110 "cursor %d\n", srwm, cursor_sr);
1111
Imre Deak98584252014-06-13 14:54:20 +03001112 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001113 } else {
Imre Deak98584252014-06-13 14:54:20 +03001114 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001115 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001116 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001117 }
1118
1119 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1120 srwm);
1121
1122 /* 965 has limitations... */
1123 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001124 (8 << DSPFW_CURSORB_SHIFT) |
1125 (8 << DSPFW_PLANEB_SHIFT) |
1126 (8 << DSPFW_PLANEA_SHIFT));
1127 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1128 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001129 /* update cursor SR watermark */
1130 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001131
1132 if (cxsr_enabled)
1133 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001134}
1135
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001136static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001137{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001138 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001139 struct drm_i915_private *dev_priv = dev->dev_private;
1140 const struct intel_watermark_params *wm_info;
1141 uint32_t fwater_lo;
1142 uint32_t fwater_hi;
1143 int cwm, srwm = 1;
1144 int fifo_size;
1145 int planea_wm, planeb_wm;
1146 struct drm_crtc *crtc, *enabled = NULL;
1147
1148 if (IS_I945GM(dev))
1149 wm_info = &i945_wm_info;
1150 else if (!IS_GEN2(dev))
1151 wm_info = &i915_wm_info;
1152 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001153 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001154
1155 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1156 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001157 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001158 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001159 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001160 if (IS_GEN2(dev))
1161 cpp = 4;
1162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001163 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001164 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001165 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001166 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001167 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001168 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001169 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001170 if (planea_wm > (long)wm_info->max_wm)
1171 planea_wm = wm_info->max_wm;
1172 }
1173
1174 if (IS_GEN2(dev))
1175 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001176
1177 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1178 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001179 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001180 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001181 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001182 if (IS_GEN2(dev))
1183 cpp = 4;
1184
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001185 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001186 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001187 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001188 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001189 if (enabled == NULL)
1190 enabled = crtc;
1191 else
1192 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001193 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001194 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001195 if (planeb_wm > (long)wm_info->max_wm)
1196 planeb_wm = wm_info->max_wm;
1197 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001198
1199 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1200
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001201 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001202 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001203
Matt Roper59bea882015-02-27 10:12:01 -08001204 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001205
1206 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001207 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001208 enabled = NULL;
1209 }
1210
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001211 /*
1212 * Overlay gets an aggressive default since video jitter is bad.
1213 */
1214 cwm = 2;
1215
1216 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001217 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001218
1219 /* Calc sr entries for one plane configs */
1220 if (HAS_FW_BLC(dev) && enabled) {
1221 /* self-refresh has much higher latency */
1222 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001223 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001224 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001225 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001226 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001227 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001228 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001229 unsigned long line_time_us;
1230 int entries;
1231
Ville Syrjälä922044c2014-02-14 14:18:57 +02001232 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001233
1234 /* Use ns/us then divide to preserve precision */
1235 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1236 pixel_size * hdisplay;
1237 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1238 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1239 srwm = wm_info->fifo_size - entries;
1240 if (srwm < 0)
1241 srwm = 1;
1242
1243 if (IS_I945G(dev) || IS_I945GM(dev))
1244 I915_WRITE(FW_BLC_SELF,
1245 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1246 else if (IS_I915GM(dev))
1247 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1248 }
1249
1250 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1251 planea_wm, planeb_wm, cwm, srwm);
1252
1253 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1254 fwater_hi = (cwm & 0x1f);
1255
1256 /* Set request length to 8 cachelines per fetch */
1257 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1258 fwater_hi = fwater_hi | (1 << 8);
1259
1260 I915_WRITE(FW_BLC, fwater_lo);
1261 I915_WRITE(FW_BLC2, fwater_hi);
1262
Imre Deak5209b1f2014-07-01 12:36:17 +03001263 if (enabled)
1264 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001265}
1266
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001267static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001268{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001269 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001270 struct drm_i915_private *dev_priv = dev->dev_private;
1271 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001272 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001273 uint32_t fwater_lo;
1274 int planea_wm;
1275
1276 crtc = single_enabled_crtc(dev);
1277 if (crtc == NULL)
1278 return;
1279
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001280 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001281 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001282 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001283 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001284 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001285 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1286 fwater_lo |= (3<<8) | planea_wm;
1287
1288 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1289
1290 I915_WRITE(FW_BLC, fwater_lo);
1291}
1292
Ville Syrjälä36587292013-07-05 11:57:16 +03001293static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1294 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001295{
1296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001297 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001298
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001299 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001300
1301 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1302 * adjust the pixel_rate here. */
1303
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001304 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001305 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001306 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001307
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001308 pipe_w = intel_crtc->config->pipe_src_w;
1309 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001310 pfit_w = (pfit_size >> 16) & 0xFFFF;
1311 pfit_h = pfit_size & 0xFFFF;
1312 if (pipe_w < pfit_w)
1313 pipe_w = pfit_w;
1314 if (pipe_h < pfit_h)
1315 pipe_h = pfit_h;
1316
1317 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1318 pfit_w * pfit_h);
1319 }
1320
1321 return pixel_rate;
1322}
1323
Ville Syrjälä37126462013-08-01 16:18:55 +03001324/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001325static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001326 uint32_t latency)
1327{
1328 uint64_t ret;
1329
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001330 if (WARN(latency == 0, "Latency value missing\n"))
1331 return UINT_MAX;
1332
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001333 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1334 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1335
1336 return ret;
1337}
1338
Ville Syrjälä37126462013-08-01 16:18:55 +03001339/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001340static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001341 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1342 uint32_t latency)
1343{
1344 uint32_t ret;
1345
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001346 if (WARN(latency == 0, "Latency value missing\n"))
1347 return UINT_MAX;
1348
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001349 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1350 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1351 ret = DIV_ROUND_UP(ret, 64) + 2;
1352 return ret;
1353}
1354
Ville Syrjälä23297042013-07-05 11:57:17 +03001355static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001356 uint8_t bytes_per_pixel)
1357{
1358 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1359}
1360
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001361struct skl_pipe_wm_parameters {
1362 bool active;
1363 uint32_t pipe_htotal;
1364 uint32_t pixel_rate; /* in KHz */
1365 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1366 struct intel_plane_wm_parameters cursor;
1367};
1368
Imre Deak820c1982013-12-17 14:46:36 +02001369struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001370 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001371 uint32_t pipe_htotal;
1372 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001373 struct intel_plane_wm_parameters pri;
1374 struct intel_plane_wm_parameters spr;
1375 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001376};
1377
Imre Deak820c1982013-12-17 14:46:36 +02001378struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001379 uint16_t pri;
1380 uint16_t spr;
1381 uint16_t cur;
1382 uint16_t fbc;
1383};
1384
Ville Syrjälä240264f2013-08-07 13:29:12 +03001385/* used in computing the new watermarks state */
1386struct intel_wm_config {
1387 unsigned int num_pipes_active;
1388 bool sprites_enabled;
1389 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001390};
1391
Ville Syrjälä37126462013-08-01 16:18:55 +03001392/*
1393 * For both WM_PIPE and WM_LP.
1394 * mem_value must be in 0.1us units.
1395 */
Imre Deak820c1982013-12-17 14:46:36 +02001396static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001397 uint32_t mem_value,
1398 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001399{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001400 uint32_t method1, method2;
1401
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001402 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001403 return 0;
1404
Ville Syrjälä23297042013-07-05 11:57:17 +03001405 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001406 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001407 mem_value);
1408
1409 if (!is_lp)
1410 return method1;
1411
Ville Syrjälä23297042013-07-05 11:57:17 +03001412 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001413 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001414 params->pri.horiz_pixels,
1415 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001416 mem_value);
1417
1418 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001419}
1420
Ville Syrjälä37126462013-08-01 16:18:55 +03001421/*
1422 * For both WM_PIPE and WM_LP.
1423 * mem_value must be in 0.1us units.
1424 */
Imre Deak820c1982013-12-17 14:46:36 +02001425static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001426 uint32_t mem_value)
1427{
1428 uint32_t method1, method2;
1429
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001430 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001431 return 0;
1432
Ville Syrjälä23297042013-07-05 11:57:17 +03001433 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001434 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001435 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001436 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001437 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001438 params->spr.horiz_pixels,
1439 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001440 mem_value);
1441 return min(method1, method2);
1442}
1443
Ville Syrjälä37126462013-08-01 16:18:55 +03001444/*
1445 * For both WM_PIPE and WM_LP.
1446 * mem_value must be in 0.1us units.
1447 */
Imre Deak820c1982013-12-17 14:46:36 +02001448static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001449 uint32_t mem_value)
1450{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001451 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001452 return 0;
1453
Ville Syrjälä23297042013-07-05 11:57:17 +03001454 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001455 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001456 params->cur.horiz_pixels,
1457 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001458 mem_value);
1459}
1460
Paulo Zanonicca32e92013-05-31 11:45:06 -03001461/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001462static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001463 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001464{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001465 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001466 return 0;
1467
Ville Syrjälä23297042013-07-05 11:57:17 +03001468 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001469 params->pri.horiz_pixels,
1470 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001471}
1472
Ville Syrjälä158ae642013-08-07 13:28:19 +03001473static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1474{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001475 if (INTEL_INFO(dev)->gen >= 8)
1476 return 3072;
1477 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001478 return 768;
1479 else
1480 return 512;
1481}
1482
Ville Syrjälä4e975082014-03-07 18:32:11 +02001483static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1484 int level, bool is_sprite)
1485{
1486 if (INTEL_INFO(dev)->gen >= 8)
1487 /* BDW primary/sprite plane watermarks */
1488 return level == 0 ? 255 : 2047;
1489 else if (INTEL_INFO(dev)->gen >= 7)
1490 /* IVB/HSW primary/sprite plane watermarks */
1491 return level == 0 ? 127 : 1023;
1492 else if (!is_sprite)
1493 /* ILK/SNB primary plane watermarks */
1494 return level == 0 ? 127 : 511;
1495 else
1496 /* ILK/SNB sprite plane watermarks */
1497 return level == 0 ? 63 : 255;
1498}
1499
1500static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1501 int level)
1502{
1503 if (INTEL_INFO(dev)->gen >= 7)
1504 return level == 0 ? 63 : 255;
1505 else
1506 return level == 0 ? 31 : 63;
1507}
1508
1509static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1510{
1511 if (INTEL_INFO(dev)->gen >= 8)
1512 return 31;
1513 else
1514 return 15;
1515}
1516
Ville Syrjälä158ae642013-08-07 13:28:19 +03001517/* Calculate the maximum primary/sprite plane watermark */
1518static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1519 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001520 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001521 enum intel_ddb_partitioning ddb_partitioning,
1522 bool is_sprite)
1523{
1524 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001525
1526 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001527 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001528 return 0;
1529
1530 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001531 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001532 fifo_size /= INTEL_INFO(dev)->num_pipes;
1533
1534 /*
1535 * For some reason the non self refresh
1536 * FIFO size is only half of the self
1537 * refresh FIFO size on ILK/SNB.
1538 */
1539 if (INTEL_INFO(dev)->gen <= 6)
1540 fifo_size /= 2;
1541 }
1542
Ville Syrjälä240264f2013-08-07 13:29:12 +03001543 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001544 /* level 0 is always calculated with 1:1 split */
1545 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1546 if (is_sprite)
1547 fifo_size *= 5;
1548 fifo_size /= 6;
1549 } else {
1550 fifo_size /= 2;
1551 }
1552 }
1553
1554 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001555 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001556}
1557
1558/* Calculate the maximum cursor plane watermark */
1559static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001560 int level,
1561 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001562{
1563 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001564 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001565 return 64;
1566
1567 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001568 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001569}
1570
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001571static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001572 int level,
1573 const struct intel_wm_config *config,
1574 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001575 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001576{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001577 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1578 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1579 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001580 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001581}
1582
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001583static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1584 int level,
1585 struct ilk_wm_maximums *max)
1586{
1587 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1588 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1589 max->cur = ilk_cursor_wm_reg_max(dev, level);
1590 max->fbc = ilk_fbc_wm_reg_max(dev);
1591}
1592
Ville Syrjäläd9395652013-10-09 19:18:10 +03001593static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001594 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001595 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001596{
1597 bool ret;
1598
1599 /* already determined to be invalid? */
1600 if (!result->enable)
1601 return false;
1602
1603 result->enable = result->pri_val <= max->pri &&
1604 result->spr_val <= max->spr &&
1605 result->cur_val <= max->cur;
1606
1607 ret = result->enable;
1608
1609 /*
1610 * HACK until we can pre-compute everything,
1611 * and thus fail gracefully if LP0 watermarks
1612 * are exceeded...
1613 */
1614 if (level == 0 && !result->enable) {
1615 if (result->pri_val > max->pri)
1616 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1617 level, result->pri_val, max->pri);
1618 if (result->spr_val > max->spr)
1619 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1620 level, result->spr_val, max->spr);
1621 if (result->cur_val > max->cur)
1622 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1623 level, result->cur_val, max->cur);
1624
1625 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1626 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1627 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1628 result->enable = true;
1629 }
1630
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001631 return ret;
1632}
1633
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001634static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001635 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001636 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001637 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001638{
1639 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1640 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1641 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1642
1643 /* WM1+ latency values stored in 0.5us units */
1644 if (level > 0) {
1645 pri_latency *= 5;
1646 spr_latency *= 5;
1647 cur_latency *= 5;
1648 }
1649
1650 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1651 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1652 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1653 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1654 result->enable = true;
1655}
1656
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001657static uint32_t
1658hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001659{
1660 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001662 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001663 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001664
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001665 if (!intel_crtc_active(crtc))
1666 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001667
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001668 /* The WM are computed with base on how long it takes to fill a single
1669 * row at the given clock rate, multiplied by 8.
1670 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001671 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1672 mode->crtc_clock);
1673 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001674 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001675
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001676 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1677 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001678}
1679
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001680static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001681{
1682 struct drm_i915_private *dev_priv = dev->dev_private;
1683
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001684 if (IS_GEN9(dev)) {
1685 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001686 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001687 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001688
1689 /* read the first set of memory latencies[0:3] */
1690 val = 0; /* data0 to be programmed to 0 for first set */
1691 mutex_lock(&dev_priv->rps.hw_lock);
1692 ret = sandybridge_pcode_read(dev_priv,
1693 GEN9_PCODE_READ_MEM_LATENCY,
1694 &val);
1695 mutex_unlock(&dev_priv->rps.hw_lock);
1696
1697 if (ret) {
1698 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1699 return;
1700 }
1701
1702 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1703 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1704 GEN9_MEM_LATENCY_LEVEL_MASK;
1705 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1706 GEN9_MEM_LATENCY_LEVEL_MASK;
1707 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1708 GEN9_MEM_LATENCY_LEVEL_MASK;
1709
1710 /* read the second set of memory latencies[4:7] */
1711 val = 1; /* data0 to be programmed to 1 for second set */
1712 mutex_lock(&dev_priv->rps.hw_lock);
1713 ret = sandybridge_pcode_read(dev_priv,
1714 GEN9_PCODE_READ_MEM_LATENCY,
1715 &val);
1716 mutex_unlock(&dev_priv->rps.hw_lock);
1717 if (ret) {
1718 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1719 return;
1720 }
1721
1722 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1723 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1724 GEN9_MEM_LATENCY_LEVEL_MASK;
1725 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1726 GEN9_MEM_LATENCY_LEVEL_MASK;
1727 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1728 GEN9_MEM_LATENCY_LEVEL_MASK;
1729
Vandana Kannan367294b2014-11-04 17:06:46 +00001730 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001731 * WaWmMemoryReadLatency:skl
1732 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001733 * punit doesn't take into account the read latency so we need
1734 * to add 2us to the various latency levels we retrieve from
1735 * the punit.
1736 * - W0 is a bit special in that it's the only level that
1737 * can't be disabled if we want to have display working, so
1738 * we always add 2us there.
1739 * - For levels >=1, punit returns 0us latency when they are
1740 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001741 *
1742 * Additionally, if a level n (n > 1) has a 0us latency, all
1743 * levels m (m >= n) need to be disabled. We make sure to
1744 * sanitize the values out of the punit to satisfy this
1745 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001746 */
1747 wm[0] += 2;
1748 for (level = 1; level <= max_level; level++)
1749 if (wm[level] != 0)
1750 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001751 else {
1752 for (i = level + 1; i <= max_level; i++)
1753 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001754
Vandana Kannan4f947382014-11-04 17:06:47 +00001755 break;
1756 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001757 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001758 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1759
1760 wm[0] = (sskpd >> 56) & 0xFF;
1761 if (wm[0] == 0)
1762 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001763 wm[1] = (sskpd >> 4) & 0xFF;
1764 wm[2] = (sskpd >> 12) & 0xFF;
1765 wm[3] = (sskpd >> 20) & 0x1FF;
1766 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001767 } else if (INTEL_INFO(dev)->gen >= 6) {
1768 uint32_t sskpd = I915_READ(MCH_SSKPD);
1769
1770 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1771 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1772 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1773 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001774 } else if (INTEL_INFO(dev)->gen >= 5) {
1775 uint32_t mltr = I915_READ(MLTR_ILK);
1776
1777 /* ILK primary LP0 latency is 700 ns */
1778 wm[0] = 7;
1779 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1780 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001781 }
1782}
1783
Ville Syrjälä53615a52013-08-01 16:18:50 +03001784static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1785{
1786 /* ILK sprite LP0 latency is 1300 ns */
1787 if (INTEL_INFO(dev)->gen == 5)
1788 wm[0] = 13;
1789}
1790
1791static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1792{
1793 /* ILK cursor LP0 latency is 1300 ns */
1794 if (INTEL_INFO(dev)->gen == 5)
1795 wm[0] = 13;
1796
1797 /* WaDoubleCursorLP3Latency:ivb */
1798 if (IS_IVYBRIDGE(dev))
1799 wm[3] *= 2;
1800}
1801
Damien Lespiau546c81f2014-05-13 15:30:26 +01001802int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001803{
1804 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001805 if (IS_GEN9(dev))
1806 return 7;
1807 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001808 return 4;
1809 else if (INTEL_INFO(dev)->gen >= 6)
1810 return 3;
1811 else
1812 return 2;
1813}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001814
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001815static void intel_print_wm_latency(struct drm_device *dev,
1816 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001817 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001818{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001819 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001820
1821 for (level = 0; level <= max_level; level++) {
1822 unsigned int latency = wm[level];
1823
1824 if (latency == 0) {
1825 DRM_ERROR("%s WM%d latency not provided\n",
1826 name, level);
1827 continue;
1828 }
1829
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001830 /*
1831 * - latencies are in us on gen9.
1832 * - before then, WM1+ latency values are in 0.5us units
1833 */
1834 if (IS_GEN9(dev))
1835 latency *= 10;
1836 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001837 latency *= 5;
1838
1839 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1840 name, level, wm[level],
1841 latency / 10, latency % 10);
1842 }
1843}
1844
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001845static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1846 uint16_t wm[5], uint16_t min)
1847{
1848 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1849
1850 if (wm[0] >= min)
1851 return false;
1852
1853 wm[0] = max(wm[0], min);
1854 for (level = 1; level <= max_level; level++)
1855 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1856
1857 return true;
1858}
1859
1860static void snb_wm_latency_quirk(struct drm_device *dev)
1861{
1862 struct drm_i915_private *dev_priv = dev->dev_private;
1863 bool changed;
1864
1865 /*
1866 * The BIOS provided WM memory latency values are often
1867 * inadequate for high resolution displays. Adjust them.
1868 */
1869 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1870 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1871 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1872
1873 if (!changed)
1874 return;
1875
1876 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1877 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1878 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1879 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1880}
1881
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001882static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001883{
1884 struct drm_i915_private *dev_priv = dev->dev_private;
1885
1886 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1887
1888 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1889 sizeof(dev_priv->wm.pri_latency));
1890 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1891 sizeof(dev_priv->wm.pri_latency));
1892
1893 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1894 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001895
1896 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1897 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1898 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001899
1900 if (IS_GEN6(dev))
1901 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001902}
1903
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001904static void skl_setup_wm_latency(struct drm_device *dev)
1905{
1906 struct drm_i915_private *dev_priv = dev->dev_private;
1907
1908 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1909 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1910}
1911
Imre Deak820c1982013-12-17 14:46:36 +02001912static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001913 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001914{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001915 struct drm_device *dev = crtc->dev;
1916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1917 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001918 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001919
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001920 if (!intel_crtc_active(crtc))
1921 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001922
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001923 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001924 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001925 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roper59bea882015-02-27 10:12:01 -08001926 p->pri.bytes_per_pixel = crtc->primary->state->fb->bits_per_pixel / 8;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001927 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001928 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08001929 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001930 /* TODO: for now, assume primary and cursor planes are always enabled. */
1931 p->pri.enabled = true;
1932 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001933
Matt Roperaf2b6532014-04-01 15:22:32 -07001934 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001935 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001936
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001937 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001938 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001939 break;
1940 }
1941 }
1942}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001943
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001944static void ilk_compute_wm_config(struct drm_device *dev,
1945 struct intel_wm_config *config)
1946{
1947 struct intel_crtc *intel_crtc;
1948
1949 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01001950 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001951 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
1952
1953 if (!wm->pipe_enabled)
1954 continue;
1955
1956 config->sprites_enabled |= wm->sprites_enabled;
1957 config->sprites_scaled |= wm->sprites_scaled;
1958 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001959 }
1960}
1961
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001962/* Compute new watermarks for the pipe */
1963static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02001964 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001965 struct intel_pipe_wm *pipe_wm)
1966{
1967 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001968 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001969 int level, max_level = ilk_wm_max_level(dev);
1970 /* LP0 watermark maximums depend on this pipe alone */
1971 struct intel_wm_config config = {
1972 .num_pipes_active = 1,
1973 .sprites_enabled = params->spr.enabled,
1974 .sprites_scaled = params->spr.scaled,
1975 };
Imre Deak820c1982013-12-17 14:46:36 +02001976 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001977
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001978 pipe_wm->pipe_enabled = params->active;
1979 pipe_wm->sprites_enabled = params->spr.enabled;
1980 pipe_wm->sprites_scaled = params->spr.scaled;
1981
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02001982 /* ILK/SNB: LP2+ watermarks only w/o sprites */
1983 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
1984 max_level = 1;
1985
1986 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
1987 if (params->spr.scaled)
1988 max_level = 0;
1989
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001990 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001991
Ville Syrjäläa42a5712014-01-07 16:14:08 +02001992 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02001993 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001994
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001995 /* LP0 watermarks always use 1/2 DDB partitioning */
1996 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
1997
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001998 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001999 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2000 return false;
2001
2002 ilk_compute_wm_reg_maximums(dev, 1, &max);
2003
2004 for (level = 1; level <= max_level; level++) {
2005 struct intel_wm_level wm = {};
2006
2007 ilk_compute_wm_level(dev_priv, level, params, &wm);
2008
2009 /*
2010 * Disable any watermark level that exceeds the
2011 * register maximums since such watermarks are
2012 * always invalid.
2013 */
2014 if (!ilk_validate_wm_level(level, &max, &wm))
2015 break;
2016
2017 pipe_wm->wm[level] = wm;
2018 }
2019
2020 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002021}
2022
2023/*
2024 * Merge the watermarks from all active pipes for a specific level.
2025 */
2026static void ilk_merge_wm_level(struct drm_device *dev,
2027 int level,
2028 struct intel_wm_level *ret_wm)
2029{
2030 const struct intel_crtc *intel_crtc;
2031
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002032 ret_wm->enable = true;
2033
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002034 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002035 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2036 const struct intel_wm_level *wm = &active->wm[level];
2037
2038 if (!active->pipe_enabled)
2039 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002040
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002041 /*
2042 * The watermark values may have been used in the past,
2043 * so we must maintain them in the registers for some
2044 * time even if the level is now disabled.
2045 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002046 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002047 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002048
2049 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2050 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2051 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2052 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2053 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002054}
2055
2056/*
2057 * Merge all low power watermarks for all active pipes.
2058 */
2059static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002060 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002061 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002062 struct intel_pipe_wm *merged)
2063{
2064 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002065 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002066
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002067 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2068 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2069 config->num_pipes_active > 1)
2070 return;
2071
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002072 /* ILK: FBC WM must be disabled always */
2073 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002074
2075 /* merge each WM1+ level */
2076 for (level = 1; level <= max_level; level++) {
2077 struct intel_wm_level *wm = &merged->wm[level];
2078
2079 ilk_merge_wm_level(dev, level, wm);
2080
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002081 if (level > last_enabled_level)
2082 wm->enable = false;
2083 else if (!ilk_validate_wm_level(level, max, wm))
2084 /* make sure all following levels get disabled */
2085 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002086
2087 /*
2088 * The spec says it is preferred to disable
2089 * FBC WMs instead of disabling a WM level.
2090 */
2091 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002092 if (wm->enable)
2093 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002094 wm->fbc_val = 0;
2095 }
2096 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002097
2098 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2099 /*
2100 * FIXME this is racy. FBC might get enabled later.
2101 * What we should check here is whether FBC can be
2102 * enabled sometime later.
2103 */
2104 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2105 for (level = 2; level <= max_level; level++) {
2106 struct intel_wm_level *wm = &merged->wm[level];
2107
2108 wm->enable = false;
2109 }
2110 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002111}
2112
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002113static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2114{
2115 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2116 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2117}
2118
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002119/* The value we need to program into the WM_LPx latency field */
2120static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2121{
2122 struct drm_i915_private *dev_priv = dev->dev_private;
2123
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002124 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002125 return 2 * level;
2126 else
2127 return dev_priv->wm.pri_latency[level];
2128}
2129
Imre Deak820c1982013-12-17 14:46:36 +02002130static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002131 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002132 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002133 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002134{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002135 struct intel_crtc *intel_crtc;
2136 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002137
Ville Syrjälä0362c782013-10-09 19:17:57 +03002138 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002139 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002140
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002141 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002142 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002143 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002144
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002145 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002146
Ville Syrjälä0362c782013-10-09 19:17:57 +03002147 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002148
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002149 /*
2150 * Maintain the watermark values even if the level is
2151 * disabled. Doing otherwise could cause underruns.
2152 */
2153 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002154 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002155 (r->pri_val << WM1_LP_SR_SHIFT) |
2156 r->cur_val;
2157
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002158 if (r->enable)
2159 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2160
Ville Syrjälä416f4722013-11-02 21:07:46 -07002161 if (INTEL_INFO(dev)->gen >= 8)
2162 results->wm_lp[wm_lp - 1] |=
2163 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2164 else
2165 results->wm_lp[wm_lp - 1] |=
2166 r->fbc_val << WM1_LP_FBC_SHIFT;
2167
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002168 /*
2169 * Always set WM1S_LP_EN when spr_val != 0, even if the
2170 * level is disabled. Doing otherwise could cause underruns.
2171 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002172 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2173 WARN_ON(wm_lp != 1);
2174 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2175 } else
2176 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002177 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002178
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002179 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002180 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002181 enum pipe pipe = intel_crtc->pipe;
2182 const struct intel_wm_level *r =
2183 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002184
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002185 if (WARN_ON(!r->enable))
2186 continue;
2187
2188 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2189
2190 results->wm_pipe[pipe] =
2191 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2192 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2193 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002194 }
2195}
2196
Paulo Zanoni861f3382013-05-31 10:19:21 -03002197/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2198 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002199static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002200 struct intel_pipe_wm *r1,
2201 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002202{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002203 int level, max_level = ilk_wm_max_level(dev);
2204 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002205
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002206 for (level = 1; level <= max_level; level++) {
2207 if (r1->wm[level].enable)
2208 level1 = level;
2209 if (r2->wm[level].enable)
2210 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002211 }
2212
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002213 if (level1 == level2) {
2214 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002215 return r2;
2216 else
2217 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002218 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002219 return r1;
2220 } else {
2221 return r2;
2222 }
2223}
2224
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002225/* dirty bits used to track which watermarks need changes */
2226#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2227#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2228#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2229#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2230#define WM_DIRTY_FBC (1 << 24)
2231#define WM_DIRTY_DDB (1 << 25)
2232
Damien Lespiau055e3932014-08-18 13:49:10 +01002233static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002234 const struct ilk_wm_values *old,
2235 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002236{
2237 unsigned int dirty = 0;
2238 enum pipe pipe;
2239 int wm_lp;
2240
Damien Lespiau055e3932014-08-18 13:49:10 +01002241 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002242 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2243 dirty |= WM_DIRTY_LINETIME(pipe);
2244 /* Must disable LP1+ watermarks too */
2245 dirty |= WM_DIRTY_LP_ALL;
2246 }
2247
2248 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2249 dirty |= WM_DIRTY_PIPE(pipe);
2250 /* Must disable LP1+ watermarks too */
2251 dirty |= WM_DIRTY_LP_ALL;
2252 }
2253 }
2254
2255 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2256 dirty |= WM_DIRTY_FBC;
2257 /* Must disable LP1+ watermarks too */
2258 dirty |= WM_DIRTY_LP_ALL;
2259 }
2260
2261 if (old->partitioning != new->partitioning) {
2262 dirty |= WM_DIRTY_DDB;
2263 /* Must disable LP1+ watermarks too */
2264 dirty |= WM_DIRTY_LP_ALL;
2265 }
2266
2267 /* LP1+ watermarks already deemed dirty, no need to continue */
2268 if (dirty & WM_DIRTY_LP_ALL)
2269 return dirty;
2270
2271 /* Find the lowest numbered LP1+ watermark in need of an update... */
2272 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2273 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2274 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2275 break;
2276 }
2277
2278 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2279 for (; wm_lp <= 3; wm_lp++)
2280 dirty |= WM_DIRTY_LP(wm_lp);
2281
2282 return dirty;
2283}
2284
Ville Syrjälä8553c182013-12-05 15:51:39 +02002285static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2286 unsigned int dirty)
2287{
Imre Deak820c1982013-12-17 14:46:36 +02002288 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002289 bool changed = false;
2290
2291 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2292 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2293 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2294 changed = true;
2295 }
2296 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2297 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2298 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2299 changed = true;
2300 }
2301 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2302 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2303 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2304 changed = true;
2305 }
2306
2307 /*
2308 * Don't touch WM1S_LP_EN here.
2309 * Doing so could cause underruns.
2310 */
2311
2312 return changed;
2313}
2314
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002315/*
2316 * The spec says we shouldn't write when we don't need, because every write
2317 * causes WMs to be re-evaluated, expending some power.
2318 */
Imre Deak820c1982013-12-17 14:46:36 +02002319static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2320 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002321{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002322 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002323 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002324 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002325 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002326
Damien Lespiau055e3932014-08-18 13:49:10 +01002327 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002328 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002329 return;
2330
Ville Syrjälä8553c182013-12-05 15:51:39 +02002331 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002332
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002333 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002334 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002335 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002336 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002337 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002338 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2339
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002340 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002341 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002342 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002343 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002344 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002345 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2346
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002347 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002348 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002349 val = I915_READ(WM_MISC);
2350 if (results->partitioning == INTEL_DDB_PART_1_2)
2351 val &= ~WM_MISC_DATA_PARTITION_5_6;
2352 else
2353 val |= WM_MISC_DATA_PARTITION_5_6;
2354 I915_WRITE(WM_MISC, val);
2355 } else {
2356 val = I915_READ(DISP_ARB_CTL2);
2357 if (results->partitioning == INTEL_DDB_PART_1_2)
2358 val &= ~DISP_DATA_PARTITION_5_6;
2359 else
2360 val |= DISP_DATA_PARTITION_5_6;
2361 I915_WRITE(DISP_ARB_CTL2, val);
2362 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002363 }
2364
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002365 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002366 val = I915_READ(DISP_ARB_CTL);
2367 if (results->enable_fbc_wm)
2368 val &= ~DISP_FBC_WM_DIS;
2369 else
2370 val |= DISP_FBC_WM_DIS;
2371 I915_WRITE(DISP_ARB_CTL, val);
2372 }
2373
Imre Deak954911e2013-12-17 14:46:34 +02002374 if (dirty & WM_DIRTY_LP(1) &&
2375 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2376 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2377
2378 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002379 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2380 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2381 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2382 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2383 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002384
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002385 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002386 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002387 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002388 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002389 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002390 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002391
2392 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002393}
2394
Ville Syrjälä8553c182013-12-05 15:51:39 +02002395static bool ilk_disable_lp_wm(struct drm_device *dev)
2396{
2397 struct drm_i915_private *dev_priv = dev->dev_private;
2398
2399 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2400}
2401
Damien Lespiaub9cec072014-11-04 17:06:43 +00002402/*
2403 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2404 * different active planes.
2405 */
2406
2407#define SKL_DDB_SIZE 896 /* in blocks */
2408
2409static void
2410skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2411 struct drm_crtc *for_crtc,
2412 const struct intel_wm_config *config,
2413 const struct skl_pipe_wm_parameters *params,
2414 struct skl_ddb_entry *alloc /* out */)
2415{
2416 struct drm_crtc *crtc;
2417 unsigned int pipe_size, ddb_size;
2418 int nth_active_pipe;
2419
2420 if (!params->active) {
2421 alloc->start = 0;
2422 alloc->end = 0;
2423 return;
2424 }
2425
2426 ddb_size = SKL_DDB_SIZE;
2427
2428 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2429
2430 nth_active_pipe = 0;
2431 for_each_crtc(dev, crtc) {
2432 if (!intel_crtc_active(crtc))
2433 continue;
2434
2435 if (crtc == for_crtc)
2436 break;
2437
2438 nth_active_pipe++;
2439 }
2440
2441 pipe_size = ddb_size / config->num_pipes_active;
2442 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002443 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002444}
2445
2446static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2447{
2448 if (config->num_pipes_active == 1)
2449 return 32;
2450
2451 return 8;
2452}
2453
Damien Lespiaua269c582014-11-04 17:06:49 +00002454static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2455{
2456 entry->start = reg & 0x3ff;
2457 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002458 if (entry->end)
2459 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002460}
2461
Damien Lespiau08db6652014-11-04 17:06:52 +00002462void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2463 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002464{
Damien Lespiaua269c582014-11-04 17:06:49 +00002465 enum pipe pipe;
2466 int plane;
2467 u32 val;
2468
2469 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002470 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002471 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2472 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2473 val);
2474 }
2475
2476 val = I915_READ(CUR_BUF_CFG(pipe));
2477 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2478 }
2479}
2480
Damien Lespiaub9cec072014-11-04 17:06:43 +00002481static unsigned int
2482skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2483{
2484 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2485}
2486
2487/*
2488 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2489 * a 8192x4096@32bpp framebuffer:
2490 * 3 * 4096 * 8192 * 4 < 2^32
2491 */
2492static unsigned int
2493skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2494 const struct skl_pipe_wm_parameters *params)
2495{
2496 unsigned int total_data_rate = 0;
2497 int plane;
2498
2499 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2500 const struct intel_plane_wm_parameters *p;
2501
2502 p = &params->plane[plane];
2503 if (!p->enabled)
2504 continue;
2505
2506 total_data_rate += skl_plane_relative_data_rate(p);
2507 }
2508
2509 return total_data_rate;
2510}
2511
2512static void
2513skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2514 const struct intel_wm_config *config,
2515 const struct skl_pipe_wm_parameters *params,
2516 struct skl_ddb_allocation *ddb /* out */)
2517{
2518 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002519 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2521 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002522 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002523 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002524 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002525 unsigned int total_data_rate;
2526 int plane;
2527
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002528 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2529 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002530 if (alloc_size == 0) {
2531 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2532 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2533 return;
2534 }
2535
2536 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002537 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2538 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002539
2540 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002541 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002542
Damien Lespiau80958152015-02-09 13:35:10 +00002543 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002544 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002545 const struct intel_plane_wm_parameters *p;
2546
2547 p = &params->plane[plane];
2548 if (!p->enabled)
2549 continue;
2550
2551 minimum[plane] = 8;
2552 alloc_size -= minimum[plane];
2553 }
2554
Damien Lespiaub9cec072014-11-04 17:06:43 +00002555 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002556 * 2. Distribute the remaining space in proportion to the amount of
2557 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002558 *
2559 * FIXME: we may not allocate every single block here.
2560 */
2561 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2562
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002563 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002564 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2565 const struct intel_plane_wm_parameters *p;
2566 unsigned int data_rate;
2567 uint16_t plane_blocks;
2568
2569 p = &params->plane[plane];
2570 if (!p->enabled)
2571 continue;
2572
2573 data_rate = skl_plane_relative_data_rate(p);
2574
2575 /*
2576 * promote the expression to 64 bits to avoid overflowing, the
2577 * result is < available as data_rate / total_data_rate < 1
2578 */
Damien Lespiau80958152015-02-09 13:35:10 +00002579 plane_blocks = minimum[plane];
2580 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2581 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002582
2583 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002584 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002585
2586 start += plane_blocks;
2587 }
2588
2589}
2590
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002591static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002592{
2593 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002594 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002595}
2596
2597/*
2598 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2599 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2600 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2601 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2602*/
2603static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2604 uint32_t latency)
2605{
2606 uint32_t wm_intermediate_val, ret;
2607
2608 if (latency == 0)
2609 return UINT_MAX;
2610
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002611 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002612 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2613
2614 return ret;
2615}
2616
2617static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2618 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002619 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002620{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002621 uint32_t ret;
2622 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2623 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002624
2625 if (latency == 0)
2626 return UINT_MAX;
2627
2628 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002629
2630 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2631 tiling == I915_FORMAT_MOD_Yf_TILED) {
2632 plane_bytes_per_line *= 4;
2633 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2634 plane_blocks_per_line /= 4;
2635 } else {
2636 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2637 }
2638
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002639 wm_intermediate_val = latency * pixel_rate;
2640 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002641 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002642
2643 return ret;
2644}
2645
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002646static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2647 const struct intel_crtc *intel_crtc)
2648{
2649 struct drm_device *dev = intel_crtc->base.dev;
2650 struct drm_i915_private *dev_priv = dev->dev_private;
2651 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2652 enum pipe pipe = intel_crtc->pipe;
2653
2654 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2655 sizeof(new_ddb->plane[pipe])))
2656 return true;
2657
2658 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2659 sizeof(new_ddb->cursor[pipe])))
2660 return true;
2661
2662 return false;
2663}
2664
2665static void skl_compute_wm_global_parameters(struct drm_device *dev,
2666 struct intel_wm_config *config)
2667{
2668 struct drm_crtc *crtc;
2669 struct drm_plane *plane;
2670
2671 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2672 config->num_pipes_active += intel_crtc_active(crtc);
2673
2674 /* FIXME: I don't think we need those two global parameters on SKL */
2675 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2676 struct intel_plane *intel_plane = to_intel_plane(plane);
2677
2678 config->sprites_enabled |= intel_plane->wm.enabled;
2679 config->sprites_scaled |= intel_plane->wm.scaled;
2680 }
2681}
2682
2683static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2684 struct skl_pipe_wm_parameters *p)
2685{
2686 struct drm_device *dev = crtc->dev;
2687 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2688 enum pipe pipe = intel_crtc->pipe;
2689 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002690 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002691 int i = 1; /* Index for sprite planes start */
2692
2693 p->active = intel_crtc_active(crtc);
2694 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002695 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2696 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002697
2698 /*
2699 * For now, assume primary and cursor planes are always enabled.
2700 */
2701 p->plane[0].enabled = true;
2702 p->plane[0].bytes_per_pixel =
Matt Roper59bea882015-02-27 10:12:01 -08002703 crtc->primary->state->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002704 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2705 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002706 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2707 fb = crtc->primary->state->fb;
2708 /*
2709 * Framebuffer can be NULL on plane disable, but it does not
2710 * matter for watermarks if we assume no tiling in that case.
2711 */
2712 if (fb)
2713 p->plane[0].tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002714
2715 p->cursor.enabled = true;
2716 p->cursor.bytes_per_pixel = 4;
Matt Roper3dd512f2015-02-27 10:12:00 -08002717 p->cursor.horiz_pixels = intel_crtc->base.cursor->state->crtc_w ?
2718 intel_crtc->base.cursor->state->crtc_w : 64;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002719 }
2720
2721 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2722 struct intel_plane *intel_plane = to_intel_plane(plane);
2723
Sonika Jindala712f8e2014-12-09 10:59:15 +05302724 if (intel_plane->pipe == pipe &&
2725 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002726 p->plane[i++] = intel_plane->wm;
2727 }
2728}
2729
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002730static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2731 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002732 struct intel_plane_wm_parameters *p_params,
2733 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002734 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002735 uint16_t *out_blocks, /* out */
2736 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002737{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002738 uint32_t latency = dev_priv->wm.skl_latency[level];
2739 uint32_t method1, method2;
2740 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2741 uint32_t res_blocks, res_lines;
2742 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002743
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002744 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002745 return false;
2746
2747 method1 = skl_wm_method1(p->pixel_rate,
2748 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002749 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002750 method2 = skl_wm_method2(p->pixel_rate,
2751 p->pipe_htotal,
2752 p_params->horiz_pixels,
2753 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002754 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002755 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002756
2757 plane_bytes_per_line = p_params->horiz_pixels *
2758 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002759 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002760
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002761 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2762 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2763 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2764 selected_result = max(method2, y_tile_minimum);
2765 } else {
2766 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2767 selected_result = min(method1, method2);
2768 else
2769 selected_result = method1;
2770 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002771
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002772 res_blocks = selected_result + 1;
2773 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002774
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002775 if (level >= 1 && level <= 7) {
2776 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2777 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2778 res_lines += 4;
2779 else
2780 res_blocks++;
2781 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002782
2783 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002784 return false;
2785
2786 *out_blocks = res_blocks;
2787 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002788
2789 return true;
2790}
2791
2792static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2793 struct skl_ddb_allocation *ddb,
2794 struct skl_pipe_wm_parameters *p,
2795 enum pipe pipe,
2796 int level,
2797 int num_planes,
2798 struct skl_wm_level *result)
2799{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002800 uint16_t ddb_blocks;
2801 int i;
2802
2803 for (i = 0; i < num_planes; i++) {
2804 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2805
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002806 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2807 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002808 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002809 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002810 &result->plane_res_b[i],
2811 &result->plane_res_l[i]);
2812 }
2813
2814 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002815 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2816 ddb_blocks, level,
2817 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002818 &result->cursor_res_l);
2819}
2820
Damien Lespiau407b50f2014-11-04 17:06:57 +00002821static uint32_t
2822skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2823{
2824 if (!intel_crtc_active(crtc))
2825 return 0;
2826
2827 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2828
2829}
2830
2831static void skl_compute_transition_wm(struct drm_crtc *crtc,
2832 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002833 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002834{
Damien Lespiau9414f562014-11-04 17:06:58 +00002835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2836 int i;
2837
Damien Lespiau407b50f2014-11-04 17:06:57 +00002838 if (!params->active)
2839 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002840
2841 /* Until we know more, just disable transition WMs */
2842 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2843 trans_wm->plane_en[i] = false;
2844 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002845}
2846
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002847static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2848 struct skl_ddb_allocation *ddb,
2849 struct skl_pipe_wm_parameters *params,
2850 struct skl_pipe_wm *pipe_wm)
2851{
2852 struct drm_device *dev = crtc->dev;
2853 const struct drm_i915_private *dev_priv = dev->dev_private;
2854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2855 int level, max_level = ilk_wm_max_level(dev);
2856
2857 for (level = 0; level <= max_level; level++) {
2858 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2859 level, intel_num_planes(intel_crtc),
2860 &pipe_wm->wm[level]);
2861 }
2862 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2863
Damien Lespiau9414f562014-11-04 17:06:58 +00002864 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002865}
2866
2867static void skl_compute_wm_results(struct drm_device *dev,
2868 struct skl_pipe_wm_parameters *p,
2869 struct skl_pipe_wm *p_wm,
2870 struct skl_wm_values *r,
2871 struct intel_crtc *intel_crtc)
2872{
2873 int level, max_level = ilk_wm_max_level(dev);
2874 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002875 uint32_t temp;
2876 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002877
2878 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002879 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2880 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002881
2882 temp |= p_wm->wm[level].plane_res_l[i] <<
2883 PLANE_WM_LINES_SHIFT;
2884 temp |= p_wm->wm[level].plane_res_b[i];
2885 if (p_wm->wm[level].plane_en[i])
2886 temp |= PLANE_WM_EN;
2887
2888 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002889 }
2890
2891 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002892
2893 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2894 temp |= p_wm->wm[level].cursor_res_b;
2895
2896 if (p_wm->wm[level].cursor_en)
2897 temp |= PLANE_WM_EN;
2898
2899 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002900
2901 }
2902
Damien Lespiau9414f562014-11-04 17:06:58 +00002903 /* transition WMs */
2904 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2905 temp = 0;
2906 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2907 temp |= p_wm->trans_wm.plane_res_b[i];
2908 if (p_wm->trans_wm.plane_en[i])
2909 temp |= PLANE_WM_EN;
2910
2911 r->plane_trans[pipe][i] = temp;
2912 }
2913
2914 temp = 0;
2915 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2916 temp |= p_wm->trans_wm.cursor_res_b;
2917 if (p_wm->trans_wm.cursor_en)
2918 temp |= PLANE_WM_EN;
2919
2920 r->cursor_trans[pipe] = temp;
2921
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002922 r->wm_linetime[pipe] = p_wm->linetime;
2923}
2924
Damien Lespiau16160e32014-11-04 17:06:53 +00002925static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2926 const struct skl_ddb_entry *entry)
2927{
2928 if (entry->end)
2929 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2930 else
2931 I915_WRITE(reg, 0);
2932}
2933
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002934static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2935 const struct skl_wm_values *new)
2936{
2937 struct drm_device *dev = dev_priv->dev;
2938 struct intel_crtc *crtc;
2939
2940 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
2941 int i, level, max_level = ilk_wm_max_level(dev);
2942 enum pipe pipe = crtc->pipe;
2943
Damien Lespiau5d374d92014-11-04 17:07:00 +00002944 if (!new->dirty[pipe])
2945 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002946
Damien Lespiau5d374d92014-11-04 17:07:00 +00002947 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
2948
2949 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002950 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00002951 I915_WRITE(PLANE_WM(pipe, i, level),
2952 new->plane[pipe][i][level]);
2953 I915_WRITE(CUR_WM(pipe, level),
2954 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002955 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00002956 for (i = 0; i < intel_num_planes(crtc); i++)
2957 I915_WRITE(PLANE_WM_TRANS(pipe, i),
2958 new->plane_trans[pipe][i]);
2959 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
2960
2961 for (i = 0; i < intel_num_planes(crtc); i++)
2962 skl_ddb_entry_write(dev_priv,
2963 PLANE_BUF_CFG(pipe, i),
2964 &new->ddb.plane[pipe][i]);
2965
2966 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
2967 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002968 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002969}
2970
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002971/*
2972 * When setting up a new DDB allocation arrangement, we need to correctly
2973 * sequence the times at which the new allocations for the pipes are taken into
2974 * account or we'll have pipes fetching from space previously allocated to
2975 * another pipe.
2976 *
2977 * Roughly the sequence looks like:
2978 * 1. re-allocate the pipe(s) with the allocation being reduced and not
2979 * overlapping with a previous light-up pipe (another way to put it is:
2980 * pipes with their new allocation strickly included into their old ones).
2981 * 2. re-allocate the other pipes that get their allocation reduced
2982 * 3. allocate the pipes having their allocation increased
2983 *
2984 * Steps 1. and 2. are here to take care of the following case:
2985 * - Initially DDB looks like this:
2986 * | B | C |
2987 * - enable pipe A.
2988 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
2989 * allocation
2990 * | A | B | C |
2991 *
2992 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
2993 */
2994
Damien Lespiaud21b7952014-11-04 17:07:03 +00002995static void
2996skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002997{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002998 int plane;
2999
Damien Lespiaud21b7952014-11-04 17:07:03 +00003000 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3001
Damien Lespiaudd740782015-02-28 14:54:08 +00003002 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003003 I915_WRITE(PLANE_SURF(pipe, plane),
3004 I915_READ(PLANE_SURF(pipe, plane)));
3005 }
3006 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3007}
3008
3009static bool
3010skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3011 const struct skl_ddb_allocation *new,
3012 enum pipe pipe)
3013{
3014 uint16_t old_size, new_size;
3015
3016 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3017 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3018
3019 return old_size != new_size &&
3020 new->pipe[pipe].start >= old->pipe[pipe].start &&
3021 new->pipe[pipe].end <= old->pipe[pipe].end;
3022}
3023
3024static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3025 struct skl_wm_values *new_values)
3026{
3027 struct drm_device *dev = dev_priv->dev;
3028 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3029 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3030 struct intel_crtc *crtc;
3031 enum pipe pipe;
3032
3033 new_ddb = &new_values->ddb;
3034 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3035
3036 /*
3037 * First pass: flush the pipes with the new allocation contained into
3038 * the old space.
3039 *
3040 * We'll wait for the vblank on those pipes to ensure we can safely
3041 * re-allocate the freed space without this pipe fetching from it.
3042 */
3043 for_each_intel_crtc(dev, crtc) {
3044 if (!crtc->active)
3045 continue;
3046
3047 pipe = crtc->pipe;
3048
3049 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3050 continue;
3051
Damien Lespiaud21b7952014-11-04 17:07:03 +00003052 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003053 intel_wait_for_vblank(dev, pipe);
3054
3055 reallocated[pipe] = true;
3056 }
3057
3058
3059 /*
3060 * Second pass: flush the pipes that are having their allocation
3061 * reduced, but overlapping with a previous allocation.
3062 *
3063 * Here as well we need to wait for the vblank to make sure the freed
3064 * space is not used anymore.
3065 */
3066 for_each_intel_crtc(dev, crtc) {
3067 if (!crtc->active)
3068 continue;
3069
3070 pipe = crtc->pipe;
3071
3072 if (reallocated[pipe])
3073 continue;
3074
3075 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3076 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003077 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003078 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303079 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003080 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003081 }
3082
3083 /*
3084 * Third pass: flush the pipes that got more space allocated.
3085 *
3086 * We don't need to actively wait for the update here, next vblank
3087 * will just get more DDB space with the correct WM values.
3088 */
3089 for_each_intel_crtc(dev, crtc) {
3090 if (!crtc->active)
3091 continue;
3092
3093 pipe = crtc->pipe;
3094
3095 /*
3096 * At this point, only the pipes more space than before are
3097 * left to re-allocate.
3098 */
3099 if (reallocated[pipe])
3100 continue;
3101
Damien Lespiaud21b7952014-11-04 17:07:03 +00003102 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003103 }
3104}
3105
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003106static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3107 struct skl_pipe_wm_parameters *params,
3108 struct intel_wm_config *config,
3109 struct skl_ddb_allocation *ddb, /* out */
3110 struct skl_pipe_wm *pipe_wm /* out */)
3111{
3112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3113
3114 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003115 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003116 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3117
3118 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3119 return false;
3120
3121 intel_crtc->wm.skl_active = *pipe_wm;
3122 return true;
3123}
3124
3125static void skl_update_other_pipe_wm(struct drm_device *dev,
3126 struct drm_crtc *crtc,
3127 struct intel_wm_config *config,
3128 struct skl_wm_values *r)
3129{
3130 struct intel_crtc *intel_crtc;
3131 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3132
3133 /*
3134 * If the WM update hasn't changed the allocation for this_crtc (the
3135 * crtc we are currently computing the new WM values for), other
3136 * enabled crtcs will keep the same allocation and we don't need to
3137 * recompute anything for them.
3138 */
3139 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3140 return;
3141
3142 /*
3143 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3144 * other active pipes need new DDB allocation and WM values.
3145 */
3146 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3147 base.head) {
3148 struct skl_pipe_wm_parameters params = {};
3149 struct skl_pipe_wm pipe_wm = {};
3150 bool wm_changed;
3151
3152 if (this_crtc->pipe == intel_crtc->pipe)
3153 continue;
3154
3155 if (!intel_crtc->active)
3156 continue;
3157
3158 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3159 &params, config,
3160 &r->ddb, &pipe_wm);
3161
3162 /*
3163 * If we end up re-computing the other pipe WM values, it's
3164 * because it was really needed, so we expect the WM values to
3165 * be different.
3166 */
3167 WARN_ON(!wm_changed);
3168
3169 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3170 r->dirty[intel_crtc->pipe] = true;
3171 }
3172}
3173
3174static void skl_update_wm(struct drm_crtc *crtc)
3175{
3176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3177 struct drm_device *dev = crtc->dev;
3178 struct drm_i915_private *dev_priv = dev->dev_private;
3179 struct skl_pipe_wm_parameters params = {};
3180 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3181 struct skl_pipe_wm pipe_wm = {};
3182 struct intel_wm_config config = {};
3183
3184 memset(results, 0, sizeof(*results));
3185
3186 skl_compute_wm_global_parameters(dev, &config);
3187
3188 if (!skl_update_pipe_wm(crtc, &params, &config,
3189 &results->ddb, &pipe_wm))
3190 return;
3191
3192 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3193 results->dirty[intel_crtc->pipe] = true;
3194
3195 skl_update_other_pipe_wm(dev, crtc, &config, results);
3196 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003197 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003198
3199 /* store the new configuration */
3200 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003201}
3202
3203static void
3204skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3205 uint32_t sprite_width, uint32_t sprite_height,
3206 int pixel_size, bool enabled, bool scaled)
3207{
3208 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003209 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003210
3211 intel_plane->wm.enabled = enabled;
3212 intel_plane->wm.scaled = scaled;
3213 intel_plane->wm.horiz_pixels = sprite_width;
3214 intel_plane->wm.vert_pixels = sprite_height;
3215 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003216 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3217 /*
3218 * Framebuffer can be NULL on plane disable, but it does not
3219 * matter for watermarks if we assume no tiling in that case.
3220 */
3221 if (fb)
3222 intel_plane->wm.tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003223
3224 skl_update_wm(crtc);
3225}
3226
Imre Deak820c1982013-12-17 14:46:36 +02003227static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003228{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003230 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003231 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003232 struct ilk_wm_maximums max;
3233 struct ilk_pipe_wm_parameters params = {};
3234 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003235 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003236 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003237 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003238 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003239
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003240 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003241
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003242 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3243
3244 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3245 return;
3246
3247 intel_crtc->wm.active = pipe_wm;
3248
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003249 ilk_compute_wm_config(dev, &config);
3250
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003251 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003252 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003253
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003254 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003255 if (INTEL_INFO(dev)->gen >= 7 &&
3256 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003257 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003258 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003259
Imre Deak820c1982013-12-17 14:46:36 +02003260 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003261 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003262 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003263 }
3264
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003265 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003266 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003267
Imre Deak820c1982013-12-17 14:46:36 +02003268 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003269
Imre Deak820c1982013-12-17 14:46:36 +02003270 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003271}
3272
Damien Lespiaued57cb82014-07-15 09:21:24 +02003273static void
3274ilk_update_sprite_wm(struct drm_plane *plane,
3275 struct drm_crtc *crtc,
3276 uint32_t sprite_width, uint32_t sprite_height,
3277 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003278{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003279 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003280 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003281
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003282 intel_plane->wm.enabled = enabled;
3283 intel_plane->wm.scaled = scaled;
3284 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003285 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003286 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003287
Ville Syrjälä8553c182013-12-05 15:51:39 +02003288 /*
3289 * IVB workaround: must disable low power watermarks for at least
3290 * one frame before enabling scaling. LP watermarks can be re-enabled
3291 * when scaling is disabled.
3292 *
3293 * WaCxSRDisabledForSpriteScaling:ivb
3294 */
3295 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3296 intel_wait_for_vblank(dev, intel_plane->pipe);
3297
Imre Deak820c1982013-12-17 14:46:36 +02003298 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003299}
3300
Pradeep Bhat30789992014-11-04 17:06:45 +00003301static void skl_pipe_wm_active_state(uint32_t val,
3302 struct skl_pipe_wm *active,
3303 bool is_transwm,
3304 bool is_cursor,
3305 int i,
3306 int level)
3307{
3308 bool is_enabled = (val & PLANE_WM_EN) != 0;
3309
3310 if (!is_transwm) {
3311 if (!is_cursor) {
3312 active->wm[level].plane_en[i] = is_enabled;
3313 active->wm[level].plane_res_b[i] =
3314 val & PLANE_WM_BLOCKS_MASK;
3315 active->wm[level].plane_res_l[i] =
3316 (val >> PLANE_WM_LINES_SHIFT) &
3317 PLANE_WM_LINES_MASK;
3318 } else {
3319 active->wm[level].cursor_en = is_enabled;
3320 active->wm[level].cursor_res_b =
3321 val & PLANE_WM_BLOCKS_MASK;
3322 active->wm[level].cursor_res_l =
3323 (val >> PLANE_WM_LINES_SHIFT) &
3324 PLANE_WM_LINES_MASK;
3325 }
3326 } else {
3327 if (!is_cursor) {
3328 active->trans_wm.plane_en[i] = is_enabled;
3329 active->trans_wm.plane_res_b[i] =
3330 val & PLANE_WM_BLOCKS_MASK;
3331 active->trans_wm.plane_res_l[i] =
3332 (val >> PLANE_WM_LINES_SHIFT) &
3333 PLANE_WM_LINES_MASK;
3334 } else {
3335 active->trans_wm.cursor_en = is_enabled;
3336 active->trans_wm.cursor_res_b =
3337 val & PLANE_WM_BLOCKS_MASK;
3338 active->trans_wm.cursor_res_l =
3339 (val >> PLANE_WM_LINES_SHIFT) &
3340 PLANE_WM_LINES_MASK;
3341 }
3342 }
3343}
3344
3345static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3346{
3347 struct drm_device *dev = crtc->dev;
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3351 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3352 enum pipe pipe = intel_crtc->pipe;
3353 int level, i, max_level;
3354 uint32_t temp;
3355
3356 max_level = ilk_wm_max_level(dev);
3357
3358 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3359
3360 for (level = 0; level <= max_level; level++) {
3361 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3362 hw->plane[pipe][i][level] =
3363 I915_READ(PLANE_WM(pipe, i, level));
3364 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3365 }
3366
3367 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3368 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3369 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3370
3371 if (!intel_crtc_active(crtc))
3372 return;
3373
3374 hw->dirty[pipe] = true;
3375
3376 active->linetime = hw->wm_linetime[pipe];
3377
3378 for (level = 0; level <= max_level; level++) {
3379 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3380 temp = hw->plane[pipe][i][level];
3381 skl_pipe_wm_active_state(temp, active, false,
3382 false, i, level);
3383 }
3384 temp = hw->cursor[pipe][level];
3385 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3386 }
3387
3388 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3389 temp = hw->plane_trans[pipe][i];
3390 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3391 }
3392
3393 temp = hw->cursor_trans[pipe];
3394 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3395}
3396
3397void skl_wm_get_hw_state(struct drm_device *dev)
3398{
Damien Lespiaua269c582014-11-04 17:06:49 +00003399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003401 struct drm_crtc *crtc;
3402
Damien Lespiaua269c582014-11-04 17:06:49 +00003403 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003404 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3405 skl_pipe_wm_get_hw_state(crtc);
3406}
3407
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003408static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3409{
3410 struct drm_device *dev = crtc->dev;
3411 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003412 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3414 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3415 enum pipe pipe = intel_crtc->pipe;
3416 static const unsigned int wm0_pipe_reg[] = {
3417 [PIPE_A] = WM0_PIPEA_ILK,
3418 [PIPE_B] = WM0_PIPEB_ILK,
3419 [PIPE_C] = WM0_PIPEC_IVB,
3420 };
3421
3422 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003423 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003424 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003425
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003426 active->pipe_enabled = intel_crtc_active(crtc);
3427
3428 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003429 u32 tmp = hw->wm_pipe[pipe];
3430
3431 /*
3432 * For active pipes LP0 watermark is marked as
3433 * enabled, and LP1+ watermaks as disabled since
3434 * we can't really reverse compute them in case
3435 * multiple pipes are active.
3436 */
3437 active->wm[0].enable = true;
3438 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3439 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3440 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3441 active->linetime = hw->wm_linetime[pipe];
3442 } else {
3443 int level, max_level = ilk_wm_max_level(dev);
3444
3445 /*
3446 * For inactive pipes, all watermark levels
3447 * should be marked as enabled but zeroed,
3448 * which is what we'd compute them to.
3449 */
3450 for (level = 0; level <= max_level; level++)
3451 active->wm[level].enable = true;
3452 }
3453}
3454
3455void ilk_wm_get_hw_state(struct drm_device *dev)
3456{
3457 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003458 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003459 struct drm_crtc *crtc;
3460
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003461 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003462 ilk_pipe_wm_get_hw_state(crtc);
3463
3464 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3465 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3466 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3467
3468 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003469 if (INTEL_INFO(dev)->gen >= 7) {
3470 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3471 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3472 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003473
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003474 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003475 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3476 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3477 else if (IS_IVYBRIDGE(dev))
3478 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3479 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003480
3481 hw->enable_fbc_wm =
3482 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3483}
3484
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003485/**
3486 * intel_update_watermarks - update FIFO watermark values based on current modes
3487 *
3488 * Calculate watermark values for the various WM regs based on current mode
3489 * and plane configuration.
3490 *
3491 * There are several cases to deal with here:
3492 * - normal (i.e. non-self-refresh)
3493 * - self-refresh (SR) mode
3494 * - lines are large relative to FIFO size (buffer can hold up to 2)
3495 * - lines are small relative to FIFO size (buffer can hold more than 2
3496 * lines), so need to account for TLB latency
3497 *
3498 * The normal calculation is:
3499 * watermark = dotclock * bytes per pixel * latency
3500 * where latency is platform & configuration dependent (we assume pessimal
3501 * values here).
3502 *
3503 * The SR calculation is:
3504 * watermark = (trunc(latency/line time)+1) * surface width *
3505 * bytes per pixel
3506 * where
3507 * line time = htotal / dotclock
3508 * surface width = hdisplay for normal plane and 64 for cursor
3509 * and latency is assumed to be high, as above.
3510 *
3511 * The final value programmed to the register should always be rounded up,
3512 * and include an extra 2 entries to account for clock crossings.
3513 *
3514 * We don't use the sprite, so we can ignore that. And on Crestline we have
3515 * to set the non-SR watermarks to 8.
3516 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003517void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003518{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003519 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003520
3521 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003522 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003523}
3524
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003525void intel_update_sprite_watermarks(struct drm_plane *plane,
3526 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003527 uint32_t sprite_width,
3528 uint32_t sprite_height,
3529 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003530 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003531{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003532 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003533
3534 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003535 dev_priv->display.update_sprite_wm(plane, crtc,
3536 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003537 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003538}
3539
Daniel Vetter92703882012-08-09 16:46:01 +02003540/**
3541 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003542 */
3543DEFINE_SPINLOCK(mchdev_lock);
3544
3545/* Global for IPS driver to get at the current i915 device. Protected by
3546 * mchdev_lock. */
3547static struct drm_i915_private *i915_mch_dev;
3548
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003549bool ironlake_set_drps(struct drm_device *dev, u8 val)
3550{
3551 struct drm_i915_private *dev_priv = dev->dev_private;
3552 u16 rgvswctl;
3553
Daniel Vetter92703882012-08-09 16:46:01 +02003554 assert_spin_locked(&mchdev_lock);
3555
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003556 rgvswctl = I915_READ16(MEMSWCTL);
3557 if (rgvswctl & MEMCTL_CMD_STS) {
3558 DRM_DEBUG("gpu busy, RCS change rejected\n");
3559 return false; /* still busy with another command */
3560 }
3561
3562 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3563 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3564 I915_WRITE16(MEMSWCTL, rgvswctl);
3565 POSTING_READ16(MEMSWCTL);
3566
3567 rgvswctl |= MEMCTL_CMD_STS;
3568 I915_WRITE16(MEMSWCTL, rgvswctl);
3569
3570 return true;
3571}
3572
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003573static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003574{
3575 struct drm_i915_private *dev_priv = dev->dev_private;
3576 u32 rgvmodectl = I915_READ(MEMMODECTL);
3577 u8 fmax, fmin, fstart, vstart;
3578
Daniel Vetter92703882012-08-09 16:46:01 +02003579 spin_lock_irq(&mchdev_lock);
3580
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003581 /* Enable temp reporting */
3582 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3583 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3584
3585 /* 100ms RC evaluation intervals */
3586 I915_WRITE(RCUPEI, 100000);
3587 I915_WRITE(RCDNEI, 100000);
3588
3589 /* Set max/min thresholds to 90ms and 80ms respectively */
3590 I915_WRITE(RCBMAXAVG, 90000);
3591 I915_WRITE(RCBMINAVG, 80000);
3592
3593 I915_WRITE(MEMIHYST, 1);
3594
3595 /* Set up min, max, and cur for interrupt handling */
3596 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3597 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3598 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3599 MEMMODE_FSTART_SHIFT;
3600
3601 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3602 PXVFREQ_PX_SHIFT;
3603
Daniel Vetter20e4d402012-08-08 23:35:39 +02003604 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3605 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003606
Daniel Vetter20e4d402012-08-08 23:35:39 +02003607 dev_priv->ips.max_delay = fstart;
3608 dev_priv->ips.min_delay = fmin;
3609 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003610
3611 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3612 fmax, fmin, fstart);
3613
3614 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3615
3616 /*
3617 * Interrupts will be enabled in ironlake_irq_postinstall
3618 */
3619
3620 I915_WRITE(VIDSTART, vstart);
3621 POSTING_READ(VIDSTART);
3622
3623 rgvmodectl |= MEMMODE_SWMODE_EN;
3624 I915_WRITE(MEMMODECTL, rgvmodectl);
3625
Daniel Vetter92703882012-08-09 16:46:01 +02003626 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003627 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003628 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003629
3630 ironlake_set_drps(dev, fstart);
3631
Daniel Vetter20e4d402012-08-08 23:35:39 +02003632 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003633 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003634 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3635 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003636 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003637
3638 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003639}
3640
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003641static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003642{
3643 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003644 u16 rgvswctl;
3645
3646 spin_lock_irq(&mchdev_lock);
3647
3648 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003649
3650 /* Ack interrupts, disable EFC interrupt */
3651 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3652 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3653 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3654 I915_WRITE(DEIIR, DE_PCU_EVENT);
3655 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3656
3657 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003658 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003659 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003660 rgvswctl |= MEMCTL_CMD_STS;
3661 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003662 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003663
Daniel Vetter92703882012-08-09 16:46:01 +02003664 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003665}
3666
Daniel Vetteracbe9472012-07-26 11:50:05 +02003667/* There's a funny hw issue where the hw returns all 0 when reading from
3668 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3669 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3670 * all limits and the gpu stuck at whatever frequency it is at atm).
3671 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003672static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003673{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003674 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003675
Daniel Vetter20b46e52012-07-26 11:16:14 +02003676 /* Only set the down limit when we've reached the lowest level to avoid
3677 * getting more interrupts, otherwise leave this clear. This prevents a
3678 * race in the hw when coming out of rc6: There's a tiny window where
3679 * the hw runs at the minimal clock before selecting the desired
3680 * frequency, if the down threshold expires in that window we will not
3681 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003682 limits = dev_priv->rps.max_freq_softlimit << 24;
3683 if (val <= dev_priv->rps.min_freq_softlimit)
3684 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003685
3686 return limits;
3687}
3688
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003689static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3690{
3691 int new_power;
3692
3693 new_power = dev_priv->rps.power;
3694 switch (dev_priv->rps.power) {
3695 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003696 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003697 new_power = BETWEEN;
3698 break;
3699
3700 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003701 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003702 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003703 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003704 new_power = HIGH_POWER;
3705 break;
3706
3707 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003708 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 +01003709 new_power = BETWEEN;
3710 break;
3711 }
3712 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003713 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003714 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003715 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003716 new_power = HIGH_POWER;
3717 if (new_power == dev_priv->rps.power)
3718 return;
3719
3720 /* Note the units here are not exactly 1us, but 1280ns. */
3721 switch (new_power) {
3722 case LOW_POWER:
3723 /* Upclock if more than 95% busy over 16ms */
3724 I915_WRITE(GEN6_RP_UP_EI, 12500);
3725 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3726
3727 /* Downclock if less than 85% busy over 32ms */
3728 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3729 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3730
3731 I915_WRITE(GEN6_RP_CONTROL,
3732 GEN6_RP_MEDIA_TURBO |
3733 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3734 GEN6_RP_MEDIA_IS_GFX |
3735 GEN6_RP_ENABLE |
3736 GEN6_RP_UP_BUSY_AVG |
3737 GEN6_RP_DOWN_IDLE_AVG);
3738 break;
3739
3740 case BETWEEN:
3741 /* Upclock if more than 90% busy over 13ms */
3742 I915_WRITE(GEN6_RP_UP_EI, 10250);
3743 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3744
3745 /* Downclock if less than 75% busy over 32ms */
3746 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3747 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3748
3749 I915_WRITE(GEN6_RP_CONTROL,
3750 GEN6_RP_MEDIA_TURBO |
3751 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3752 GEN6_RP_MEDIA_IS_GFX |
3753 GEN6_RP_ENABLE |
3754 GEN6_RP_UP_BUSY_AVG |
3755 GEN6_RP_DOWN_IDLE_AVG);
3756 break;
3757
3758 case HIGH_POWER:
3759 /* Upclock if more than 85% busy over 10ms */
3760 I915_WRITE(GEN6_RP_UP_EI, 8000);
3761 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3762
3763 /* Downclock if less than 60% busy over 32ms */
3764 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3765 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
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
3777 dev_priv->rps.power = new_power;
3778 dev_priv->rps.last_adj = 0;
3779}
3780
Chris Wilson2876ce72014-03-28 08:03:34 +00003781static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3782{
3783 u32 mask = 0;
3784
3785 if (val > dev_priv->rps.min_freq_softlimit)
3786 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3787 if (val < dev_priv->rps.max_freq_softlimit)
3788 mask |= GEN6_PM_RP_UP_THRESHOLD;
3789
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003790 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3791 mask &= dev_priv->pm_rps_events;
3792
Imre Deak59d02a12014-12-19 19:33:26 +02003793 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003794}
3795
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003796/* gen6_set_rps is called to update the frequency request, but should also be
3797 * called when the range (min_delay and max_delay) is modified so that we can
3798 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003799static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003800{
3801 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003802
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003803 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003804 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3805 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003806
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003807 /* min/max delay may still have been modified so be sure to
3808 * write the limits value.
3809 */
3810 if (val != dev_priv->rps.cur_freq) {
3811 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003812
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003813 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003814 I915_WRITE(GEN6_RPNSWREQ,
3815 HSW_FREQUENCY(val));
3816 else
3817 I915_WRITE(GEN6_RPNSWREQ,
3818 GEN6_FREQUENCY(val) |
3819 GEN6_OFFSET(0) |
3820 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003821 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003822
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003823 /* Make sure we continue to get interrupts
3824 * until we hit the minimum or maximum frequencies.
3825 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003826 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003827 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003828
Ben Widawskyd5570a72012-09-07 19:43:41 -07003829 POSTING_READ(GEN6_RPNSWREQ);
3830
Ben Widawskyb39fb292014-03-19 18:31:11 -07003831 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003832 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003833}
3834
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003835static void valleyview_set_rps(struct drm_device *dev, u8 val)
3836{
3837 struct drm_i915_private *dev_priv = dev->dev_private;
3838
3839 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3840 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3841 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3842
3843 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3844 "Odd GPU freq value\n"))
3845 val &= ~1;
3846
3847 if (val != dev_priv->rps.cur_freq)
3848 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3849
3850 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3851
3852 dev_priv->rps.cur_freq = val;
3853 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3854}
3855
Deepak S76c3552f2014-01-30 23:08:16 +05303856/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3857 *
3858 * * If Gfx is Idle, then
3859 * 1. Mask Turbo interrupts
3860 * 2. Bring up Gfx clock
3861 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3862 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3863 * 5. Unmask Turbo interrupts
3864*/
3865static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3866{
Deepak S5549d252014-06-28 11:26:11 +05303867 struct drm_device *dev = dev_priv->dev;
3868
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003869 /* CHV and latest VLV don't need to force the gfx clock */
3870 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303871 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3872 return;
3873 }
3874
Deepak S76c3552f2014-01-30 23:08:16 +05303875 /*
3876 * When we are idle. Drop to min voltage state.
3877 */
3878
Ben Widawskyb39fb292014-03-19 18:31:11 -07003879 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303880 return;
3881
3882 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003883 I915_WRITE(GEN6_PMINTRMSK,
3884 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303885
Imre Deak650ad972014-04-18 16:35:02 +03003886 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303887
Ben Widawskyb39fb292014-03-19 18:31:11 -07003888 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303889
3890 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003891 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303892
3893 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003894 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303895 DRM_ERROR("timed out waiting for Punit\n");
3896
Imre Deak650ad972014-04-18 16:35:02 +03003897 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303898
Chris Wilson2876ce72014-03-28 08:03:34 +00003899 I915_WRITE(GEN6_PMINTRMSK,
3900 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303901}
3902
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003903void gen6_rps_idle(struct drm_i915_private *dev_priv)
3904{
Damien Lespiau691bb712013-12-12 14:36:36 +00003905 struct drm_device *dev = dev_priv->dev;
3906
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003907 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003908 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003909 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303910 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003911 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003912 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003913 dev_priv->rps.last_adj = 0;
3914 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003915 mutex_unlock(&dev_priv->rps.hw_lock);
3916}
3917
3918void gen6_rps_boost(struct drm_i915_private *dev_priv)
3919{
3920 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003921 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003922 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003923 dev_priv->rps.last_adj = 0;
3924 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003925 mutex_unlock(&dev_priv->rps.hw_lock);
3926}
3927
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003928void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003929{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003930 if (IS_VALLEYVIEW(dev))
3931 valleyview_set_rps(dev, val);
3932 else
3933 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003934}
3935
Zhe Wang20e49362014-11-04 17:07:05 +00003936static void gen9_disable_rps(struct drm_device *dev)
3937{
3938 struct drm_i915_private *dev_priv = dev->dev_private;
3939
3940 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00003941 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00003942}
3943
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003944static void gen6_disable_rps(struct drm_device *dev)
3945{
3946 struct drm_i915_private *dev_priv = dev->dev_private;
3947
3948 I915_WRITE(GEN6_RC_CONTROL, 0);
3949 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003950}
3951
Deepak S38807742014-05-23 21:00:15 +05303952static void cherryview_disable_rps(struct drm_device *dev)
3953{
3954 struct drm_i915_private *dev_priv = dev->dev_private;
3955
3956 I915_WRITE(GEN6_RC_CONTROL, 0);
3957}
3958
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003959static void valleyview_disable_rps(struct drm_device *dev)
3960{
3961 struct drm_i915_private *dev_priv = dev->dev_private;
3962
Deepak S98a2e5f2014-08-18 10:35:27 -07003963 /* we're doing forcewake before Disabling RC6,
3964 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02003965 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07003966
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003967 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003968
Mika Kuoppala59bad942015-01-16 11:34:40 +02003969 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003970}
3971
Ben Widawskydc39fff2013-10-18 12:32:07 -07003972static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3973{
Imre Deak91ca6892014-04-14 20:24:25 +03003974 if (IS_VALLEYVIEW(dev)) {
3975 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3976 mode = GEN6_RC_CTL_RC6_ENABLE;
3977 else
3978 mode = 0;
3979 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003980 if (HAS_RC6p(dev))
3981 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
3982 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3983 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3984 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3985
3986 else
3987 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
3988 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003989}
3990
Imre Deake6069ca2014-04-18 16:01:02 +03003991static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003992{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003993 /* No RC6 before Ironlake */
3994 if (INTEL_INFO(dev)->gen < 5)
3995 return 0;
3996
Imre Deake6069ca2014-04-18 16:01:02 +03003997 /* RC6 is only on Ironlake mobile not on desktop */
3998 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3999 return 0;
4000
Daniel Vetter456470e2012-08-08 23:35:40 +02004001 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004002 if (enable_rc6 >= 0) {
4003 int mask;
4004
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004005 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004006 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4007 INTEL_RC6pp_ENABLE;
4008 else
4009 mask = INTEL_RC6_ENABLE;
4010
4011 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004012 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4013 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004014
4015 return enable_rc6 & mask;
4016 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004017
Chris Wilson6567d742012-11-10 10:00:06 +00004018 /* Disable RC6 on Ironlake */
4019 if (INTEL_INFO(dev)->gen == 5)
4020 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004021
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004022 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004023 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004024
4025 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004026}
4027
Imre Deake6069ca2014-04-18 16:01:02 +03004028int intel_enable_rc6(const struct drm_device *dev)
4029{
4030 return i915.enable_rc6;
4031}
4032
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004033static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004034{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004035 struct drm_i915_private *dev_priv = dev->dev_private;
4036 uint32_t rp_state_cap;
4037 u32 ddcc_status = 0;
4038 int ret;
4039
4040 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004041 /* All of these values are in units of 50MHz */
4042 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004043 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004044 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004045 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004046 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004047 /* hw_max = RP0 until we check for overclocking */
4048 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4049
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004050 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4051 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4052 ret = sandybridge_pcode_read(dev_priv,
4053 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4054 &ddcc_status);
4055 if (0 == ret)
4056 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004057 clamp_t(u8,
4058 ((ddcc_status >> 8) & 0xff),
4059 dev_priv->rps.min_freq,
4060 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004061 }
4062
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004063 /* Preserve min/max settings in case of re-init */
4064 if (dev_priv->rps.max_freq_softlimit == 0)
4065 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4066
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004067 if (dev_priv->rps.min_freq_softlimit == 0) {
4068 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4069 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004070 /* max(RPe, 450 MHz) */
4071 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004072 else
4073 dev_priv->rps.min_freq_softlimit =
4074 dev_priv->rps.min_freq;
4075 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004076}
4077
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004078/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004079static void gen9_enable_rps(struct drm_device *dev)
4080{
4081 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004082
4083 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4084
Damien Lespiauba1c5542015-01-16 18:07:26 +00004085 gen6_init_rps_frequencies(dev);
4086
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004087 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4088 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4089
4090 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4091 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4092 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4093 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4094 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4095 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4096 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4097 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4098 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4099 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4100 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4101 GEN6_RP_DOWN_IDLE_AVG);
4102
4103 gen6_enable_rps_interrupts(dev);
4104
4105 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4106}
4107
4108static void gen9_enable_rc6(struct drm_device *dev)
4109{
4110 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004111 struct intel_engine_cs *ring;
4112 uint32_t rc6_mask = 0;
4113 int unused;
4114
4115 /* 1a: Software RC state - RC0 */
4116 I915_WRITE(GEN6_RC_STATE, 0);
4117
4118 /* 1b: Get forcewake during program sequence. Although the driver
4119 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004120 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004121
4122 /* 2a: Disable RC states. */
4123 I915_WRITE(GEN6_RC_CONTROL, 0);
4124
4125 /* 2b: Program RC6 thresholds.*/
4126 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4127 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4128 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4129 for_each_ring(ring, dev_priv, unused)
4130 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4131 I915_WRITE(GEN6_RC_SLEEP, 0);
4132 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4133
Zhe Wang38c23522015-01-20 12:23:04 +00004134 /* 2c: Program Coarse Power Gating Policies. */
4135 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4136 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4137
Zhe Wang20e49362014-11-04 17:07:05 +00004138 /* 3a: Enable RC6 */
4139 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4140 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4141 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4142 "on" : "off");
4143 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4144 GEN6_RC_CTL_EI_MODE(1) |
4145 rc6_mask);
4146
Zhe Wang38c23522015-01-20 12:23:04 +00004147 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4148 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4149
Mika Kuoppala59bad942015-01-16 11:34:40 +02004150 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004151
4152}
4153
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004154static void gen8_enable_rps(struct drm_device *dev)
4155{
4156 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004157 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004158 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004159 int unused;
4160
4161 /* 1a: Software RC state - RC0 */
4162 I915_WRITE(GEN6_RC_STATE, 0);
4163
4164 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4165 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004166 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004167
4168 /* 2a: Disable RC states. */
4169 I915_WRITE(GEN6_RC_CONTROL, 0);
4170
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004171 /* Initialize rps frequencies */
4172 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004173
4174 /* 2b: Program RC6 thresholds.*/
4175 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4176 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4177 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4178 for_each_ring(ring, dev_priv, unused)
4179 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4180 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004181 if (IS_BROADWELL(dev))
4182 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4183 else
4184 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004185
4186 /* 3: Enable RC6 */
4187 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4188 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004189 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004190 if (IS_BROADWELL(dev))
4191 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4192 GEN7_RC_CTL_TO_MODE |
4193 rc6_mask);
4194 else
4195 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4196 GEN6_RC_CTL_EI_MODE(1) |
4197 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004198
4199 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004200 I915_WRITE(GEN6_RPNSWREQ,
4201 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4202 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4203 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004204 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4205 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004206
Daniel Vetter7526ed72014-09-29 15:07:19 +02004207 /* Docs recommend 900MHz, and 300 MHz respectively */
4208 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4209 dev_priv->rps.max_freq_softlimit << 24 |
4210 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004211
Daniel Vetter7526ed72014-09-29 15:07:19 +02004212 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4213 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4214 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4215 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004216
Daniel Vetter7526ed72014-09-29 15:07:19 +02004217 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004218
4219 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004220 I915_WRITE(GEN6_RP_CONTROL,
4221 GEN6_RP_MEDIA_TURBO |
4222 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4223 GEN6_RP_MEDIA_IS_GFX |
4224 GEN6_RP_ENABLE |
4225 GEN6_RP_UP_BUSY_AVG |
4226 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004227
Daniel Vetter7526ed72014-09-29 15:07:19 +02004228 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004229
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004230 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4231 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004232
Mika Kuoppala59bad942015-01-16 11:34:40 +02004233 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004234}
4235
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004236static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004237{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004238 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004239 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004240 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004241 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004242 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004243 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004244
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004245 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004246
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004247 /* Here begins a magic sequence of register writes to enable
4248 * auto-downclocking.
4249 *
4250 * Perhaps there might be some value in exposing these to
4251 * userspace...
4252 */
4253 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004254
4255 /* Clear the DBG now so we don't confuse earlier errors */
4256 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4257 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4258 I915_WRITE(GTFIFODBG, gtfifodbg);
4259 }
4260
Mika Kuoppala59bad942015-01-16 11:34:40 +02004261 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004262
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004263 /* Initialize rps frequencies */
4264 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004265
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004266 /* disable the counters and set deterministic thresholds */
4267 I915_WRITE(GEN6_RC_CONTROL, 0);
4268
4269 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4270 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4271 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4272 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4273 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4274
Chris Wilsonb4519512012-05-11 14:29:30 +01004275 for_each_ring(ring, dev_priv, i)
4276 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004277
4278 I915_WRITE(GEN6_RC_SLEEP, 0);
4279 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004280 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004281 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4282 else
4283 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004284 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004285 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4286
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004287 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004288 rc6_mode = intel_enable_rc6(dev_priv->dev);
4289 if (rc6_mode & INTEL_RC6_ENABLE)
4290 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4291
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004292 /* We don't use those on Haswell */
4293 if (!IS_HASWELL(dev)) {
4294 if (rc6_mode & INTEL_RC6p_ENABLE)
4295 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004296
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004297 if (rc6_mode & INTEL_RC6pp_ENABLE)
4298 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4299 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004300
Ben Widawskydc39fff2013-10-18 12:32:07 -07004301 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004302
4303 I915_WRITE(GEN6_RC_CONTROL,
4304 rc6_mask |
4305 GEN6_RC_CTL_EI_MODE(1) |
4306 GEN6_RC_CTL_HW_ENABLE);
4307
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004308 /* Power down if completely idle for over 50ms */
4309 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004310 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004311
Ben Widawsky42c05262012-09-26 10:34:00 -07004312 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004313 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004314 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004315
4316 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4317 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4318 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004319 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004320 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004321 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004322 }
4323
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004324 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004325 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004326
Ben Widawsky31643d52012-09-26 10:34:01 -07004327 rc6vids = 0;
4328 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4329 if (IS_GEN6(dev) && ret) {
4330 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4331 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4332 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4333 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4334 rc6vids &= 0xffff00;
4335 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4336 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4337 if (ret)
4338 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4339 }
4340
Mika Kuoppala59bad942015-01-16 11:34:40 +02004341 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004342}
4343
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004344static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004345{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004346 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004347 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004348 unsigned int gpu_freq;
4349 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004350 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004351 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004352
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004353 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004354
Ben Widawskyeda79642013-10-07 17:15:48 -03004355 policy = cpufreq_cpu_get(0);
4356 if (policy) {
4357 max_ia_freq = policy->cpuinfo.max_freq;
4358 cpufreq_cpu_put(policy);
4359 } else {
4360 /*
4361 * Default to measured freq if none found, PCU will ensure we
4362 * don't go over
4363 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004364 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004365 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004366
4367 /* Convert from kHz to MHz */
4368 max_ia_freq /= 1000;
4369
Ben Widawsky153b4b952013-10-22 22:05:09 -07004370 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004371 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4372 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004373
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004374 /*
4375 * For each potential GPU frequency, load a ring frequency we'd like
4376 * to use for memory access. We do this by specifying the IA frequency
4377 * the PCU should use as a reference to determine the ring frequency.
4378 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004379 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004380 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004381 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004382 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004383
Ben Widawsky46c764d2013-11-02 21:07:49 -07004384 if (INTEL_INFO(dev)->gen >= 8) {
4385 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4386 ring_freq = max(min_ring_freq, gpu_freq);
4387 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004388 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004389 ring_freq = max(min_ring_freq, ring_freq);
4390 /* leave ia_freq as the default, chosen by cpufreq */
4391 } else {
4392 /* On older processors, there is no separate ring
4393 * clock domain, so in order to boost the bandwidth
4394 * of the ring, we need to upclock the CPU (ia_freq).
4395 *
4396 * For GPU frequencies less than 750MHz,
4397 * just use the lowest ring freq.
4398 */
4399 if (gpu_freq < min_freq)
4400 ia_freq = 800;
4401 else
4402 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4403 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4404 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004405
Ben Widawsky42c05262012-09-26 10:34:00 -07004406 sandybridge_pcode_write(dev_priv,
4407 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004408 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4409 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4410 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004411 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004412}
4413
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004414void gen6_update_ring_freq(struct drm_device *dev)
4415{
4416 struct drm_i915_private *dev_priv = dev->dev_private;
4417
4418 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4419 return;
4420
4421 mutex_lock(&dev_priv->rps.hw_lock);
4422 __gen6_update_ring_freq(dev);
4423 mutex_unlock(&dev_priv->rps.hw_lock);
4424}
4425
Ville Syrjälä03af2042014-06-28 02:03:53 +03004426static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304427{
Deepak S095acd52015-01-17 11:05:59 +05304428 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304429 u32 val, rp0;
4430
Deepak S095acd52015-01-17 11:05:59 +05304431 if (dev->pdev->revision >= 0x20) {
4432 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304433
Deepak S095acd52015-01-17 11:05:59 +05304434 switch (INTEL_INFO(dev)->eu_total) {
4435 case 8:
4436 /* (2 * 4) config */
4437 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4438 break;
4439 case 12:
4440 /* (2 * 6) config */
4441 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4442 break;
4443 case 16:
4444 /* (2 * 8) config */
4445 default:
4446 /* Setting (2 * 8) Min RP0 for any other combination */
4447 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4448 break;
4449 }
4450 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4451 } else {
4452 /* For pre-production hardware */
4453 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4454 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4455 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4456 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304457 return rp0;
4458}
4459
4460static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4461{
4462 u32 val, rpe;
4463
4464 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4465 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4466
4467 return rpe;
4468}
4469
Deepak S7707df42014-07-12 18:46:14 +05304470static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4471{
Deepak S095acd52015-01-17 11:05:59 +05304472 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304473 u32 val, rp1;
4474
Deepak S095acd52015-01-17 11:05:59 +05304475 if (dev->pdev->revision >= 0x20) {
4476 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4477 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4478 } else {
4479 /* For pre-production hardware */
4480 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4481 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4482 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4483 }
Deepak S7707df42014-07-12 18:46:14 +05304484 return rp1;
4485}
4486
Ville Syrjälä03af2042014-06-28 02:03:53 +03004487static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304488{
Deepak S095acd52015-01-17 11:05:59 +05304489 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304490 u32 val, rpn;
4491
Deepak S095acd52015-01-17 11:05:59 +05304492 if (dev->pdev->revision >= 0x20) {
4493 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4494 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4495 FB_GFX_FREQ_FUSE_MASK);
4496 } else { /* For pre-production hardware */
4497 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4498 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4499 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4500 }
4501
Deepak S2b6b3a02014-05-27 15:59:30 +05304502 return rpn;
4503}
4504
Deepak Sf8f2b002014-07-10 13:16:21 +05304505static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4506{
4507 u32 val, rp1;
4508
4509 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4510
4511 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4512
4513 return rp1;
4514}
4515
Ville Syrjälä03af2042014-06-28 02:03:53 +03004516static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004517{
4518 u32 val, rp0;
4519
Jani Nikula64936252013-05-22 15:36:20 +03004520 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004521
4522 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4523 /* Clamp to max */
4524 rp0 = min_t(u32, rp0, 0xea);
4525
4526 return rp0;
4527}
4528
4529static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4530{
4531 u32 val, rpe;
4532
Jani Nikula64936252013-05-22 15:36:20 +03004533 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004534 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004535 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004536 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4537
4538 return rpe;
4539}
4540
Ville Syrjälä03af2042014-06-28 02:03:53 +03004541static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004542{
Jani Nikula64936252013-05-22 15:36:20 +03004543 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004544}
4545
Imre Deakae484342014-03-31 15:10:44 +03004546/* Check that the pctx buffer wasn't move under us. */
4547static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4548{
4549 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4550
4551 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4552 dev_priv->vlv_pctx->stolen->start);
4553}
4554
Deepak S38807742014-05-23 21:00:15 +05304555
4556/* Check that the pcbr address is not empty. */
4557static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4558{
4559 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4560
4561 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4562}
4563
4564static void cherryview_setup_pctx(struct drm_device *dev)
4565{
4566 struct drm_i915_private *dev_priv = dev->dev_private;
4567 unsigned long pctx_paddr, paddr;
4568 struct i915_gtt *gtt = &dev_priv->gtt;
4569 u32 pcbr;
4570 int pctx_size = 32*1024;
4571
4572 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4573
4574 pcbr = I915_READ(VLV_PCBR);
4575 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004576 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304577 paddr = (dev_priv->mm.stolen_base +
4578 (gtt->stolen_size - pctx_size));
4579
4580 pctx_paddr = (paddr & (~4095));
4581 I915_WRITE(VLV_PCBR, pctx_paddr);
4582 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004583
4584 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304585}
4586
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004587static void valleyview_setup_pctx(struct drm_device *dev)
4588{
4589 struct drm_i915_private *dev_priv = dev->dev_private;
4590 struct drm_i915_gem_object *pctx;
4591 unsigned long pctx_paddr;
4592 u32 pcbr;
4593 int pctx_size = 24*1024;
4594
Imre Deak17b0c1f2014-02-11 21:39:06 +02004595 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4596
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004597 pcbr = I915_READ(VLV_PCBR);
4598 if (pcbr) {
4599 /* BIOS set it up already, grab the pre-alloc'd space */
4600 int pcbr_offset;
4601
4602 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4603 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4604 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004605 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004606 pctx_size);
4607 goto out;
4608 }
4609
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004610 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4611
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004612 /*
4613 * From the Gunit register HAS:
4614 * The Gfx driver is expected to program this register and ensure
4615 * proper allocation within Gfx stolen memory. For example, this
4616 * register should be programmed such than the PCBR range does not
4617 * overlap with other ranges, such as the frame buffer, protected
4618 * memory, or any other relevant ranges.
4619 */
4620 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4621 if (!pctx) {
4622 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4623 return;
4624 }
4625
4626 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4627 I915_WRITE(VLV_PCBR, pctx_paddr);
4628
4629out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004630 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004631 dev_priv->vlv_pctx = pctx;
4632}
4633
Imre Deakae484342014-03-31 15:10:44 +03004634static void valleyview_cleanup_pctx(struct drm_device *dev)
4635{
4636 struct drm_i915_private *dev_priv = dev->dev_private;
4637
4638 if (WARN_ON(!dev_priv->vlv_pctx))
4639 return;
4640
4641 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4642 dev_priv->vlv_pctx = NULL;
4643}
4644
Imre Deak4e805192014-04-14 20:24:41 +03004645static void valleyview_init_gt_powersave(struct drm_device *dev)
4646{
4647 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004648 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004649
4650 valleyview_setup_pctx(dev);
4651
4652 mutex_lock(&dev_priv->rps.hw_lock);
4653
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004654 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4655 switch ((val >> 6) & 3) {
4656 case 0:
4657 case 1:
4658 dev_priv->mem_freq = 800;
4659 break;
4660 case 2:
4661 dev_priv->mem_freq = 1066;
4662 break;
4663 case 3:
4664 dev_priv->mem_freq = 1333;
4665 break;
4666 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004667 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004668
Imre Deak4e805192014-04-14 20:24:41 +03004669 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4670 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4671 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004672 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004673 dev_priv->rps.max_freq);
4674
4675 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4676 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004677 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004678 dev_priv->rps.efficient_freq);
4679
Deepak Sf8f2b002014-07-10 13:16:21 +05304680 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4681 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004682 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304683 dev_priv->rps.rp1_freq);
4684
Imre Deak4e805192014-04-14 20:24:41 +03004685 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4686 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004687 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004688 dev_priv->rps.min_freq);
4689
4690 /* Preserve min/max settings in case of re-init */
4691 if (dev_priv->rps.max_freq_softlimit == 0)
4692 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4693
4694 if (dev_priv->rps.min_freq_softlimit == 0)
4695 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4696
4697 mutex_unlock(&dev_priv->rps.hw_lock);
4698}
4699
Deepak S38807742014-05-23 21:00:15 +05304700static void cherryview_init_gt_powersave(struct drm_device *dev)
4701{
Deepak S2b6b3a02014-05-27 15:59:30 +05304702 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004703 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304704
Deepak S38807742014-05-23 21:00:15 +05304705 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304706
4707 mutex_lock(&dev_priv->rps.hw_lock);
4708
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004709 mutex_lock(&dev_priv->dpio_lock);
4710 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4711 mutex_unlock(&dev_priv->dpio_lock);
4712
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004713 switch ((val >> 2) & 0x7) {
4714 case 0:
4715 case 1:
4716 dev_priv->rps.cz_freq = 200;
4717 dev_priv->mem_freq = 1600;
4718 break;
4719 case 2:
4720 dev_priv->rps.cz_freq = 267;
4721 dev_priv->mem_freq = 1600;
4722 break;
4723 case 3:
4724 dev_priv->rps.cz_freq = 333;
4725 dev_priv->mem_freq = 2000;
4726 break;
4727 case 4:
4728 dev_priv->rps.cz_freq = 320;
4729 dev_priv->mem_freq = 1600;
4730 break;
4731 case 5:
4732 dev_priv->rps.cz_freq = 400;
4733 dev_priv->mem_freq = 1600;
4734 break;
4735 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004736 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004737
Deepak S2b6b3a02014-05-27 15:59:30 +05304738 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4739 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4740 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004741 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304742 dev_priv->rps.max_freq);
4743
4744 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4745 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004746 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304747 dev_priv->rps.efficient_freq);
4748
Deepak S7707df42014-07-12 18:46:14 +05304749 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4750 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004751 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304752 dev_priv->rps.rp1_freq);
4753
Deepak S2b6b3a02014-05-27 15:59:30 +05304754 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4755 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004756 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304757 dev_priv->rps.min_freq);
4758
Ville Syrjälä1c147622014-08-18 14:42:43 +03004759 WARN_ONCE((dev_priv->rps.max_freq |
4760 dev_priv->rps.efficient_freq |
4761 dev_priv->rps.rp1_freq |
4762 dev_priv->rps.min_freq) & 1,
4763 "Odd GPU freq values\n");
4764
Deepak S2b6b3a02014-05-27 15:59:30 +05304765 /* Preserve min/max settings in case of re-init */
4766 if (dev_priv->rps.max_freq_softlimit == 0)
4767 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4768
4769 if (dev_priv->rps.min_freq_softlimit == 0)
4770 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4771
4772 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304773}
4774
Imre Deak4e805192014-04-14 20:24:41 +03004775static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4776{
4777 valleyview_cleanup_pctx(dev);
4778}
4779
Deepak S38807742014-05-23 21:00:15 +05304780static void cherryview_enable_rps(struct drm_device *dev)
4781{
4782 struct drm_i915_private *dev_priv = dev->dev_private;
4783 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304784 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304785 int i;
4786
4787 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4788
4789 gtfifodbg = I915_READ(GTFIFODBG);
4790 if (gtfifodbg) {
4791 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4792 gtfifodbg);
4793 I915_WRITE(GTFIFODBG, gtfifodbg);
4794 }
4795
4796 cherryview_check_pctx(dev_priv);
4797
4798 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4799 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004800 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304801
Ville Syrjälä160614a2015-01-19 13:50:47 +02004802 /* Disable RC states. */
4803 I915_WRITE(GEN6_RC_CONTROL, 0);
4804
Deepak S38807742014-05-23 21:00:15 +05304805 /* 2a: Program RC6 thresholds.*/
4806 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4807 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4808 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4809
4810 for_each_ring(ring, dev_priv, i)
4811 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4812 I915_WRITE(GEN6_RC_SLEEP, 0);
4813
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004814 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4815 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304816
4817 /* allows RC6 residency counter to work */
4818 I915_WRITE(VLV_COUNTER_CONTROL,
4819 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4820 VLV_MEDIA_RC6_COUNT_EN |
4821 VLV_RENDER_RC6_COUNT_EN));
4822
4823 /* For now we assume BIOS is allocating and populating the PCBR */
4824 pcbr = I915_READ(VLV_PCBR);
4825
Deepak S38807742014-05-23 21:00:15 +05304826 /* 3: Enable RC6 */
4827 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4828 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004829 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304830
4831 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4832
Deepak S2b6b3a02014-05-27 15:59:30 +05304833 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004834 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304835 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4836 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4837 I915_WRITE(GEN6_RP_UP_EI, 66000);
4838 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4839
4840 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4841
4842 /* 5: Enable RPS */
4843 I915_WRITE(GEN6_RP_CONTROL,
4844 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004845 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304846 GEN6_RP_ENABLE |
4847 GEN6_RP_UP_BUSY_AVG |
4848 GEN6_RP_DOWN_IDLE_AVG);
4849
4850 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4851
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004852 /* RPS code assumes GPLL is used */
4853 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4854
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004855 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304856 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4857
4858 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4859 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004860 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304861 dev_priv->rps.cur_freq);
4862
4863 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004864 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304865 dev_priv->rps.efficient_freq);
4866
4867 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4868
Mika Kuoppala59bad942015-01-16 11:34:40 +02004869 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304870}
4871
Jesse Barnes0a073b82013-04-17 15:54:58 -07004872static void valleyview_enable_rps(struct drm_device *dev)
4873{
4874 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004875 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004876 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004877 int i;
4878
4879 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4880
Imre Deakae484342014-03-31 15:10:44 +03004881 valleyview_check_pctx(dev_priv);
4882
Jesse Barnes0a073b82013-04-17 15:54:58 -07004883 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004884 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4885 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004886 I915_WRITE(GTFIFODBG, gtfifodbg);
4887 }
4888
Deepak Sc8d9a592013-11-23 14:55:42 +05304889 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004890 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004891
Ville Syrjälä160614a2015-01-19 13:50:47 +02004892 /* Disable RC states. */
4893 I915_WRITE(GEN6_RC_CONTROL, 0);
4894
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004895 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004896 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4897 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4898 I915_WRITE(GEN6_RP_UP_EI, 66000);
4899 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4900
4901 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4902
4903 I915_WRITE(GEN6_RP_CONTROL,
4904 GEN6_RP_MEDIA_TURBO |
4905 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4906 GEN6_RP_MEDIA_IS_GFX |
4907 GEN6_RP_ENABLE |
4908 GEN6_RP_UP_BUSY_AVG |
4909 GEN6_RP_DOWN_IDLE_CONT);
4910
4911 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4912 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4913 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4914
4915 for_each_ring(ring, dev_priv, i)
4916 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4917
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08004918 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004919
4920 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004921 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004922 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4923 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004924 VLV_MEDIA_RC6_COUNT_EN |
4925 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004926
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004927 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004928 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004929
4930 intel_print_rc6_info(dev, rc6_mode);
4931
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004932 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004933
Jani Nikula64936252013-05-22 15:36:20 +03004934 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004935
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004936 /* RPS code assumes GPLL is used */
4937 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4938
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004939 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07004940 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4941
Ben Widawskyb39fb292014-03-19 18:31:11 -07004942 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004943 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004944 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004945 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004946
Ville Syrjälä73008b92013-06-25 19:21:01 +03004947 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004948 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004949 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004950
Ben Widawskyb39fb292014-03-19 18:31:11 -07004951 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004952
Mika Kuoppala59bad942015-01-16 11:34:40 +02004953 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004954}
4955
Eugeni Dodonovdde18882012-04-18 15:29:24 -03004956static unsigned long intel_pxfreq(u32 vidfreq)
4957{
4958 unsigned long freq;
4959 int div = (vidfreq & 0x3f0000) >> 16;
4960 int post = (vidfreq & 0x3000) >> 12;
4961 int pre = (vidfreq & 0x7);
4962
4963 if (!pre)
4964 return 0;
4965
4966 freq = ((div * 133333) / ((1<<post) * pre));
4967
4968 return freq;
4969}
4970
Daniel Vettereb48eb02012-04-26 23:28:12 +02004971static const struct cparams {
4972 u16 i;
4973 u16 t;
4974 u16 m;
4975 u16 c;
4976} cparams[] = {
4977 { 1, 1333, 301, 28664 },
4978 { 1, 1066, 294, 24460 },
4979 { 1, 800, 294, 25192 },
4980 { 0, 1333, 276, 27605 },
4981 { 0, 1066, 276, 27605 },
4982 { 0, 800, 231, 23784 },
4983};
4984
Chris Wilsonf531dcb22012-09-25 10:16:12 +01004985static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02004986{
4987 u64 total_count, diff, ret;
4988 u32 count1, count2, count3, m = 0, c = 0;
4989 unsigned long now = jiffies_to_msecs(jiffies), diff1;
4990 int i;
4991
Daniel Vetter02d71952012-08-09 16:44:54 +02004992 assert_spin_locked(&mchdev_lock);
4993
Daniel Vetter20e4d402012-08-08 23:35:39 +02004994 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02004995
4996 /* Prevent division-by-zero if we are asking too fast.
4997 * Also, we don't get interesting results if we are polling
4998 * faster than once in 10ms, so just return the saved value
4999 * in such cases.
5000 */
5001 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005002 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005003
5004 count1 = I915_READ(DMIEC);
5005 count2 = I915_READ(DDREC);
5006 count3 = I915_READ(CSIEC);
5007
5008 total_count = count1 + count2 + count3;
5009
5010 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005011 if (total_count < dev_priv->ips.last_count1) {
5012 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005013 diff += total_count;
5014 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005015 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005016 }
5017
5018 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005019 if (cparams[i].i == dev_priv->ips.c_m &&
5020 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005021 m = cparams[i].m;
5022 c = cparams[i].c;
5023 break;
5024 }
5025 }
5026
5027 diff = div_u64(diff, diff1);
5028 ret = ((m * diff) + c);
5029 ret = div_u64(ret, 10);
5030
Daniel Vetter20e4d402012-08-08 23:35:39 +02005031 dev_priv->ips.last_count1 = total_count;
5032 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005033
Daniel Vetter20e4d402012-08-08 23:35:39 +02005034 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005035
5036 return ret;
5037}
5038
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005039unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5040{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005041 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005042 unsigned long val;
5043
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005044 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005045 return 0;
5046
5047 spin_lock_irq(&mchdev_lock);
5048
5049 val = __i915_chipset_val(dev_priv);
5050
5051 spin_unlock_irq(&mchdev_lock);
5052
5053 return val;
5054}
5055
Daniel Vettereb48eb02012-04-26 23:28:12 +02005056unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5057{
5058 unsigned long m, x, b;
5059 u32 tsfs;
5060
5061 tsfs = I915_READ(TSFS);
5062
5063 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5064 x = I915_READ8(TR1);
5065
5066 b = tsfs & TSFS_INTR_MASK;
5067
5068 return ((m * x) / 127) - b;
5069}
5070
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005071static int _pxvid_to_vd(u8 pxvid)
5072{
5073 if (pxvid == 0)
5074 return 0;
5075
5076 if (pxvid >= 8 && pxvid < 31)
5077 pxvid = 31;
5078
5079 return (pxvid + 2) * 125;
5080}
5081
5082static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005083{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005084 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005085 const int vd = _pxvid_to_vd(pxvid);
5086 const int vm = vd - 1125;
5087
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005088 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005089 return vm > 0 ? vm : 0;
5090
5091 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005092}
5093
Daniel Vetter02d71952012-08-09 16:44:54 +02005094static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005095{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005096 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005097 u32 count;
5098
Daniel Vetter02d71952012-08-09 16:44:54 +02005099 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005100
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005101 now = ktime_get_raw_ns();
5102 diffms = now - dev_priv->ips.last_time2;
5103 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005104
5105 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005106 if (!diffms)
5107 return;
5108
5109 count = I915_READ(GFXEC);
5110
Daniel Vetter20e4d402012-08-08 23:35:39 +02005111 if (count < dev_priv->ips.last_count2) {
5112 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005113 diff += count;
5114 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005115 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005116 }
5117
Daniel Vetter20e4d402012-08-08 23:35:39 +02005118 dev_priv->ips.last_count2 = count;
5119 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005120
5121 /* More magic constants... */
5122 diff = diff * 1181;
5123 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005124 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005125}
5126
Daniel Vetter02d71952012-08-09 16:44:54 +02005127void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5128{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005129 struct drm_device *dev = dev_priv->dev;
5130
5131 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005132 return;
5133
Daniel Vetter92703882012-08-09 16:46:01 +02005134 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005135
5136 __i915_update_gfx_val(dev_priv);
5137
Daniel Vetter92703882012-08-09 16:46:01 +02005138 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005139}
5140
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005141static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005142{
5143 unsigned long t, corr, state1, corr2, state2;
5144 u32 pxvid, ext_v;
5145
Daniel Vetter02d71952012-08-09 16:44:54 +02005146 assert_spin_locked(&mchdev_lock);
5147
Ben Widawskyb39fb292014-03-19 18:31:11 -07005148 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005149 pxvid = (pxvid >> 24) & 0x7f;
5150 ext_v = pvid_to_extvid(dev_priv, pxvid);
5151
5152 state1 = ext_v;
5153
5154 t = i915_mch_val(dev_priv);
5155
5156 /* Revel in the empirically derived constants */
5157
5158 /* Correction factor in 1/100000 units */
5159 if (t > 80)
5160 corr = ((t * 2349) + 135940);
5161 else if (t >= 50)
5162 corr = ((t * 964) + 29317);
5163 else /* < 50 */
5164 corr = ((t * 301) + 1004);
5165
5166 corr = corr * ((150142 * state1) / 10000 - 78642);
5167 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005168 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005169
5170 state2 = (corr2 * state1) / 10000;
5171 state2 /= 100; /* convert to mW */
5172
Daniel Vetter02d71952012-08-09 16:44:54 +02005173 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005174
Daniel Vetter20e4d402012-08-08 23:35:39 +02005175 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005176}
5177
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005178unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5179{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005180 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005181 unsigned long val;
5182
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005183 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005184 return 0;
5185
5186 spin_lock_irq(&mchdev_lock);
5187
5188 val = __i915_gfx_val(dev_priv);
5189
5190 spin_unlock_irq(&mchdev_lock);
5191
5192 return val;
5193}
5194
Daniel Vettereb48eb02012-04-26 23:28:12 +02005195/**
5196 * i915_read_mch_val - return value for IPS use
5197 *
5198 * Calculate and return a value for the IPS driver to use when deciding whether
5199 * we have thermal and power headroom to increase CPU or GPU power budget.
5200 */
5201unsigned long i915_read_mch_val(void)
5202{
5203 struct drm_i915_private *dev_priv;
5204 unsigned long chipset_val, graphics_val, ret = 0;
5205
Daniel Vetter92703882012-08-09 16:46:01 +02005206 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005207 if (!i915_mch_dev)
5208 goto out_unlock;
5209 dev_priv = i915_mch_dev;
5210
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005211 chipset_val = __i915_chipset_val(dev_priv);
5212 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005213
5214 ret = chipset_val + graphics_val;
5215
5216out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005217 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005218
5219 return ret;
5220}
5221EXPORT_SYMBOL_GPL(i915_read_mch_val);
5222
5223/**
5224 * i915_gpu_raise - raise GPU frequency limit
5225 *
5226 * Raise the limit; IPS indicates we have thermal headroom.
5227 */
5228bool i915_gpu_raise(void)
5229{
5230 struct drm_i915_private *dev_priv;
5231 bool ret = true;
5232
Daniel Vetter92703882012-08-09 16:46:01 +02005233 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005234 if (!i915_mch_dev) {
5235 ret = false;
5236 goto out_unlock;
5237 }
5238 dev_priv = i915_mch_dev;
5239
Daniel Vetter20e4d402012-08-08 23:35:39 +02005240 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5241 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005242
5243out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005244 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005245
5246 return ret;
5247}
5248EXPORT_SYMBOL_GPL(i915_gpu_raise);
5249
5250/**
5251 * i915_gpu_lower - lower GPU frequency limit
5252 *
5253 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5254 * frequency maximum.
5255 */
5256bool i915_gpu_lower(void)
5257{
5258 struct drm_i915_private *dev_priv;
5259 bool ret = true;
5260
Daniel Vetter92703882012-08-09 16:46:01 +02005261 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005262 if (!i915_mch_dev) {
5263 ret = false;
5264 goto out_unlock;
5265 }
5266 dev_priv = i915_mch_dev;
5267
Daniel Vetter20e4d402012-08-08 23:35:39 +02005268 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5269 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005270
5271out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005272 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005273
5274 return ret;
5275}
5276EXPORT_SYMBOL_GPL(i915_gpu_lower);
5277
5278/**
5279 * i915_gpu_busy - indicate GPU business to IPS
5280 *
5281 * Tell the IPS driver whether or not the GPU is busy.
5282 */
5283bool i915_gpu_busy(void)
5284{
5285 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005286 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005287 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005288 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005289
Daniel Vetter92703882012-08-09 16:46:01 +02005290 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005291 if (!i915_mch_dev)
5292 goto out_unlock;
5293 dev_priv = i915_mch_dev;
5294
Chris Wilsonf047e392012-07-21 12:31:41 +01005295 for_each_ring(ring, dev_priv, i)
5296 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005297
5298out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005299 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005300
5301 return ret;
5302}
5303EXPORT_SYMBOL_GPL(i915_gpu_busy);
5304
5305/**
5306 * i915_gpu_turbo_disable - disable graphics turbo
5307 *
5308 * Disable graphics turbo by resetting the max frequency and setting the
5309 * current frequency to the default.
5310 */
5311bool i915_gpu_turbo_disable(void)
5312{
5313 struct drm_i915_private *dev_priv;
5314 bool ret = true;
5315
Daniel Vetter92703882012-08-09 16:46:01 +02005316 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005317 if (!i915_mch_dev) {
5318 ret = false;
5319 goto out_unlock;
5320 }
5321 dev_priv = i915_mch_dev;
5322
Daniel Vetter20e4d402012-08-08 23:35:39 +02005323 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005324
Daniel Vetter20e4d402012-08-08 23:35:39 +02005325 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005326 ret = false;
5327
5328out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005329 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005330
5331 return ret;
5332}
5333EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5334
5335/**
5336 * Tells the intel_ips driver that the i915 driver is now loaded, if
5337 * IPS got loaded first.
5338 *
5339 * This awkward dance is so that neither module has to depend on the
5340 * other in order for IPS to do the appropriate communication of
5341 * GPU turbo limits to i915.
5342 */
5343static void
5344ips_ping_for_i915_load(void)
5345{
5346 void (*link)(void);
5347
5348 link = symbol_get(ips_link_to_i915_driver);
5349 if (link) {
5350 link();
5351 symbol_put(ips_link_to_i915_driver);
5352 }
5353}
5354
5355void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5356{
Daniel Vetter02d71952012-08-09 16:44:54 +02005357 /* We only register the i915 ips part with intel-ips once everything is
5358 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005359 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005360 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005361 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005362
5363 ips_ping_for_i915_load();
5364}
5365
5366void intel_gpu_ips_teardown(void)
5367{
Daniel Vetter92703882012-08-09 16:46:01 +02005368 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005369 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005370 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005371}
Deepak S76c3552f2014-01-30 23:08:16 +05305372
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005373static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005374{
5375 struct drm_i915_private *dev_priv = dev->dev_private;
5376 u32 lcfuse;
5377 u8 pxw[16];
5378 int i;
5379
5380 /* Disable to program */
5381 I915_WRITE(ECR, 0);
5382 POSTING_READ(ECR);
5383
5384 /* Program energy weights for various events */
5385 I915_WRITE(SDEW, 0x15040d00);
5386 I915_WRITE(CSIEW0, 0x007f0000);
5387 I915_WRITE(CSIEW1, 0x1e220004);
5388 I915_WRITE(CSIEW2, 0x04000004);
5389
5390 for (i = 0; i < 5; i++)
5391 I915_WRITE(PEW + (i * 4), 0);
5392 for (i = 0; i < 3; i++)
5393 I915_WRITE(DEW + (i * 4), 0);
5394
5395 /* Program P-state weights to account for frequency power adjustment */
5396 for (i = 0; i < 16; i++) {
5397 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5398 unsigned long freq = intel_pxfreq(pxvidfreq);
5399 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5400 PXVFREQ_PX_SHIFT;
5401 unsigned long val;
5402
5403 val = vid * vid;
5404 val *= (freq / 1000);
5405 val *= 255;
5406 val /= (127*127*900);
5407 if (val > 0xff)
5408 DRM_ERROR("bad pxval: %ld\n", val);
5409 pxw[i] = val;
5410 }
5411 /* Render standby states get 0 weight */
5412 pxw[14] = 0;
5413 pxw[15] = 0;
5414
5415 for (i = 0; i < 4; i++) {
5416 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5417 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5418 I915_WRITE(PXW + (i * 4), val);
5419 }
5420
5421 /* Adjust magic regs to magic values (more experimental results) */
5422 I915_WRITE(OGW0, 0);
5423 I915_WRITE(OGW1, 0);
5424 I915_WRITE(EG0, 0x00007f00);
5425 I915_WRITE(EG1, 0x0000000e);
5426 I915_WRITE(EG2, 0x000e0000);
5427 I915_WRITE(EG3, 0x68000300);
5428 I915_WRITE(EG4, 0x42000000);
5429 I915_WRITE(EG5, 0x00140031);
5430 I915_WRITE(EG6, 0);
5431 I915_WRITE(EG7, 0);
5432
5433 for (i = 0; i < 8; i++)
5434 I915_WRITE(PXWL + (i * 4), 0);
5435
5436 /* Enable PMON + select events */
5437 I915_WRITE(ECR, 0x80000019);
5438
5439 lcfuse = I915_READ(LCFUSE02);
5440
Daniel Vetter20e4d402012-08-08 23:35:39 +02005441 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005442}
5443
Imre Deakae484342014-03-31 15:10:44 +03005444void intel_init_gt_powersave(struct drm_device *dev)
5445{
Imre Deake6069ca2014-04-18 16:01:02 +03005446 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5447
Deepak S38807742014-05-23 21:00:15 +05305448 if (IS_CHERRYVIEW(dev))
5449 cherryview_init_gt_powersave(dev);
5450 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005451 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005452}
5453
5454void intel_cleanup_gt_powersave(struct drm_device *dev)
5455{
Deepak S38807742014-05-23 21:00:15 +05305456 if (IS_CHERRYVIEW(dev))
5457 return;
5458 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005459 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005460}
5461
Imre Deakdbea3ce2014-12-15 18:59:28 +02005462static void gen6_suspend_rps(struct drm_device *dev)
5463{
5464 struct drm_i915_private *dev_priv = dev->dev_private;
5465
5466 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5467
5468 /*
5469 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5470 * is added for it.
5471 */
5472 if (INTEL_INFO(dev)->gen < 9)
5473 gen6_disable_rps_interrupts(dev);
5474}
5475
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005476/**
5477 * intel_suspend_gt_powersave - suspend PM work and helper threads
5478 * @dev: drm device
5479 *
5480 * We don't want to disable RC6 or other features here, we just want
5481 * to make sure any work we've queued has finished and won't bother
5482 * us while we're suspended.
5483 */
5484void intel_suspend_gt_powersave(struct drm_device *dev)
5485{
5486 struct drm_i915_private *dev_priv = dev->dev_private;
5487
Imre Deakd4d70aa2014-11-19 15:30:04 +02005488 if (INTEL_INFO(dev)->gen < 6)
5489 return;
5490
Imre Deakdbea3ce2014-12-15 18:59:28 +02005491 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305492
5493 /* Force GPU to min freq during suspend */
5494 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005495}
5496
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005497void intel_disable_gt_powersave(struct drm_device *dev)
5498{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005499 struct drm_i915_private *dev_priv = dev->dev_private;
5500
Daniel Vetter930ebb42012-06-29 23:32:16 +02005501 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005502 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305503 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005504 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005505
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005506 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005507 if (INTEL_INFO(dev)->gen >= 9)
5508 gen9_disable_rps(dev);
5509 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305510 cherryview_disable_rps(dev);
5511 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005512 valleyview_disable_rps(dev);
5513 else
5514 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005515
Chris Wilsonc0951f02013-10-10 21:58:50 +01005516 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005517 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005518 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005519}
5520
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005521static void intel_gen6_powersave_work(struct work_struct *work)
5522{
5523 struct drm_i915_private *dev_priv =
5524 container_of(work, struct drm_i915_private,
5525 rps.delayed_resume_work.work);
5526 struct drm_device *dev = dev_priv->dev;
5527
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005528 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005529
Imre Deak3cc134e2014-11-19 15:30:03 +02005530 /*
5531 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5532 * added for it.
5533 */
5534 if (INTEL_INFO(dev)->gen < 9)
5535 gen6_reset_rps_interrupts(dev);
5536
Deepak S38807742014-05-23 21:00:15 +05305537 if (IS_CHERRYVIEW(dev)) {
5538 cherryview_enable_rps(dev);
5539 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005540 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005541 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005542 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005543 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005544 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005545 } else if (IS_BROADWELL(dev)) {
5546 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005547 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005548 } else {
5549 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005550 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005551 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005552 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005553
5554 if (INTEL_INFO(dev)->gen < 9)
5555 gen6_enable_rps_interrupts(dev);
5556
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005557 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005558
5559 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005560}
5561
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005562void intel_enable_gt_powersave(struct drm_device *dev)
5563{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005564 struct drm_i915_private *dev_priv = dev->dev_private;
5565
Yu Zhangf61018b2015-02-10 19:05:52 +08005566 /* Powersaving is controlled by the host when inside a VM */
5567 if (intel_vgpu_active(dev))
5568 return;
5569
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005570 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005571 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005572 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005573 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005574 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305575 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005576 /*
5577 * PCU communication is slow and this doesn't need to be
5578 * done at any specific time, so do this out of our fast path
5579 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005580 *
5581 * We depend on the HW RC6 power context save/restore
5582 * mechanism when entering D3 through runtime PM suspend. So
5583 * disable RPM until RPS/RC6 is properly setup. We can only
5584 * get here via the driver load/system resume/runtime resume
5585 * paths, so the _noresume version is enough (and in case of
5586 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005587 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005588 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5589 round_jiffies_up_relative(HZ)))
5590 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005591 }
5592}
5593
Imre Deakc6df39b2014-04-14 20:24:29 +03005594void intel_reset_gt_powersave(struct drm_device *dev)
5595{
5596 struct drm_i915_private *dev_priv = dev->dev_private;
5597
Imre Deakdbea3ce2014-12-15 18:59:28 +02005598 if (INTEL_INFO(dev)->gen < 6)
5599 return;
5600
5601 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005602 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005603}
5604
Daniel Vetter3107bd42012-10-31 22:52:31 +01005605static void ibx_init_clock_gating(struct drm_device *dev)
5606{
5607 struct drm_i915_private *dev_priv = dev->dev_private;
5608
5609 /*
5610 * On Ibex Peak and Cougar Point, we need to disable clock
5611 * gating for the panel power sequencer or it will fail to
5612 * start up when no ports are active.
5613 */
5614 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5615}
5616
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005617static void g4x_disable_trickle_feed(struct drm_device *dev)
5618{
5619 struct drm_i915_private *dev_priv = dev->dev_private;
5620 int pipe;
5621
Damien Lespiau055e3932014-08-18 13:49:10 +01005622 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005623 I915_WRITE(DSPCNTR(pipe),
5624 I915_READ(DSPCNTR(pipe)) |
5625 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005626 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005627 }
5628}
5629
Ville Syrjälä017636c2013-12-05 15:51:37 +02005630static void ilk_init_lp_watermarks(struct drm_device *dev)
5631{
5632 struct drm_i915_private *dev_priv = dev->dev_private;
5633
5634 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5635 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5636 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5637
5638 /*
5639 * Don't touch WM1S_LP_EN here.
5640 * Doing so could cause underruns.
5641 */
5642}
5643
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005644static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005645{
5646 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005647 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005648
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005649 /*
5650 * Required for FBC
5651 * WaFbcDisableDpfcClockGating:ilk
5652 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005653 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5654 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5655 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005656
5657 I915_WRITE(PCH_3DCGDIS0,
5658 MARIUNIT_CLOCK_GATE_DISABLE |
5659 SVSMUNIT_CLOCK_GATE_DISABLE);
5660 I915_WRITE(PCH_3DCGDIS1,
5661 VFMUNIT_CLOCK_GATE_DISABLE);
5662
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005663 /*
5664 * According to the spec the following bits should be set in
5665 * order to enable memory self-refresh
5666 * The bit 22/21 of 0x42004
5667 * The bit 5 of 0x42020
5668 * The bit 15 of 0x45000
5669 */
5670 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5671 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5672 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005673 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005674 I915_WRITE(DISP_ARB_CTL,
5675 (I915_READ(DISP_ARB_CTL) |
5676 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005677
5678 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005679
5680 /*
5681 * Based on the document from hardware guys the following bits
5682 * should be set unconditionally in order to enable FBC.
5683 * The bit 22 of 0x42000
5684 * The bit 22 of 0x42004
5685 * The bit 7,8,9 of 0x42020.
5686 */
5687 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005688 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005689 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5690 I915_READ(ILK_DISPLAY_CHICKEN1) |
5691 ILK_FBCQ_DIS);
5692 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5693 I915_READ(ILK_DISPLAY_CHICKEN2) |
5694 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005695 }
5696
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005697 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5698
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005699 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5700 I915_READ(ILK_DISPLAY_CHICKEN2) |
5701 ILK_ELPIN_409_SELECT);
5702 I915_WRITE(_3D_CHICKEN2,
5703 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5704 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005705
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005706 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005707 I915_WRITE(CACHE_MODE_0,
5708 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005709
Akash Goel4e046322014-04-04 17:14:38 +05305710 /* WaDisable_RenderCache_OperationalFlush:ilk */
5711 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5712
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005713 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005714
Daniel Vetter3107bd42012-10-31 22:52:31 +01005715 ibx_init_clock_gating(dev);
5716}
5717
5718static void cpt_init_clock_gating(struct drm_device *dev)
5719{
5720 struct drm_i915_private *dev_priv = dev->dev_private;
5721 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005722 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005723
5724 /*
5725 * On Ibex Peak and Cougar Point, we need to disable clock
5726 * gating for the panel power sequencer or it will fail to
5727 * start up when no ports are active.
5728 */
Jesse Barnescd664072013-10-02 10:34:19 -07005729 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5730 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5731 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005732 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5733 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005734 /* The below fixes the weird display corruption, a few pixels shifted
5735 * downward, on (only) LVDS of some HP laptops with IVY.
5736 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005737 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005738 val = I915_READ(TRANS_CHICKEN2(pipe));
5739 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5740 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005741 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005742 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005743 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5744 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5745 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005746 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5747 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005748 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005749 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005750 I915_WRITE(TRANS_CHICKEN1(pipe),
5751 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5752 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005753}
5754
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005755static void gen6_check_mch_setup(struct drm_device *dev)
5756{
5757 struct drm_i915_private *dev_priv = dev->dev_private;
5758 uint32_t tmp;
5759
5760 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005761 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5762 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5763 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005764}
5765
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005766static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005767{
5768 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005769 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005770
Damien Lespiau231e54f2012-10-19 17:55:41 +01005771 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005772
5773 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5774 I915_READ(ILK_DISPLAY_CHICKEN2) |
5775 ILK_ELPIN_409_SELECT);
5776
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005777 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005778 I915_WRITE(_3D_CHICKEN,
5779 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5780
Akash Goel4e046322014-04-04 17:14:38 +05305781 /* WaDisable_RenderCache_OperationalFlush:snb */
5782 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5783
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005784 /*
5785 * BSpec recoomends 8x4 when MSAA is used,
5786 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005787 *
5788 * Note that PS/WM thread counts depend on the WIZ hashing
5789 * disable bit, which we don't touch here, but it's good
5790 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005791 */
5792 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005793 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005794
Ville Syrjälä017636c2013-12-05 15:51:37 +02005795 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005796
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005797 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005798 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005799
5800 I915_WRITE(GEN6_UCGCTL1,
5801 I915_READ(GEN6_UCGCTL1) |
5802 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5803 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5804
5805 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5806 * gating disable must be set. Failure to set it results in
5807 * flickering pixels due to Z write ordering failures after
5808 * some amount of runtime in the Mesa "fire" demo, and Unigine
5809 * Sanctuary and Tropics, and apparently anything else with
5810 * alpha test or pixel discard.
5811 *
5812 * According to the spec, bit 11 (RCCUNIT) must also be set,
5813 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005814 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005815 * WaDisableRCCUnitClockGating:snb
5816 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005817 */
5818 I915_WRITE(GEN6_UCGCTL2,
5819 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5820 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5821
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005822 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005823 I915_WRITE(_3D_CHICKEN3,
5824 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005825
5826 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005827 * Bspec says:
5828 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5829 * 3DSTATE_SF number of SF output attributes is more than 16."
5830 */
5831 I915_WRITE(_3D_CHICKEN3,
5832 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5833
5834 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005835 * According to the spec the following bits should be
5836 * set in order to enable memory self-refresh and fbc:
5837 * The bit21 and bit22 of 0x42000
5838 * The bit21 and bit22 of 0x42004
5839 * The bit5 and bit7 of 0x42020
5840 * The bit14 of 0x70180
5841 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005842 *
5843 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005844 */
5845 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5846 I915_READ(ILK_DISPLAY_CHICKEN1) |
5847 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5848 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5849 I915_READ(ILK_DISPLAY_CHICKEN2) |
5850 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005851 I915_WRITE(ILK_DSPCLK_GATE_D,
5852 I915_READ(ILK_DSPCLK_GATE_D) |
5853 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5854 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005855
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005856 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005857
Daniel Vetter3107bd42012-10-31 22:52:31 +01005858 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005859
5860 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005861}
5862
5863static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5864{
5865 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5866
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005867 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005868 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005869 *
5870 * This actually overrides the dispatch
5871 * mode for all thread types.
5872 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005873 reg &= ~GEN7_FF_SCHED_MASK;
5874 reg |= GEN7_FF_TS_SCHED_HW;
5875 reg |= GEN7_FF_VS_SCHED_HW;
5876 reg |= GEN7_FF_DS_SCHED_HW;
5877
5878 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5879}
5880
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005881static void lpt_init_clock_gating(struct drm_device *dev)
5882{
5883 struct drm_i915_private *dev_priv = dev->dev_private;
5884
5885 /*
5886 * TODO: this bit should only be enabled when really needed, then
5887 * disabled when not needed anymore in order to save power.
5888 */
5889 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5890 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5891 I915_READ(SOUTH_DSPCLK_GATE_D) |
5892 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005893
5894 /* WADPOClockGatingDisable:hsw */
5895 I915_WRITE(_TRANSA_CHICKEN1,
5896 I915_READ(_TRANSA_CHICKEN1) |
5897 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005898}
5899
Imre Deak7d708ee2013-04-17 14:04:50 +03005900static void lpt_suspend_hw(struct drm_device *dev)
5901{
5902 struct drm_i915_private *dev_priv = dev->dev_private;
5903
5904 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5905 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5906
5907 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5908 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5909 }
5910}
5911
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005912static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005913{
5914 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005915 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005916
5917 I915_WRITE(WM3_LP_ILK, 0);
5918 I915_WRITE(WM2_LP_ILK, 0);
5919 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005920
Ben Widawskyab57fff2013-12-12 15:28:04 -08005921 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005922 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005923
Ben Widawskyab57fff2013-12-12 15:28:04 -08005924 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005925 I915_WRITE(CHICKEN_PAR1_1,
5926 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5927
Ben Widawskyab57fff2013-12-12 15:28:04 -08005928 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005929 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005930 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005931 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005932 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005933 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005934
Ben Widawskyab57fff2013-12-12 15:28:04 -08005935 /* WaVSRefCountFullforceMissDisable:bdw */
5936 /* WaDSRefCountFullforceMissDisable:bdw */
5937 I915_WRITE(GEN7_FF_THREAD_MODE,
5938 I915_READ(GEN7_FF_THREAD_MODE) &
5939 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005940
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02005941 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
5942 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02005943
5944 /* WaDisableSDEUnitClockGating:bdw */
5945 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
5946 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00005947
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03005948 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005949}
5950
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005951static void haswell_init_clock_gating(struct drm_device *dev)
5952{
5953 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005954
Ville Syrjälä017636c2013-12-05 15:51:37 +02005955 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005956
Francisco Jerezf3fc4882013-10-02 15:53:16 -07005957 /* L3 caching of data atomics doesn't work -- disable it. */
5958 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
5959 I915_WRITE(HSW_ROW_CHICKEN3,
5960 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
5961
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005962 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005963 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
5964 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
5965 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
5966
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02005967 /* WaVSRefCountFullforceMissDisable:hsw */
5968 I915_WRITE(GEN7_FF_THREAD_MODE,
5969 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005970
Akash Goel4e046322014-04-04 17:14:38 +05305971 /* WaDisable_RenderCache_OperationalFlush:hsw */
5972 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5973
Chia-I Wufe27c602014-01-28 13:29:33 +08005974 /* enable HiZ Raw Stall Optimization */
5975 I915_WRITE(CACHE_MODE_0_GEN7,
5976 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
5977
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005978 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03005979 I915_WRITE(CACHE_MODE_1,
5980 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03005981
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005982 /*
5983 * BSpec recommends 8x4 when MSAA is used,
5984 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005985 *
5986 * Note that PS/WM thread counts depend on the WIZ hashing
5987 * disable bit, which we don't touch here, but it's good
5988 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005989 */
5990 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005991 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02005992
Kenneth Graunke94411592014-12-31 16:23:00 -08005993 /* WaSampleCChickenBitEnable:hsw */
5994 I915_WRITE(HALF_SLICE_CHICKEN3,
5995 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
5996
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005997 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07005998 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
5999
Paulo Zanoni90a88642013-05-03 17:23:45 -03006000 /* WaRsPkgCStateDisplayPMReq:hsw */
6001 I915_WRITE(CHICKEN_PAR1_1,
6002 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006003
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006004 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006005}
6006
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006007static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006008{
6009 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006010 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006011
Ville Syrjälä017636c2013-12-05 15:51:37 +02006012 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006013
Damien Lespiau231e54f2012-10-19 17:55:41 +01006014 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006015
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006016 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006017 I915_WRITE(_3D_CHICKEN3,
6018 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6019
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006020 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006021 I915_WRITE(IVB_CHICKEN3,
6022 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6023 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6024
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006025 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006026 if (IS_IVB_GT1(dev))
6027 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6028 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006029
Akash Goel4e046322014-04-04 17:14:38 +05306030 /* WaDisable_RenderCache_OperationalFlush:ivb */
6031 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6032
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006033 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006034 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6035 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6036
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006037 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006038 I915_WRITE(GEN7_L3CNTLREG1,
6039 GEN7_WA_FOR_GEN7_L3_CONTROL);
6040 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006041 GEN7_WA_L3_CHICKEN_MODE);
6042 if (IS_IVB_GT1(dev))
6043 I915_WRITE(GEN7_ROW_CHICKEN2,
6044 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006045 else {
6046 /* must write both registers */
6047 I915_WRITE(GEN7_ROW_CHICKEN2,
6048 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006049 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6050 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006051 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006052
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006053 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006054 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6055 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6056
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006057 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006058 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006059 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006060 */
6061 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006062 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006063
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006064 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006065 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6066 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6067 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6068
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006069 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006070
6071 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006072
Chris Wilson22721342014-03-04 09:41:43 +00006073 if (0) { /* causes HiZ corruption on ivb:gt1 */
6074 /* enable HiZ Raw Stall Optimization */
6075 I915_WRITE(CACHE_MODE_0_GEN7,
6076 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6077 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006078
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006079 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006080 I915_WRITE(CACHE_MODE_1,
6081 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006082
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006083 /*
6084 * BSpec recommends 8x4 when MSAA is used,
6085 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006086 *
6087 * Note that PS/WM thread counts depend on the WIZ hashing
6088 * disable bit, which we don't touch here, but it's good
6089 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006090 */
6091 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006092 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006093
Ben Widawsky20848222012-05-04 18:58:59 -07006094 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6095 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6096 snpcr |= GEN6_MBC_SNPCR_MED;
6097 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006098
Ben Widawskyab5c6082013-04-05 13:12:41 -07006099 if (!HAS_PCH_NOP(dev))
6100 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006101
6102 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006103}
6104
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006105static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006106{
6107 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006108
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03006109 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006110
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006111 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006112 I915_WRITE(_3D_CHICKEN3,
6113 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6114
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006115 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006116 I915_WRITE(IVB_CHICKEN3,
6117 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6118 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6119
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006120 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006121 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006122 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006123 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6124 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006125
Akash Goel4e046322014-04-04 17:14:38 +05306126 /* WaDisable_RenderCache_OperationalFlush:vlv */
6127 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6128
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006129 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006130 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6131 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6132
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006133 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006134 I915_WRITE(GEN7_ROW_CHICKEN2,
6135 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6136
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006137 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006138 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6139 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6140 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6141
Ville Syrjälä46680e02014-01-22 21:33:01 +02006142 gen7_setup_fixed_func_scheduler(dev_priv);
6143
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006144 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006145 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006146 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006147 */
6148 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006149 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006150
Akash Goelc98f5062014-03-24 23:00:07 +05306151 /* WaDisableL3Bank2xClockGate:vlv
6152 * Disabling L3 clock gating- MMIO 940c[25] = 1
6153 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6154 I915_WRITE(GEN7_UCGCTL4,
6155 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006156
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006157 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006158
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006159 /*
6160 * BSpec says this must be set, even though
6161 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6162 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006163 I915_WRITE(CACHE_MODE_1,
6164 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006165
6166 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006167 * BSpec recommends 8x4 when MSAA is used,
6168 * however in practice 16x4 seems fastest.
6169 *
6170 * Note that PS/WM thread counts depend on the WIZ hashing
6171 * disable bit, which we don't touch here, but it's good
6172 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6173 */
6174 I915_WRITE(GEN7_GT_MODE,
6175 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6176
6177 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006178 * WaIncreaseL3CreditsForVLVB0:vlv
6179 * This is the hardware default actually.
6180 */
6181 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6182
6183 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006184 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006185 * Disable clock gating on th GCFG unit to prevent a delay
6186 * in the reporting of vblank events.
6187 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006188 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006189}
6190
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006191static void cherryview_init_clock_gating(struct drm_device *dev)
6192{
6193 struct drm_i915_private *dev_priv = dev->dev_private;
6194
6195 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6196
6197 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006198
Ville Syrjälä232ce332014-04-09 13:28:35 +03006199 /* WaVSRefCountFullforceMissDisable:chv */
6200 /* WaDSRefCountFullforceMissDisable:chv */
6201 I915_WRITE(GEN7_FF_THREAD_MODE,
6202 I915_READ(GEN7_FF_THREAD_MODE) &
6203 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006204
6205 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6206 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6207 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006208
6209 /* WaDisableCSUnitClockGating:chv */
6210 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6211 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006212
6213 /* WaDisableSDEUnitClockGating:chv */
6214 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6215 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006216}
6217
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006218static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006219{
6220 struct drm_i915_private *dev_priv = dev->dev_private;
6221 uint32_t dspclk_gate;
6222
6223 I915_WRITE(RENCLK_GATE_D1, 0);
6224 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6225 GS_UNIT_CLOCK_GATE_DISABLE |
6226 CL_UNIT_CLOCK_GATE_DISABLE);
6227 I915_WRITE(RAMCLK_GATE_D, 0);
6228 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6229 OVRUNIT_CLOCK_GATE_DISABLE |
6230 OVCUNIT_CLOCK_GATE_DISABLE;
6231 if (IS_GM45(dev))
6232 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6233 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006234
6235 /* WaDisableRenderCachePipelinedFlush */
6236 I915_WRITE(CACHE_MODE_0,
6237 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006238
Akash Goel4e046322014-04-04 17:14:38 +05306239 /* WaDisable_RenderCache_OperationalFlush:g4x */
6240 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6241
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006242 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006243}
6244
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006245static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006246{
6247 struct drm_i915_private *dev_priv = dev->dev_private;
6248
6249 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6250 I915_WRITE(RENCLK_GATE_D2, 0);
6251 I915_WRITE(DSPCLK_GATE_D, 0);
6252 I915_WRITE(RAMCLK_GATE_D, 0);
6253 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006254 I915_WRITE(MI_ARB_STATE,
6255 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306256
6257 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6258 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006259}
6260
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006261static void broadwater_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;
6264
6265 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6266 I965_RCC_CLOCK_GATE_DISABLE |
6267 I965_RCPB_CLOCK_GATE_DISABLE |
6268 I965_ISC_CLOCK_GATE_DISABLE |
6269 I965_FBC_CLOCK_GATE_DISABLE);
6270 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006271 I915_WRITE(MI_ARB_STATE,
6272 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306273
6274 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6275 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006276}
6277
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006278static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006279{
6280 struct drm_i915_private *dev_priv = dev->dev_private;
6281 u32 dstate = I915_READ(D_STATE);
6282
6283 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6284 DSTATE_DOT_CLOCK_GATING;
6285 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006286
6287 if (IS_PINEVIEW(dev))
6288 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006289
6290 /* IIR "flip pending" means done if this bit is set */
6291 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006292
6293 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006294 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006295
6296 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6297 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006298
6299 I915_WRITE(MI_ARB_STATE,
6300 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006301}
6302
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006303static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006304{
6305 struct drm_i915_private *dev_priv = dev->dev_private;
6306
6307 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006308
6309 /* interrupts should cause a wake up from C3 */
6310 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6311 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006312
6313 I915_WRITE(MEM_MODE,
6314 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006315}
6316
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006317static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006318{
6319 struct drm_i915_private *dev_priv = dev->dev_private;
6320
6321 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006322
6323 I915_WRITE(MEM_MODE,
6324 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6325 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006326}
6327
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006328void intel_init_clock_gating(struct drm_device *dev)
6329{
6330 struct drm_i915_private *dev_priv = dev->dev_private;
6331
Damien Lespiauc57e3552015-02-09 19:33:05 +00006332 if (dev_priv->display.init_clock_gating)
6333 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006334}
6335
Imre Deak7d708ee2013-04-17 14:04:50 +03006336void intel_suspend_hw(struct drm_device *dev)
6337{
6338 if (HAS_PCH_LPT(dev))
6339 lpt_suspend_hw(dev);
6340}
6341
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006342/* Set up chip specific power management-related functions */
6343void intel_init_pm(struct drm_device *dev)
6344{
6345 struct drm_i915_private *dev_priv = dev->dev_private;
6346
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006347 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006348
Daniel Vetterc921aba2012-04-26 23:28:17 +02006349 /* For cxsr */
6350 if (IS_PINEVIEW(dev))
6351 i915_pineview_get_mem_freq(dev);
6352 else if (IS_GEN5(dev))
6353 i915_ironlake_get_mem_freq(dev);
6354
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006355 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006356 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006357 skl_setup_wm_latency(dev);
6358
Damien Lespiau45db2192015-02-09 19:33:09 +00006359 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006360 dev_priv->display.update_wm = skl_update_wm;
6361 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306362 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006363 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006364
Ville Syrjäläbd602542014-01-07 16:14:10 +02006365 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6366 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6367 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6368 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6369 dev_priv->display.update_wm = ilk_update_wm;
6370 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6371 } else {
6372 DRM_DEBUG_KMS("Failed to read display plane latency. "
6373 "Disable CxSR\n");
6374 }
6375
6376 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006377 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006378 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006379 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006380 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006381 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006382 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006383 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006384 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006385 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006386 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03006387 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306388 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006389 dev_priv->display.init_clock_gating =
6390 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006391 } else if (IS_VALLEYVIEW(dev)) {
6392 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306393 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006394 dev_priv->display.init_clock_gating =
6395 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006396 } else if (IS_PINEVIEW(dev)) {
6397 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6398 dev_priv->is_ddr3,
6399 dev_priv->fsb_freq,
6400 dev_priv->mem_freq)) {
6401 DRM_INFO("failed to find known CxSR latency "
6402 "(found ddr%s fsb freq %d, mem freq %d), "
6403 "disabling CxSR\n",
6404 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6405 dev_priv->fsb_freq, dev_priv->mem_freq);
6406 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006407 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006408 dev_priv->display.update_wm = NULL;
6409 } else
6410 dev_priv->display.update_wm = pineview_update_wm;
6411 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6412 } else if (IS_G4X(dev)) {
6413 dev_priv->display.update_wm = g4x_update_wm;
6414 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6415 } else if (IS_GEN4(dev)) {
6416 dev_priv->display.update_wm = i965_update_wm;
6417 if (IS_CRESTLINE(dev))
6418 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6419 else if (IS_BROADWATER(dev))
6420 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6421 } else if (IS_GEN3(dev)) {
6422 dev_priv->display.update_wm = i9xx_update_wm;
6423 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6424 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006425 } else if (IS_GEN2(dev)) {
6426 if (INTEL_INFO(dev)->num_pipes == 1) {
6427 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006428 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006429 } else {
6430 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006431 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006432 }
6433
6434 if (IS_I85X(dev) || IS_I865G(dev))
6435 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6436 else
6437 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6438 } else {
6439 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006440 }
6441}
6442
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006443int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006444{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006445 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006446
6447 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6448 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6449 return -EAGAIN;
6450 }
6451
6452 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006453 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006454 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6455
6456 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6457 500)) {
6458 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6459 return -ETIMEDOUT;
6460 }
6461
6462 *val = I915_READ(GEN6_PCODE_DATA);
6463 I915_WRITE(GEN6_PCODE_DATA, 0);
6464
6465 return 0;
6466}
6467
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006468int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006469{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006470 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006471
6472 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6473 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6474 return -EAGAIN;
6475 }
6476
6477 I915_WRITE(GEN6_PCODE_DATA, val);
6478 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6479
6480 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6481 500)) {
6482 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6483 return -ETIMEDOUT;
6484 }
6485
6486 I915_WRITE(GEN6_PCODE_DATA, 0);
6487
6488 return 0;
6489}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006490
Ville Syrjälädd06f882014-11-10 22:55:12 +02006491static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006492{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006493 switch (czclk_freq) {
6494 case 200:
6495 return 10;
6496 case 267:
6497 return 12;
6498 case 320:
6499 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006500 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006501 case 400:
6502 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006503 default:
6504 return -1;
6505 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006506}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006507
Ville Syrjälädd06f882014-11-10 22:55:12 +02006508static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6509{
6510 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6511
6512 div = vlv_gpu_freq_div(czclk_freq);
6513 if (div < 0)
6514 return div;
6515
6516 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006517}
6518
Fengguang Wub55dd642014-07-12 11:21:39 +02006519static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006520{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006521 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006522
Ville Syrjälädd06f882014-11-10 22:55:12 +02006523 mul = vlv_gpu_freq_div(czclk_freq);
6524 if (mul < 0)
6525 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006526
Ville Syrjälädd06f882014-11-10 22:55:12 +02006527 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006528}
6529
Fengguang Wub55dd642014-07-12 11:21:39 +02006530static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306531{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006532 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306533
Ville Syrjälädd06f882014-11-10 22:55:12 +02006534 div = vlv_gpu_freq_div(czclk_freq) / 2;
6535 if (div < 0)
6536 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306537
Ville Syrjälädd06f882014-11-10 22:55:12 +02006538 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306539}
6540
Fengguang Wub55dd642014-07-12 11:21:39 +02006541static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306542{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006543 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306544
Ville Syrjälädd06f882014-11-10 22:55:12 +02006545 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6546 if (mul < 0)
6547 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306548
Ville Syrjälä1c147622014-08-18 14:42:43 +03006549 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006550 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306551}
6552
Ville Syrjälä616bc822015-01-23 21:04:25 +02006553int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6554{
6555 if (IS_CHERRYVIEW(dev_priv->dev))
6556 return chv_gpu_freq(dev_priv, val);
6557 else if (IS_VALLEYVIEW(dev_priv->dev))
6558 return byt_gpu_freq(dev_priv, val);
6559 else
6560 return val * GT_FREQUENCY_MULTIPLIER;
6561}
6562
Ville Syrjälä616bc822015-01-23 21:04:25 +02006563int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6564{
Deepak S22b1b2f2014-07-12 14:54:33 +05306565 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006566 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306567 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006568 return byt_freq_opcode(dev_priv, val);
6569 else
6570 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306571}
6572
Daniel Vetterf742a552013-12-06 10:17:53 +01006573void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006574{
6575 struct drm_i915_private *dev_priv = dev->dev_private;
6576
Daniel Vetterf742a552013-12-06 10:17:53 +01006577 mutex_init(&dev_priv->rps.hw_lock);
6578
Chris Wilson907b28c2013-07-19 20:36:52 +01006579 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6580 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006581
Paulo Zanoni33688d92014-03-07 20:08:19 -03006582 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006583}