blob: e90ec21dd9b24b14632426d0613616e794edf027 [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
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200266static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
267{
268 u32 val;
269
270 mutex_lock(&dev_priv->rps.hw_lock);
271
272 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
273 if (enable)
274 val |= DSP_MAXFIFO_PM5_ENABLE;
275 else
276 val &= ~DSP_MAXFIFO_PM5_ENABLE;
277 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
278
279 mutex_unlock(&dev_priv->rps.hw_lock);
280}
281
Imre Deak5209b1f2014-07-01 12:36:17 +0300282void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300283{
Imre Deak5209b1f2014-07-01 12:36:17 +0300284 struct drm_device *dev = dev_priv->dev;
285 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300286
Imre Deak5209b1f2014-07-01 12:36:17 +0300287 if (IS_VALLEYVIEW(dev)) {
288 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200289 if (IS_CHERRYVIEW(dev))
290 chv_set_memory_pm5(dev_priv, enable);
Imre Deak5209b1f2014-07-01 12:36:17 +0300291 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
292 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
293 } else if (IS_PINEVIEW(dev)) {
294 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
295 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
296 I915_WRITE(DSPFW3, val);
297 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
298 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
299 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
300 I915_WRITE(FW_BLC_SELF, val);
301 } else if (IS_I915GM(dev)) {
302 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
303 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
304 I915_WRITE(INSTPM, val);
305 } else {
306 return;
307 }
308
309 DRM_DEBUG_KMS("memory self-refresh is %s\n",
310 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300311}
312
313/*
314 * Latency for FIFO fetches is dependent on several factors:
315 * - memory configuration (speed, channels)
316 * - chipset
317 * - current MCH state
318 * It can be fairly high in some situations, so here we assume a fairly
319 * pessimal value. It's a tradeoff between extra memory fetches (if we
320 * set this value too high, the FIFO will fetch frequently to stay full)
321 * and power consumption (set it too low to save power and we might see
322 * FIFO underruns and display "flicker").
323 *
324 * A value of 5us seems to be a good balance; safe for very low end
325 * platforms but not overly aggressive on lower latency configs.
326 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100327static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300328
Ville Syrjäläb5004722015-03-05 21:19:47 +0200329#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
330 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
331
332static int vlv_get_fifo_size(struct drm_device *dev,
333 enum pipe pipe, int plane)
334{
335 struct drm_i915_private *dev_priv = dev->dev_private;
336 int sprite0_start, sprite1_start, size;
337
338 switch (pipe) {
339 uint32_t dsparb, dsparb2, dsparb3;
340 case PIPE_A:
341 dsparb = I915_READ(DSPARB);
342 dsparb2 = I915_READ(DSPARB2);
343 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
344 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
345 break;
346 case PIPE_B:
347 dsparb = I915_READ(DSPARB);
348 dsparb2 = I915_READ(DSPARB2);
349 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
350 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
351 break;
352 case PIPE_C:
353 dsparb2 = I915_READ(DSPARB2);
354 dsparb3 = I915_READ(DSPARB3);
355 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
356 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
357 break;
358 default:
359 return 0;
360 }
361
362 switch (plane) {
363 case 0:
364 size = sprite0_start;
365 break;
366 case 1:
367 size = sprite1_start - sprite0_start;
368 break;
369 case 2:
370 size = 512 - 1 - sprite1_start;
371 break;
372 default:
373 return 0;
374 }
375
376 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
377 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
378 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
379 size);
380
381 return size;
382}
383
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300384static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300385{
386 struct drm_i915_private *dev_priv = dev->dev_private;
387 uint32_t dsparb = I915_READ(DSPARB);
388 int size;
389
390 size = dsparb & 0x7f;
391 if (plane)
392 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
393
394 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
395 plane ? "B" : "A", size);
396
397 return size;
398}
399
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200400static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300401{
402 struct drm_i915_private *dev_priv = dev->dev_private;
403 uint32_t dsparb = I915_READ(DSPARB);
404 int size;
405
406 size = dsparb & 0x1ff;
407 if (plane)
408 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
409 size >>= 1; /* Convert to cachelines */
410
411 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
412 plane ? "B" : "A", size);
413
414 return size;
415}
416
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300417static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300418{
419 struct drm_i915_private *dev_priv = dev->dev_private;
420 uint32_t dsparb = I915_READ(DSPARB);
421 int size;
422
423 size = dsparb & 0x7f;
424 size >>= 2; /* Convert to cachelines */
425
426 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
427 plane ? "B" : "A",
428 size);
429
430 return size;
431}
432
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300433/* Pineview has different values for various configs */
434static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300435 .fifo_size = PINEVIEW_DISPLAY_FIFO,
436 .max_wm = PINEVIEW_MAX_WM,
437 .default_wm = PINEVIEW_DFT_WM,
438 .guard_size = PINEVIEW_GUARD_WM,
439 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300440};
441static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300442 .fifo_size = PINEVIEW_DISPLAY_FIFO,
443 .max_wm = PINEVIEW_MAX_WM,
444 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
445 .guard_size = PINEVIEW_GUARD_WM,
446 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300447};
448static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300449 .fifo_size = PINEVIEW_CURSOR_FIFO,
450 .max_wm = PINEVIEW_CURSOR_MAX_WM,
451 .default_wm = PINEVIEW_CURSOR_DFT_WM,
452 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
453 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300454};
455static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300456 .fifo_size = PINEVIEW_CURSOR_FIFO,
457 .max_wm = PINEVIEW_CURSOR_MAX_WM,
458 .default_wm = PINEVIEW_CURSOR_DFT_WM,
459 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
460 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300461};
462static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300463 .fifo_size = G4X_FIFO_SIZE,
464 .max_wm = G4X_MAX_WM,
465 .default_wm = G4X_MAX_WM,
466 .guard_size = 2,
467 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300468};
469static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300470 .fifo_size = I965_CURSOR_FIFO,
471 .max_wm = I965_CURSOR_MAX_WM,
472 .default_wm = I965_CURSOR_DFT_WM,
473 .guard_size = 2,
474 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300475};
476static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300477 .fifo_size = VALLEYVIEW_FIFO_SIZE,
478 .max_wm = VALLEYVIEW_MAX_WM,
479 .default_wm = VALLEYVIEW_MAX_WM,
480 .guard_size = 2,
481 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482};
483static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300484 .fifo_size = I965_CURSOR_FIFO,
485 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
486 .default_wm = I965_CURSOR_DFT_WM,
487 .guard_size = 2,
488 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300489};
490static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300491 .fifo_size = I965_CURSOR_FIFO,
492 .max_wm = I965_CURSOR_MAX_WM,
493 .default_wm = I965_CURSOR_DFT_WM,
494 .guard_size = 2,
495 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300496};
497static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300498 .fifo_size = I945_FIFO_SIZE,
499 .max_wm = I915_MAX_WM,
500 .default_wm = 1,
501 .guard_size = 2,
502 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300503};
504static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300505 .fifo_size = I915_FIFO_SIZE,
506 .max_wm = I915_MAX_WM,
507 .default_wm = 1,
508 .guard_size = 2,
509 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300510};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300511static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300512 .fifo_size = I855GM_FIFO_SIZE,
513 .max_wm = I915_MAX_WM,
514 .default_wm = 1,
515 .guard_size = 2,
516 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300518static const struct intel_watermark_params i830_bc_wm_info = {
519 .fifo_size = I855GM_FIFO_SIZE,
520 .max_wm = I915_MAX_WM/2,
521 .default_wm = 1,
522 .guard_size = 2,
523 .cacheline_size = I830_FIFO_LINE_SIZE,
524};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200525static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300526 .fifo_size = I830_FIFO_SIZE,
527 .max_wm = I915_MAX_WM,
528 .default_wm = 1,
529 .guard_size = 2,
530 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531};
532
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533/**
534 * intel_calculate_wm - calculate watermark level
535 * @clock_in_khz: pixel clock
536 * @wm: chip FIFO params
537 * @pixel_size: display pixel size
538 * @latency_ns: memory latency for the platform
539 *
540 * Calculate the watermark level (the level at which the display plane will
541 * start fetching from memory again). Each chip has a different display
542 * FIFO size and allocation, so the caller needs to figure that out and pass
543 * in the correct intel_watermark_params structure.
544 *
545 * As the pixel clock runs, the FIFO will be drained at a rate that depends
546 * on the pixel size. When it reaches the watermark level, it'll start
547 * fetching FIFO line sized based chunks from memory until the FIFO fills
548 * past the watermark point. If the FIFO drains completely, a FIFO underrun
549 * will occur, and a display engine hang could result.
550 */
551static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
552 const struct intel_watermark_params *wm,
553 int fifo_size,
554 int pixel_size,
555 unsigned long latency_ns)
556{
557 long entries_required, wm_size;
558
559 /*
560 * Note: we need to make sure we don't overflow for various clock &
561 * latency values.
562 * clocks go from a few thousand to several hundred thousand.
563 * latency is usually a few thousand
564 */
565 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
566 1000;
567 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
568
569 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
570
571 wm_size = fifo_size - (entries_required + wm->guard_size);
572
573 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
574
575 /* Don't promote wm_size to unsigned... */
576 if (wm_size > (long)wm->max_wm)
577 wm_size = wm->max_wm;
578 if (wm_size <= 0)
579 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300580
581 /*
582 * Bspec seems to indicate that the value shouldn't be lower than
583 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
584 * Lets go for 8 which is the burst size since certain platforms
585 * already use a hardcoded 8 (which is what the spec says should be
586 * done).
587 */
588 if (wm_size <= 8)
589 wm_size = 8;
590
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300591 return wm_size;
592}
593
594static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
595{
596 struct drm_crtc *crtc, *enabled = NULL;
597
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100598 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000599 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300600 if (enabled)
601 return NULL;
602 enabled = crtc;
603 }
604 }
605
606 return enabled;
607}
608
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300609static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300610{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300611 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300612 struct drm_i915_private *dev_priv = dev->dev_private;
613 struct drm_crtc *crtc;
614 const struct cxsr_latency *latency;
615 u32 reg;
616 unsigned long wm;
617
618 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
619 dev_priv->fsb_freq, dev_priv->mem_freq);
620 if (!latency) {
621 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300622 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623 return;
624 }
625
626 crtc = single_enabled_crtc(dev);
627 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100628 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800629 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100630 int clock;
631
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200632 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100633 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300634
635 /* Display SR */
636 wm = intel_calculate_wm(clock, &pineview_display_wm,
637 pineview_display_wm.fifo_size,
638 pixel_size, latency->display_sr);
639 reg = I915_READ(DSPFW1);
640 reg &= ~DSPFW_SR_MASK;
641 reg |= wm << DSPFW_SR_SHIFT;
642 I915_WRITE(DSPFW1, reg);
643 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
644
645 /* cursor SR */
646 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
647 pineview_display_wm.fifo_size,
648 pixel_size, latency->cursor_sr);
649 reg = I915_READ(DSPFW3);
650 reg &= ~DSPFW_CURSOR_SR_MASK;
651 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
652 I915_WRITE(DSPFW3, reg);
653
654 /* Display HPLL off SR */
655 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
656 pineview_display_hplloff_wm.fifo_size,
657 pixel_size, latency->display_hpll_disable);
658 reg = I915_READ(DSPFW3);
659 reg &= ~DSPFW_HPLL_SR_MASK;
660 reg |= wm & DSPFW_HPLL_SR_MASK;
661 I915_WRITE(DSPFW3, reg);
662
663 /* cursor HPLL off SR */
664 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
665 pineview_display_hplloff_wm.fifo_size,
666 pixel_size, latency->cursor_hpll_disable);
667 reg = I915_READ(DSPFW3);
668 reg &= ~DSPFW_HPLL_CURSOR_MASK;
669 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
670 I915_WRITE(DSPFW3, reg);
671 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
672
Imre Deak5209b1f2014-07-01 12:36:17 +0300673 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300674 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300675 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300676 }
677}
678
679static bool g4x_compute_wm0(struct drm_device *dev,
680 int plane,
681 const struct intel_watermark_params *display,
682 int display_latency_ns,
683 const struct intel_watermark_params *cursor,
684 int cursor_latency_ns,
685 int *plane_wm,
686 int *cursor_wm)
687{
688 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300689 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300690 int htotal, hdisplay, clock, pixel_size;
691 int line_time_us, line_count;
692 int entries, tlb_miss;
693
694 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000695 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300696 *cursor_wm = cursor->guard_size;
697 *plane_wm = display->guard_size;
698 return false;
699 }
700
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200701 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100702 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800703 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200704 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800705 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300706
707 /* Use the small buffer method to calculate plane watermark */
708 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
709 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
710 if (tlb_miss > 0)
711 entries += tlb_miss;
712 entries = DIV_ROUND_UP(entries, display->cacheline_size);
713 *plane_wm = entries + display->guard_size;
714 if (*plane_wm > (int)display->max_wm)
715 *plane_wm = display->max_wm;
716
717 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200718 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300719 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800720 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300721 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
722 if (tlb_miss > 0)
723 entries += tlb_miss;
724 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
725 *cursor_wm = entries + cursor->guard_size;
726 if (*cursor_wm > (int)cursor->max_wm)
727 *cursor_wm = (int)cursor->max_wm;
728
729 return true;
730}
731
732/*
733 * Check the wm result.
734 *
735 * If any calculated watermark values is larger than the maximum value that
736 * can be programmed into the associated watermark register, that watermark
737 * must be disabled.
738 */
739static bool g4x_check_srwm(struct drm_device *dev,
740 int display_wm, int cursor_wm,
741 const struct intel_watermark_params *display,
742 const struct intel_watermark_params *cursor)
743{
744 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
745 display_wm, cursor_wm);
746
747 if (display_wm > display->max_wm) {
748 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
749 display_wm, display->max_wm);
750 return false;
751 }
752
753 if (cursor_wm > cursor->max_wm) {
754 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
755 cursor_wm, cursor->max_wm);
756 return false;
757 }
758
759 if (!(display_wm || cursor_wm)) {
760 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
761 return false;
762 }
763
764 return true;
765}
766
767static bool g4x_compute_srwm(struct drm_device *dev,
768 int plane,
769 int latency_ns,
770 const struct intel_watermark_params *display,
771 const struct intel_watermark_params *cursor,
772 int *display_wm, int *cursor_wm)
773{
774 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300775 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300776 int hdisplay, htotal, pixel_size, clock;
777 unsigned long line_time_us;
778 int line_count, line_size;
779 int small, large;
780 int entries;
781
782 if (!latency_ns) {
783 *display_wm = *cursor_wm = 0;
784 return false;
785 }
786
787 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200788 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100789 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800790 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200791 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800792 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300793
Ville Syrjälä922044c2014-02-14 14:18:57 +0200794 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300795 line_count = (latency_ns / line_time_us + 1000) / 1000;
796 line_size = hdisplay * pixel_size;
797
798 /* Use the minimum of the small and large buffer method for primary */
799 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
800 large = line_count * line_size;
801
802 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
803 *display_wm = entries + display->guard_size;
804
805 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800806 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300807 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
808 *cursor_wm = entries + cursor->guard_size;
809
810 return g4x_check_srwm(dev,
811 *display_wm, *cursor_wm,
812 display, cursor);
813}
814
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200815static void vlv_write_wm_values(struct intel_crtc *crtc,
816 const struct vlv_wm_values *wm)
817{
818 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
819 enum pipe pipe = crtc->pipe;
820
821 I915_WRITE(VLV_DDL(pipe),
822 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
823 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
824 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
825 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
826
Ville Syrjäläae801522015-03-05 21:19:49 +0200827 I915_WRITE(DSPFW1,
828 ((wm->sr.plane << DSPFW_SR_SHIFT) & DSPFW_SR_MASK) |
829 ((wm->pipe[PIPE_B].cursor << DSPFW_CURSORB_SHIFT) & DSPFW_CURSORB_MASK) |
830 ((wm->pipe[PIPE_B].primary << DSPFW_PLANEB_SHIFT) & DSPFW_PLANEB_MASK_VLV) |
831 ((wm->pipe[PIPE_A].primary << DSPFW_PLANEA_SHIFT) & DSPFW_PLANEA_MASK_VLV));
832 I915_WRITE(DSPFW2,
833 ((wm->pipe[PIPE_A].sprite[1] << DSPFW_SPRITEB_SHIFT) & DSPFW_SPRITEB_MASK_VLV) |
834 ((wm->pipe[PIPE_A].cursor << DSPFW_CURSORA_SHIFT) & DSPFW_CURSORA_MASK) |
835 ((wm->pipe[PIPE_A].sprite[0] << DSPFW_SPRITEA_SHIFT) & DSPFW_SPRITEA_MASK_VLV));
836 I915_WRITE(DSPFW3,
837 ((wm->sr.cursor << DSPFW_CURSOR_SR_SHIFT) & DSPFW_CURSOR_SR_MASK));
838
839 if (IS_CHERRYVIEW(dev_priv)) {
840 I915_WRITE(DSPFW7_CHV,
841 ((wm->pipe[PIPE_B].sprite[1] << DSPFW_SPRITED_SHIFT) & DSPFW_SPRITED_MASK) |
842 ((wm->pipe[PIPE_B].sprite[0] << DSPFW_SPRITEC_SHIFT) & DSPFW_SPRITEC_MASK));
843 I915_WRITE(DSPFW8_CHV,
844 ((wm->pipe[PIPE_C].sprite[1] << DSPFW_SPRITEF_SHIFT) & DSPFW_SPRITEF_MASK) |
845 ((wm->pipe[PIPE_C].sprite[0] << DSPFW_SPRITEE_SHIFT) & DSPFW_SPRITEE_MASK));
846 I915_WRITE(DSPFW9_CHV,
847 ((wm->pipe[PIPE_C].primary << DSPFW_PLANEC_SHIFT) & DSPFW_PLANEC_MASK) |
848 ((wm->pipe[PIPE_C].cursor << DSPFW_CURSORC_SHIFT) & DSPFW_CURSORC_MASK));
849 I915_WRITE(DSPHOWM,
850 (((wm->sr.plane >> 9) << DSPFW_SR_HI_SHIFT) & DSPFW_SR_HI_MASK) |
851 (((wm->pipe[PIPE_C].sprite[1] >> 8) << DSPFW_SPRITEF_HI_SHIFT) & DSPFW_SPRITEF_HI_MASK) |
852 (((wm->pipe[PIPE_C].sprite[0] >> 8) << DSPFW_SPRITEE_HI_SHIFT) & DSPFW_SPRITEE_HI_MASK) |
853 (((wm->pipe[PIPE_C].primary >> 8) << DSPFW_PLANEC_HI_SHIFT) & DSPFW_PLANEC_HI_MASK) |
854 (((wm->pipe[PIPE_B].sprite[1] >> 8) << DSPFW_SPRITED_HI_SHIFT) & DSPFW_SPRITED_HI_MASK) |
855 (((wm->pipe[PIPE_B].sprite[0] >> 8) << DSPFW_SPRITEC_HI_SHIFT) & DSPFW_SPRITEC_HI_MASK) |
856 (((wm->pipe[PIPE_B].primary >> 8) << DSPFW_PLANEB_HI_SHIFT) & DSPFW_PLANEB_HI_MASK) |
857 (((wm->pipe[PIPE_A].sprite[1] >> 8) << DSPFW_SPRITEB_HI_SHIFT) & DSPFW_SPRITEB_HI_MASK) |
858 (((wm->pipe[PIPE_A].sprite[0] >> 8) << DSPFW_SPRITEA_HI_SHIFT) & DSPFW_SPRITEA_HI_MASK) |
859 (((wm->pipe[PIPE_A].primary >> 8) << DSPFW_PLANEA_HI_SHIFT) & DSPFW_PLANEA_HI_MASK));
860 } else {
861 I915_WRITE(DSPFW7,
862 ((wm->pipe[PIPE_B].sprite[1] << DSPFW_SPRITED_SHIFT) & DSPFW_SPRITED_MASK) |
863 ((wm->pipe[PIPE_B].sprite[0] << DSPFW_SPRITEC_SHIFT) & DSPFW_SPRITEC_MASK));
864 I915_WRITE(DSPHOWM,
865 (((wm->sr.plane >> 9) << DSPFW_SR_HI_SHIFT) & DSPFW_SR_HI_MASK) |
866 (((wm->pipe[PIPE_B].sprite[1] >> 8) << DSPFW_SPRITED_HI_SHIFT) & DSPFW_SPRITED_HI_MASK) |
867 (((wm->pipe[PIPE_B].sprite[0] >> 8) << DSPFW_SPRITEC_HI_SHIFT) & DSPFW_SPRITEC_HI_MASK) |
868 (((wm->pipe[PIPE_B].primary >> 8) << DSPFW_PLANEB_HI_SHIFT) & DSPFW_PLANEB_HI_MASK) |
869 (((wm->pipe[PIPE_A].sprite[1] >> 8) << DSPFW_SPRITEB_HI_SHIFT) & DSPFW_SPRITEB_HI_MASK) |
870 (((wm->pipe[PIPE_A].sprite[0] >> 8) << DSPFW_SPRITEA_HI_SHIFT) & DSPFW_SPRITEA_HI_MASK) |
871 (((wm->pipe[PIPE_A].primary >> 8) << DSPFW_PLANEA_HI_SHIFT) & DSPFW_PLANEA_HI_MASK));
872 }
873
874 POSTING_READ(DSPFW1);
875
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200876 dev_priv->wm.vlv = *wm;
877}
878
Ville Syrjälä341c5262015-03-05 21:19:44 +0200879static uint8_t vlv_compute_drain_latency(struct drm_crtc *crtc,
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200880 struct drm_plane *plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300881{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700882 struct drm_device *dev = crtc->dev;
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
884 int entries, prec_mult, drain_latency, pixel_size;
885 int clock = intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä341c5262015-03-05 21:19:44 +0200886 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200888 /*
889 * FIXME the plane might have an fb
890 * but be invisible (eg. due to clipping)
891 */
892 if (!intel_crtc->active || !plane->state->fb)
893 return 0;
894
Gajanan Bhat0948c262014-08-07 01:58:24 +0530895 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200896 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200898 pixel_size = drm_format_plane_cpp(plane->state->fb->pixel_format, 0);
899
Gajanan Bhat0948c262014-08-07 01:58:24 +0530900 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200901 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300902
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530903 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200904
Ville Syrjälä341c5262015-03-05 21:19:44 +0200905 prec_mult = high_precision;
906 drain_latency = 64 * prec_mult * 4 / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300907
Ville Syrjälä341c5262015-03-05 21:19:44 +0200908 if (drain_latency > DRAIN_LATENCY_MASK) {
909 prec_mult /= 2;
910 drain_latency = 64 * prec_mult * 4 / entries;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200911 }
912
Ville Syrjälä341c5262015-03-05 21:19:44 +0200913 if (drain_latency > DRAIN_LATENCY_MASK)
914 drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300915
Ville Syrjälä341c5262015-03-05 21:19:44 +0200916 return drain_latency | (prec_mult == high_precision ?
917 DDL_PRECISION_HIGH : DDL_PRECISION_LOW);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300918}
919
Ville Syrjäläae801522015-03-05 21:19:49 +0200920static int vlv_compute_wm(struct intel_crtc *crtc,
921 struct intel_plane *plane,
922 int fifo_size)
923{
924 int clock, entries, pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300925
Ville Syrjäläae801522015-03-05 21:19:49 +0200926 /*
927 * FIXME the plane might have an fb
928 * but be invisible (eg. due to clipping)
929 */
930 if (!crtc->active || !plane->base.state->fb)
931 return 0;
932
933 pixel_size = drm_format_plane_cpp(plane->base.state->fb->pixel_format, 0);
934 clock = crtc->config->base.adjusted_mode.crtc_clock;
935
936 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
937
938 /*
939 * Set up the watermark such that we don't start issuing memory
940 * requests until we are within PND's max deadline value (256us).
941 * Idea being to be idle as long as possible while still taking
942 * advatange of PND's deadline scheduling. The limit of 8
943 * cachelines (used when the FIFO will anyway drain in less time
944 * than 256us) should match what we would be done if trickle
945 * feed were enabled.
946 */
947 return fifo_size - clamp(DIV_ROUND_UP(256 * entries, 64), 0, fifo_size - 8);
948}
949
950static bool vlv_compute_sr_wm(struct drm_device *dev,
951 struct vlv_wm_values *wm)
952{
953 struct drm_i915_private *dev_priv = to_i915(dev);
954 struct drm_crtc *crtc;
955 enum pipe pipe = INVALID_PIPE;
956 int num_planes = 0;
957 int fifo_size = 0;
958 struct intel_plane *plane;
959
960 wm->sr.cursor = wm->sr.plane = 0;
961
962 crtc = single_enabled_crtc(dev);
963 /* maxfifo not supported on pipe C */
964 if (crtc && to_intel_crtc(crtc)->pipe != PIPE_C) {
965 pipe = to_intel_crtc(crtc)->pipe;
966 num_planes = !!wm->pipe[pipe].primary +
967 !!wm->pipe[pipe].sprite[0] +
968 !!wm->pipe[pipe].sprite[1];
969 fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
970 }
971
972 if (fifo_size == 0 || num_planes > 1)
973 return false;
974
975 wm->sr.cursor = vlv_compute_wm(to_intel_crtc(crtc),
976 to_intel_plane(crtc->cursor), 0x3f);
977
978 list_for_each_entry(plane, &dev->mode_config.plane_list, base.head) {
979 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
980 continue;
981
982 if (plane->pipe != pipe)
983 continue;
984
985 wm->sr.plane = vlv_compute_wm(to_intel_crtc(crtc),
986 plane, fifo_size);
987 if (wm->sr.plane != 0)
988 break;
989 }
990
991 return true;
992}
993
994static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300995{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700996 struct drm_device *dev = crtc->dev;
997 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gajanan Bhat0948c262014-08-07 01:58:24 +0530999 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläae801522015-03-05 21:19:49 +02001000 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001001 struct vlv_wm_values wm = dev_priv->wm.vlv;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001002
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001003 wm.ddl[pipe].primary = vlv_compute_drain_latency(crtc, crtc->primary);
Ville Syrjäläae801522015-03-05 21:19:49 +02001004 wm.pipe[pipe].primary = vlv_compute_wm(intel_crtc,
1005 to_intel_plane(crtc->primary),
1006 vlv_get_fifo_size(dev, pipe, 0));
1007
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001008 wm.ddl[pipe].cursor = vlv_compute_drain_latency(crtc, crtc->cursor);
Ville Syrjäläae801522015-03-05 21:19:49 +02001009 wm.pipe[pipe].cursor = vlv_compute_wm(intel_crtc,
1010 to_intel_plane(crtc->cursor),
1011 0x3f);
1012
1013 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1014
1015 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1016 return;
1017
1018 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1019 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1020 wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1021 wm.sr.plane, wm.sr.cursor);
1022
1023 if (!cxsr_enabled)
1024 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301025
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001026 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001027
1028 if (cxsr_enabled)
1029 intel_set_memory_cxsr(dev_priv, true);
1030}
1031
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301032static void valleyview_update_sprite_wm(struct drm_plane *plane,
1033 struct drm_crtc *crtc,
1034 uint32_t sprite_width,
1035 uint32_t sprite_height,
1036 int pixel_size,
1037 bool enabled, bool scaled)
1038{
1039 struct drm_device *dev = crtc->dev;
1040 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1042 enum pipe pipe = intel_crtc->pipe;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301043 int sprite = to_intel_plane(plane)->plane;
Ville Syrjäläae801522015-03-05 21:19:49 +02001044 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001045 struct vlv_wm_values wm = dev_priv->wm.vlv;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301046
Ville Syrjäläae801522015-03-05 21:19:49 +02001047 if (enabled) {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001048 wm.ddl[pipe].sprite[sprite] =
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001049 vlv_compute_drain_latency(crtc, plane);
Ville Syrjäläae801522015-03-05 21:19:49 +02001050
1051 wm.pipe[pipe].sprite[sprite] =
1052 vlv_compute_wm(intel_crtc,
1053 to_intel_plane(plane),
1054 vlv_get_fifo_size(dev, pipe, sprite+1));
1055 } else {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001056 wm.ddl[pipe].sprite[sprite] = 0;
Ville Syrjäläae801522015-03-05 21:19:49 +02001057 wm.pipe[pipe].sprite[sprite] = 0;
1058 }
1059
1060 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1061
1062 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1063 return;
1064
1065 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: sprite %c=%d, "
1066 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1067 sprite_name(pipe, sprite),
1068 wm.pipe[pipe].sprite[sprite],
1069 wm.sr.plane, wm.sr.cursor);
1070
1071 if (!cxsr_enabled)
1072 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301073
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001074 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjäläae801522015-03-05 21:19:49 +02001075
1076 if (cxsr_enabled)
1077 intel_set_memory_cxsr(dev_priv, true);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301078}
1079
Ville Syrjäläae801522015-03-05 21:19:49 +02001080#define single_plane_enabled(mask) is_power_of_2(mask)
1081
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001082static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001083{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001084 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001085 static const int sr_latency_ns = 12000;
1086 struct drm_i915_private *dev_priv = dev->dev_private;
1087 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1088 int plane_sr, cursor_sr;
1089 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001090 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001091
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001092 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001093 &g4x_wm_info, pessimal_latency_ns,
1094 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001095 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001096 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001097
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001098 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001099 &g4x_wm_info, pessimal_latency_ns,
1100 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001101 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001102 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001103
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001104 if (single_plane_enabled(enabled) &&
1105 g4x_compute_srwm(dev, ffs(enabled) - 1,
1106 sr_latency_ns,
1107 &g4x_wm_info,
1108 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001109 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001110 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001111 } else {
Imre Deak98584252014-06-13 14:54:20 +03001112 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001113 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001114 plane_sr = cursor_sr = 0;
1115 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001116
Ville Syrjäläa5043452014-06-28 02:04:18 +03001117 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1118 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001119 planea_wm, cursora_wm,
1120 planeb_wm, cursorb_wm,
1121 plane_sr, cursor_sr);
1122
1123 I915_WRITE(DSPFW1,
1124 (plane_sr << DSPFW_SR_SHIFT) |
1125 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1126 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001127 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001128 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001129 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001130 (cursora_wm << DSPFW_CURSORA_SHIFT));
1131 /* HPLL off in SR has some issues on G4x... disable it */
1132 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001133 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001134 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001135
1136 if (cxsr_enabled)
1137 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001138}
1139
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001140static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001141{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001142 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001143 struct drm_i915_private *dev_priv = dev->dev_private;
1144 struct drm_crtc *crtc;
1145 int srwm = 1;
1146 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001147 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001148
1149 /* Calc sr entries for one plane configs */
1150 crtc = single_enabled_crtc(dev);
1151 if (crtc) {
1152 /* self-refresh has much higher latency */
1153 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001154 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001155 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001156 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001157 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001158 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001159 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001160 unsigned long line_time_us;
1161 int entries;
1162
Ville Syrjälä922044c2014-02-14 14:18:57 +02001163 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001164
1165 /* Use ns/us then divide to preserve precision */
1166 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1167 pixel_size * hdisplay;
1168 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1169 srwm = I965_FIFO_SIZE - entries;
1170 if (srwm < 0)
1171 srwm = 1;
1172 srwm &= 0x1ff;
1173 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1174 entries, srwm);
1175
1176 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001177 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001178 entries = DIV_ROUND_UP(entries,
1179 i965_cursor_wm_info.cacheline_size);
1180 cursor_sr = i965_cursor_wm_info.fifo_size -
1181 (entries + i965_cursor_wm_info.guard_size);
1182
1183 if (cursor_sr > i965_cursor_wm_info.max_wm)
1184 cursor_sr = i965_cursor_wm_info.max_wm;
1185
1186 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1187 "cursor %d\n", srwm, cursor_sr);
1188
Imre Deak98584252014-06-13 14:54:20 +03001189 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001190 } else {
Imre Deak98584252014-06-13 14:54:20 +03001191 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001192 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001193 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001194 }
1195
1196 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1197 srwm);
1198
1199 /* 965 has limitations... */
1200 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001201 (8 << DSPFW_CURSORB_SHIFT) |
1202 (8 << DSPFW_PLANEB_SHIFT) |
1203 (8 << DSPFW_PLANEA_SHIFT));
1204 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1205 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001206 /* update cursor SR watermark */
1207 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001208
1209 if (cxsr_enabled)
1210 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001211}
1212
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001213static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001214{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001215 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001216 struct drm_i915_private *dev_priv = dev->dev_private;
1217 const struct intel_watermark_params *wm_info;
1218 uint32_t fwater_lo;
1219 uint32_t fwater_hi;
1220 int cwm, srwm = 1;
1221 int fifo_size;
1222 int planea_wm, planeb_wm;
1223 struct drm_crtc *crtc, *enabled = NULL;
1224
1225 if (IS_I945GM(dev))
1226 wm_info = &i945_wm_info;
1227 else if (!IS_GEN2(dev))
1228 wm_info = &i915_wm_info;
1229 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001230 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001231
1232 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1233 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001234 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001235 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001236 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001237 if (IS_GEN2(dev))
1238 cpp = 4;
1239
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001240 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001241 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001242 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001243 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001244 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001245 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001246 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001247 if (planea_wm > (long)wm_info->max_wm)
1248 planea_wm = wm_info->max_wm;
1249 }
1250
1251 if (IS_GEN2(dev))
1252 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001253
1254 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1255 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001256 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001257 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001258 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001259 if (IS_GEN2(dev))
1260 cpp = 4;
1261
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001262 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001263 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001264 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001265 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001266 if (enabled == NULL)
1267 enabled = crtc;
1268 else
1269 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001270 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001271 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001272 if (planeb_wm > (long)wm_info->max_wm)
1273 planeb_wm = wm_info->max_wm;
1274 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001275
1276 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1277
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001278 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001279 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001280
Matt Roper59bea882015-02-27 10:12:01 -08001281 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001282
1283 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001284 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001285 enabled = NULL;
1286 }
1287
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001288 /*
1289 * Overlay gets an aggressive default since video jitter is bad.
1290 */
1291 cwm = 2;
1292
1293 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001294 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001295
1296 /* Calc sr entries for one plane configs */
1297 if (HAS_FW_BLC(dev) && enabled) {
1298 /* self-refresh has much higher latency */
1299 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001300 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001301 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001302 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001303 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001304 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001305 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001306 unsigned long line_time_us;
1307 int entries;
1308
Ville Syrjälä922044c2014-02-14 14:18:57 +02001309 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001310
1311 /* Use ns/us then divide to preserve precision */
1312 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1313 pixel_size * hdisplay;
1314 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1315 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1316 srwm = wm_info->fifo_size - entries;
1317 if (srwm < 0)
1318 srwm = 1;
1319
1320 if (IS_I945G(dev) || IS_I945GM(dev))
1321 I915_WRITE(FW_BLC_SELF,
1322 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1323 else if (IS_I915GM(dev))
1324 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1325 }
1326
1327 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1328 planea_wm, planeb_wm, cwm, srwm);
1329
1330 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1331 fwater_hi = (cwm & 0x1f);
1332
1333 /* Set request length to 8 cachelines per fetch */
1334 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1335 fwater_hi = fwater_hi | (1 << 8);
1336
1337 I915_WRITE(FW_BLC, fwater_lo);
1338 I915_WRITE(FW_BLC2, fwater_hi);
1339
Imre Deak5209b1f2014-07-01 12:36:17 +03001340 if (enabled)
1341 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001342}
1343
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001344static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001345{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001346 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001347 struct drm_i915_private *dev_priv = dev->dev_private;
1348 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001349 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001350 uint32_t fwater_lo;
1351 int planea_wm;
1352
1353 crtc = single_enabled_crtc(dev);
1354 if (crtc == NULL)
1355 return;
1356
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001357 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001358 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001359 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001360 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001361 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001362 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1363 fwater_lo |= (3<<8) | planea_wm;
1364
1365 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1366
1367 I915_WRITE(FW_BLC, fwater_lo);
1368}
1369
Ville Syrjälä36587292013-07-05 11:57:16 +03001370static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1371 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001372{
1373 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001374 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001375
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001376 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001377
1378 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1379 * adjust the pixel_rate here. */
1380
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001381 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001382 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001383 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001384
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001385 pipe_w = intel_crtc->config->pipe_src_w;
1386 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001387 pfit_w = (pfit_size >> 16) & 0xFFFF;
1388 pfit_h = pfit_size & 0xFFFF;
1389 if (pipe_w < pfit_w)
1390 pipe_w = pfit_w;
1391 if (pipe_h < pfit_h)
1392 pipe_h = pfit_h;
1393
1394 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1395 pfit_w * pfit_h);
1396 }
1397
1398 return pixel_rate;
1399}
1400
Ville Syrjälä37126462013-08-01 16:18:55 +03001401/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001402static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001403 uint32_t latency)
1404{
1405 uint64_t ret;
1406
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001407 if (WARN(latency == 0, "Latency value missing\n"))
1408 return UINT_MAX;
1409
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001410 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1411 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1412
1413 return ret;
1414}
1415
Ville Syrjälä37126462013-08-01 16:18:55 +03001416/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001417static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001418 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1419 uint32_t latency)
1420{
1421 uint32_t ret;
1422
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001423 if (WARN(latency == 0, "Latency value missing\n"))
1424 return UINT_MAX;
1425
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001426 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1427 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1428 ret = DIV_ROUND_UP(ret, 64) + 2;
1429 return ret;
1430}
1431
Ville Syrjälä23297042013-07-05 11:57:17 +03001432static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001433 uint8_t bytes_per_pixel)
1434{
1435 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1436}
1437
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001438struct skl_pipe_wm_parameters {
1439 bool active;
1440 uint32_t pipe_htotal;
1441 uint32_t pixel_rate; /* in KHz */
1442 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1443 struct intel_plane_wm_parameters cursor;
1444};
1445
Imre Deak820c1982013-12-17 14:46:36 +02001446struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001447 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001448 uint32_t pipe_htotal;
1449 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001450 struct intel_plane_wm_parameters pri;
1451 struct intel_plane_wm_parameters spr;
1452 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001453};
1454
Imre Deak820c1982013-12-17 14:46:36 +02001455struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001456 uint16_t pri;
1457 uint16_t spr;
1458 uint16_t cur;
1459 uint16_t fbc;
1460};
1461
Ville Syrjälä240264f2013-08-07 13:29:12 +03001462/* used in computing the new watermarks state */
1463struct intel_wm_config {
1464 unsigned int num_pipes_active;
1465 bool sprites_enabled;
1466 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001467};
1468
Ville Syrjälä37126462013-08-01 16:18:55 +03001469/*
1470 * For both WM_PIPE and WM_LP.
1471 * mem_value must be in 0.1us units.
1472 */
Imre Deak820c1982013-12-17 14:46:36 +02001473static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001474 uint32_t mem_value,
1475 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001476{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001477 uint32_t method1, method2;
1478
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001479 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001480 return 0;
1481
Ville Syrjälä23297042013-07-05 11:57:17 +03001482 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001483 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001484 mem_value);
1485
1486 if (!is_lp)
1487 return method1;
1488
Ville Syrjälä23297042013-07-05 11:57:17 +03001489 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001490 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001491 params->pri.horiz_pixels,
1492 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001493 mem_value);
1494
1495 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001496}
1497
Ville Syrjälä37126462013-08-01 16:18:55 +03001498/*
1499 * For both WM_PIPE and WM_LP.
1500 * mem_value must be in 0.1us units.
1501 */
Imre Deak820c1982013-12-17 14:46:36 +02001502static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001503 uint32_t mem_value)
1504{
1505 uint32_t method1, method2;
1506
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001507 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001508 return 0;
1509
Ville Syrjälä23297042013-07-05 11:57:17 +03001510 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001511 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001512 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001513 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001514 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001515 params->spr.horiz_pixels,
1516 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001517 mem_value);
1518 return min(method1, method2);
1519}
1520
Ville Syrjälä37126462013-08-01 16:18:55 +03001521/*
1522 * For both WM_PIPE and WM_LP.
1523 * mem_value must be in 0.1us units.
1524 */
Imre Deak820c1982013-12-17 14:46:36 +02001525static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001526 uint32_t mem_value)
1527{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001528 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001529 return 0;
1530
Ville Syrjälä23297042013-07-05 11:57:17 +03001531 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001532 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001533 params->cur.horiz_pixels,
1534 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001535 mem_value);
1536}
1537
Paulo Zanonicca32e92013-05-31 11:45:06 -03001538/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001539static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001540 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001541{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001542 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001543 return 0;
1544
Ville Syrjälä23297042013-07-05 11:57:17 +03001545 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001546 params->pri.horiz_pixels,
1547 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001548}
1549
Ville Syrjälä158ae642013-08-07 13:28:19 +03001550static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1551{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001552 if (INTEL_INFO(dev)->gen >= 8)
1553 return 3072;
1554 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001555 return 768;
1556 else
1557 return 512;
1558}
1559
Ville Syrjälä4e975082014-03-07 18:32:11 +02001560static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1561 int level, bool is_sprite)
1562{
1563 if (INTEL_INFO(dev)->gen >= 8)
1564 /* BDW primary/sprite plane watermarks */
1565 return level == 0 ? 255 : 2047;
1566 else if (INTEL_INFO(dev)->gen >= 7)
1567 /* IVB/HSW primary/sprite plane watermarks */
1568 return level == 0 ? 127 : 1023;
1569 else if (!is_sprite)
1570 /* ILK/SNB primary plane watermarks */
1571 return level == 0 ? 127 : 511;
1572 else
1573 /* ILK/SNB sprite plane watermarks */
1574 return level == 0 ? 63 : 255;
1575}
1576
1577static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1578 int level)
1579{
1580 if (INTEL_INFO(dev)->gen >= 7)
1581 return level == 0 ? 63 : 255;
1582 else
1583 return level == 0 ? 31 : 63;
1584}
1585
1586static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1587{
1588 if (INTEL_INFO(dev)->gen >= 8)
1589 return 31;
1590 else
1591 return 15;
1592}
1593
Ville Syrjälä158ae642013-08-07 13:28:19 +03001594/* Calculate the maximum primary/sprite plane watermark */
1595static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1596 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001597 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001598 enum intel_ddb_partitioning ddb_partitioning,
1599 bool is_sprite)
1600{
1601 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001602
1603 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001604 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001605 return 0;
1606
1607 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001608 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001609 fifo_size /= INTEL_INFO(dev)->num_pipes;
1610
1611 /*
1612 * For some reason the non self refresh
1613 * FIFO size is only half of the self
1614 * refresh FIFO size on ILK/SNB.
1615 */
1616 if (INTEL_INFO(dev)->gen <= 6)
1617 fifo_size /= 2;
1618 }
1619
Ville Syrjälä240264f2013-08-07 13:29:12 +03001620 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001621 /* level 0 is always calculated with 1:1 split */
1622 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1623 if (is_sprite)
1624 fifo_size *= 5;
1625 fifo_size /= 6;
1626 } else {
1627 fifo_size /= 2;
1628 }
1629 }
1630
1631 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001632 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001633}
1634
1635/* Calculate the maximum cursor plane watermark */
1636static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001637 int level,
1638 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001639{
1640 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001641 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001642 return 64;
1643
1644 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001645 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001646}
1647
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001648static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001649 int level,
1650 const struct intel_wm_config *config,
1651 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001652 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001653{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001654 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1655 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1656 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001657 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001658}
1659
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001660static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1661 int level,
1662 struct ilk_wm_maximums *max)
1663{
1664 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1665 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1666 max->cur = ilk_cursor_wm_reg_max(dev, level);
1667 max->fbc = ilk_fbc_wm_reg_max(dev);
1668}
1669
Ville Syrjäläd9395652013-10-09 19:18:10 +03001670static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001671 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001672 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001673{
1674 bool ret;
1675
1676 /* already determined to be invalid? */
1677 if (!result->enable)
1678 return false;
1679
1680 result->enable = result->pri_val <= max->pri &&
1681 result->spr_val <= max->spr &&
1682 result->cur_val <= max->cur;
1683
1684 ret = result->enable;
1685
1686 /*
1687 * HACK until we can pre-compute everything,
1688 * and thus fail gracefully if LP0 watermarks
1689 * are exceeded...
1690 */
1691 if (level == 0 && !result->enable) {
1692 if (result->pri_val > max->pri)
1693 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1694 level, result->pri_val, max->pri);
1695 if (result->spr_val > max->spr)
1696 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1697 level, result->spr_val, max->spr);
1698 if (result->cur_val > max->cur)
1699 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1700 level, result->cur_val, max->cur);
1701
1702 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1703 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1704 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1705 result->enable = true;
1706 }
1707
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001708 return ret;
1709}
1710
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001711static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001712 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001713 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001714 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001715{
1716 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1717 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1718 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1719
1720 /* WM1+ latency values stored in 0.5us units */
1721 if (level > 0) {
1722 pri_latency *= 5;
1723 spr_latency *= 5;
1724 cur_latency *= 5;
1725 }
1726
1727 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1728 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1729 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1730 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1731 result->enable = true;
1732}
1733
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001734static uint32_t
1735hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001736{
1737 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001739 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001740 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001741
Matt Roper3ef00282015-03-09 10:19:24 -07001742 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001743 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001744
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001745 /* The WM are computed with base on how long it takes to fill a single
1746 * row at the given clock rate, multiplied by 8.
1747 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001748 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1749 mode->crtc_clock);
1750 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001751 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001752
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001753 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1754 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001755}
1756
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001757static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001758{
1759 struct drm_i915_private *dev_priv = dev->dev_private;
1760
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001761 if (IS_GEN9(dev)) {
1762 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001763 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001764 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001765
1766 /* read the first set of memory latencies[0:3] */
1767 val = 0; /* data0 to be programmed to 0 for first set */
1768 mutex_lock(&dev_priv->rps.hw_lock);
1769 ret = sandybridge_pcode_read(dev_priv,
1770 GEN9_PCODE_READ_MEM_LATENCY,
1771 &val);
1772 mutex_unlock(&dev_priv->rps.hw_lock);
1773
1774 if (ret) {
1775 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1776 return;
1777 }
1778
1779 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1780 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1781 GEN9_MEM_LATENCY_LEVEL_MASK;
1782 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1783 GEN9_MEM_LATENCY_LEVEL_MASK;
1784 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1785 GEN9_MEM_LATENCY_LEVEL_MASK;
1786
1787 /* read the second set of memory latencies[4:7] */
1788 val = 1; /* data0 to be programmed to 1 for second set */
1789 mutex_lock(&dev_priv->rps.hw_lock);
1790 ret = sandybridge_pcode_read(dev_priv,
1791 GEN9_PCODE_READ_MEM_LATENCY,
1792 &val);
1793 mutex_unlock(&dev_priv->rps.hw_lock);
1794 if (ret) {
1795 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1796 return;
1797 }
1798
1799 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1800 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1801 GEN9_MEM_LATENCY_LEVEL_MASK;
1802 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1803 GEN9_MEM_LATENCY_LEVEL_MASK;
1804 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1805 GEN9_MEM_LATENCY_LEVEL_MASK;
1806
Vandana Kannan367294b2014-11-04 17:06:46 +00001807 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001808 * WaWmMemoryReadLatency:skl
1809 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001810 * punit doesn't take into account the read latency so we need
1811 * to add 2us to the various latency levels we retrieve from
1812 * the punit.
1813 * - W0 is a bit special in that it's the only level that
1814 * can't be disabled if we want to have display working, so
1815 * we always add 2us there.
1816 * - For levels >=1, punit returns 0us latency when they are
1817 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001818 *
1819 * Additionally, if a level n (n > 1) has a 0us latency, all
1820 * levels m (m >= n) need to be disabled. We make sure to
1821 * sanitize the values out of the punit to satisfy this
1822 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001823 */
1824 wm[0] += 2;
1825 for (level = 1; level <= max_level; level++)
1826 if (wm[level] != 0)
1827 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001828 else {
1829 for (i = level + 1; i <= max_level; i++)
1830 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001831
Vandana Kannan4f947382014-11-04 17:06:47 +00001832 break;
1833 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001834 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001835 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1836
1837 wm[0] = (sskpd >> 56) & 0xFF;
1838 if (wm[0] == 0)
1839 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001840 wm[1] = (sskpd >> 4) & 0xFF;
1841 wm[2] = (sskpd >> 12) & 0xFF;
1842 wm[3] = (sskpd >> 20) & 0x1FF;
1843 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001844 } else if (INTEL_INFO(dev)->gen >= 6) {
1845 uint32_t sskpd = I915_READ(MCH_SSKPD);
1846
1847 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1848 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1849 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1850 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001851 } else if (INTEL_INFO(dev)->gen >= 5) {
1852 uint32_t mltr = I915_READ(MLTR_ILK);
1853
1854 /* ILK primary LP0 latency is 700 ns */
1855 wm[0] = 7;
1856 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1857 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001858 }
1859}
1860
Ville Syrjälä53615a52013-08-01 16:18:50 +03001861static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1862{
1863 /* ILK sprite LP0 latency is 1300 ns */
1864 if (INTEL_INFO(dev)->gen == 5)
1865 wm[0] = 13;
1866}
1867
1868static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1869{
1870 /* ILK cursor LP0 latency is 1300 ns */
1871 if (INTEL_INFO(dev)->gen == 5)
1872 wm[0] = 13;
1873
1874 /* WaDoubleCursorLP3Latency:ivb */
1875 if (IS_IVYBRIDGE(dev))
1876 wm[3] *= 2;
1877}
1878
Damien Lespiau546c81f2014-05-13 15:30:26 +01001879int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001880{
1881 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001882 if (IS_GEN9(dev))
1883 return 7;
1884 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001885 return 4;
1886 else if (INTEL_INFO(dev)->gen >= 6)
1887 return 3;
1888 else
1889 return 2;
1890}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001891
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001892static void intel_print_wm_latency(struct drm_device *dev,
1893 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001894 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001895{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001896 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001897
1898 for (level = 0; level <= max_level; level++) {
1899 unsigned int latency = wm[level];
1900
1901 if (latency == 0) {
1902 DRM_ERROR("%s WM%d latency not provided\n",
1903 name, level);
1904 continue;
1905 }
1906
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001907 /*
1908 * - latencies are in us on gen9.
1909 * - before then, WM1+ latency values are in 0.5us units
1910 */
1911 if (IS_GEN9(dev))
1912 latency *= 10;
1913 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001914 latency *= 5;
1915
1916 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1917 name, level, wm[level],
1918 latency / 10, latency % 10);
1919 }
1920}
1921
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001922static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1923 uint16_t wm[5], uint16_t min)
1924{
1925 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1926
1927 if (wm[0] >= min)
1928 return false;
1929
1930 wm[0] = max(wm[0], min);
1931 for (level = 1; level <= max_level; level++)
1932 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1933
1934 return true;
1935}
1936
1937static void snb_wm_latency_quirk(struct drm_device *dev)
1938{
1939 struct drm_i915_private *dev_priv = dev->dev_private;
1940 bool changed;
1941
1942 /*
1943 * The BIOS provided WM memory latency values are often
1944 * inadequate for high resolution displays. Adjust them.
1945 */
1946 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1947 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1948 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1949
1950 if (!changed)
1951 return;
1952
1953 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1954 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1955 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1956 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1957}
1958
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001959static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001960{
1961 struct drm_i915_private *dev_priv = dev->dev_private;
1962
1963 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1964
1965 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1966 sizeof(dev_priv->wm.pri_latency));
1967 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1968 sizeof(dev_priv->wm.pri_latency));
1969
1970 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1971 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001972
1973 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1974 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1975 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001976
1977 if (IS_GEN6(dev))
1978 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001979}
1980
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001981static void skl_setup_wm_latency(struct drm_device *dev)
1982{
1983 struct drm_i915_private *dev_priv = dev->dev_private;
1984
1985 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1986 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1987}
1988
Imre Deak820c1982013-12-17 14:46:36 +02001989static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001990 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001991{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001992 struct drm_device *dev = crtc->dev;
1993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1994 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001995 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001996
Matt Roper3ef00282015-03-09 10:19:24 -07001997 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001998 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001999
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002000 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002001 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002002 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roper59bea882015-02-27 10:12:01 -08002003 p->pri.bytes_per_pixel = crtc->primary->state->fb->bits_per_pixel / 8;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002004 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002005 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08002006 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002007 /* TODO: for now, assume primary and cursor planes are always enabled. */
2008 p->pri.enabled = true;
2009 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002010
Matt Roperaf2b6532014-04-01 15:22:32 -07002011 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002012 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002013
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002014 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002015 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002016 break;
2017 }
2018 }
2019}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002020
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002021static void ilk_compute_wm_config(struct drm_device *dev,
2022 struct intel_wm_config *config)
2023{
2024 struct intel_crtc *intel_crtc;
2025
2026 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002027 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002028 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2029
2030 if (!wm->pipe_enabled)
2031 continue;
2032
2033 config->sprites_enabled |= wm->sprites_enabled;
2034 config->sprites_scaled |= wm->sprites_scaled;
2035 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002036 }
2037}
2038
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002039/* Compute new watermarks for the pipe */
2040static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002041 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002042 struct intel_pipe_wm *pipe_wm)
2043{
2044 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002045 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002046 int level, max_level = ilk_wm_max_level(dev);
2047 /* LP0 watermark maximums depend on this pipe alone */
2048 struct intel_wm_config config = {
2049 .num_pipes_active = 1,
2050 .sprites_enabled = params->spr.enabled,
2051 .sprites_scaled = params->spr.scaled,
2052 };
Imre Deak820c1982013-12-17 14:46:36 +02002053 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002054
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002055 pipe_wm->pipe_enabled = params->active;
2056 pipe_wm->sprites_enabled = params->spr.enabled;
2057 pipe_wm->sprites_scaled = params->spr.scaled;
2058
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002059 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2060 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2061 max_level = 1;
2062
2063 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2064 if (params->spr.scaled)
2065 max_level = 0;
2066
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002067 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002068
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002069 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002070 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002071
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002072 /* LP0 watermarks always use 1/2 DDB partitioning */
2073 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2074
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002075 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002076 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2077 return false;
2078
2079 ilk_compute_wm_reg_maximums(dev, 1, &max);
2080
2081 for (level = 1; level <= max_level; level++) {
2082 struct intel_wm_level wm = {};
2083
2084 ilk_compute_wm_level(dev_priv, level, params, &wm);
2085
2086 /*
2087 * Disable any watermark level that exceeds the
2088 * register maximums since such watermarks are
2089 * always invalid.
2090 */
2091 if (!ilk_validate_wm_level(level, &max, &wm))
2092 break;
2093
2094 pipe_wm->wm[level] = wm;
2095 }
2096
2097 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002098}
2099
2100/*
2101 * Merge the watermarks from all active pipes for a specific level.
2102 */
2103static void ilk_merge_wm_level(struct drm_device *dev,
2104 int level,
2105 struct intel_wm_level *ret_wm)
2106{
2107 const struct intel_crtc *intel_crtc;
2108
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002109 ret_wm->enable = true;
2110
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002111 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002112 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2113 const struct intel_wm_level *wm = &active->wm[level];
2114
2115 if (!active->pipe_enabled)
2116 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002117
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002118 /*
2119 * The watermark values may have been used in the past,
2120 * so we must maintain them in the registers for some
2121 * time even if the level is now disabled.
2122 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002123 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002124 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002125
2126 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2127 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2128 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2129 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2130 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002131}
2132
2133/*
2134 * Merge all low power watermarks for all active pipes.
2135 */
2136static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002137 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002138 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002139 struct intel_pipe_wm *merged)
2140{
2141 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002142 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002143
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002144 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2145 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2146 config->num_pipes_active > 1)
2147 return;
2148
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002149 /* ILK: FBC WM must be disabled always */
2150 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002151
2152 /* merge each WM1+ level */
2153 for (level = 1; level <= max_level; level++) {
2154 struct intel_wm_level *wm = &merged->wm[level];
2155
2156 ilk_merge_wm_level(dev, level, wm);
2157
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002158 if (level > last_enabled_level)
2159 wm->enable = false;
2160 else if (!ilk_validate_wm_level(level, max, wm))
2161 /* make sure all following levels get disabled */
2162 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002163
2164 /*
2165 * The spec says it is preferred to disable
2166 * FBC WMs instead of disabling a WM level.
2167 */
2168 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002169 if (wm->enable)
2170 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002171 wm->fbc_val = 0;
2172 }
2173 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002174
2175 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2176 /*
2177 * FIXME this is racy. FBC might get enabled later.
2178 * What we should check here is whether FBC can be
2179 * enabled sometime later.
2180 */
2181 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2182 for (level = 2; level <= max_level; level++) {
2183 struct intel_wm_level *wm = &merged->wm[level];
2184
2185 wm->enable = false;
2186 }
2187 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002188}
2189
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002190static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2191{
2192 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2193 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2194}
2195
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002196/* The value we need to program into the WM_LPx latency field */
2197static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2198{
2199 struct drm_i915_private *dev_priv = dev->dev_private;
2200
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002201 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002202 return 2 * level;
2203 else
2204 return dev_priv->wm.pri_latency[level];
2205}
2206
Imre Deak820c1982013-12-17 14:46:36 +02002207static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002208 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002209 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002210 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002211{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002212 struct intel_crtc *intel_crtc;
2213 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002214
Ville Syrjälä0362c782013-10-09 19:17:57 +03002215 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002216 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002217
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002218 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002219 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002220 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002221
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002222 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002223
Ville Syrjälä0362c782013-10-09 19:17:57 +03002224 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002225
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002226 /*
2227 * Maintain the watermark values even if the level is
2228 * disabled. Doing otherwise could cause underruns.
2229 */
2230 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002231 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002232 (r->pri_val << WM1_LP_SR_SHIFT) |
2233 r->cur_val;
2234
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002235 if (r->enable)
2236 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2237
Ville Syrjälä416f4722013-11-02 21:07:46 -07002238 if (INTEL_INFO(dev)->gen >= 8)
2239 results->wm_lp[wm_lp - 1] |=
2240 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2241 else
2242 results->wm_lp[wm_lp - 1] |=
2243 r->fbc_val << WM1_LP_FBC_SHIFT;
2244
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002245 /*
2246 * Always set WM1S_LP_EN when spr_val != 0, even if the
2247 * level is disabled. Doing otherwise could cause underruns.
2248 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002249 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2250 WARN_ON(wm_lp != 1);
2251 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2252 } else
2253 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002254 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002255
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002256 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002257 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002258 enum pipe pipe = intel_crtc->pipe;
2259 const struct intel_wm_level *r =
2260 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002261
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002262 if (WARN_ON(!r->enable))
2263 continue;
2264
2265 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2266
2267 results->wm_pipe[pipe] =
2268 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2269 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2270 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002271 }
2272}
2273
Paulo Zanoni861f3382013-05-31 10:19:21 -03002274/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2275 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002276static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002277 struct intel_pipe_wm *r1,
2278 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002279{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002280 int level, max_level = ilk_wm_max_level(dev);
2281 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002282
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002283 for (level = 1; level <= max_level; level++) {
2284 if (r1->wm[level].enable)
2285 level1 = level;
2286 if (r2->wm[level].enable)
2287 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002288 }
2289
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002290 if (level1 == level2) {
2291 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002292 return r2;
2293 else
2294 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002295 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002296 return r1;
2297 } else {
2298 return r2;
2299 }
2300}
2301
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002302/* dirty bits used to track which watermarks need changes */
2303#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2304#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2305#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2306#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2307#define WM_DIRTY_FBC (1 << 24)
2308#define WM_DIRTY_DDB (1 << 25)
2309
Damien Lespiau055e3932014-08-18 13:49:10 +01002310static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002311 const struct ilk_wm_values *old,
2312 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002313{
2314 unsigned int dirty = 0;
2315 enum pipe pipe;
2316 int wm_lp;
2317
Damien Lespiau055e3932014-08-18 13:49:10 +01002318 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002319 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2320 dirty |= WM_DIRTY_LINETIME(pipe);
2321 /* Must disable LP1+ watermarks too */
2322 dirty |= WM_DIRTY_LP_ALL;
2323 }
2324
2325 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2326 dirty |= WM_DIRTY_PIPE(pipe);
2327 /* Must disable LP1+ watermarks too */
2328 dirty |= WM_DIRTY_LP_ALL;
2329 }
2330 }
2331
2332 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2333 dirty |= WM_DIRTY_FBC;
2334 /* Must disable LP1+ watermarks too */
2335 dirty |= WM_DIRTY_LP_ALL;
2336 }
2337
2338 if (old->partitioning != new->partitioning) {
2339 dirty |= WM_DIRTY_DDB;
2340 /* Must disable LP1+ watermarks too */
2341 dirty |= WM_DIRTY_LP_ALL;
2342 }
2343
2344 /* LP1+ watermarks already deemed dirty, no need to continue */
2345 if (dirty & WM_DIRTY_LP_ALL)
2346 return dirty;
2347
2348 /* Find the lowest numbered LP1+ watermark in need of an update... */
2349 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2350 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2351 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2352 break;
2353 }
2354
2355 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2356 for (; wm_lp <= 3; wm_lp++)
2357 dirty |= WM_DIRTY_LP(wm_lp);
2358
2359 return dirty;
2360}
2361
Ville Syrjälä8553c182013-12-05 15:51:39 +02002362static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2363 unsigned int dirty)
2364{
Imre Deak820c1982013-12-17 14:46:36 +02002365 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002366 bool changed = false;
2367
2368 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2369 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2370 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2371 changed = true;
2372 }
2373 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2374 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2375 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2376 changed = true;
2377 }
2378 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2379 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2380 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2381 changed = true;
2382 }
2383
2384 /*
2385 * Don't touch WM1S_LP_EN here.
2386 * Doing so could cause underruns.
2387 */
2388
2389 return changed;
2390}
2391
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002392/*
2393 * The spec says we shouldn't write when we don't need, because every write
2394 * causes WMs to be re-evaluated, expending some power.
2395 */
Imre Deak820c1982013-12-17 14:46:36 +02002396static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2397 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002398{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002399 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002400 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002401 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002402 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002403
Damien Lespiau055e3932014-08-18 13:49:10 +01002404 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002405 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002406 return;
2407
Ville Syrjälä8553c182013-12-05 15:51:39 +02002408 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002409
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002410 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002411 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002412 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002413 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002414 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002415 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2416
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002417 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002418 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002419 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002420 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002421 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002422 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2423
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002424 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002425 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002426 val = I915_READ(WM_MISC);
2427 if (results->partitioning == INTEL_DDB_PART_1_2)
2428 val &= ~WM_MISC_DATA_PARTITION_5_6;
2429 else
2430 val |= WM_MISC_DATA_PARTITION_5_6;
2431 I915_WRITE(WM_MISC, val);
2432 } else {
2433 val = I915_READ(DISP_ARB_CTL2);
2434 if (results->partitioning == INTEL_DDB_PART_1_2)
2435 val &= ~DISP_DATA_PARTITION_5_6;
2436 else
2437 val |= DISP_DATA_PARTITION_5_6;
2438 I915_WRITE(DISP_ARB_CTL2, val);
2439 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002440 }
2441
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002442 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002443 val = I915_READ(DISP_ARB_CTL);
2444 if (results->enable_fbc_wm)
2445 val &= ~DISP_FBC_WM_DIS;
2446 else
2447 val |= DISP_FBC_WM_DIS;
2448 I915_WRITE(DISP_ARB_CTL, val);
2449 }
2450
Imre Deak954911e2013-12-17 14:46:34 +02002451 if (dirty & WM_DIRTY_LP(1) &&
2452 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2453 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2454
2455 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002456 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2457 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2458 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2459 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2460 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002461
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002462 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002463 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002464 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002466 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002467 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002468
2469 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002470}
2471
Ville Syrjälä8553c182013-12-05 15:51:39 +02002472static bool ilk_disable_lp_wm(struct drm_device *dev)
2473{
2474 struct drm_i915_private *dev_priv = dev->dev_private;
2475
2476 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2477}
2478
Damien Lespiaub9cec072014-11-04 17:06:43 +00002479/*
2480 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2481 * different active planes.
2482 */
2483
2484#define SKL_DDB_SIZE 896 /* in blocks */
2485
2486static void
2487skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2488 struct drm_crtc *for_crtc,
2489 const struct intel_wm_config *config,
2490 const struct skl_pipe_wm_parameters *params,
2491 struct skl_ddb_entry *alloc /* out */)
2492{
2493 struct drm_crtc *crtc;
2494 unsigned int pipe_size, ddb_size;
2495 int nth_active_pipe;
2496
2497 if (!params->active) {
2498 alloc->start = 0;
2499 alloc->end = 0;
2500 return;
2501 }
2502
2503 ddb_size = SKL_DDB_SIZE;
2504
2505 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2506
2507 nth_active_pipe = 0;
2508 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002509 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002510 continue;
2511
2512 if (crtc == for_crtc)
2513 break;
2514
2515 nth_active_pipe++;
2516 }
2517
2518 pipe_size = ddb_size / config->num_pipes_active;
2519 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002520 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002521}
2522
2523static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2524{
2525 if (config->num_pipes_active == 1)
2526 return 32;
2527
2528 return 8;
2529}
2530
Damien Lespiaua269c582014-11-04 17:06:49 +00002531static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2532{
2533 entry->start = reg & 0x3ff;
2534 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002535 if (entry->end)
2536 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002537}
2538
Damien Lespiau08db6652014-11-04 17:06:52 +00002539void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2540 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002541{
Damien Lespiaua269c582014-11-04 17:06:49 +00002542 enum pipe pipe;
2543 int plane;
2544 u32 val;
2545
2546 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002547 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002548 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2549 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2550 val);
2551 }
2552
2553 val = I915_READ(CUR_BUF_CFG(pipe));
2554 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2555 }
2556}
2557
Damien Lespiaub9cec072014-11-04 17:06:43 +00002558static unsigned int
2559skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2560{
2561 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2562}
2563
2564/*
2565 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2566 * a 8192x4096@32bpp framebuffer:
2567 * 3 * 4096 * 8192 * 4 < 2^32
2568 */
2569static unsigned int
2570skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2571 const struct skl_pipe_wm_parameters *params)
2572{
2573 unsigned int total_data_rate = 0;
2574 int plane;
2575
2576 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2577 const struct intel_plane_wm_parameters *p;
2578
2579 p = &params->plane[plane];
2580 if (!p->enabled)
2581 continue;
2582
2583 total_data_rate += skl_plane_relative_data_rate(p);
2584 }
2585
2586 return total_data_rate;
2587}
2588
2589static void
2590skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2591 const struct intel_wm_config *config,
2592 const struct skl_pipe_wm_parameters *params,
2593 struct skl_ddb_allocation *ddb /* out */)
2594{
2595 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002596 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2598 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002599 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002600 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002601 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002602 unsigned int total_data_rate;
2603 int plane;
2604
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002605 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2606 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002607 if (alloc_size == 0) {
2608 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2609 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2610 return;
2611 }
2612
2613 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002614 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2615 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002616
2617 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002618 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002619
Damien Lespiau80958152015-02-09 13:35:10 +00002620 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002621 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002622 const struct intel_plane_wm_parameters *p;
2623
2624 p = &params->plane[plane];
2625 if (!p->enabled)
2626 continue;
2627
2628 minimum[plane] = 8;
2629 alloc_size -= minimum[plane];
2630 }
2631
Damien Lespiaub9cec072014-11-04 17:06:43 +00002632 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002633 * 2. Distribute the remaining space in proportion to the amount of
2634 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002635 *
2636 * FIXME: we may not allocate every single block here.
2637 */
2638 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2639
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002640 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002641 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2642 const struct intel_plane_wm_parameters *p;
2643 unsigned int data_rate;
2644 uint16_t plane_blocks;
2645
2646 p = &params->plane[plane];
2647 if (!p->enabled)
2648 continue;
2649
2650 data_rate = skl_plane_relative_data_rate(p);
2651
2652 /*
2653 * promote the expression to 64 bits to avoid overflowing, the
2654 * result is < available as data_rate / total_data_rate < 1
2655 */
Damien Lespiau80958152015-02-09 13:35:10 +00002656 plane_blocks = minimum[plane];
2657 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2658 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002659
2660 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002661 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002662
2663 start += plane_blocks;
2664 }
2665
2666}
2667
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002668static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002669{
2670 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002671 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002672}
2673
2674/*
2675 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2676 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2677 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2678 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2679*/
2680static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2681 uint32_t latency)
2682{
2683 uint32_t wm_intermediate_val, ret;
2684
2685 if (latency == 0)
2686 return UINT_MAX;
2687
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002688 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002689 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2690
2691 return ret;
2692}
2693
2694static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2695 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002696 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002697{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002698 uint32_t ret;
2699 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2700 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002701
2702 if (latency == 0)
2703 return UINT_MAX;
2704
2705 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002706
2707 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2708 tiling == I915_FORMAT_MOD_Yf_TILED) {
2709 plane_bytes_per_line *= 4;
2710 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2711 plane_blocks_per_line /= 4;
2712 } else {
2713 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2714 }
2715
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002716 wm_intermediate_val = latency * pixel_rate;
2717 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002718 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002719
2720 return ret;
2721}
2722
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002723static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2724 const struct intel_crtc *intel_crtc)
2725{
2726 struct drm_device *dev = intel_crtc->base.dev;
2727 struct drm_i915_private *dev_priv = dev->dev_private;
2728 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2729 enum pipe pipe = intel_crtc->pipe;
2730
2731 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2732 sizeof(new_ddb->plane[pipe])))
2733 return true;
2734
2735 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2736 sizeof(new_ddb->cursor[pipe])))
2737 return true;
2738
2739 return false;
2740}
2741
2742static void skl_compute_wm_global_parameters(struct drm_device *dev,
2743 struct intel_wm_config *config)
2744{
2745 struct drm_crtc *crtc;
2746 struct drm_plane *plane;
2747
2748 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07002749 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002750
2751 /* FIXME: I don't think we need those two global parameters on SKL */
2752 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2753 struct intel_plane *intel_plane = to_intel_plane(plane);
2754
2755 config->sprites_enabled |= intel_plane->wm.enabled;
2756 config->sprites_scaled |= intel_plane->wm.scaled;
2757 }
2758}
2759
2760static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2761 struct skl_pipe_wm_parameters *p)
2762{
2763 struct drm_device *dev = crtc->dev;
2764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2765 enum pipe pipe = intel_crtc->pipe;
2766 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002767 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002768 int i = 1; /* Index for sprite planes start */
2769
Matt Roper3ef00282015-03-09 10:19:24 -07002770 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002771 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002772 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2773 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002774
2775 /*
2776 * For now, assume primary and cursor planes are always enabled.
2777 */
2778 p->plane[0].enabled = true;
2779 p->plane[0].bytes_per_pixel =
Matt Roper59bea882015-02-27 10:12:01 -08002780 crtc->primary->state->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002781 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2782 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002783 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2784 fb = crtc->primary->state->fb;
2785 /*
2786 * Framebuffer can be NULL on plane disable, but it does not
2787 * matter for watermarks if we assume no tiling in that case.
2788 */
2789 if (fb)
2790 p->plane[0].tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002791
2792 p->cursor.enabled = true;
2793 p->cursor.bytes_per_pixel = 4;
Matt Roper3dd512f2015-02-27 10:12:00 -08002794 p->cursor.horiz_pixels = intel_crtc->base.cursor->state->crtc_w ?
2795 intel_crtc->base.cursor->state->crtc_w : 64;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002796 }
2797
2798 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2799 struct intel_plane *intel_plane = to_intel_plane(plane);
2800
Sonika Jindala712f8e2014-12-09 10:59:15 +05302801 if (intel_plane->pipe == pipe &&
2802 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002803 p->plane[i++] = intel_plane->wm;
2804 }
2805}
2806
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002807static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2808 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002809 struct intel_plane_wm_parameters *p_params,
2810 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002811 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002812 uint16_t *out_blocks, /* out */
2813 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002814{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002815 uint32_t latency = dev_priv->wm.skl_latency[level];
2816 uint32_t method1, method2;
2817 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2818 uint32_t res_blocks, res_lines;
2819 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002820
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002821 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002822 return false;
2823
2824 method1 = skl_wm_method1(p->pixel_rate,
2825 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002826 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002827 method2 = skl_wm_method2(p->pixel_rate,
2828 p->pipe_htotal,
2829 p_params->horiz_pixels,
2830 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002831 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002832 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002833
2834 plane_bytes_per_line = p_params->horiz_pixels *
2835 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002836 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002837
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002838 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2839 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2840 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2841 selected_result = max(method2, y_tile_minimum);
2842 } else {
2843 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2844 selected_result = min(method1, method2);
2845 else
2846 selected_result = method1;
2847 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002848
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002849 res_blocks = selected_result + 1;
2850 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002851
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002852 if (level >= 1 && level <= 7) {
2853 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2854 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2855 res_lines += 4;
2856 else
2857 res_blocks++;
2858 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002859
2860 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002861 return false;
2862
2863 *out_blocks = res_blocks;
2864 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002865
2866 return true;
2867}
2868
2869static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2870 struct skl_ddb_allocation *ddb,
2871 struct skl_pipe_wm_parameters *p,
2872 enum pipe pipe,
2873 int level,
2874 int num_planes,
2875 struct skl_wm_level *result)
2876{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002877 uint16_t ddb_blocks;
2878 int i;
2879
2880 for (i = 0; i < num_planes; i++) {
2881 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2882
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002883 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2884 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002885 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002886 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002887 &result->plane_res_b[i],
2888 &result->plane_res_l[i]);
2889 }
2890
2891 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002892 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2893 ddb_blocks, level,
2894 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002895 &result->cursor_res_l);
2896}
2897
Damien Lespiau407b50f2014-11-04 17:06:57 +00002898static uint32_t
2899skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2900{
Matt Roper3ef00282015-03-09 10:19:24 -07002901 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002902 return 0;
2903
2904 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2905
2906}
2907
2908static void skl_compute_transition_wm(struct drm_crtc *crtc,
2909 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002910 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002911{
Damien Lespiau9414f562014-11-04 17:06:58 +00002912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2913 int i;
2914
Damien Lespiau407b50f2014-11-04 17:06:57 +00002915 if (!params->active)
2916 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002917
2918 /* Until we know more, just disable transition WMs */
2919 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2920 trans_wm->plane_en[i] = false;
2921 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002922}
2923
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002924static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2925 struct skl_ddb_allocation *ddb,
2926 struct skl_pipe_wm_parameters *params,
2927 struct skl_pipe_wm *pipe_wm)
2928{
2929 struct drm_device *dev = crtc->dev;
2930 const struct drm_i915_private *dev_priv = dev->dev_private;
2931 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2932 int level, max_level = ilk_wm_max_level(dev);
2933
2934 for (level = 0; level <= max_level; level++) {
2935 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2936 level, intel_num_planes(intel_crtc),
2937 &pipe_wm->wm[level]);
2938 }
2939 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2940
Damien Lespiau9414f562014-11-04 17:06:58 +00002941 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002942}
2943
2944static void skl_compute_wm_results(struct drm_device *dev,
2945 struct skl_pipe_wm_parameters *p,
2946 struct skl_pipe_wm *p_wm,
2947 struct skl_wm_values *r,
2948 struct intel_crtc *intel_crtc)
2949{
2950 int level, max_level = ilk_wm_max_level(dev);
2951 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002952 uint32_t temp;
2953 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002954
2955 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002956 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2957 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002958
2959 temp |= p_wm->wm[level].plane_res_l[i] <<
2960 PLANE_WM_LINES_SHIFT;
2961 temp |= p_wm->wm[level].plane_res_b[i];
2962 if (p_wm->wm[level].plane_en[i])
2963 temp |= PLANE_WM_EN;
2964
2965 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002966 }
2967
2968 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002969
2970 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2971 temp |= p_wm->wm[level].cursor_res_b;
2972
2973 if (p_wm->wm[level].cursor_en)
2974 temp |= PLANE_WM_EN;
2975
2976 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002977
2978 }
2979
Damien Lespiau9414f562014-11-04 17:06:58 +00002980 /* transition WMs */
2981 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2982 temp = 0;
2983 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2984 temp |= p_wm->trans_wm.plane_res_b[i];
2985 if (p_wm->trans_wm.plane_en[i])
2986 temp |= PLANE_WM_EN;
2987
2988 r->plane_trans[pipe][i] = temp;
2989 }
2990
2991 temp = 0;
2992 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2993 temp |= p_wm->trans_wm.cursor_res_b;
2994 if (p_wm->trans_wm.cursor_en)
2995 temp |= PLANE_WM_EN;
2996
2997 r->cursor_trans[pipe] = temp;
2998
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002999 r->wm_linetime[pipe] = p_wm->linetime;
3000}
3001
Damien Lespiau16160e32014-11-04 17:06:53 +00003002static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3003 const struct skl_ddb_entry *entry)
3004{
3005 if (entry->end)
3006 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3007 else
3008 I915_WRITE(reg, 0);
3009}
3010
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003011static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3012 const struct skl_wm_values *new)
3013{
3014 struct drm_device *dev = dev_priv->dev;
3015 struct intel_crtc *crtc;
3016
3017 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3018 int i, level, max_level = ilk_wm_max_level(dev);
3019 enum pipe pipe = crtc->pipe;
3020
Damien Lespiau5d374d92014-11-04 17:07:00 +00003021 if (!new->dirty[pipe])
3022 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003023
Damien Lespiau5d374d92014-11-04 17:07:00 +00003024 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3025
3026 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003027 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003028 I915_WRITE(PLANE_WM(pipe, i, level),
3029 new->plane[pipe][i][level]);
3030 I915_WRITE(CUR_WM(pipe, level),
3031 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003032 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003033 for (i = 0; i < intel_num_planes(crtc); i++)
3034 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3035 new->plane_trans[pipe][i]);
3036 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3037
3038 for (i = 0; i < intel_num_planes(crtc); i++)
3039 skl_ddb_entry_write(dev_priv,
3040 PLANE_BUF_CFG(pipe, i),
3041 &new->ddb.plane[pipe][i]);
3042
3043 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3044 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003045 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003046}
3047
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003048/*
3049 * When setting up a new DDB allocation arrangement, we need to correctly
3050 * sequence the times at which the new allocations for the pipes are taken into
3051 * account or we'll have pipes fetching from space previously allocated to
3052 * another pipe.
3053 *
3054 * Roughly the sequence looks like:
3055 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3056 * overlapping with a previous light-up pipe (another way to put it is:
3057 * pipes with their new allocation strickly included into their old ones).
3058 * 2. re-allocate the other pipes that get their allocation reduced
3059 * 3. allocate the pipes having their allocation increased
3060 *
3061 * Steps 1. and 2. are here to take care of the following case:
3062 * - Initially DDB looks like this:
3063 * | B | C |
3064 * - enable pipe A.
3065 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3066 * allocation
3067 * | A | B | C |
3068 *
3069 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3070 */
3071
Damien Lespiaud21b7952014-11-04 17:07:03 +00003072static void
3073skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003074{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003075 int plane;
3076
Damien Lespiaud21b7952014-11-04 17:07:03 +00003077 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3078
Damien Lespiaudd740782015-02-28 14:54:08 +00003079 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003080 I915_WRITE(PLANE_SURF(pipe, plane),
3081 I915_READ(PLANE_SURF(pipe, plane)));
3082 }
3083 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3084}
3085
3086static bool
3087skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3088 const struct skl_ddb_allocation *new,
3089 enum pipe pipe)
3090{
3091 uint16_t old_size, new_size;
3092
3093 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3094 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3095
3096 return old_size != new_size &&
3097 new->pipe[pipe].start >= old->pipe[pipe].start &&
3098 new->pipe[pipe].end <= old->pipe[pipe].end;
3099}
3100
3101static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3102 struct skl_wm_values *new_values)
3103{
3104 struct drm_device *dev = dev_priv->dev;
3105 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3106 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3107 struct intel_crtc *crtc;
3108 enum pipe pipe;
3109
3110 new_ddb = &new_values->ddb;
3111 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3112
3113 /*
3114 * First pass: flush the pipes with the new allocation contained into
3115 * the old space.
3116 *
3117 * We'll wait for the vblank on those pipes to ensure we can safely
3118 * re-allocate the freed space without this pipe fetching from it.
3119 */
3120 for_each_intel_crtc(dev, crtc) {
3121 if (!crtc->active)
3122 continue;
3123
3124 pipe = crtc->pipe;
3125
3126 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3127 continue;
3128
Damien Lespiaud21b7952014-11-04 17:07:03 +00003129 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003130 intel_wait_for_vblank(dev, pipe);
3131
3132 reallocated[pipe] = true;
3133 }
3134
3135
3136 /*
3137 * Second pass: flush the pipes that are having their allocation
3138 * reduced, but overlapping with a previous allocation.
3139 *
3140 * Here as well we need to wait for the vblank to make sure the freed
3141 * space is not used anymore.
3142 */
3143 for_each_intel_crtc(dev, crtc) {
3144 if (!crtc->active)
3145 continue;
3146
3147 pipe = crtc->pipe;
3148
3149 if (reallocated[pipe])
3150 continue;
3151
3152 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3153 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003154 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003155 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303156 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003157 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003158 }
3159
3160 /*
3161 * Third pass: flush the pipes that got more space allocated.
3162 *
3163 * We don't need to actively wait for the update here, next vblank
3164 * will just get more DDB space with the correct WM values.
3165 */
3166 for_each_intel_crtc(dev, crtc) {
3167 if (!crtc->active)
3168 continue;
3169
3170 pipe = crtc->pipe;
3171
3172 /*
3173 * At this point, only the pipes more space than before are
3174 * left to re-allocate.
3175 */
3176 if (reallocated[pipe])
3177 continue;
3178
Damien Lespiaud21b7952014-11-04 17:07:03 +00003179 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003180 }
3181}
3182
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003183static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3184 struct skl_pipe_wm_parameters *params,
3185 struct intel_wm_config *config,
3186 struct skl_ddb_allocation *ddb, /* out */
3187 struct skl_pipe_wm *pipe_wm /* out */)
3188{
3189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3190
3191 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003192 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003193 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3194
3195 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3196 return false;
3197
3198 intel_crtc->wm.skl_active = *pipe_wm;
3199 return true;
3200}
3201
3202static void skl_update_other_pipe_wm(struct drm_device *dev,
3203 struct drm_crtc *crtc,
3204 struct intel_wm_config *config,
3205 struct skl_wm_values *r)
3206{
3207 struct intel_crtc *intel_crtc;
3208 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3209
3210 /*
3211 * If the WM update hasn't changed the allocation for this_crtc (the
3212 * crtc we are currently computing the new WM values for), other
3213 * enabled crtcs will keep the same allocation and we don't need to
3214 * recompute anything for them.
3215 */
3216 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3217 return;
3218
3219 /*
3220 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3221 * other active pipes need new DDB allocation and WM values.
3222 */
3223 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3224 base.head) {
3225 struct skl_pipe_wm_parameters params = {};
3226 struct skl_pipe_wm pipe_wm = {};
3227 bool wm_changed;
3228
3229 if (this_crtc->pipe == intel_crtc->pipe)
3230 continue;
3231
3232 if (!intel_crtc->active)
3233 continue;
3234
3235 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3236 &params, config,
3237 &r->ddb, &pipe_wm);
3238
3239 /*
3240 * If we end up re-computing the other pipe WM values, it's
3241 * because it was really needed, so we expect the WM values to
3242 * be different.
3243 */
3244 WARN_ON(!wm_changed);
3245
3246 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3247 r->dirty[intel_crtc->pipe] = true;
3248 }
3249}
3250
3251static void skl_update_wm(struct drm_crtc *crtc)
3252{
3253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3254 struct drm_device *dev = crtc->dev;
3255 struct drm_i915_private *dev_priv = dev->dev_private;
3256 struct skl_pipe_wm_parameters params = {};
3257 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3258 struct skl_pipe_wm pipe_wm = {};
3259 struct intel_wm_config config = {};
3260
3261 memset(results, 0, sizeof(*results));
3262
3263 skl_compute_wm_global_parameters(dev, &config);
3264
3265 if (!skl_update_pipe_wm(crtc, &params, &config,
3266 &results->ddb, &pipe_wm))
3267 return;
3268
3269 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3270 results->dirty[intel_crtc->pipe] = true;
3271
3272 skl_update_other_pipe_wm(dev, crtc, &config, results);
3273 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003274 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003275
3276 /* store the new configuration */
3277 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003278}
3279
3280static void
3281skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3282 uint32_t sprite_width, uint32_t sprite_height,
3283 int pixel_size, bool enabled, bool scaled)
3284{
3285 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003286 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003287
3288 intel_plane->wm.enabled = enabled;
3289 intel_plane->wm.scaled = scaled;
3290 intel_plane->wm.horiz_pixels = sprite_width;
3291 intel_plane->wm.vert_pixels = sprite_height;
3292 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003293 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3294 /*
3295 * Framebuffer can be NULL on plane disable, but it does not
3296 * matter for watermarks if we assume no tiling in that case.
3297 */
3298 if (fb)
3299 intel_plane->wm.tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003300
3301 skl_update_wm(crtc);
3302}
3303
Imre Deak820c1982013-12-17 14:46:36 +02003304static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003305{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003307 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003308 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003309 struct ilk_wm_maximums max;
3310 struct ilk_pipe_wm_parameters params = {};
3311 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003312 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003313 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003314 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003315 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003316
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003317 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003318
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003319 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3320
3321 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3322 return;
3323
3324 intel_crtc->wm.active = pipe_wm;
3325
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003326 ilk_compute_wm_config(dev, &config);
3327
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003328 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003329 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003330
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003331 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003332 if (INTEL_INFO(dev)->gen >= 7 &&
3333 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003334 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003335 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003336
Imre Deak820c1982013-12-17 14:46:36 +02003337 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003338 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003339 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003340 }
3341
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003342 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003343 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003344
Imre Deak820c1982013-12-17 14:46:36 +02003345 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003346
Imre Deak820c1982013-12-17 14:46:36 +02003347 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003348}
3349
Damien Lespiaued57cb82014-07-15 09:21:24 +02003350static void
3351ilk_update_sprite_wm(struct drm_plane *plane,
3352 struct drm_crtc *crtc,
3353 uint32_t sprite_width, uint32_t sprite_height,
3354 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003355{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003356 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003357 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003358
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003359 intel_plane->wm.enabled = enabled;
3360 intel_plane->wm.scaled = scaled;
3361 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003362 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003363 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003364
Ville Syrjälä8553c182013-12-05 15:51:39 +02003365 /*
3366 * IVB workaround: must disable low power watermarks for at least
3367 * one frame before enabling scaling. LP watermarks can be re-enabled
3368 * when scaling is disabled.
3369 *
3370 * WaCxSRDisabledForSpriteScaling:ivb
3371 */
3372 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3373 intel_wait_for_vblank(dev, intel_plane->pipe);
3374
Imre Deak820c1982013-12-17 14:46:36 +02003375 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003376}
3377
Pradeep Bhat30789992014-11-04 17:06:45 +00003378static void skl_pipe_wm_active_state(uint32_t val,
3379 struct skl_pipe_wm *active,
3380 bool is_transwm,
3381 bool is_cursor,
3382 int i,
3383 int level)
3384{
3385 bool is_enabled = (val & PLANE_WM_EN) != 0;
3386
3387 if (!is_transwm) {
3388 if (!is_cursor) {
3389 active->wm[level].plane_en[i] = is_enabled;
3390 active->wm[level].plane_res_b[i] =
3391 val & PLANE_WM_BLOCKS_MASK;
3392 active->wm[level].plane_res_l[i] =
3393 (val >> PLANE_WM_LINES_SHIFT) &
3394 PLANE_WM_LINES_MASK;
3395 } else {
3396 active->wm[level].cursor_en = is_enabled;
3397 active->wm[level].cursor_res_b =
3398 val & PLANE_WM_BLOCKS_MASK;
3399 active->wm[level].cursor_res_l =
3400 (val >> PLANE_WM_LINES_SHIFT) &
3401 PLANE_WM_LINES_MASK;
3402 }
3403 } else {
3404 if (!is_cursor) {
3405 active->trans_wm.plane_en[i] = is_enabled;
3406 active->trans_wm.plane_res_b[i] =
3407 val & PLANE_WM_BLOCKS_MASK;
3408 active->trans_wm.plane_res_l[i] =
3409 (val >> PLANE_WM_LINES_SHIFT) &
3410 PLANE_WM_LINES_MASK;
3411 } else {
3412 active->trans_wm.cursor_en = is_enabled;
3413 active->trans_wm.cursor_res_b =
3414 val & PLANE_WM_BLOCKS_MASK;
3415 active->trans_wm.cursor_res_l =
3416 (val >> PLANE_WM_LINES_SHIFT) &
3417 PLANE_WM_LINES_MASK;
3418 }
3419 }
3420}
3421
3422static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3423{
3424 struct drm_device *dev = crtc->dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3428 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3429 enum pipe pipe = intel_crtc->pipe;
3430 int level, i, max_level;
3431 uint32_t temp;
3432
3433 max_level = ilk_wm_max_level(dev);
3434
3435 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3436
3437 for (level = 0; level <= max_level; level++) {
3438 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3439 hw->plane[pipe][i][level] =
3440 I915_READ(PLANE_WM(pipe, i, level));
3441 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3442 }
3443
3444 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3445 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3446 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3447
Matt Roper3ef00282015-03-09 10:19:24 -07003448 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003449 return;
3450
3451 hw->dirty[pipe] = true;
3452
3453 active->linetime = hw->wm_linetime[pipe];
3454
3455 for (level = 0; level <= max_level; level++) {
3456 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3457 temp = hw->plane[pipe][i][level];
3458 skl_pipe_wm_active_state(temp, active, false,
3459 false, i, level);
3460 }
3461 temp = hw->cursor[pipe][level];
3462 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3463 }
3464
3465 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3466 temp = hw->plane_trans[pipe][i];
3467 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3468 }
3469
3470 temp = hw->cursor_trans[pipe];
3471 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3472}
3473
3474void skl_wm_get_hw_state(struct drm_device *dev)
3475{
Damien Lespiaua269c582014-11-04 17:06:49 +00003476 struct drm_i915_private *dev_priv = dev->dev_private;
3477 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003478 struct drm_crtc *crtc;
3479
Damien Lespiaua269c582014-11-04 17:06:49 +00003480 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003481 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3482 skl_pipe_wm_get_hw_state(crtc);
3483}
3484
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003485static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3486{
3487 struct drm_device *dev = crtc->dev;
3488 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003489 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3491 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3492 enum pipe pipe = intel_crtc->pipe;
3493 static const unsigned int wm0_pipe_reg[] = {
3494 [PIPE_A] = WM0_PIPEA_ILK,
3495 [PIPE_B] = WM0_PIPEB_ILK,
3496 [PIPE_C] = WM0_PIPEC_IVB,
3497 };
3498
3499 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003500 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003501 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003502
Matt Roper3ef00282015-03-09 10:19:24 -07003503 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003504
3505 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003506 u32 tmp = hw->wm_pipe[pipe];
3507
3508 /*
3509 * For active pipes LP0 watermark is marked as
3510 * enabled, and LP1+ watermaks as disabled since
3511 * we can't really reverse compute them in case
3512 * multiple pipes are active.
3513 */
3514 active->wm[0].enable = true;
3515 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3516 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3517 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3518 active->linetime = hw->wm_linetime[pipe];
3519 } else {
3520 int level, max_level = ilk_wm_max_level(dev);
3521
3522 /*
3523 * For inactive pipes, all watermark levels
3524 * should be marked as enabled but zeroed,
3525 * which is what we'd compute them to.
3526 */
3527 for (level = 0; level <= max_level; level++)
3528 active->wm[level].enable = true;
3529 }
3530}
3531
3532void ilk_wm_get_hw_state(struct drm_device *dev)
3533{
3534 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003535 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003536 struct drm_crtc *crtc;
3537
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003538 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003539 ilk_pipe_wm_get_hw_state(crtc);
3540
3541 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3542 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3543 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3544
3545 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003546 if (INTEL_INFO(dev)->gen >= 7) {
3547 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3548 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3549 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003550
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003551 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003552 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3553 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3554 else if (IS_IVYBRIDGE(dev))
3555 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3556 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003557
3558 hw->enable_fbc_wm =
3559 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3560}
3561
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003562/**
3563 * intel_update_watermarks - update FIFO watermark values based on current modes
3564 *
3565 * Calculate watermark values for the various WM regs based on current mode
3566 * and plane configuration.
3567 *
3568 * There are several cases to deal with here:
3569 * - normal (i.e. non-self-refresh)
3570 * - self-refresh (SR) mode
3571 * - lines are large relative to FIFO size (buffer can hold up to 2)
3572 * - lines are small relative to FIFO size (buffer can hold more than 2
3573 * lines), so need to account for TLB latency
3574 *
3575 * The normal calculation is:
3576 * watermark = dotclock * bytes per pixel * latency
3577 * where latency is platform & configuration dependent (we assume pessimal
3578 * values here).
3579 *
3580 * The SR calculation is:
3581 * watermark = (trunc(latency/line time)+1) * surface width *
3582 * bytes per pixel
3583 * where
3584 * line time = htotal / dotclock
3585 * surface width = hdisplay for normal plane and 64 for cursor
3586 * and latency is assumed to be high, as above.
3587 *
3588 * The final value programmed to the register should always be rounded up,
3589 * and include an extra 2 entries to account for clock crossings.
3590 *
3591 * We don't use the sprite, so we can ignore that. And on Crestline we have
3592 * to set the non-SR watermarks to 8.
3593 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003594void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003595{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003596 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003597
3598 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003599 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003600}
3601
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003602void intel_update_sprite_watermarks(struct drm_plane *plane,
3603 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003604 uint32_t sprite_width,
3605 uint32_t sprite_height,
3606 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003607 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003608{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003609 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003610
3611 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003612 dev_priv->display.update_sprite_wm(plane, crtc,
3613 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003614 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003615}
3616
Daniel Vetter92703882012-08-09 16:46:01 +02003617/**
3618 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003619 */
3620DEFINE_SPINLOCK(mchdev_lock);
3621
3622/* Global for IPS driver to get at the current i915 device. Protected by
3623 * mchdev_lock. */
3624static struct drm_i915_private *i915_mch_dev;
3625
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003626bool ironlake_set_drps(struct drm_device *dev, u8 val)
3627{
3628 struct drm_i915_private *dev_priv = dev->dev_private;
3629 u16 rgvswctl;
3630
Daniel Vetter92703882012-08-09 16:46:01 +02003631 assert_spin_locked(&mchdev_lock);
3632
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003633 rgvswctl = I915_READ16(MEMSWCTL);
3634 if (rgvswctl & MEMCTL_CMD_STS) {
3635 DRM_DEBUG("gpu busy, RCS change rejected\n");
3636 return false; /* still busy with another command */
3637 }
3638
3639 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3640 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3641 I915_WRITE16(MEMSWCTL, rgvswctl);
3642 POSTING_READ16(MEMSWCTL);
3643
3644 rgvswctl |= MEMCTL_CMD_STS;
3645 I915_WRITE16(MEMSWCTL, rgvswctl);
3646
3647 return true;
3648}
3649
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003650static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003651{
3652 struct drm_i915_private *dev_priv = dev->dev_private;
3653 u32 rgvmodectl = I915_READ(MEMMODECTL);
3654 u8 fmax, fmin, fstart, vstart;
3655
Daniel Vetter92703882012-08-09 16:46:01 +02003656 spin_lock_irq(&mchdev_lock);
3657
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003658 /* Enable temp reporting */
3659 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3660 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3661
3662 /* 100ms RC evaluation intervals */
3663 I915_WRITE(RCUPEI, 100000);
3664 I915_WRITE(RCDNEI, 100000);
3665
3666 /* Set max/min thresholds to 90ms and 80ms respectively */
3667 I915_WRITE(RCBMAXAVG, 90000);
3668 I915_WRITE(RCBMINAVG, 80000);
3669
3670 I915_WRITE(MEMIHYST, 1);
3671
3672 /* Set up min, max, and cur for interrupt handling */
3673 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3674 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3675 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3676 MEMMODE_FSTART_SHIFT;
3677
3678 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3679 PXVFREQ_PX_SHIFT;
3680
Daniel Vetter20e4d402012-08-08 23:35:39 +02003681 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3682 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003683
Daniel Vetter20e4d402012-08-08 23:35:39 +02003684 dev_priv->ips.max_delay = fstart;
3685 dev_priv->ips.min_delay = fmin;
3686 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003687
3688 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3689 fmax, fmin, fstart);
3690
3691 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3692
3693 /*
3694 * Interrupts will be enabled in ironlake_irq_postinstall
3695 */
3696
3697 I915_WRITE(VIDSTART, vstart);
3698 POSTING_READ(VIDSTART);
3699
3700 rgvmodectl |= MEMMODE_SWMODE_EN;
3701 I915_WRITE(MEMMODECTL, rgvmodectl);
3702
Daniel Vetter92703882012-08-09 16:46:01 +02003703 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003704 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003705 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003706
3707 ironlake_set_drps(dev, fstart);
3708
Daniel Vetter20e4d402012-08-08 23:35:39 +02003709 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003710 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003711 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3712 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003713 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003714
3715 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003716}
3717
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003718static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003719{
3720 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003721 u16 rgvswctl;
3722
3723 spin_lock_irq(&mchdev_lock);
3724
3725 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003726
3727 /* Ack interrupts, disable EFC interrupt */
3728 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3729 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3730 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3731 I915_WRITE(DEIIR, DE_PCU_EVENT);
3732 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3733
3734 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003735 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003736 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003737 rgvswctl |= MEMCTL_CMD_STS;
3738 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003739 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003740
Daniel Vetter92703882012-08-09 16:46:01 +02003741 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003742}
3743
Daniel Vetteracbe9472012-07-26 11:50:05 +02003744/* There's a funny hw issue where the hw returns all 0 when reading from
3745 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3746 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3747 * all limits and the gpu stuck at whatever frequency it is at atm).
3748 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003749static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003750{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003751 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003752
Daniel Vetter20b46e52012-07-26 11:16:14 +02003753 /* Only set the down limit when we've reached the lowest level to avoid
3754 * getting more interrupts, otherwise leave this clear. This prevents a
3755 * race in the hw when coming out of rc6: There's a tiny window where
3756 * the hw runs at the minimal clock before selecting the desired
3757 * frequency, if the down threshold expires in that window we will not
3758 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003759 limits = dev_priv->rps.max_freq_softlimit << 24;
3760 if (val <= dev_priv->rps.min_freq_softlimit)
3761 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003762
3763 return limits;
3764}
3765
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003766static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3767{
3768 int new_power;
3769
3770 new_power = dev_priv->rps.power;
3771 switch (dev_priv->rps.power) {
3772 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003773 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003774 new_power = BETWEEN;
3775 break;
3776
3777 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003778 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003779 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003780 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003781 new_power = HIGH_POWER;
3782 break;
3783
3784 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003785 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 +01003786 new_power = BETWEEN;
3787 break;
3788 }
3789 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003790 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003791 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003792 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003793 new_power = HIGH_POWER;
3794 if (new_power == dev_priv->rps.power)
3795 return;
3796
3797 /* Note the units here are not exactly 1us, but 1280ns. */
3798 switch (new_power) {
3799 case LOW_POWER:
3800 /* Upclock if more than 95% busy over 16ms */
3801 I915_WRITE(GEN6_RP_UP_EI, 12500);
3802 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3803
3804 /* Downclock if less than 85% busy over 32ms */
3805 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3806 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3807
3808 I915_WRITE(GEN6_RP_CONTROL,
3809 GEN6_RP_MEDIA_TURBO |
3810 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3811 GEN6_RP_MEDIA_IS_GFX |
3812 GEN6_RP_ENABLE |
3813 GEN6_RP_UP_BUSY_AVG |
3814 GEN6_RP_DOWN_IDLE_AVG);
3815 break;
3816
3817 case BETWEEN:
3818 /* Upclock if more than 90% busy over 13ms */
3819 I915_WRITE(GEN6_RP_UP_EI, 10250);
3820 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3821
3822 /* Downclock if less than 75% busy over 32ms */
3823 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3824 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3825
3826 I915_WRITE(GEN6_RP_CONTROL,
3827 GEN6_RP_MEDIA_TURBO |
3828 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3829 GEN6_RP_MEDIA_IS_GFX |
3830 GEN6_RP_ENABLE |
3831 GEN6_RP_UP_BUSY_AVG |
3832 GEN6_RP_DOWN_IDLE_AVG);
3833 break;
3834
3835 case HIGH_POWER:
3836 /* Upclock if more than 85% busy over 10ms */
3837 I915_WRITE(GEN6_RP_UP_EI, 8000);
3838 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3839
3840 /* Downclock if less than 60% busy over 32ms */
3841 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3842 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3843
3844 I915_WRITE(GEN6_RP_CONTROL,
3845 GEN6_RP_MEDIA_TURBO |
3846 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3847 GEN6_RP_MEDIA_IS_GFX |
3848 GEN6_RP_ENABLE |
3849 GEN6_RP_UP_BUSY_AVG |
3850 GEN6_RP_DOWN_IDLE_AVG);
3851 break;
3852 }
3853
3854 dev_priv->rps.power = new_power;
3855 dev_priv->rps.last_adj = 0;
3856}
3857
Chris Wilson2876ce72014-03-28 08:03:34 +00003858static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3859{
3860 u32 mask = 0;
3861
3862 if (val > dev_priv->rps.min_freq_softlimit)
3863 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3864 if (val < dev_priv->rps.max_freq_softlimit)
3865 mask |= GEN6_PM_RP_UP_THRESHOLD;
3866
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003867 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3868 mask &= dev_priv->pm_rps_events;
3869
Imre Deak59d02a12014-12-19 19:33:26 +02003870 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003871}
3872
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003873/* gen6_set_rps is called to update the frequency request, but should also be
3874 * called when the range (min_delay and max_delay) is modified so that we can
3875 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003876static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003877{
3878 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003879
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003880 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003881 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3882 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003883
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003884 /* min/max delay may still have been modified so be sure to
3885 * write the limits value.
3886 */
3887 if (val != dev_priv->rps.cur_freq) {
3888 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003889
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003890 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003891 I915_WRITE(GEN6_RPNSWREQ,
3892 HSW_FREQUENCY(val));
3893 else
3894 I915_WRITE(GEN6_RPNSWREQ,
3895 GEN6_FREQUENCY(val) |
3896 GEN6_OFFSET(0) |
3897 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003898 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003899
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003900 /* Make sure we continue to get interrupts
3901 * until we hit the minimum or maximum frequencies.
3902 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003903 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003904 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003905
Ben Widawskyd5570a72012-09-07 19:43:41 -07003906 POSTING_READ(GEN6_RPNSWREQ);
3907
Ben Widawskyb39fb292014-03-19 18:31:11 -07003908 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003909 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003910}
3911
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003912static void valleyview_set_rps(struct drm_device *dev, u8 val)
3913{
3914 struct drm_i915_private *dev_priv = dev->dev_private;
3915
3916 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3917 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3918 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3919
3920 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3921 "Odd GPU freq value\n"))
3922 val &= ~1;
3923
3924 if (val != dev_priv->rps.cur_freq)
3925 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3926
3927 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3928
3929 dev_priv->rps.cur_freq = val;
3930 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3931}
3932
Deepak S76c3552f2014-01-30 23:08:16 +05303933/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3934 *
3935 * * If Gfx is Idle, then
3936 * 1. Mask Turbo interrupts
3937 * 2. Bring up Gfx clock
3938 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3939 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3940 * 5. Unmask Turbo interrupts
3941*/
3942static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3943{
Deepak S5549d252014-06-28 11:26:11 +05303944 struct drm_device *dev = dev_priv->dev;
3945
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003946 /* CHV and latest VLV don't need to force the gfx clock */
3947 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303948 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3949 return;
3950 }
3951
Deepak S76c3552f2014-01-30 23:08:16 +05303952 /*
3953 * When we are idle. Drop to min voltage state.
3954 */
3955
Ben Widawskyb39fb292014-03-19 18:31:11 -07003956 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303957 return;
3958
3959 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003960 I915_WRITE(GEN6_PMINTRMSK,
3961 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303962
Imre Deak650ad972014-04-18 16:35:02 +03003963 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303964
Ben Widawskyb39fb292014-03-19 18:31:11 -07003965 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303966
3967 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003968 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303969
3970 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003971 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303972 DRM_ERROR("timed out waiting for Punit\n");
3973
Imre Deak650ad972014-04-18 16:35:02 +03003974 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303975
Chris Wilson2876ce72014-03-28 08:03:34 +00003976 I915_WRITE(GEN6_PMINTRMSK,
3977 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303978}
3979
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003980void gen6_rps_idle(struct drm_i915_private *dev_priv)
3981{
Damien Lespiau691bb712013-12-12 14:36:36 +00003982 struct drm_device *dev = dev_priv->dev;
3983
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003984 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003985 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003986 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303987 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003988 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003989 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003990 dev_priv->rps.last_adj = 0;
3991 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003992 mutex_unlock(&dev_priv->rps.hw_lock);
3993}
3994
3995void gen6_rps_boost(struct drm_i915_private *dev_priv)
3996{
3997 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003998 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003999 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004000 dev_priv->rps.last_adj = 0;
4001 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004002 mutex_unlock(&dev_priv->rps.hw_lock);
4003}
4004
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004005void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004006{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004007 if (IS_VALLEYVIEW(dev))
4008 valleyview_set_rps(dev, val);
4009 else
4010 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004011}
4012
Zhe Wang20e49362014-11-04 17:07:05 +00004013static void gen9_disable_rps(struct drm_device *dev)
4014{
4015 struct drm_i915_private *dev_priv = dev->dev_private;
4016
4017 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004018 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004019}
4020
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004021static void gen6_disable_rps(struct drm_device *dev)
4022{
4023 struct drm_i915_private *dev_priv = dev->dev_private;
4024
4025 I915_WRITE(GEN6_RC_CONTROL, 0);
4026 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004027}
4028
Deepak S38807742014-05-23 21:00:15 +05304029static void cherryview_disable_rps(struct drm_device *dev)
4030{
4031 struct drm_i915_private *dev_priv = dev->dev_private;
4032
4033 I915_WRITE(GEN6_RC_CONTROL, 0);
4034}
4035
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004036static void valleyview_disable_rps(struct drm_device *dev)
4037{
4038 struct drm_i915_private *dev_priv = dev->dev_private;
4039
Deepak S98a2e5f2014-08-18 10:35:27 -07004040 /* we're doing forcewake before Disabling RC6,
4041 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004042 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004043
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004044 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004045
Mika Kuoppala59bad942015-01-16 11:34:40 +02004046 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004047}
4048
Ben Widawskydc39fff2013-10-18 12:32:07 -07004049static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4050{
Imre Deak91ca6892014-04-14 20:24:25 +03004051 if (IS_VALLEYVIEW(dev)) {
4052 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4053 mode = GEN6_RC_CTL_RC6_ENABLE;
4054 else
4055 mode = 0;
4056 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004057 if (HAS_RC6p(dev))
4058 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4059 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4060 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4061 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4062
4063 else
4064 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4065 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004066}
4067
Imre Deake6069ca2014-04-18 16:01:02 +03004068static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004069{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004070 /* No RC6 before Ironlake */
4071 if (INTEL_INFO(dev)->gen < 5)
4072 return 0;
4073
Imre Deake6069ca2014-04-18 16:01:02 +03004074 /* RC6 is only on Ironlake mobile not on desktop */
4075 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4076 return 0;
4077
Daniel Vetter456470e2012-08-08 23:35:40 +02004078 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004079 if (enable_rc6 >= 0) {
4080 int mask;
4081
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004082 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004083 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4084 INTEL_RC6pp_ENABLE;
4085 else
4086 mask = INTEL_RC6_ENABLE;
4087
4088 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004089 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4090 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004091
4092 return enable_rc6 & mask;
4093 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004094
Chris Wilson6567d742012-11-10 10:00:06 +00004095 /* Disable RC6 on Ironlake */
4096 if (INTEL_INFO(dev)->gen == 5)
4097 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004098
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004099 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004100 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004101
4102 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004103}
4104
Imre Deake6069ca2014-04-18 16:01:02 +03004105int intel_enable_rc6(const struct drm_device *dev)
4106{
4107 return i915.enable_rc6;
4108}
4109
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004110static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004111{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004112 struct drm_i915_private *dev_priv = dev->dev_private;
4113 uint32_t rp_state_cap;
4114 u32 ddcc_status = 0;
4115 int ret;
4116
4117 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004118 /* All of these values are in units of 50MHz */
4119 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004120 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004121 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004122 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004123 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004124 /* hw_max = RP0 until we check for overclocking */
4125 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4126
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004127 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4128 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4129 ret = sandybridge_pcode_read(dev_priv,
4130 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4131 &ddcc_status);
4132 if (0 == ret)
4133 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004134 clamp_t(u8,
4135 ((ddcc_status >> 8) & 0xff),
4136 dev_priv->rps.min_freq,
4137 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004138 }
4139
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004140 /* Preserve min/max settings in case of re-init */
4141 if (dev_priv->rps.max_freq_softlimit == 0)
4142 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4143
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004144 if (dev_priv->rps.min_freq_softlimit == 0) {
4145 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4146 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004147 /* max(RPe, 450 MHz) */
4148 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004149 else
4150 dev_priv->rps.min_freq_softlimit =
4151 dev_priv->rps.min_freq;
4152 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004153}
4154
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004155/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004156static void gen9_enable_rps(struct drm_device *dev)
4157{
4158 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004159
4160 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4161
Damien Lespiauba1c5542015-01-16 18:07:26 +00004162 gen6_init_rps_frequencies(dev);
4163
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004164 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4165 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4166
4167 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4168 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4169 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4170 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4171 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4172 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4173 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4174 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4175 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4176 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4177 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4178 GEN6_RP_DOWN_IDLE_AVG);
4179
4180 gen6_enable_rps_interrupts(dev);
4181
4182 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4183}
4184
4185static void gen9_enable_rc6(struct drm_device *dev)
4186{
4187 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004188 struct intel_engine_cs *ring;
4189 uint32_t rc6_mask = 0;
4190 int unused;
4191
4192 /* 1a: Software RC state - RC0 */
4193 I915_WRITE(GEN6_RC_STATE, 0);
4194
4195 /* 1b: Get forcewake during program sequence. Although the driver
4196 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004197 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004198
4199 /* 2a: Disable RC states. */
4200 I915_WRITE(GEN6_RC_CONTROL, 0);
4201
4202 /* 2b: Program RC6 thresholds.*/
4203 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4204 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4205 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4206 for_each_ring(ring, dev_priv, unused)
4207 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4208 I915_WRITE(GEN6_RC_SLEEP, 0);
4209 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4210
Zhe Wang38c23522015-01-20 12:23:04 +00004211 /* 2c: Program Coarse Power Gating Policies. */
4212 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4213 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4214
Zhe Wang20e49362014-11-04 17:07:05 +00004215 /* 3a: Enable RC6 */
4216 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4217 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4218 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4219 "on" : "off");
4220 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4221 GEN6_RC_CTL_EI_MODE(1) |
4222 rc6_mask);
4223
Zhe Wang38c23522015-01-20 12:23:04 +00004224 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4225 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4226
Mika Kuoppala59bad942015-01-16 11:34:40 +02004227 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004228
4229}
4230
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004231static void gen8_enable_rps(struct drm_device *dev)
4232{
4233 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004234 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004235 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004236 int unused;
4237
4238 /* 1a: Software RC state - RC0 */
4239 I915_WRITE(GEN6_RC_STATE, 0);
4240
4241 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4242 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004243 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004244
4245 /* 2a: Disable RC states. */
4246 I915_WRITE(GEN6_RC_CONTROL, 0);
4247
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004248 /* Initialize rps frequencies */
4249 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004250
4251 /* 2b: Program RC6 thresholds.*/
4252 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4253 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4254 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4255 for_each_ring(ring, dev_priv, unused)
4256 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4257 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004258 if (IS_BROADWELL(dev))
4259 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4260 else
4261 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004262
4263 /* 3: Enable RC6 */
4264 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4265 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004266 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004267 if (IS_BROADWELL(dev))
4268 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4269 GEN7_RC_CTL_TO_MODE |
4270 rc6_mask);
4271 else
4272 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4273 GEN6_RC_CTL_EI_MODE(1) |
4274 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004275
4276 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004277 I915_WRITE(GEN6_RPNSWREQ,
4278 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4279 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4280 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004281 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4282 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004283
Daniel Vetter7526ed72014-09-29 15:07:19 +02004284 /* Docs recommend 900MHz, and 300 MHz respectively */
4285 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4286 dev_priv->rps.max_freq_softlimit << 24 |
4287 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004288
Daniel Vetter7526ed72014-09-29 15:07:19 +02004289 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4290 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4291 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4292 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004293
Daniel Vetter7526ed72014-09-29 15:07:19 +02004294 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004295
4296 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004297 I915_WRITE(GEN6_RP_CONTROL,
4298 GEN6_RP_MEDIA_TURBO |
4299 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4300 GEN6_RP_MEDIA_IS_GFX |
4301 GEN6_RP_ENABLE |
4302 GEN6_RP_UP_BUSY_AVG |
4303 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004304
Daniel Vetter7526ed72014-09-29 15:07:19 +02004305 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004306
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004307 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4308 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004309
Mika Kuoppala59bad942015-01-16 11:34:40 +02004310 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004311}
4312
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004313static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004314{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004315 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004316 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004317 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004318 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004319 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004320 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004321
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004322 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004323
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004324 /* Here begins a magic sequence of register writes to enable
4325 * auto-downclocking.
4326 *
4327 * Perhaps there might be some value in exposing these to
4328 * userspace...
4329 */
4330 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004331
4332 /* Clear the DBG now so we don't confuse earlier errors */
4333 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4334 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4335 I915_WRITE(GTFIFODBG, gtfifodbg);
4336 }
4337
Mika Kuoppala59bad942015-01-16 11:34:40 +02004338 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004339
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004340 /* Initialize rps frequencies */
4341 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004342
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004343 /* disable the counters and set deterministic thresholds */
4344 I915_WRITE(GEN6_RC_CONTROL, 0);
4345
4346 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4347 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4348 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4349 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4350 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4351
Chris Wilsonb4519512012-05-11 14:29:30 +01004352 for_each_ring(ring, dev_priv, i)
4353 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004354
4355 I915_WRITE(GEN6_RC_SLEEP, 0);
4356 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004357 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004358 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4359 else
4360 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004361 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004362 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4363
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004364 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004365 rc6_mode = intel_enable_rc6(dev_priv->dev);
4366 if (rc6_mode & INTEL_RC6_ENABLE)
4367 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4368
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004369 /* We don't use those on Haswell */
4370 if (!IS_HASWELL(dev)) {
4371 if (rc6_mode & INTEL_RC6p_ENABLE)
4372 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004373
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004374 if (rc6_mode & INTEL_RC6pp_ENABLE)
4375 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4376 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004377
Ben Widawskydc39fff2013-10-18 12:32:07 -07004378 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004379
4380 I915_WRITE(GEN6_RC_CONTROL,
4381 rc6_mask |
4382 GEN6_RC_CTL_EI_MODE(1) |
4383 GEN6_RC_CTL_HW_ENABLE);
4384
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004385 /* Power down if completely idle for over 50ms */
4386 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004387 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004388
Ben Widawsky42c05262012-09-26 10:34:00 -07004389 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004390 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004391 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004392
4393 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4394 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4395 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004396 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004397 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004398 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004399 }
4400
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004401 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004402 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004403
Ben Widawsky31643d52012-09-26 10:34:01 -07004404 rc6vids = 0;
4405 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4406 if (IS_GEN6(dev) && ret) {
4407 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4408 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4409 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4410 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4411 rc6vids &= 0xffff00;
4412 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4413 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4414 if (ret)
4415 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4416 }
4417
Mika Kuoppala59bad942015-01-16 11:34:40 +02004418 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004419}
4420
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004421static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004422{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004423 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004424 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004425 unsigned int gpu_freq;
4426 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004427 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004428 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004429
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004430 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004431
Ben Widawskyeda79642013-10-07 17:15:48 -03004432 policy = cpufreq_cpu_get(0);
4433 if (policy) {
4434 max_ia_freq = policy->cpuinfo.max_freq;
4435 cpufreq_cpu_put(policy);
4436 } else {
4437 /*
4438 * Default to measured freq if none found, PCU will ensure we
4439 * don't go over
4440 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004441 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004442 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004443
4444 /* Convert from kHz to MHz */
4445 max_ia_freq /= 1000;
4446
Ben Widawsky153b4b952013-10-22 22:05:09 -07004447 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004448 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4449 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004450
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004451 /*
4452 * For each potential GPU frequency, load a ring frequency we'd like
4453 * to use for memory access. We do this by specifying the IA frequency
4454 * the PCU should use as a reference to determine the ring frequency.
4455 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004456 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004457 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004458 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004459 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004460
Ben Widawsky46c764d2013-11-02 21:07:49 -07004461 if (INTEL_INFO(dev)->gen >= 8) {
4462 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4463 ring_freq = max(min_ring_freq, gpu_freq);
4464 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004465 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004466 ring_freq = max(min_ring_freq, ring_freq);
4467 /* leave ia_freq as the default, chosen by cpufreq */
4468 } else {
4469 /* On older processors, there is no separate ring
4470 * clock domain, so in order to boost the bandwidth
4471 * of the ring, we need to upclock the CPU (ia_freq).
4472 *
4473 * For GPU frequencies less than 750MHz,
4474 * just use the lowest ring freq.
4475 */
4476 if (gpu_freq < min_freq)
4477 ia_freq = 800;
4478 else
4479 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4480 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4481 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004482
Ben Widawsky42c05262012-09-26 10:34:00 -07004483 sandybridge_pcode_write(dev_priv,
4484 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004485 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4486 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4487 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004488 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004489}
4490
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004491void gen6_update_ring_freq(struct drm_device *dev)
4492{
4493 struct drm_i915_private *dev_priv = dev->dev_private;
4494
4495 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4496 return;
4497
4498 mutex_lock(&dev_priv->rps.hw_lock);
4499 __gen6_update_ring_freq(dev);
4500 mutex_unlock(&dev_priv->rps.hw_lock);
4501}
4502
Ville Syrjälä03af2042014-06-28 02:03:53 +03004503static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304504{
Deepak S095acd52015-01-17 11:05:59 +05304505 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304506 u32 val, rp0;
4507
Deepak S095acd52015-01-17 11:05:59 +05304508 if (dev->pdev->revision >= 0x20) {
4509 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304510
Deepak S095acd52015-01-17 11:05:59 +05304511 switch (INTEL_INFO(dev)->eu_total) {
4512 case 8:
4513 /* (2 * 4) config */
4514 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4515 break;
4516 case 12:
4517 /* (2 * 6) config */
4518 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4519 break;
4520 case 16:
4521 /* (2 * 8) config */
4522 default:
4523 /* Setting (2 * 8) Min RP0 for any other combination */
4524 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4525 break;
4526 }
4527 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4528 } else {
4529 /* For pre-production hardware */
4530 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4531 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4532 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4533 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304534 return rp0;
4535}
4536
4537static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4538{
4539 u32 val, rpe;
4540
4541 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4542 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4543
4544 return rpe;
4545}
4546
Deepak S7707df42014-07-12 18:46:14 +05304547static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4548{
Deepak S095acd52015-01-17 11:05:59 +05304549 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304550 u32 val, rp1;
4551
Deepak S095acd52015-01-17 11:05:59 +05304552 if (dev->pdev->revision >= 0x20) {
4553 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4554 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4555 } else {
4556 /* For pre-production hardware */
4557 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4558 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4559 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4560 }
Deepak S7707df42014-07-12 18:46:14 +05304561 return rp1;
4562}
4563
Ville Syrjälä03af2042014-06-28 02:03:53 +03004564static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304565{
Deepak S095acd52015-01-17 11:05:59 +05304566 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304567 u32 val, rpn;
4568
Deepak S095acd52015-01-17 11:05:59 +05304569 if (dev->pdev->revision >= 0x20) {
4570 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4571 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4572 FB_GFX_FREQ_FUSE_MASK);
4573 } else { /* For pre-production hardware */
4574 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4575 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4576 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4577 }
4578
Deepak S2b6b3a02014-05-27 15:59:30 +05304579 return rpn;
4580}
4581
Deepak Sf8f2b002014-07-10 13:16:21 +05304582static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4583{
4584 u32 val, rp1;
4585
4586 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4587
4588 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4589
4590 return rp1;
4591}
4592
Ville Syrjälä03af2042014-06-28 02:03:53 +03004593static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004594{
4595 u32 val, rp0;
4596
Jani Nikula64936252013-05-22 15:36:20 +03004597 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004598
4599 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4600 /* Clamp to max */
4601 rp0 = min_t(u32, rp0, 0xea);
4602
4603 return rp0;
4604}
4605
4606static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4607{
4608 u32 val, rpe;
4609
Jani Nikula64936252013-05-22 15:36:20 +03004610 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004611 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004612 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004613 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4614
4615 return rpe;
4616}
4617
Ville Syrjälä03af2042014-06-28 02:03:53 +03004618static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004619{
Jani Nikula64936252013-05-22 15:36:20 +03004620 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004621}
4622
Imre Deakae484342014-03-31 15:10:44 +03004623/* Check that the pctx buffer wasn't move under us. */
4624static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4625{
4626 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4627
4628 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4629 dev_priv->vlv_pctx->stolen->start);
4630}
4631
Deepak S38807742014-05-23 21:00:15 +05304632
4633/* Check that the pcbr address is not empty. */
4634static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4635{
4636 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4637
4638 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4639}
4640
4641static void cherryview_setup_pctx(struct drm_device *dev)
4642{
4643 struct drm_i915_private *dev_priv = dev->dev_private;
4644 unsigned long pctx_paddr, paddr;
4645 struct i915_gtt *gtt = &dev_priv->gtt;
4646 u32 pcbr;
4647 int pctx_size = 32*1024;
4648
4649 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4650
4651 pcbr = I915_READ(VLV_PCBR);
4652 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004653 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304654 paddr = (dev_priv->mm.stolen_base +
4655 (gtt->stolen_size - pctx_size));
4656
4657 pctx_paddr = (paddr & (~4095));
4658 I915_WRITE(VLV_PCBR, pctx_paddr);
4659 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004660
4661 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304662}
4663
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004664static void valleyview_setup_pctx(struct drm_device *dev)
4665{
4666 struct drm_i915_private *dev_priv = dev->dev_private;
4667 struct drm_i915_gem_object *pctx;
4668 unsigned long pctx_paddr;
4669 u32 pcbr;
4670 int pctx_size = 24*1024;
4671
Imre Deak17b0c1f2014-02-11 21:39:06 +02004672 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4673
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004674 pcbr = I915_READ(VLV_PCBR);
4675 if (pcbr) {
4676 /* BIOS set it up already, grab the pre-alloc'd space */
4677 int pcbr_offset;
4678
4679 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4680 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4681 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004682 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004683 pctx_size);
4684 goto out;
4685 }
4686
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004687 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4688
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004689 /*
4690 * From the Gunit register HAS:
4691 * The Gfx driver is expected to program this register and ensure
4692 * proper allocation within Gfx stolen memory. For example, this
4693 * register should be programmed such than the PCBR range does not
4694 * overlap with other ranges, such as the frame buffer, protected
4695 * memory, or any other relevant ranges.
4696 */
4697 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4698 if (!pctx) {
4699 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4700 return;
4701 }
4702
4703 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4704 I915_WRITE(VLV_PCBR, pctx_paddr);
4705
4706out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004707 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004708 dev_priv->vlv_pctx = pctx;
4709}
4710
Imre Deakae484342014-03-31 15:10:44 +03004711static void valleyview_cleanup_pctx(struct drm_device *dev)
4712{
4713 struct drm_i915_private *dev_priv = dev->dev_private;
4714
4715 if (WARN_ON(!dev_priv->vlv_pctx))
4716 return;
4717
4718 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4719 dev_priv->vlv_pctx = NULL;
4720}
4721
Imre Deak4e805192014-04-14 20:24:41 +03004722static void valleyview_init_gt_powersave(struct drm_device *dev)
4723{
4724 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004725 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004726
4727 valleyview_setup_pctx(dev);
4728
4729 mutex_lock(&dev_priv->rps.hw_lock);
4730
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004731 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4732 switch ((val >> 6) & 3) {
4733 case 0:
4734 case 1:
4735 dev_priv->mem_freq = 800;
4736 break;
4737 case 2:
4738 dev_priv->mem_freq = 1066;
4739 break;
4740 case 3:
4741 dev_priv->mem_freq = 1333;
4742 break;
4743 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004744 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004745
Imre Deak4e805192014-04-14 20:24:41 +03004746 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4747 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4748 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004749 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004750 dev_priv->rps.max_freq);
4751
4752 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4753 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004754 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004755 dev_priv->rps.efficient_freq);
4756
Deepak Sf8f2b002014-07-10 13:16:21 +05304757 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4758 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004759 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304760 dev_priv->rps.rp1_freq);
4761
Imre Deak4e805192014-04-14 20:24:41 +03004762 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4763 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004764 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004765 dev_priv->rps.min_freq);
4766
4767 /* Preserve min/max settings in case of re-init */
4768 if (dev_priv->rps.max_freq_softlimit == 0)
4769 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4770
4771 if (dev_priv->rps.min_freq_softlimit == 0)
4772 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4773
4774 mutex_unlock(&dev_priv->rps.hw_lock);
4775}
4776
Deepak S38807742014-05-23 21:00:15 +05304777static void cherryview_init_gt_powersave(struct drm_device *dev)
4778{
Deepak S2b6b3a02014-05-27 15:59:30 +05304779 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004780 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304781
Deepak S38807742014-05-23 21:00:15 +05304782 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304783
4784 mutex_lock(&dev_priv->rps.hw_lock);
4785
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004786 mutex_lock(&dev_priv->dpio_lock);
4787 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4788 mutex_unlock(&dev_priv->dpio_lock);
4789
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004790 switch ((val >> 2) & 0x7) {
4791 case 0:
4792 case 1:
4793 dev_priv->rps.cz_freq = 200;
4794 dev_priv->mem_freq = 1600;
4795 break;
4796 case 2:
4797 dev_priv->rps.cz_freq = 267;
4798 dev_priv->mem_freq = 1600;
4799 break;
4800 case 3:
4801 dev_priv->rps.cz_freq = 333;
4802 dev_priv->mem_freq = 2000;
4803 break;
4804 case 4:
4805 dev_priv->rps.cz_freq = 320;
4806 dev_priv->mem_freq = 1600;
4807 break;
4808 case 5:
4809 dev_priv->rps.cz_freq = 400;
4810 dev_priv->mem_freq = 1600;
4811 break;
4812 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004813 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004814
Deepak S2b6b3a02014-05-27 15:59:30 +05304815 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4816 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4817 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004818 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304819 dev_priv->rps.max_freq);
4820
4821 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4822 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004823 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304824 dev_priv->rps.efficient_freq);
4825
Deepak S7707df42014-07-12 18:46:14 +05304826 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4827 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004828 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304829 dev_priv->rps.rp1_freq);
4830
Deepak S2b6b3a02014-05-27 15:59:30 +05304831 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4832 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004833 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304834 dev_priv->rps.min_freq);
4835
Ville Syrjälä1c147622014-08-18 14:42:43 +03004836 WARN_ONCE((dev_priv->rps.max_freq |
4837 dev_priv->rps.efficient_freq |
4838 dev_priv->rps.rp1_freq |
4839 dev_priv->rps.min_freq) & 1,
4840 "Odd GPU freq values\n");
4841
Deepak S2b6b3a02014-05-27 15:59:30 +05304842 /* Preserve min/max settings in case of re-init */
4843 if (dev_priv->rps.max_freq_softlimit == 0)
4844 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4845
4846 if (dev_priv->rps.min_freq_softlimit == 0)
4847 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4848
4849 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304850}
4851
Imre Deak4e805192014-04-14 20:24:41 +03004852static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4853{
4854 valleyview_cleanup_pctx(dev);
4855}
4856
Deepak S38807742014-05-23 21:00:15 +05304857static void cherryview_enable_rps(struct drm_device *dev)
4858{
4859 struct drm_i915_private *dev_priv = dev->dev_private;
4860 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304861 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304862 int i;
4863
4864 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4865
4866 gtfifodbg = I915_READ(GTFIFODBG);
4867 if (gtfifodbg) {
4868 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4869 gtfifodbg);
4870 I915_WRITE(GTFIFODBG, gtfifodbg);
4871 }
4872
4873 cherryview_check_pctx(dev_priv);
4874
4875 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4876 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004877 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304878
Ville Syrjälä160614a2015-01-19 13:50:47 +02004879 /* Disable RC states. */
4880 I915_WRITE(GEN6_RC_CONTROL, 0);
4881
Deepak S38807742014-05-23 21:00:15 +05304882 /* 2a: Program RC6 thresholds.*/
4883 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4884 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4885 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4886
4887 for_each_ring(ring, dev_priv, i)
4888 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4889 I915_WRITE(GEN6_RC_SLEEP, 0);
4890
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004891 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4892 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304893
4894 /* allows RC6 residency counter to work */
4895 I915_WRITE(VLV_COUNTER_CONTROL,
4896 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4897 VLV_MEDIA_RC6_COUNT_EN |
4898 VLV_RENDER_RC6_COUNT_EN));
4899
4900 /* For now we assume BIOS is allocating and populating the PCBR */
4901 pcbr = I915_READ(VLV_PCBR);
4902
Deepak S38807742014-05-23 21:00:15 +05304903 /* 3: Enable RC6 */
4904 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4905 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004906 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304907
4908 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4909
Deepak S2b6b3a02014-05-27 15:59:30 +05304910 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004911 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304912 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4913 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4914 I915_WRITE(GEN6_RP_UP_EI, 66000);
4915 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4916
4917 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4918
4919 /* 5: Enable RPS */
4920 I915_WRITE(GEN6_RP_CONTROL,
4921 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004922 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304923 GEN6_RP_ENABLE |
4924 GEN6_RP_UP_BUSY_AVG |
4925 GEN6_RP_DOWN_IDLE_AVG);
4926
4927 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4928
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004929 /* RPS code assumes GPLL is used */
4930 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4931
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004932 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304933 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4934
4935 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4936 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004937 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304938 dev_priv->rps.cur_freq);
4939
4940 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004941 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304942 dev_priv->rps.efficient_freq);
4943
4944 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4945
Mika Kuoppala59bad942015-01-16 11:34:40 +02004946 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304947}
4948
Jesse Barnes0a073b82013-04-17 15:54:58 -07004949static void valleyview_enable_rps(struct drm_device *dev)
4950{
4951 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004952 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004953 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004954 int i;
4955
4956 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4957
Imre Deakae484342014-03-31 15:10:44 +03004958 valleyview_check_pctx(dev_priv);
4959
Jesse Barnes0a073b82013-04-17 15:54:58 -07004960 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004961 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4962 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004963 I915_WRITE(GTFIFODBG, gtfifodbg);
4964 }
4965
Deepak Sc8d9a592013-11-23 14:55:42 +05304966 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004967 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004968
Ville Syrjälä160614a2015-01-19 13:50:47 +02004969 /* Disable RC states. */
4970 I915_WRITE(GEN6_RC_CONTROL, 0);
4971
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004972 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004973 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4974 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4975 I915_WRITE(GEN6_RP_UP_EI, 66000);
4976 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4977
4978 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4979
4980 I915_WRITE(GEN6_RP_CONTROL,
4981 GEN6_RP_MEDIA_TURBO |
4982 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4983 GEN6_RP_MEDIA_IS_GFX |
4984 GEN6_RP_ENABLE |
4985 GEN6_RP_UP_BUSY_AVG |
4986 GEN6_RP_DOWN_IDLE_CONT);
4987
4988 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4989 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4990 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4991
4992 for_each_ring(ring, dev_priv, i)
4993 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4994
Jesse Barnes2f0aa302013-11-15 09:32:11 -08004995 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004996
4997 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004998 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004999 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5000 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005001 VLV_MEDIA_RC6_COUNT_EN |
5002 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005003
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005004 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005005 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005006
5007 intel_print_rc6_info(dev, rc6_mode);
5008
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005009 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005010
Jani Nikula64936252013-05-22 15:36:20 +03005011 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005012
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005013 /* RPS code assumes GPLL is used */
5014 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5015
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005016 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07005017 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5018
Ben Widawskyb39fb292014-03-19 18:31:11 -07005019 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005020 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005021 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005022 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005023
Ville Syrjälä73008b92013-06-25 19:21:01 +03005024 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005025 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005026 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005027
Ben Widawskyb39fb292014-03-19 18:31:11 -07005028 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005029
Mika Kuoppala59bad942015-01-16 11:34:40 +02005030 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005031}
5032
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005033static unsigned long intel_pxfreq(u32 vidfreq)
5034{
5035 unsigned long freq;
5036 int div = (vidfreq & 0x3f0000) >> 16;
5037 int post = (vidfreq & 0x3000) >> 12;
5038 int pre = (vidfreq & 0x7);
5039
5040 if (!pre)
5041 return 0;
5042
5043 freq = ((div * 133333) / ((1<<post) * pre));
5044
5045 return freq;
5046}
5047
Daniel Vettereb48eb02012-04-26 23:28:12 +02005048static const struct cparams {
5049 u16 i;
5050 u16 t;
5051 u16 m;
5052 u16 c;
5053} cparams[] = {
5054 { 1, 1333, 301, 28664 },
5055 { 1, 1066, 294, 24460 },
5056 { 1, 800, 294, 25192 },
5057 { 0, 1333, 276, 27605 },
5058 { 0, 1066, 276, 27605 },
5059 { 0, 800, 231, 23784 },
5060};
5061
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005062static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005063{
5064 u64 total_count, diff, ret;
5065 u32 count1, count2, count3, m = 0, c = 0;
5066 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5067 int i;
5068
Daniel Vetter02d71952012-08-09 16:44:54 +02005069 assert_spin_locked(&mchdev_lock);
5070
Daniel Vetter20e4d402012-08-08 23:35:39 +02005071 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005072
5073 /* Prevent division-by-zero if we are asking too fast.
5074 * Also, we don't get interesting results if we are polling
5075 * faster than once in 10ms, so just return the saved value
5076 * in such cases.
5077 */
5078 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005079 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005080
5081 count1 = I915_READ(DMIEC);
5082 count2 = I915_READ(DDREC);
5083 count3 = I915_READ(CSIEC);
5084
5085 total_count = count1 + count2 + count3;
5086
5087 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005088 if (total_count < dev_priv->ips.last_count1) {
5089 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005090 diff += total_count;
5091 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005092 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005093 }
5094
5095 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005096 if (cparams[i].i == dev_priv->ips.c_m &&
5097 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005098 m = cparams[i].m;
5099 c = cparams[i].c;
5100 break;
5101 }
5102 }
5103
5104 diff = div_u64(diff, diff1);
5105 ret = ((m * diff) + c);
5106 ret = div_u64(ret, 10);
5107
Daniel Vetter20e4d402012-08-08 23:35:39 +02005108 dev_priv->ips.last_count1 = total_count;
5109 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005110
Daniel Vetter20e4d402012-08-08 23:35:39 +02005111 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005112
5113 return ret;
5114}
5115
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005116unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5117{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005118 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005119 unsigned long val;
5120
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005121 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005122 return 0;
5123
5124 spin_lock_irq(&mchdev_lock);
5125
5126 val = __i915_chipset_val(dev_priv);
5127
5128 spin_unlock_irq(&mchdev_lock);
5129
5130 return val;
5131}
5132
Daniel Vettereb48eb02012-04-26 23:28:12 +02005133unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5134{
5135 unsigned long m, x, b;
5136 u32 tsfs;
5137
5138 tsfs = I915_READ(TSFS);
5139
5140 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5141 x = I915_READ8(TR1);
5142
5143 b = tsfs & TSFS_INTR_MASK;
5144
5145 return ((m * x) / 127) - b;
5146}
5147
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005148static int _pxvid_to_vd(u8 pxvid)
5149{
5150 if (pxvid == 0)
5151 return 0;
5152
5153 if (pxvid >= 8 && pxvid < 31)
5154 pxvid = 31;
5155
5156 return (pxvid + 2) * 125;
5157}
5158
5159static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005160{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005161 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005162 const int vd = _pxvid_to_vd(pxvid);
5163 const int vm = vd - 1125;
5164
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005165 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005166 return vm > 0 ? vm : 0;
5167
5168 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005169}
5170
Daniel Vetter02d71952012-08-09 16:44:54 +02005171static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005172{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005173 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005174 u32 count;
5175
Daniel Vetter02d71952012-08-09 16:44:54 +02005176 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005177
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005178 now = ktime_get_raw_ns();
5179 diffms = now - dev_priv->ips.last_time2;
5180 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005181
5182 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005183 if (!diffms)
5184 return;
5185
5186 count = I915_READ(GFXEC);
5187
Daniel Vetter20e4d402012-08-08 23:35:39 +02005188 if (count < dev_priv->ips.last_count2) {
5189 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005190 diff += count;
5191 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005192 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005193 }
5194
Daniel Vetter20e4d402012-08-08 23:35:39 +02005195 dev_priv->ips.last_count2 = count;
5196 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005197
5198 /* More magic constants... */
5199 diff = diff * 1181;
5200 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005201 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005202}
5203
Daniel Vetter02d71952012-08-09 16:44:54 +02005204void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5205{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005206 struct drm_device *dev = dev_priv->dev;
5207
5208 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005209 return;
5210
Daniel Vetter92703882012-08-09 16:46:01 +02005211 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005212
5213 __i915_update_gfx_val(dev_priv);
5214
Daniel Vetter92703882012-08-09 16:46:01 +02005215 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005216}
5217
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005218static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005219{
5220 unsigned long t, corr, state1, corr2, state2;
5221 u32 pxvid, ext_v;
5222
Daniel Vetter02d71952012-08-09 16:44:54 +02005223 assert_spin_locked(&mchdev_lock);
5224
Ben Widawskyb39fb292014-03-19 18:31:11 -07005225 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005226 pxvid = (pxvid >> 24) & 0x7f;
5227 ext_v = pvid_to_extvid(dev_priv, pxvid);
5228
5229 state1 = ext_v;
5230
5231 t = i915_mch_val(dev_priv);
5232
5233 /* Revel in the empirically derived constants */
5234
5235 /* Correction factor in 1/100000 units */
5236 if (t > 80)
5237 corr = ((t * 2349) + 135940);
5238 else if (t >= 50)
5239 corr = ((t * 964) + 29317);
5240 else /* < 50 */
5241 corr = ((t * 301) + 1004);
5242
5243 corr = corr * ((150142 * state1) / 10000 - 78642);
5244 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005245 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005246
5247 state2 = (corr2 * state1) / 10000;
5248 state2 /= 100; /* convert to mW */
5249
Daniel Vetter02d71952012-08-09 16:44:54 +02005250 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005251
Daniel Vetter20e4d402012-08-08 23:35:39 +02005252 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005253}
5254
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005255unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5256{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005257 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005258 unsigned long val;
5259
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005260 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005261 return 0;
5262
5263 spin_lock_irq(&mchdev_lock);
5264
5265 val = __i915_gfx_val(dev_priv);
5266
5267 spin_unlock_irq(&mchdev_lock);
5268
5269 return val;
5270}
5271
Daniel Vettereb48eb02012-04-26 23:28:12 +02005272/**
5273 * i915_read_mch_val - return value for IPS use
5274 *
5275 * Calculate and return a value for the IPS driver to use when deciding whether
5276 * we have thermal and power headroom to increase CPU or GPU power budget.
5277 */
5278unsigned long i915_read_mch_val(void)
5279{
5280 struct drm_i915_private *dev_priv;
5281 unsigned long chipset_val, graphics_val, ret = 0;
5282
Daniel Vetter92703882012-08-09 16:46:01 +02005283 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005284 if (!i915_mch_dev)
5285 goto out_unlock;
5286 dev_priv = i915_mch_dev;
5287
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005288 chipset_val = __i915_chipset_val(dev_priv);
5289 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005290
5291 ret = chipset_val + graphics_val;
5292
5293out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005294 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005295
5296 return ret;
5297}
5298EXPORT_SYMBOL_GPL(i915_read_mch_val);
5299
5300/**
5301 * i915_gpu_raise - raise GPU frequency limit
5302 *
5303 * Raise the limit; IPS indicates we have thermal headroom.
5304 */
5305bool i915_gpu_raise(void)
5306{
5307 struct drm_i915_private *dev_priv;
5308 bool ret = true;
5309
Daniel Vetter92703882012-08-09 16:46:01 +02005310 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005311 if (!i915_mch_dev) {
5312 ret = false;
5313 goto out_unlock;
5314 }
5315 dev_priv = i915_mch_dev;
5316
Daniel Vetter20e4d402012-08-08 23:35:39 +02005317 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5318 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005319
5320out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005321 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005322
5323 return ret;
5324}
5325EXPORT_SYMBOL_GPL(i915_gpu_raise);
5326
5327/**
5328 * i915_gpu_lower - lower GPU frequency limit
5329 *
5330 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5331 * frequency maximum.
5332 */
5333bool i915_gpu_lower(void)
5334{
5335 struct drm_i915_private *dev_priv;
5336 bool ret = true;
5337
Daniel Vetter92703882012-08-09 16:46:01 +02005338 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005339 if (!i915_mch_dev) {
5340 ret = false;
5341 goto out_unlock;
5342 }
5343 dev_priv = i915_mch_dev;
5344
Daniel Vetter20e4d402012-08-08 23:35:39 +02005345 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5346 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005347
5348out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005349 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005350
5351 return ret;
5352}
5353EXPORT_SYMBOL_GPL(i915_gpu_lower);
5354
5355/**
5356 * i915_gpu_busy - indicate GPU business to IPS
5357 *
5358 * Tell the IPS driver whether or not the GPU is busy.
5359 */
5360bool i915_gpu_busy(void)
5361{
5362 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005363 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005364 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005365 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005366
Daniel Vetter92703882012-08-09 16:46:01 +02005367 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005368 if (!i915_mch_dev)
5369 goto out_unlock;
5370 dev_priv = i915_mch_dev;
5371
Chris Wilsonf047e392012-07-21 12:31:41 +01005372 for_each_ring(ring, dev_priv, i)
5373 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005374
5375out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005376 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005377
5378 return ret;
5379}
5380EXPORT_SYMBOL_GPL(i915_gpu_busy);
5381
5382/**
5383 * i915_gpu_turbo_disable - disable graphics turbo
5384 *
5385 * Disable graphics turbo by resetting the max frequency and setting the
5386 * current frequency to the default.
5387 */
5388bool i915_gpu_turbo_disable(void)
5389{
5390 struct drm_i915_private *dev_priv;
5391 bool ret = true;
5392
Daniel Vetter92703882012-08-09 16:46:01 +02005393 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005394 if (!i915_mch_dev) {
5395 ret = false;
5396 goto out_unlock;
5397 }
5398 dev_priv = i915_mch_dev;
5399
Daniel Vetter20e4d402012-08-08 23:35:39 +02005400 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005401
Daniel Vetter20e4d402012-08-08 23:35:39 +02005402 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005403 ret = false;
5404
5405out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005406 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005407
5408 return ret;
5409}
5410EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5411
5412/**
5413 * Tells the intel_ips driver that the i915 driver is now loaded, if
5414 * IPS got loaded first.
5415 *
5416 * This awkward dance is so that neither module has to depend on the
5417 * other in order for IPS to do the appropriate communication of
5418 * GPU turbo limits to i915.
5419 */
5420static void
5421ips_ping_for_i915_load(void)
5422{
5423 void (*link)(void);
5424
5425 link = symbol_get(ips_link_to_i915_driver);
5426 if (link) {
5427 link();
5428 symbol_put(ips_link_to_i915_driver);
5429 }
5430}
5431
5432void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5433{
Daniel Vetter02d71952012-08-09 16:44:54 +02005434 /* We only register the i915 ips part with intel-ips once everything is
5435 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005436 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005437 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005438 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005439
5440 ips_ping_for_i915_load();
5441}
5442
5443void intel_gpu_ips_teardown(void)
5444{
Daniel Vetter92703882012-08-09 16:46:01 +02005445 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005446 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005447 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005448}
Deepak S76c3552f2014-01-30 23:08:16 +05305449
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005450static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005451{
5452 struct drm_i915_private *dev_priv = dev->dev_private;
5453 u32 lcfuse;
5454 u8 pxw[16];
5455 int i;
5456
5457 /* Disable to program */
5458 I915_WRITE(ECR, 0);
5459 POSTING_READ(ECR);
5460
5461 /* Program energy weights for various events */
5462 I915_WRITE(SDEW, 0x15040d00);
5463 I915_WRITE(CSIEW0, 0x007f0000);
5464 I915_WRITE(CSIEW1, 0x1e220004);
5465 I915_WRITE(CSIEW2, 0x04000004);
5466
5467 for (i = 0; i < 5; i++)
5468 I915_WRITE(PEW + (i * 4), 0);
5469 for (i = 0; i < 3; i++)
5470 I915_WRITE(DEW + (i * 4), 0);
5471
5472 /* Program P-state weights to account for frequency power adjustment */
5473 for (i = 0; i < 16; i++) {
5474 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5475 unsigned long freq = intel_pxfreq(pxvidfreq);
5476 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5477 PXVFREQ_PX_SHIFT;
5478 unsigned long val;
5479
5480 val = vid * vid;
5481 val *= (freq / 1000);
5482 val *= 255;
5483 val /= (127*127*900);
5484 if (val > 0xff)
5485 DRM_ERROR("bad pxval: %ld\n", val);
5486 pxw[i] = val;
5487 }
5488 /* Render standby states get 0 weight */
5489 pxw[14] = 0;
5490 pxw[15] = 0;
5491
5492 for (i = 0; i < 4; i++) {
5493 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5494 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5495 I915_WRITE(PXW + (i * 4), val);
5496 }
5497
5498 /* Adjust magic regs to magic values (more experimental results) */
5499 I915_WRITE(OGW0, 0);
5500 I915_WRITE(OGW1, 0);
5501 I915_WRITE(EG0, 0x00007f00);
5502 I915_WRITE(EG1, 0x0000000e);
5503 I915_WRITE(EG2, 0x000e0000);
5504 I915_WRITE(EG3, 0x68000300);
5505 I915_WRITE(EG4, 0x42000000);
5506 I915_WRITE(EG5, 0x00140031);
5507 I915_WRITE(EG6, 0);
5508 I915_WRITE(EG7, 0);
5509
5510 for (i = 0; i < 8; i++)
5511 I915_WRITE(PXWL + (i * 4), 0);
5512
5513 /* Enable PMON + select events */
5514 I915_WRITE(ECR, 0x80000019);
5515
5516 lcfuse = I915_READ(LCFUSE02);
5517
Daniel Vetter20e4d402012-08-08 23:35:39 +02005518 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005519}
5520
Imre Deakae484342014-03-31 15:10:44 +03005521void intel_init_gt_powersave(struct drm_device *dev)
5522{
Imre Deake6069ca2014-04-18 16:01:02 +03005523 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5524
Deepak S38807742014-05-23 21:00:15 +05305525 if (IS_CHERRYVIEW(dev))
5526 cherryview_init_gt_powersave(dev);
5527 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005528 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005529}
5530
5531void intel_cleanup_gt_powersave(struct drm_device *dev)
5532{
Deepak S38807742014-05-23 21:00:15 +05305533 if (IS_CHERRYVIEW(dev))
5534 return;
5535 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005536 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005537}
5538
Imre Deakdbea3ce2014-12-15 18:59:28 +02005539static void gen6_suspend_rps(struct drm_device *dev)
5540{
5541 struct drm_i915_private *dev_priv = dev->dev_private;
5542
5543 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5544
5545 /*
5546 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5547 * is added for it.
5548 */
5549 if (INTEL_INFO(dev)->gen < 9)
5550 gen6_disable_rps_interrupts(dev);
5551}
5552
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005553/**
5554 * intel_suspend_gt_powersave - suspend PM work and helper threads
5555 * @dev: drm device
5556 *
5557 * We don't want to disable RC6 or other features here, we just want
5558 * to make sure any work we've queued has finished and won't bother
5559 * us while we're suspended.
5560 */
5561void intel_suspend_gt_powersave(struct drm_device *dev)
5562{
5563 struct drm_i915_private *dev_priv = dev->dev_private;
5564
Imre Deakd4d70aa2014-11-19 15:30:04 +02005565 if (INTEL_INFO(dev)->gen < 6)
5566 return;
5567
Imre Deakdbea3ce2014-12-15 18:59:28 +02005568 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305569
5570 /* Force GPU to min freq during suspend */
5571 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005572}
5573
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005574void intel_disable_gt_powersave(struct drm_device *dev)
5575{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005576 struct drm_i915_private *dev_priv = dev->dev_private;
5577
Daniel Vetter930ebb42012-06-29 23:32:16 +02005578 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005579 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305580 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005581 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005582
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005583 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005584 if (INTEL_INFO(dev)->gen >= 9)
5585 gen9_disable_rps(dev);
5586 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305587 cherryview_disable_rps(dev);
5588 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005589 valleyview_disable_rps(dev);
5590 else
5591 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005592
Chris Wilsonc0951f02013-10-10 21:58:50 +01005593 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005594 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005595 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005596}
5597
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005598static void intel_gen6_powersave_work(struct work_struct *work)
5599{
5600 struct drm_i915_private *dev_priv =
5601 container_of(work, struct drm_i915_private,
5602 rps.delayed_resume_work.work);
5603 struct drm_device *dev = dev_priv->dev;
5604
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005605 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005606
Imre Deak3cc134e2014-11-19 15:30:03 +02005607 /*
5608 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5609 * added for it.
5610 */
5611 if (INTEL_INFO(dev)->gen < 9)
5612 gen6_reset_rps_interrupts(dev);
5613
Deepak S38807742014-05-23 21:00:15 +05305614 if (IS_CHERRYVIEW(dev)) {
5615 cherryview_enable_rps(dev);
5616 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005617 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005618 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005619 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005620 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005621 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005622 } else if (IS_BROADWELL(dev)) {
5623 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005624 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005625 } else {
5626 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005627 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005628 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005629 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005630
5631 if (INTEL_INFO(dev)->gen < 9)
5632 gen6_enable_rps_interrupts(dev);
5633
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005634 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005635
5636 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005637}
5638
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005639void intel_enable_gt_powersave(struct drm_device *dev)
5640{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005641 struct drm_i915_private *dev_priv = dev->dev_private;
5642
Yu Zhangf61018b2015-02-10 19:05:52 +08005643 /* Powersaving is controlled by the host when inside a VM */
5644 if (intel_vgpu_active(dev))
5645 return;
5646
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005647 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005648 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005649 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005650 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005651 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305652 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005653 /*
5654 * PCU communication is slow and this doesn't need to be
5655 * done at any specific time, so do this out of our fast path
5656 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005657 *
5658 * We depend on the HW RC6 power context save/restore
5659 * mechanism when entering D3 through runtime PM suspend. So
5660 * disable RPM until RPS/RC6 is properly setup. We can only
5661 * get here via the driver load/system resume/runtime resume
5662 * paths, so the _noresume version is enough (and in case of
5663 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005664 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005665 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5666 round_jiffies_up_relative(HZ)))
5667 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005668 }
5669}
5670
Imre Deakc6df39b2014-04-14 20:24:29 +03005671void intel_reset_gt_powersave(struct drm_device *dev)
5672{
5673 struct drm_i915_private *dev_priv = dev->dev_private;
5674
Imre Deakdbea3ce2014-12-15 18:59:28 +02005675 if (INTEL_INFO(dev)->gen < 6)
5676 return;
5677
5678 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005679 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005680}
5681
Daniel Vetter3107bd42012-10-31 22:52:31 +01005682static void ibx_init_clock_gating(struct drm_device *dev)
5683{
5684 struct drm_i915_private *dev_priv = dev->dev_private;
5685
5686 /*
5687 * On Ibex Peak and Cougar Point, we need to disable clock
5688 * gating for the panel power sequencer or it will fail to
5689 * start up when no ports are active.
5690 */
5691 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5692}
5693
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005694static void g4x_disable_trickle_feed(struct drm_device *dev)
5695{
5696 struct drm_i915_private *dev_priv = dev->dev_private;
5697 int pipe;
5698
Damien Lespiau055e3932014-08-18 13:49:10 +01005699 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005700 I915_WRITE(DSPCNTR(pipe),
5701 I915_READ(DSPCNTR(pipe)) |
5702 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005703 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005704 }
5705}
5706
Ville Syrjälä017636c2013-12-05 15:51:37 +02005707static void ilk_init_lp_watermarks(struct drm_device *dev)
5708{
5709 struct drm_i915_private *dev_priv = dev->dev_private;
5710
5711 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5712 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5713 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5714
5715 /*
5716 * Don't touch WM1S_LP_EN here.
5717 * Doing so could cause underruns.
5718 */
5719}
5720
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005721static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005722{
5723 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005724 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005725
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005726 /*
5727 * Required for FBC
5728 * WaFbcDisableDpfcClockGating:ilk
5729 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005730 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5731 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5732 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005733
5734 I915_WRITE(PCH_3DCGDIS0,
5735 MARIUNIT_CLOCK_GATE_DISABLE |
5736 SVSMUNIT_CLOCK_GATE_DISABLE);
5737 I915_WRITE(PCH_3DCGDIS1,
5738 VFMUNIT_CLOCK_GATE_DISABLE);
5739
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005740 /*
5741 * According to the spec the following bits should be set in
5742 * order to enable memory self-refresh
5743 * The bit 22/21 of 0x42004
5744 * The bit 5 of 0x42020
5745 * The bit 15 of 0x45000
5746 */
5747 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5748 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5749 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005750 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005751 I915_WRITE(DISP_ARB_CTL,
5752 (I915_READ(DISP_ARB_CTL) |
5753 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005754
5755 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005756
5757 /*
5758 * Based on the document from hardware guys the following bits
5759 * should be set unconditionally in order to enable FBC.
5760 * The bit 22 of 0x42000
5761 * The bit 22 of 0x42004
5762 * The bit 7,8,9 of 0x42020.
5763 */
5764 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005765 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005766 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5767 I915_READ(ILK_DISPLAY_CHICKEN1) |
5768 ILK_FBCQ_DIS);
5769 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5770 I915_READ(ILK_DISPLAY_CHICKEN2) |
5771 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005772 }
5773
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005774 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5775
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005776 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5777 I915_READ(ILK_DISPLAY_CHICKEN2) |
5778 ILK_ELPIN_409_SELECT);
5779 I915_WRITE(_3D_CHICKEN2,
5780 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5781 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005782
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005783 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005784 I915_WRITE(CACHE_MODE_0,
5785 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005786
Akash Goel4e046322014-04-04 17:14:38 +05305787 /* WaDisable_RenderCache_OperationalFlush:ilk */
5788 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5789
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005790 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005791
Daniel Vetter3107bd42012-10-31 22:52:31 +01005792 ibx_init_clock_gating(dev);
5793}
5794
5795static void cpt_init_clock_gating(struct drm_device *dev)
5796{
5797 struct drm_i915_private *dev_priv = dev->dev_private;
5798 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005799 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005800
5801 /*
5802 * On Ibex Peak and Cougar Point, we need to disable clock
5803 * gating for the panel power sequencer or it will fail to
5804 * start up when no ports are active.
5805 */
Jesse Barnescd664072013-10-02 10:34:19 -07005806 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5807 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5808 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005809 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5810 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005811 /* The below fixes the weird display corruption, a few pixels shifted
5812 * downward, on (only) LVDS of some HP laptops with IVY.
5813 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005814 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005815 val = I915_READ(TRANS_CHICKEN2(pipe));
5816 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5817 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005818 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005819 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005820 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5821 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5822 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005823 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5824 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005825 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005826 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005827 I915_WRITE(TRANS_CHICKEN1(pipe),
5828 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5829 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005830}
5831
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005832static void gen6_check_mch_setup(struct drm_device *dev)
5833{
5834 struct drm_i915_private *dev_priv = dev->dev_private;
5835 uint32_t tmp;
5836
5837 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005838 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5839 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5840 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005841}
5842
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005843static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005844{
5845 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005846 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005847
Damien Lespiau231e54f2012-10-19 17:55:41 +01005848 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005849
5850 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5851 I915_READ(ILK_DISPLAY_CHICKEN2) |
5852 ILK_ELPIN_409_SELECT);
5853
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005854 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005855 I915_WRITE(_3D_CHICKEN,
5856 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5857
Akash Goel4e046322014-04-04 17:14:38 +05305858 /* WaDisable_RenderCache_OperationalFlush:snb */
5859 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5860
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005861 /*
5862 * BSpec recoomends 8x4 when MSAA is used,
5863 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005864 *
5865 * Note that PS/WM thread counts depend on the WIZ hashing
5866 * disable bit, which we don't touch here, but it's good
5867 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005868 */
5869 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005870 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005871
Ville Syrjälä017636c2013-12-05 15:51:37 +02005872 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005873
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005874 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005875 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005876
5877 I915_WRITE(GEN6_UCGCTL1,
5878 I915_READ(GEN6_UCGCTL1) |
5879 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5880 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5881
5882 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5883 * gating disable must be set. Failure to set it results in
5884 * flickering pixels due to Z write ordering failures after
5885 * some amount of runtime in the Mesa "fire" demo, and Unigine
5886 * Sanctuary and Tropics, and apparently anything else with
5887 * alpha test or pixel discard.
5888 *
5889 * According to the spec, bit 11 (RCCUNIT) must also be set,
5890 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005891 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005892 * WaDisableRCCUnitClockGating:snb
5893 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005894 */
5895 I915_WRITE(GEN6_UCGCTL2,
5896 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5897 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5898
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005899 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005900 I915_WRITE(_3D_CHICKEN3,
5901 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005902
5903 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005904 * Bspec says:
5905 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5906 * 3DSTATE_SF number of SF output attributes is more than 16."
5907 */
5908 I915_WRITE(_3D_CHICKEN3,
5909 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5910
5911 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005912 * According to the spec the following bits should be
5913 * set in order to enable memory self-refresh and fbc:
5914 * The bit21 and bit22 of 0x42000
5915 * The bit21 and bit22 of 0x42004
5916 * The bit5 and bit7 of 0x42020
5917 * The bit14 of 0x70180
5918 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005919 *
5920 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005921 */
5922 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5923 I915_READ(ILK_DISPLAY_CHICKEN1) |
5924 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5925 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5926 I915_READ(ILK_DISPLAY_CHICKEN2) |
5927 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005928 I915_WRITE(ILK_DSPCLK_GATE_D,
5929 I915_READ(ILK_DSPCLK_GATE_D) |
5930 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5931 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005932
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005933 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005934
Daniel Vetter3107bd42012-10-31 22:52:31 +01005935 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005936
5937 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005938}
5939
5940static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5941{
5942 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5943
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005944 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005945 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005946 *
5947 * This actually overrides the dispatch
5948 * mode for all thread types.
5949 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005950 reg &= ~GEN7_FF_SCHED_MASK;
5951 reg |= GEN7_FF_TS_SCHED_HW;
5952 reg |= GEN7_FF_VS_SCHED_HW;
5953 reg |= GEN7_FF_DS_SCHED_HW;
5954
5955 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5956}
5957
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005958static void lpt_init_clock_gating(struct drm_device *dev)
5959{
5960 struct drm_i915_private *dev_priv = dev->dev_private;
5961
5962 /*
5963 * TODO: this bit should only be enabled when really needed, then
5964 * disabled when not needed anymore in order to save power.
5965 */
5966 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5967 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5968 I915_READ(SOUTH_DSPCLK_GATE_D) |
5969 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005970
5971 /* WADPOClockGatingDisable:hsw */
5972 I915_WRITE(_TRANSA_CHICKEN1,
5973 I915_READ(_TRANSA_CHICKEN1) |
5974 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005975}
5976
Imre Deak7d708ee2013-04-17 14:04:50 +03005977static void lpt_suspend_hw(struct drm_device *dev)
5978{
5979 struct drm_i915_private *dev_priv = dev->dev_private;
5980
5981 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5982 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5983
5984 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5985 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5986 }
5987}
5988
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005989static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005990{
5991 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005992 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005993
5994 I915_WRITE(WM3_LP_ILK, 0);
5995 I915_WRITE(WM2_LP_ILK, 0);
5996 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005997
Ben Widawskyab57fff2013-12-12 15:28:04 -08005998 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005999 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006000
Ben Widawskyab57fff2013-12-12 15:28:04 -08006001 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006002 I915_WRITE(CHICKEN_PAR1_1,
6003 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6004
Ben Widawskyab57fff2013-12-12 15:28:04 -08006005 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006006 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006007 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006008 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006009 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006010 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006011
Ben Widawskyab57fff2013-12-12 15:28:04 -08006012 /* WaVSRefCountFullforceMissDisable:bdw */
6013 /* WaDSRefCountFullforceMissDisable:bdw */
6014 I915_WRITE(GEN7_FF_THREAD_MODE,
6015 I915_READ(GEN7_FF_THREAD_MODE) &
6016 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006017
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006018 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6019 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006020
6021 /* WaDisableSDEUnitClockGating:bdw */
6022 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6023 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006024
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006025 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006026}
6027
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006028static void haswell_init_clock_gating(struct drm_device *dev)
6029{
6030 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006031
Ville Syrjälä017636c2013-12-05 15:51:37 +02006032 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006033
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006034 /* L3 caching of data atomics doesn't work -- disable it. */
6035 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6036 I915_WRITE(HSW_ROW_CHICKEN3,
6037 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6038
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006039 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006040 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6041 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6042 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6043
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006044 /* WaVSRefCountFullforceMissDisable:hsw */
6045 I915_WRITE(GEN7_FF_THREAD_MODE,
6046 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006047
Akash Goel4e046322014-04-04 17:14:38 +05306048 /* WaDisable_RenderCache_OperationalFlush:hsw */
6049 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6050
Chia-I Wufe27c602014-01-28 13:29:33 +08006051 /* enable HiZ Raw Stall Optimization */
6052 I915_WRITE(CACHE_MODE_0_GEN7,
6053 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6054
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006055 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006056 I915_WRITE(CACHE_MODE_1,
6057 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006058
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006059 /*
6060 * BSpec recommends 8x4 when MSAA is used,
6061 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006062 *
6063 * Note that PS/WM thread counts depend on the WIZ hashing
6064 * disable bit, which we don't touch here, but it's good
6065 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006066 */
6067 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006068 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006069
Kenneth Graunke94411592014-12-31 16:23:00 -08006070 /* WaSampleCChickenBitEnable:hsw */
6071 I915_WRITE(HALF_SLICE_CHICKEN3,
6072 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6073
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006074 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006075 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6076
Paulo Zanoni90a88642013-05-03 17:23:45 -03006077 /* WaRsPkgCStateDisplayPMReq:hsw */
6078 I915_WRITE(CHICKEN_PAR1_1,
6079 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006080
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006081 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006082}
6083
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006084static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006085{
6086 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006087 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006088
Ville Syrjälä017636c2013-12-05 15:51:37 +02006089 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006090
Damien Lespiau231e54f2012-10-19 17:55:41 +01006091 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006092
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006093 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006094 I915_WRITE(_3D_CHICKEN3,
6095 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6096
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006097 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006098 I915_WRITE(IVB_CHICKEN3,
6099 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6100 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6101
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006102 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006103 if (IS_IVB_GT1(dev))
6104 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6105 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006106
Akash Goel4e046322014-04-04 17:14:38 +05306107 /* WaDisable_RenderCache_OperationalFlush:ivb */
6108 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6109
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006110 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006111 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6112 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6113
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006114 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006115 I915_WRITE(GEN7_L3CNTLREG1,
6116 GEN7_WA_FOR_GEN7_L3_CONTROL);
6117 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006118 GEN7_WA_L3_CHICKEN_MODE);
6119 if (IS_IVB_GT1(dev))
6120 I915_WRITE(GEN7_ROW_CHICKEN2,
6121 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006122 else {
6123 /* must write both registers */
6124 I915_WRITE(GEN7_ROW_CHICKEN2,
6125 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006126 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6127 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006128 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006129
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006130 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006131 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6132 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6133
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006134 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006135 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006136 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006137 */
6138 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006139 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006140
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006141 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006142 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6143 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6144 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6145
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006146 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006147
6148 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006149
Chris Wilson22721342014-03-04 09:41:43 +00006150 if (0) { /* causes HiZ corruption on ivb:gt1 */
6151 /* enable HiZ Raw Stall Optimization */
6152 I915_WRITE(CACHE_MODE_0_GEN7,
6153 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6154 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006155
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006156 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006157 I915_WRITE(CACHE_MODE_1,
6158 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006159
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006160 /*
6161 * BSpec recommends 8x4 when MSAA is used,
6162 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006163 *
6164 * Note that PS/WM thread counts depend on the WIZ hashing
6165 * disable bit, which we don't touch here, but it's good
6166 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006167 */
6168 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006169 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006170
Ben Widawsky20848222012-05-04 18:58:59 -07006171 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6172 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6173 snpcr |= GEN6_MBC_SNPCR_MED;
6174 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006175
Ben Widawskyab5c6082013-04-05 13:12:41 -07006176 if (!HAS_PCH_NOP(dev))
6177 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006178
6179 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006180}
6181
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006182static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6183{
6184 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6185
6186 /*
6187 * Disable trickle feed and enable pnd deadline calculation
6188 */
6189 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6190 I915_WRITE(CBR1_VLV, 0);
6191}
6192
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006193static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006194{
6195 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006196
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006197 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006198
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006199 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006200 I915_WRITE(_3D_CHICKEN3,
6201 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6202
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006203 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006204 I915_WRITE(IVB_CHICKEN3,
6205 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6206 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6207
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006208 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006209 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006210 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006211 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6212 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006213
Akash Goel4e046322014-04-04 17:14:38 +05306214 /* WaDisable_RenderCache_OperationalFlush:vlv */
6215 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6216
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006217 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006218 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6219 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6220
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006221 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006222 I915_WRITE(GEN7_ROW_CHICKEN2,
6223 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6224
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006225 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006226 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6227 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6228 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6229
Ville Syrjälä46680e02014-01-22 21:33:01 +02006230 gen7_setup_fixed_func_scheduler(dev_priv);
6231
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006232 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006233 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006234 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006235 */
6236 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006237 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006238
Akash Goelc98f5062014-03-24 23:00:07 +05306239 /* WaDisableL3Bank2xClockGate:vlv
6240 * Disabling L3 clock gating- MMIO 940c[25] = 1
6241 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6242 I915_WRITE(GEN7_UCGCTL4,
6243 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006244
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006245 /*
6246 * BSpec says this must be set, even though
6247 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6248 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006249 I915_WRITE(CACHE_MODE_1,
6250 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006251
6252 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006253 * BSpec recommends 8x4 when MSAA is used,
6254 * however in practice 16x4 seems fastest.
6255 *
6256 * Note that PS/WM thread counts depend on the WIZ hashing
6257 * disable bit, which we don't touch here, but it's good
6258 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6259 */
6260 I915_WRITE(GEN7_GT_MODE,
6261 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6262
6263 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006264 * WaIncreaseL3CreditsForVLVB0:vlv
6265 * This is the hardware default actually.
6266 */
6267 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6268
6269 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006270 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006271 * Disable clock gating on th GCFG unit to prevent a delay
6272 * in the reporting of vblank events.
6273 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006274 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006275}
6276
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006277static void cherryview_init_clock_gating(struct drm_device *dev)
6278{
6279 struct drm_i915_private *dev_priv = dev->dev_private;
6280
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006281 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006282
Ville Syrjälä232ce332014-04-09 13:28:35 +03006283 /* WaVSRefCountFullforceMissDisable:chv */
6284 /* WaDSRefCountFullforceMissDisable:chv */
6285 I915_WRITE(GEN7_FF_THREAD_MODE,
6286 I915_READ(GEN7_FF_THREAD_MODE) &
6287 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006288
6289 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6290 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6291 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006292
6293 /* WaDisableCSUnitClockGating:chv */
6294 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6295 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006296
6297 /* WaDisableSDEUnitClockGating:chv */
6298 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6299 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006300}
6301
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006302static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006303{
6304 struct drm_i915_private *dev_priv = dev->dev_private;
6305 uint32_t dspclk_gate;
6306
6307 I915_WRITE(RENCLK_GATE_D1, 0);
6308 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6309 GS_UNIT_CLOCK_GATE_DISABLE |
6310 CL_UNIT_CLOCK_GATE_DISABLE);
6311 I915_WRITE(RAMCLK_GATE_D, 0);
6312 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6313 OVRUNIT_CLOCK_GATE_DISABLE |
6314 OVCUNIT_CLOCK_GATE_DISABLE;
6315 if (IS_GM45(dev))
6316 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6317 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006318
6319 /* WaDisableRenderCachePipelinedFlush */
6320 I915_WRITE(CACHE_MODE_0,
6321 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006322
Akash Goel4e046322014-04-04 17:14:38 +05306323 /* WaDisable_RenderCache_OperationalFlush:g4x */
6324 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6325
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006326 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006327}
6328
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006329static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006330{
6331 struct drm_i915_private *dev_priv = dev->dev_private;
6332
6333 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6334 I915_WRITE(RENCLK_GATE_D2, 0);
6335 I915_WRITE(DSPCLK_GATE_D, 0);
6336 I915_WRITE(RAMCLK_GATE_D, 0);
6337 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006338 I915_WRITE(MI_ARB_STATE,
6339 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306340
6341 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6342 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006343}
6344
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006345static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006346{
6347 struct drm_i915_private *dev_priv = dev->dev_private;
6348
6349 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6350 I965_RCC_CLOCK_GATE_DISABLE |
6351 I965_RCPB_CLOCK_GATE_DISABLE |
6352 I965_ISC_CLOCK_GATE_DISABLE |
6353 I965_FBC_CLOCK_GATE_DISABLE);
6354 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006355 I915_WRITE(MI_ARB_STATE,
6356 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306357
6358 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6359 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006360}
6361
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006362static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006363{
6364 struct drm_i915_private *dev_priv = dev->dev_private;
6365 u32 dstate = I915_READ(D_STATE);
6366
6367 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6368 DSTATE_DOT_CLOCK_GATING;
6369 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006370
6371 if (IS_PINEVIEW(dev))
6372 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006373
6374 /* IIR "flip pending" means done if this bit is set */
6375 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006376
6377 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006378 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006379
6380 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6381 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006382
6383 I915_WRITE(MI_ARB_STATE,
6384 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006385}
6386
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006387static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006388{
6389 struct drm_i915_private *dev_priv = dev->dev_private;
6390
6391 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006392
6393 /* interrupts should cause a wake up from C3 */
6394 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6395 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006396
6397 I915_WRITE(MEM_MODE,
6398 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006399}
6400
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006401static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006402{
6403 struct drm_i915_private *dev_priv = dev->dev_private;
6404
6405 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006406
6407 I915_WRITE(MEM_MODE,
6408 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6409 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006410}
6411
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006412void intel_init_clock_gating(struct drm_device *dev)
6413{
6414 struct drm_i915_private *dev_priv = dev->dev_private;
6415
Damien Lespiauc57e3552015-02-09 19:33:05 +00006416 if (dev_priv->display.init_clock_gating)
6417 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006418}
6419
Imre Deak7d708ee2013-04-17 14:04:50 +03006420void intel_suspend_hw(struct drm_device *dev)
6421{
6422 if (HAS_PCH_LPT(dev))
6423 lpt_suspend_hw(dev);
6424}
6425
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006426/* Set up chip specific power management-related functions */
6427void intel_init_pm(struct drm_device *dev)
6428{
6429 struct drm_i915_private *dev_priv = dev->dev_private;
6430
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006431 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006432
Daniel Vetterc921aba2012-04-26 23:28:17 +02006433 /* For cxsr */
6434 if (IS_PINEVIEW(dev))
6435 i915_pineview_get_mem_freq(dev);
6436 else if (IS_GEN5(dev))
6437 i915_ironlake_get_mem_freq(dev);
6438
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006439 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006440 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006441 skl_setup_wm_latency(dev);
6442
Damien Lespiau45db2192015-02-09 19:33:09 +00006443 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006444 dev_priv->display.update_wm = skl_update_wm;
6445 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306446 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006447 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006448
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006449 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6450 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6451 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6452 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6453 dev_priv->display.update_wm = ilk_update_wm;
6454 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6455 } else {
6456 DRM_DEBUG_KMS("Failed to read display plane latency. "
6457 "Disable CxSR\n");
6458 }
6459
6460 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006461 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006462 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006463 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006464 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006465 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006466 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006467 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006468 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006469 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006470 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläae801522015-03-05 21:19:49 +02006471 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306472 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006473 dev_priv->display.init_clock_gating =
6474 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006475 } else if (IS_VALLEYVIEW(dev)) {
6476 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306477 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006478 dev_priv->display.init_clock_gating =
6479 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006480 } else if (IS_PINEVIEW(dev)) {
6481 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6482 dev_priv->is_ddr3,
6483 dev_priv->fsb_freq,
6484 dev_priv->mem_freq)) {
6485 DRM_INFO("failed to find known CxSR latency "
6486 "(found ddr%s fsb freq %d, mem freq %d), "
6487 "disabling CxSR\n",
6488 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6489 dev_priv->fsb_freq, dev_priv->mem_freq);
6490 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006491 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006492 dev_priv->display.update_wm = NULL;
6493 } else
6494 dev_priv->display.update_wm = pineview_update_wm;
6495 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6496 } else if (IS_G4X(dev)) {
6497 dev_priv->display.update_wm = g4x_update_wm;
6498 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6499 } else if (IS_GEN4(dev)) {
6500 dev_priv->display.update_wm = i965_update_wm;
6501 if (IS_CRESTLINE(dev))
6502 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6503 else if (IS_BROADWATER(dev))
6504 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6505 } else if (IS_GEN3(dev)) {
6506 dev_priv->display.update_wm = i9xx_update_wm;
6507 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6508 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006509 } else if (IS_GEN2(dev)) {
6510 if (INTEL_INFO(dev)->num_pipes == 1) {
6511 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006512 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006513 } else {
6514 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006515 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006516 }
6517
6518 if (IS_I85X(dev) || IS_I865G(dev))
6519 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6520 else
6521 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6522 } else {
6523 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006524 }
6525}
6526
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006527int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006528{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006529 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006530
6531 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6532 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6533 return -EAGAIN;
6534 }
6535
6536 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006537 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006538 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6539
6540 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6541 500)) {
6542 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6543 return -ETIMEDOUT;
6544 }
6545
6546 *val = I915_READ(GEN6_PCODE_DATA);
6547 I915_WRITE(GEN6_PCODE_DATA, 0);
6548
6549 return 0;
6550}
6551
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006552int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006553{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006554 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006555
6556 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6557 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6558 return -EAGAIN;
6559 }
6560
6561 I915_WRITE(GEN6_PCODE_DATA, val);
6562 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6563
6564 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6565 500)) {
6566 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6567 return -ETIMEDOUT;
6568 }
6569
6570 I915_WRITE(GEN6_PCODE_DATA, 0);
6571
6572 return 0;
6573}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006574
Ville Syrjälädd06f882014-11-10 22:55:12 +02006575static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006576{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006577 switch (czclk_freq) {
6578 case 200:
6579 return 10;
6580 case 267:
6581 return 12;
6582 case 320:
6583 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006584 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006585 case 400:
6586 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006587 default:
6588 return -1;
6589 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006590}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006591
Ville Syrjälädd06f882014-11-10 22:55:12 +02006592static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6593{
6594 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6595
6596 div = vlv_gpu_freq_div(czclk_freq);
6597 if (div < 0)
6598 return div;
6599
6600 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006601}
6602
Fengguang Wub55dd642014-07-12 11:21:39 +02006603static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006604{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006605 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006606
Ville Syrjälädd06f882014-11-10 22:55:12 +02006607 mul = vlv_gpu_freq_div(czclk_freq);
6608 if (mul < 0)
6609 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006610
Ville Syrjälädd06f882014-11-10 22:55:12 +02006611 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006612}
6613
Fengguang Wub55dd642014-07-12 11:21:39 +02006614static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306615{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006616 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306617
Ville Syrjälädd06f882014-11-10 22:55:12 +02006618 div = vlv_gpu_freq_div(czclk_freq) / 2;
6619 if (div < 0)
6620 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306621
Ville Syrjälädd06f882014-11-10 22:55:12 +02006622 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306623}
6624
Fengguang Wub55dd642014-07-12 11:21:39 +02006625static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306626{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006627 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306628
Ville Syrjälädd06f882014-11-10 22:55:12 +02006629 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6630 if (mul < 0)
6631 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306632
Ville Syrjälä1c147622014-08-18 14:42:43 +03006633 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006634 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306635}
6636
Ville Syrjälä616bc822015-01-23 21:04:25 +02006637int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6638{
6639 if (IS_CHERRYVIEW(dev_priv->dev))
6640 return chv_gpu_freq(dev_priv, val);
6641 else if (IS_VALLEYVIEW(dev_priv->dev))
6642 return byt_gpu_freq(dev_priv, val);
6643 else
6644 return val * GT_FREQUENCY_MULTIPLIER;
6645}
6646
Ville Syrjälä616bc822015-01-23 21:04:25 +02006647int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6648{
Deepak S22b1b2f2014-07-12 14:54:33 +05306649 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006650 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306651 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006652 return byt_freq_opcode(dev_priv, val);
6653 else
6654 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306655}
6656
Daniel Vetterf742a552013-12-06 10:17:53 +01006657void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006658{
6659 struct drm_i915_private *dev_priv = dev->dev_private;
6660
Daniel Vetterf742a552013-12-06 10:17:53 +01006661 mutex_init(&dev_priv->rps.hw_lock);
6662
Chris Wilson907b28c2013-07-19 20:36:52 +01006663 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6664 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006665
Paulo Zanoni33688d92014-03-07 20:08:19 -03006666 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006667}