blob: 1e25e3e26a6787a19a99347e4324ecef7381eb3d [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äfc1ac8d2015-03-05 21:19:52 +0200266static void chv_set_memory_dvfs(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_DDR_SETUP2);
273 if (enable)
274 val &= ~FORCE_DDR_HIGH_FREQ;
275 else
276 val |= FORCE_DDR_HIGH_FREQ;
277 val &= ~FORCE_DDR_LOW_FREQ;
278 val |= FORCE_DDR_FREQ_REQ_ACK;
279 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
280
281 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
282 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
283 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
284
285 mutex_unlock(&dev_priv->rps.hw_lock);
286}
287
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200288static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
289{
290 u32 val;
291
292 mutex_lock(&dev_priv->rps.hw_lock);
293
294 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
295 if (enable)
296 val |= DSP_MAXFIFO_PM5_ENABLE;
297 else
298 val &= ~DSP_MAXFIFO_PM5_ENABLE;
299 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
300
301 mutex_unlock(&dev_priv->rps.hw_lock);
302}
303
Ville Syrjäläf4998962015-03-10 17:02:21 +0200304#define FW_WM(value, plane) \
305 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
306
Imre Deak5209b1f2014-07-01 12:36:17 +0300307void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300308{
Imre Deak5209b1f2014-07-01 12:36:17 +0300309 struct drm_device *dev = dev_priv->dev;
310 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300311
Imre Deak5209b1f2014-07-01 12:36:17 +0300312 if (IS_VALLEYVIEW(dev)) {
313 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200314 if (IS_CHERRYVIEW(dev))
315 chv_set_memory_pm5(dev_priv, enable);
Imre Deak5209b1f2014-07-01 12:36:17 +0300316 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
317 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
318 } else if (IS_PINEVIEW(dev)) {
319 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
320 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
321 I915_WRITE(DSPFW3, val);
322 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
323 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
324 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
325 I915_WRITE(FW_BLC_SELF, val);
326 } else if (IS_I915GM(dev)) {
327 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
328 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
329 I915_WRITE(INSTPM, val);
330 } else {
331 return;
332 }
333
334 DRM_DEBUG_KMS("memory self-refresh is %s\n",
335 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300336}
337
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200338
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300339/*
340 * Latency for FIFO fetches is dependent on several factors:
341 * - memory configuration (speed, channels)
342 * - chipset
343 * - current MCH state
344 * It can be fairly high in some situations, so here we assume a fairly
345 * pessimal value. It's a tradeoff between extra memory fetches (if we
346 * set this value too high, the FIFO will fetch frequently to stay full)
347 * and power consumption (set it too low to save power and we might see
348 * FIFO underruns and display "flicker").
349 *
350 * A value of 5us seems to be a good balance; safe for very low end
351 * platforms but not overly aggressive on lower latency configs.
352 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100353static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300354
Ville Syrjäläb5004722015-03-05 21:19:47 +0200355#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
356 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
357
358static int vlv_get_fifo_size(struct drm_device *dev,
359 enum pipe pipe, int plane)
360{
361 struct drm_i915_private *dev_priv = dev->dev_private;
362 int sprite0_start, sprite1_start, size;
363
364 switch (pipe) {
365 uint32_t dsparb, dsparb2, dsparb3;
366 case PIPE_A:
367 dsparb = I915_READ(DSPARB);
368 dsparb2 = I915_READ(DSPARB2);
369 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
370 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
371 break;
372 case PIPE_B:
373 dsparb = I915_READ(DSPARB);
374 dsparb2 = I915_READ(DSPARB2);
375 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
376 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
377 break;
378 case PIPE_C:
379 dsparb2 = I915_READ(DSPARB2);
380 dsparb3 = I915_READ(DSPARB3);
381 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
382 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
383 break;
384 default:
385 return 0;
386 }
387
388 switch (plane) {
389 case 0:
390 size = sprite0_start;
391 break;
392 case 1:
393 size = sprite1_start - sprite0_start;
394 break;
395 case 2:
396 size = 512 - 1 - sprite1_start;
397 break;
398 default:
399 return 0;
400 }
401
402 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
403 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
404 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
405 size);
406
407 return size;
408}
409
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300410static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300411{
412 struct drm_i915_private *dev_priv = dev->dev_private;
413 uint32_t dsparb = I915_READ(DSPARB);
414 int size;
415
416 size = dsparb & 0x7f;
417 if (plane)
418 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
419
420 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
421 plane ? "B" : "A", size);
422
423 return size;
424}
425
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200426static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300427{
428 struct drm_i915_private *dev_priv = dev->dev_private;
429 uint32_t dsparb = I915_READ(DSPARB);
430 int size;
431
432 size = dsparb & 0x1ff;
433 if (plane)
434 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
435 size >>= 1; /* Convert to cachelines */
436
437 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
438 plane ? "B" : "A", size);
439
440 return size;
441}
442
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300443static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300444{
445 struct drm_i915_private *dev_priv = dev->dev_private;
446 uint32_t dsparb = I915_READ(DSPARB);
447 int size;
448
449 size = dsparb & 0x7f;
450 size >>= 2; /* Convert to cachelines */
451
452 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
453 plane ? "B" : "A",
454 size);
455
456 return size;
457}
458
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300459/* Pineview has different values for various configs */
460static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300461 .fifo_size = PINEVIEW_DISPLAY_FIFO,
462 .max_wm = PINEVIEW_MAX_WM,
463 .default_wm = PINEVIEW_DFT_WM,
464 .guard_size = PINEVIEW_GUARD_WM,
465 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300466};
467static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300468 .fifo_size = PINEVIEW_DISPLAY_FIFO,
469 .max_wm = PINEVIEW_MAX_WM,
470 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
471 .guard_size = PINEVIEW_GUARD_WM,
472 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300473};
474static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300475 .fifo_size = PINEVIEW_CURSOR_FIFO,
476 .max_wm = PINEVIEW_CURSOR_MAX_WM,
477 .default_wm = PINEVIEW_CURSOR_DFT_WM,
478 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
479 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300480};
481static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300482 .fifo_size = PINEVIEW_CURSOR_FIFO,
483 .max_wm = PINEVIEW_CURSOR_MAX_WM,
484 .default_wm = PINEVIEW_CURSOR_DFT_WM,
485 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
486 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300487};
488static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300489 .fifo_size = G4X_FIFO_SIZE,
490 .max_wm = G4X_MAX_WM,
491 .default_wm = G4X_MAX_WM,
492 .guard_size = 2,
493 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300494};
495static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300496 .fifo_size = I965_CURSOR_FIFO,
497 .max_wm = I965_CURSOR_MAX_WM,
498 .default_wm = I965_CURSOR_DFT_WM,
499 .guard_size = 2,
500 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300501};
502static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300503 .fifo_size = VALLEYVIEW_FIFO_SIZE,
504 .max_wm = VALLEYVIEW_MAX_WM,
505 .default_wm = VALLEYVIEW_MAX_WM,
506 .guard_size = 2,
507 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300508};
509static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300510 .fifo_size = I965_CURSOR_FIFO,
511 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
512 .default_wm = I965_CURSOR_DFT_WM,
513 .guard_size = 2,
514 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515};
516static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300517 .fifo_size = I965_CURSOR_FIFO,
518 .max_wm = I965_CURSOR_MAX_WM,
519 .default_wm = I965_CURSOR_DFT_WM,
520 .guard_size = 2,
521 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300522};
523static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300524 .fifo_size = I945_FIFO_SIZE,
525 .max_wm = I915_MAX_WM,
526 .default_wm = 1,
527 .guard_size = 2,
528 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300529};
530static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300531 .fifo_size = I915_FIFO_SIZE,
532 .max_wm = I915_MAX_WM,
533 .default_wm = 1,
534 .guard_size = 2,
535 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300536};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300537static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300538 .fifo_size = I855GM_FIFO_SIZE,
539 .max_wm = I915_MAX_WM,
540 .default_wm = 1,
541 .guard_size = 2,
542 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300543};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300544static const struct intel_watermark_params i830_bc_wm_info = {
545 .fifo_size = I855GM_FIFO_SIZE,
546 .max_wm = I915_MAX_WM/2,
547 .default_wm = 1,
548 .guard_size = 2,
549 .cacheline_size = I830_FIFO_LINE_SIZE,
550};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200551static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300552 .fifo_size = I830_FIFO_SIZE,
553 .max_wm = I915_MAX_WM,
554 .default_wm = 1,
555 .guard_size = 2,
556 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300557};
558
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559/**
560 * intel_calculate_wm - calculate watermark level
561 * @clock_in_khz: pixel clock
562 * @wm: chip FIFO params
563 * @pixel_size: display pixel size
564 * @latency_ns: memory latency for the platform
565 *
566 * Calculate the watermark level (the level at which the display plane will
567 * start fetching from memory again). Each chip has a different display
568 * FIFO size and allocation, so the caller needs to figure that out and pass
569 * in the correct intel_watermark_params structure.
570 *
571 * As the pixel clock runs, the FIFO will be drained at a rate that depends
572 * on the pixel size. When it reaches the watermark level, it'll start
573 * fetching FIFO line sized based chunks from memory until the FIFO fills
574 * past the watermark point. If the FIFO drains completely, a FIFO underrun
575 * will occur, and a display engine hang could result.
576 */
577static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
578 const struct intel_watermark_params *wm,
579 int fifo_size,
580 int pixel_size,
581 unsigned long latency_ns)
582{
583 long entries_required, wm_size;
584
585 /*
586 * Note: we need to make sure we don't overflow for various clock &
587 * latency values.
588 * clocks go from a few thousand to several hundred thousand.
589 * latency is usually a few thousand
590 */
591 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
592 1000;
593 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
594
595 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
596
597 wm_size = fifo_size - (entries_required + wm->guard_size);
598
599 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
600
601 /* Don't promote wm_size to unsigned... */
602 if (wm_size > (long)wm->max_wm)
603 wm_size = wm->max_wm;
604 if (wm_size <= 0)
605 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300606
607 /*
608 * Bspec seems to indicate that the value shouldn't be lower than
609 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
610 * Lets go for 8 which is the burst size since certain platforms
611 * already use a hardcoded 8 (which is what the spec says should be
612 * done).
613 */
614 if (wm_size <= 8)
615 wm_size = 8;
616
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300617 return wm_size;
618}
619
620static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
621{
622 struct drm_crtc *crtc, *enabled = NULL;
623
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100624 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000625 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300626 if (enabled)
627 return NULL;
628 enabled = crtc;
629 }
630 }
631
632 return enabled;
633}
634
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300635static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300636{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300637 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300638 struct drm_i915_private *dev_priv = dev->dev_private;
639 struct drm_crtc *crtc;
640 const struct cxsr_latency *latency;
641 u32 reg;
642 unsigned long wm;
643
644 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
645 dev_priv->fsb_freq, dev_priv->mem_freq);
646 if (!latency) {
647 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300648 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300649 return;
650 }
651
652 crtc = single_enabled_crtc(dev);
653 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100654 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800655 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100656 int clock;
657
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200658 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100659 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300660
661 /* Display SR */
662 wm = intel_calculate_wm(clock, &pineview_display_wm,
663 pineview_display_wm.fifo_size,
664 pixel_size, latency->display_sr);
665 reg = I915_READ(DSPFW1);
666 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200667 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300668 I915_WRITE(DSPFW1, reg);
669 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
670
671 /* cursor SR */
672 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
673 pineview_display_wm.fifo_size,
674 pixel_size, latency->cursor_sr);
675 reg = I915_READ(DSPFW3);
676 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200677 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300678 I915_WRITE(DSPFW3, reg);
679
680 /* Display HPLL off SR */
681 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
682 pineview_display_hplloff_wm.fifo_size,
683 pixel_size, latency->display_hpll_disable);
684 reg = I915_READ(DSPFW3);
685 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200686 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300687 I915_WRITE(DSPFW3, reg);
688
689 /* cursor HPLL off SR */
690 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
691 pineview_display_hplloff_wm.fifo_size,
692 pixel_size, latency->cursor_hpll_disable);
693 reg = I915_READ(DSPFW3);
694 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200695 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300696 I915_WRITE(DSPFW3, reg);
697 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
698
Imre Deak5209b1f2014-07-01 12:36:17 +0300699 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300700 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300701 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300702 }
703}
704
705static bool g4x_compute_wm0(struct drm_device *dev,
706 int plane,
707 const struct intel_watermark_params *display,
708 int display_latency_ns,
709 const struct intel_watermark_params *cursor,
710 int cursor_latency_ns,
711 int *plane_wm,
712 int *cursor_wm)
713{
714 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300715 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300716 int htotal, hdisplay, clock, pixel_size;
717 int line_time_us, line_count;
718 int entries, tlb_miss;
719
720 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000721 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 *cursor_wm = cursor->guard_size;
723 *plane_wm = display->guard_size;
724 return false;
725 }
726
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200727 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100728 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800729 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200730 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800731 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732
733 /* Use the small buffer method to calculate plane watermark */
734 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
735 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
736 if (tlb_miss > 0)
737 entries += tlb_miss;
738 entries = DIV_ROUND_UP(entries, display->cacheline_size);
739 *plane_wm = entries + display->guard_size;
740 if (*plane_wm > (int)display->max_wm)
741 *plane_wm = display->max_wm;
742
743 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200744 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300745 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800746 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300747 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
748 if (tlb_miss > 0)
749 entries += tlb_miss;
750 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
751 *cursor_wm = entries + cursor->guard_size;
752 if (*cursor_wm > (int)cursor->max_wm)
753 *cursor_wm = (int)cursor->max_wm;
754
755 return true;
756}
757
758/*
759 * Check the wm result.
760 *
761 * If any calculated watermark values is larger than the maximum value that
762 * can be programmed into the associated watermark register, that watermark
763 * must be disabled.
764 */
765static bool g4x_check_srwm(struct drm_device *dev,
766 int display_wm, int cursor_wm,
767 const struct intel_watermark_params *display,
768 const struct intel_watermark_params *cursor)
769{
770 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
771 display_wm, cursor_wm);
772
773 if (display_wm > display->max_wm) {
774 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
775 display_wm, display->max_wm);
776 return false;
777 }
778
779 if (cursor_wm > cursor->max_wm) {
780 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
781 cursor_wm, cursor->max_wm);
782 return false;
783 }
784
785 if (!(display_wm || cursor_wm)) {
786 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
787 return false;
788 }
789
790 return true;
791}
792
793static bool g4x_compute_srwm(struct drm_device *dev,
794 int plane,
795 int latency_ns,
796 const struct intel_watermark_params *display,
797 const struct intel_watermark_params *cursor,
798 int *display_wm, int *cursor_wm)
799{
800 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300801 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300802 int hdisplay, htotal, pixel_size, clock;
803 unsigned long line_time_us;
804 int line_count, line_size;
805 int small, large;
806 int entries;
807
808 if (!latency_ns) {
809 *display_wm = *cursor_wm = 0;
810 return false;
811 }
812
813 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200814 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100815 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800816 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200817 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800818 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300819
Ville Syrjälä922044c2014-02-14 14:18:57 +0200820 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300821 line_count = (latency_ns / line_time_us + 1000) / 1000;
822 line_size = hdisplay * pixel_size;
823
824 /* Use the minimum of the small and large buffer method for primary */
825 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
826 large = line_count * line_size;
827
828 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
829 *display_wm = entries + display->guard_size;
830
831 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800832 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300833 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
834 *cursor_wm = entries + cursor->guard_size;
835
836 return g4x_check_srwm(dev,
837 *display_wm, *cursor_wm,
838 display, cursor);
839}
840
Ville Syrjälä15665972015-03-10 16:16:28 +0200841#define FW_WM_VLV(value, plane) \
842 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
843
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200844static void vlv_write_wm_values(struct intel_crtc *crtc,
845 const struct vlv_wm_values *wm)
846{
847 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
848 enum pipe pipe = crtc->pipe;
849
850 I915_WRITE(VLV_DDL(pipe),
851 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
852 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
853 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
854 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
855
Ville Syrjäläae801522015-03-05 21:19:49 +0200856 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200857 FW_WM(wm->sr.plane, SR) |
858 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
859 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
860 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200861 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200862 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
863 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
864 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200865 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200866 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200867
868 if (IS_CHERRYVIEW(dev_priv)) {
869 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200870 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
871 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200872 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200873 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
874 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200875 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200876 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
877 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200878 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200879 FW_WM(wm->sr.plane >> 9, SR_HI) |
880 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
881 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
882 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
883 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
884 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
885 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
886 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
887 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
888 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200889 } else {
890 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200891 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
892 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200893 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200894 FW_WM(wm->sr.plane >> 9, SR_HI) |
895 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
896 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
897 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
898 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
899 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
900 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200901 }
902
903 POSTING_READ(DSPFW1);
904
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200905 dev_priv->wm.vlv = *wm;
906}
907
Ville Syrjälä15665972015-03-10 16:16:28 +0200908#undef FW_WM_VLV
909
Ville Syrjälä341c5262015-03-05 21:19:44 +0200910static uint8_t vlv_compute_drain_latency(struct drm_crtc *crtc,
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200911 struct drm_plane *plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300912{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700913 struct drm_device *dev = crtc->dev;
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200914 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
915 int entries, prec_mult, drain_latency, pixel_size;
916 int clock = intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä341c5262015-03-05 21:19:44 +0200917 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300918
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200919 /*
920 * FIXME the plane might have an fb
921 * but be invisible (eg. due to clipping)
922 */
923 if (!intel_crtc->active || !plane->state->fb)
924 return 0;
925
Gajanan Bhat0948c262014-08-07 01:58:24 +0530926 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200927 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300928
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200929 pixel_size = drm_format_plane_cpp(plane->state->fb->pixel_format, 0);
930
Gajanan Bhat0948c262014-08-07 01:58:24 +0530931 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200932 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300933
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530934 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200935
Ville Syrjälä341c5262015-03-05 21:19:44 +0200936 prec_mult = high_precision;
937 drain_latency = 64 * prec_mult * 4 / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938
Ville Syrjälä341c5262015-03-05 21:19:44 +0200939 if (drain_latency > DRAIN_LATENCY_MASK) {
940 prec_mult /= 2;
941 drain_latency = 64 * prec_mult * 4 / entries;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200942 }
943
Ville Syrjälä341c5262015-03-05 21:19:44 +0200944 if (drain_latency > DRAIN_LATENCY_MASK)
945 drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300946
Ville Syrjälä341c5262015-03-05 21:19:44 +0200947 return drain_latency | (prec_mult == high_precision ?
948 DDL_PRECISION_HIGH : DDL_PRECISION_LOW);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300949}
950
Ville Syrjäläae801522015-03-05 21:19:49 +0200951static int vlv_compute_wm(struct intel_crtc *crtc,
952 struct intel_plane *plane,
953 int fifo_size)
954{
955 int clock, entries, pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300956
Ville Syrjäläae801522015-03-05 21:19:49 +0200957 /*
958 * FIXME the plane might have an fb
959 * but be invisible (eg. due to clipping)
960 */
961 if (!crtc->active || !plane->base.state->fb)
962 return 0;
963
964 pixel_size = drm_format_plane_cpp(plane->base.state->fb->pixel_format, 0);
965 clock = crtc->config->base.adjusted_mode.crtc_clock;
966
967 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
968
969 /*
970 * Set up the watermark such that we don't start issuing memory
971 * requests until we are within PND's max deadline value (256us).
972 * Idea being to be idle as long as possible while still taking
973 * advatange of PND's deadline scheduling. The limit of 8
974 * cachelines (used when the FIFO will anyway drain in less time
975 * than 256us) should match what we would be done if trickle
976 * feed were enabled.
977 */
978 return fifo_size - clamp(DIV_ROUND_UP(256 * entries, 64), 0, fifo_size - 8);
979}
980
981static bool vlv_compute_sr_wm(struct drm_device *dev,
982 struct vlv_wm_values *wm)
983{
984 struct drm_i915_private *dev_priv = to_i915(dev);
985 struct drm_crtc *crtc;
986 enum pipe pipe = INVALID_PIPE;
987 int num_planes = 0;
988 int fifo_size = 0;
989 struct intel_plane *plane;
990
991 wm->sr.cursor = wm->sr.plane = 0;
992
993 crtc = single_enabled_crtc(dev);
994 /* maxfifo not supported on pipe C */
995 if (crtc && to_intel_crtc(crtc)->pipe != PIPE_C) {
996 pipe = to_intel_crtc(crtc)->pipe;
997 num_planes = !!wm->pipe[pipe].primary +
998 !!wm->pipe[pipe].sprite[0] +
999 !!wm->pipe[pipe].sprite[1];
1000 fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
1001 }
1002
1003 if (fifo_size == 0 || num_planes > 1)
1004 return false;
1005
1006 wm->sr.cursor = vlv_compute_wm(to_intel_crtc(crtc),
1007 to_intel_plane(crtc->cursor), 0x3f);
1008
1009 list_for_each_entry(plane, &dev->mode_config.plane_list, base.head) {
1010 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1011 continue;
1012
1013 if (plane->pipe != pipe)
1014 continue;
1015
1016 wm->sr.plane = vlv_compute_wm(to_intel_crtc(crtc),
1017 plane, fifo_size);
1018 if (wm->sr.plane != 0)
1019 break;
1020 }
1021
1022 return true;
1023}
1024
1025static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001026{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001027 struct drm_device *dev = crtc->dev;
1028 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301030 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläae801522015-03-05 21:19:49 +02001031 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001032 struct vlv_wm_values wm = dev_priv->wm.vlv;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001033
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001034 wm.ddl[pipe].primary = vlv_compute_drain_latency(crtc, crtc->primary);
Ville Syrjäläae801522015-03-05 21:19:49 +02001035 wm.pipe[pipe].primary = vlv_compute_wm(intel_crtc,
1036 to_intel_plane(crtc->primary),
1037 vlv_get_fifo_size(dev, pipe, 0));
1038
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001039 wm.ddl[pipe].cursor = vlv_compute_drain_latency(crtc, crtc->cursor);
Ville Syrjäläae801522015-03-05 21:19:49 +02001040 wm.pipe[pipe].cursor = vlv_compute_wm(intel_crtc,
1041 to_intel_plane(crtc->cursor),
1042 0x3f);
1043
1044 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1045
1046 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1047 return;
1048
1049 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1050 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1051 wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1052 wm.sr.plane, wm.sr.cursor);
1053
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +02001054 /*
1055 * FIXME DDR DVFS introduces massive memory latencies which
1056 * are not known to system agent so any deadline specified
1057 * by the display may not be respected. To support DDR DVFS
1058 * the watermark code needs to be rewritten to essentially
1059 * bypass deadline mechanism and rely solely on the
1060 * watermarks. For now disable DDR DVFS.
1061 */
1062 if (IS_CHERRYVIEW(dev_priv))
1063 chv_set_memory_dvfs(dev_priv, false);
1064
Ville Syrjäläae801522015-03-05 21:19:49 +02001065 if (!cxsr_enabled)
1066 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301067
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001068 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001069
1070 if (cxsr_enabled)
1071 intel_set_memory_cxsr(dev_priv, true);
1072}
1073
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301074static void valleyview_update_sprite_wm(struct drm_plane *plane,
1075 struct drm_crtc *crtc,
1076 uint32_t sprite_width,
1077 uint32_t sprite_height,
1078 int pixel_size,
1079 bool enabled, bool scaled)
1080{
1081 struct drm_device *dev = crtc->dev;
1082 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1084 enum pipe pipe = intel_crtc->pipe;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301085 int sprite = to_intel_plane(plane)->plane;
Ville Syrjäläae801522015-03-05 21:19:49 +02001086 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001087 struct vlv_wm_values wm = dev_priv->wm.vlv;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301088
Ville Syrjäläae801522015-03-05 21:19:49 +02001089 if (enabled) {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001090 wm.ddl[pipe].sprite[sprite] =
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001091 vlv_compute_drain_latency(crtc, plane);
Ville Syrjäläae801522015-03-05 21:19:49 +02001092
1093 wm.pipe[pipe].sprite[sprite] =
1094 vlv_compute_wm(intel_crtc,
1095 to_intel_plane(plane),
1096 vlv_get_fifo_size(dev, pipe, sprite+1));
1097 } else {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001098 wm.ddl[pipe].sprite[sprite] = 0;
Ville Syrjäläae801522015-03-05 21:19:49 +02001099 wm.pipe[pipe].sprite[sprite] = 0;
1100 }
1101
1102 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1103
1104 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1105 return;
1106
1107 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: sprite %c=%d, "
1108 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1109 sprite_name(pipe, sprite),
1110 wm.pipe[pipe].sprite[sprite],
1111 wm.sr.plane, wm.sr.cursor);
1112
1113 if (!cxsr_enabled)
1114 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301115
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001116 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjäläae801522015-03-05 21:19:49 +02001117
1118 if (cxsr_enabled)
1119 intel_set_memory_cxsr(dev_priv, true);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301120}
1121
Ville Syrjäläae801522015-03-05 21:19:49 +02001122#define single_plane_enabled(mask) is_power_of_2(mask)
1123
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001124static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001125{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001126 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001127 static const int sr_latency_ns = 12000;
1128 struct drm_i915_private *dev_priv = dev->dev_private;
1129 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1130 int plane_sr, cursor_sr;
1131 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001132 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001133
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001134 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001135 &g4x_wm_info, pessimal_latency_ns,
1136 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001137 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001138 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001139
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001140 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001141 &g4x_wm_info, pessimal_latency_ns,
1142 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001143 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001144 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001145
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001146 if (single_plane_enabled(enabled) &&
1147 g4x_compute_srwm(dev, ffs(enabled) - 1,
1148 sr_latency_ns,
1149 &g4x_wm_info,
1150 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001151 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001152 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001153 } else {
Imre Deak98584252014-06-13 14:54:20 +03001154 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001155 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001156 plane_sr = cursor_sr = 0;
1157 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001158
Ville Syrjäläa5043452014-06-28 02:04:18 +03001159 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1160 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001161 planea_wm, cursora_wm,
1162 planeb_wm, cursorb_wm,
1163 plane_sr, cursor_sr);
1164
1165 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001166 FW_WM(plane_sr, SR) |
1167 FW_WM(cursorb_wm, CURSORB) |
1168 FW_WM(planeb_wm, PLANEB) |
1169 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001170 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001171 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001172 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001173 /* HPLL off in SR has some issues on G4x... disable it */
1174 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001175 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001176 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001177
1178 if (cxsr_enabled)
1179 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001180}
1181
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001182static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001183{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001184 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001185 struct drm_i915_private *dev_priv = dev->dev_private;
1186 struct drm_crtc *crtc;
1187 int srwm = 1;
1188 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001189 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001190
1191 /* Calc sr entries for one plane configs */
1192 crtc = single_enabled_crtc(dev);
1193 if (crtc) {
1194 /* self-refresh has much higher latency */
1195 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001196 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001197 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001198 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001199 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001200 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001201 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001202 unsigned long line_time_us;
1203 int entries;
1204
Ville Syrjälä922044c2014-02-14 14:18:57 +02001205 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001206
1207 /* Use ns/us then divide to preserve precision */
1208 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1209 pixel_size * hdisplay;
1210 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1211 srwm = I965_FIFO_SIZE - entries;
1212 if (srwm < 0)
1213 srwm = 1;
1214 srwm &= 0x1ff;
1215 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1216 entries, srwm);
1217
1218 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001219 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001220 entries = DIV_ROUND_UP(entries,
1221 i965_cursor_wm_info.cacheline_size);
1222 cursor_sr = i965_cursor_wm_info.fifo_size -
1223 (entries + i965_cursor_wm_info.guard_size);
1224
1225 if (cursor_sr > i965_cursor_wm_info.max_wm)
1226 cursor_sr = i965_cursor_wm_info.max_wm;
1227
1228 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1229 "cursor %d\n", srwm, cursor_sr);
1230
Imre Deak98584252014-06-13 14:54:20 +03001231 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001232 } else {
Imre Deak98584252014-06-13 14:54:20 +03001233 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001234 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001235 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001236 }
1237
1238 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1239 srwm);
1240
1241 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001242 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1243 FW_WM(8, CURSORB) |
1244 FW_WM(8, PLANEB) |
1245 FW_WM(8, PLANEA));
1246 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1247 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001248 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001249 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001250
1251 if (cxsr_enabled)
1252 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001253}
1254
Ville Syrjäläf4998962015-03-10 17:02:21 +02001255#undef FW_WM
1256
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001257static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001258{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001259 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001260 struct drm_i915_private *dev_priv = dev->dev_private;
1261 const struct intel_watermark_params *wm_info;
1262 uint32_t fwater_lo;
1263 uint32_t fwater_hi;
1264 int cwm, srwm = 1;
1265 int fifo_size;
1266 int planea_wm, planeb_wm;
1267 struct drm_crtc *crtc, *enabled = NULL;
1268
1269 if (IS_I945GM(dev))
1270 wm_info = &i945_wm_info;
1271 else if (!IS_GEN2(dev))
1272 wm_info = &i915_wm_info;
1273 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001274 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001275
1276 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1277 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001278 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001279 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001280 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001281 if (IS_GEN2(dev))
1282 cpp = 4;
1283
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001284 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001285 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001286 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001287 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001288 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001289 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001290 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001291 if (planea_wm > (long)wm_info->max_wm)
1292 planea_wm = wm_info->max_wm;
1293 }
1294
1295 if (IS_GEN2(dev))
1296 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001297
1298 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1299 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001300 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001301 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001302 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001303 if (IS_GEN2(dev))
1304 cpp = 4;
1305
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001306 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001307 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001308 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001309 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001310 if (enabled == NULL)
1311 enabled = crtc;
1312 else
1313 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001314 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001315 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001316 if (planeb_wm > (long)wm_info->max_wm)
1317 planeb_wm = wm_info->max_wm;
1318 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001319
1320 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1321
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001322 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001323 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001324
Matt Roper59bea882015-02-27 10:12:01 -08001325 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001326
1327 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001328 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001329 enabled = NULL;
1330 }
1331
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001332 /*
1333 * Overlay gets an aggressive default since video jitter is bad.
1334 */
1335 cwm = 2;
1336
1337 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001338 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001339
1340 /* Calc sr entries for one plane configs */
1341 if (HAS_FW_BLC(dev) && enabled) {
1342 /* self-refresh has much higher latency */
1343 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001344 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001345 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001346 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001347 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001348 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001349 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001350 unsigned long line_time_us;
1351 int entries;
1352
Ville Syrjälä922044c2014-02-14 14:18:57 +02001353 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001354
1355 /* Use ns/us then divide to preserve precision */
1356 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1357 pixel_size * hdisplay;
1358 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1359 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1360 srwm = wm_info->fifo_size - entries;
1361 if (srwm < 0)
1362 srwm = 1;
1363
1364 if (IS_I945G(dev) || IS_I945GM(dev))
1365 I915_WRITE(FW_BLC_SELF,
1366 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1367 else if (IS_I915GM(dev))
1368 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1369 }
1370
1371 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1372 planea_wm, planeb_wm, cwm, srwm);
1373
1374 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1375 fwater_hi = (cwm & 0x1f);
1376
1377 /* Set request length to 8 cachelines per fetch */
1378 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1379 fwater_hi = fwater_hi | (1 << 8);
1380
1381 I915_WRITE(FW_BLC, fwater_lo);
1382 I915_WRITE(FW_BLC2, fwater_hi);
1383
Imre Deak5209b1f2014-07-01 12:36:17 +03001384 if (enabled)
1385 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001386}
1387
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001388static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001389{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001390 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001391 struct drm_i915_private *dev_priv = dev->dev_private;
1392 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001393 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001394 uint32_t fwater_lo;
1395 int planea_wm;
1396
1397 crtc = single_enabled_crtc(dev);
1398 if (crtc == NULL)
1399 return;
1400
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001401 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001402 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001403 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001404 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001405 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001406 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1407 fwater_lo |= (3<<8) | planea_wm;
1408
1409 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1410
1411 I915_WRITE(FW_BLC, fwater_lo);
1412}
1413
Ville Syrjälä36587292013-07-05 11:57:16 +03001414static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1415 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001416{
1417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001418 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001419
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001420 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001421
1422 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1423 * adjust the pixel_rate here. */
1424
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001425 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001426 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001427 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001428
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001429 pipe_w = intel_crtc->config->pipe_src_w;
1430 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001431 pfit_w = (pfit_size >> 16) & 0xFFFF;
1432 pfit_h = pfit_size & 0xFFFF;
1433 if (pipe_w < pfit_w)
1434 pipe_w = pfit_w;
1435 if (pipe_h < pfit_h)
1436 pipe_h = pfit_h;
1437
1438 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1439 pfit_w * pfit_h);
1440 }
1441
1442 return pixel_rate;
1443}
1444
Ville Syrjälä37126462013-08-01 16:18:55 +03001445/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001446static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001447 uint32_t latency)
1448{
1449 uint64_t ret;
1450
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001451 if (WARN(latency == 0, "Latency value missing\n"))
1452 return UINT_MAX;
1453
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001454 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1455 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1456
1457 return ret;
1458}
1459
Ville Syrjälä37126462013-08-01 16:18:55 +03001460/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001461static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001462 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1463 uint32_t latency)
1464{
1465 uint32_t ret;
1466
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001467 if (WARN(latency == 0, "Latency value missing\n"))
1468 return UINT_MAX;
1469
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001470 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1471 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1472 ret = DIV_ROUND_UP(ret, 64) + 2;
1473 return ret;
1474}
1475
Ville Syrjälä23297042013-07-05 11:57:17 +03001476static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001477 uint8_t bytes_per_pixel)
1478{
1479 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1480}
1481
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001482struct skl_pipe_wm_parameters {
1483 bool active;
1484 uint32_t pipe_htotal;
1485 uint32_t pixel_rate; /* in KHz */
1486 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1487 struct intel_plane_wm_parameters cursor;
1488};
1489
Imre Deak820c1982013-12-17 14:46:36 +02001490struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001491 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001492 uint32_t pipe_htotal;
1493 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001494 struct intel_plane_wm_parameters pri;
1495 struct intel_plane_wm_parameters spr;
1496 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001497};
1498
Imre Deak820c1982013-12-17 14:46:36 +02001499struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001500 uint16_t pri;
1501 uint16_t spr;
1502 uint16_t cur;
1503 uint16_t fbc;
1504};
1505
Ville Syrjälä240264f2013-08-07 13:29:12 +03001506/* used in computing the new watermarks state */
1507struct intel_wm_config {
1508 unsigned int num_pipes_active;
1509 bool sprites_enabled;
1510 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001511};
1512
Ville Syrjälä37126462013-08-01 16:18:55 +03001513/*
1514 * For both WM_PIPE and WM_LP.
1515 * mem_value must be in 0.1us units.
1516 */
Imre Deak820c1982013-12-17 14:46:36 +02001517static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001518 uint32_t mem_value,
1519 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001520{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001521 uint32_t method1, method2;
1522
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001523 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001524 return 0;
1525
Ville Syrjälä23297042013-07-05 11:57:17 +03001526 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001527 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001528 mem_value);
1529
1530 if (!is_lp)
1531 return method1;
1532
Ville Syrjälä23297042013-07-05 11:57:17 +03001533 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001534 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001535 params->pri.horiz_pixels,
1536 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001537 mem_value);
1538
1539 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001540}
1541
Ville Syrjälä37126462013-08-01 16:18:55 +03001542/*
1543 * For both WM_PIPE and WM_LP.
1544 * mem_value must be in 0.1us units.
1545 */
Imre Deak820c1982013-12-17 14:46:36 +02001546static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001547 uint32_t mem_value)
1548{
1549 uint32_t method1, method2;
1550
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001551 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001552 return 0;
1553
Ville Syrjälä23297042013-07-05 11:57:17 +03001554 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001555 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001556 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001557 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001558 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001559 params->spr.horiz_pixels,
1560 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001561 mem_value);
1562 return min(method1, method2);
1563}
1564
Ville Syrjälä37126462013-08-01 16:18:55 +03001565/*
1566 * For both WM_PIPE and WM_LP.
1567 * mem_value must be in 0.1us units.
1568 */
Imre Deak820c1982013-12-17 14:46:36 +02001569static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001570 uint32_t mem_value)
1571{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001572 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001573 return 0;
1574
Ville Syrjälä23297042013-07-05 11:57:17 +03001575 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001576 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001577 params->cur.horiz_pixels,
1578 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001579 mem_value);
1580}
1581
Paulo Zanonicca32e92013-05-31 11:45:06 -03001582/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001583static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001584 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001585{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001586 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001587 return 0;
1588
Ville Syrjälä23297042013-07-05 11:57:17 +03001589 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001590 params->pri.horiz_pixels,
1591 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001592}
1593
Ville Syrjälä158ae642013-08-07 13:28:19 +03001594static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1595{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001596 if (INTEL_INFO(dev)->gen >= 8)
1597 return 3072;
1598 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001599 return 768;
1600 else
1601 return 512;
1602}
1603
Ville Syrjälä4e975082014-03-07 18:32:11 +02001604static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1605 int level, bool is_sprite)
1606{
1607 if (INTEL_INFO(dev)->gen >= 8)
1608 /* BDW primary/sprite plane watermarks */
1609 return level == 0 ? 255 : 2047;
1610 else if (INTEL_INFO(dev)->gen >= 7)
1611 /* IVB/HSW primary/sprite plane watermarks */
1612 return level == 0 ? 127 : 1023;
1613 else if (!is_sprite)
1614 /* ILK/SNB primary plane watermarks */
1615 return level == 0 ? 127 : 511;
1616 else
1617 /* ILK/SNB sprite plane watermarks */
1618 return level == 0 ? 63 : 255;
1619}
1620
1621static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1622 int level)
1623{
1624 if (INTEL_INFO(dev)->gen >= 7)
1625 return level == 0 ? 63 : 255;
1626 else
1627 return level == 0 ? 31 : 63;
1628}
1629
1630static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1631{
1632 if (INTEL_INFO(dev)->gen >= 8)
1633 return 31;
1634 else
1635 return 15;
1636}
1637
Ville Syrjälä158ae642013-08-07 13:28:19 +03001638/* Calculate the maximum primary/sprite plane watermark */
1639static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1640 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001641 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001642 enum intel_ddb_partitioning ddb_partitioning,
1643 bool is_sprite)
1644{
1645 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001646
1647 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001648 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001649 return 0;
1650
1651 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001652 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001653 fifo_size /= INTEL_INFO(dev)->num_pipes;
1654
1655 /*
1656 * For some reason the non self refresh
1657 * FIFO size is only half of the self
1658 * refresh FIFO size on ILK/SNB.
1659 */
1660 if (INTEL_INFO(dev)->gen <= 6)
1661 fifo_size /= 2;
1662 }
1663
Ville Syrjälä240264f2013-08-07 13:29:12 +03001664 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001665 /* level 0 is always calculated with 1:1 split */
1666 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1667 if (is_sprite)
1668 fifo_size *= 5;
1669 fifo_size /= 6;
1670 } else {
1671 fifo_size /= 2;
1672 }
1673 }
1674
1675 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001676 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001677}
1678
1679/* Calculate the maximum cursor plane watermark */
1680static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001681 int level,
1682 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001683{
1684 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001685 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001686 return 64;
1687
1688 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001689 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001690}
1691
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001692static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001693 int level,
1694 const struct intel_wm_config *config,
1695 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001696 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001697{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001698 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1699 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1700 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001701 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001702}
1703
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001704static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1705 int level,
1706 struct ilk_wm_maximums *max)
1707{
1708 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1709 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1710 max->cur = ilk_cursor_wm_reg_max(dev, level);
1711 max->fbc = ilk_fbc_wm_reg_max(dev);
1712}
1713
Ville Syrjäläd9395652013-10-09 19:18:10 +03001714static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001715 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001716 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001717{
1718 bool ret;
1719
1720 /* already determined to be invalid? */
1721 if (!result->enable)
1722 return false;
1723
1724 result->enable = result->pri_val <= max->pri &&
1725 result->spr_val <= max->spr &&
1726 result->cur_val <= max->cur;
1727
1728 ret = result->enable;
1729
1730 /*
1731 * HACK until we can pre-compute everything,
1732 * and thus fail gracefully if LP0 watermarks
1733 * are exceeded...
1734 */
1735 if (level == 0 && !result->enable) {
1736 if (result->pri_val > max->pri)
1737 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1738 level, result->pri_val, max->pri);
1739 if (result->spr_val > max->spr)
1740 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1741 level, result->spr_val, max->spr);
1742 if (result->cur_val > max->cur)
1743 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1744 level, result->cur_val, max->cur);
1745
1746 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1747 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1748 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1749 result->enable = true;
1750 }
1751
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001752 return ret;
1753}
1754
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001755static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001756 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001757 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001758 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001759{
1760 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1761 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1762 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1763
1764 /* WM1+ latency values stored in 0.5us units */
1765 if (level > 0) {
1766 pri_latency *= 5;
1767 spr_latency *= 5;
1768 cur_latency *= 5;
1769 }
1770
1771 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1772 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1773 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1774 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1775 result->enable = true;
1776}
1777
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001778static uint32_t
1779hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001780{
1781 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001783 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001784 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001785
Matt Roper3ef00282015-03-09 10:19:24 -07001786 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001787 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001788
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001789 /* The WM are computed with base on how long it takes to fill a single
1790 * row at the given clock rate, multiplied by 8.
1791 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001792 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1793 mode->crtc_clock);
1794 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001795 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001796
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001797 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1798 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001799}
1800
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001801static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001802{
1803 struct drm_i915_private *dev_priv = dev->dev_private;
1804
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001805 if (IS_GEN9(dev)) {
1806 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001807 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001808 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001809
1810 /* read the first set of memory latencies[0:3] */
1811 val = 0; /* data0 to be programmed to 0 for first set */
1812 mutex_lock(&dev_priv->rps.hw_lock);
1813 ret = sandybridge_pcode_read(dev_priv,
1814 GEN9_PCODE_READ_MEM_LATENCY,
1815 &val);
1816 mutex_unlock(&dev_priv->rps.hw_lock);
1817
1818 if (ret) {
1819 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1820 return;
1821 }
1822
1823 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1824 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1825 GEN9_MEM_LATENCY_LEVEL_MASK;
1826 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1827 GEN9_MEM_LATENCY_LEVEL_MASK;
1828 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1829 GEN9_MEM_LATENCY_LEVEL_MASK;
1830
1831 /* read the second set of memory latencies[4:7] */
1832 val = 1; /* data0 to be programmed to 1 for second set */
1833 mutex_lock(&dev_priv->rps.hw_lock);
1834 ret = sandybridge_pcode_read(dev_priv,
1835 GEN9_PCODE_READ_MEM_LATENCY,
1836 &val);
1837 mutex_unlock(&dev_priv->rps.hw_lock);
1838 if (ret) {
1839 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1840 return;
1841 }
1842
1843 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1844 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1845 GEN9_MEM_LATENCY_LEVEL_MASK;
1846 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1847 GEN9_MEM_LATENCY_LEVEL_MASK;
1848 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1849 GEN9_MEM_LATENCY_LEVEL_MASK;
1850
Vandana Kannan367294b2014-11-04 17:06:46 +00001851 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001852 * WaWmMemoryReadLatency:skl
1853 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001854 * punit doesn't take into account the read latency so we need
1855 * to add 2us to the various latency levels we retrieve from
1856 * the punit.
1857 * - W0 is a bit special in that it's the only level that
1858 * can't be disabled if we want to have display working, so
1859 * we always add 2us there.
1860 * - For levels >=1, punit returns 0us latency when they are
1861 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001862 *
1863 * Additionally, if a level n (n > 1) has a 0us latency, all
1864 * levels m (m >= n) need to be disabled. We make sure to
1865 * sanitize the values out of the punit to satisfy this
1866 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001867 */
1868 wm[0] += 2;
1869 for (level = 1; level <= max_level; level++)
1870 if (wm[level] != 0)
1871 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001872 else {
1873 for (i = level + 1; i <= max_level; i++)
1874 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001875
Vandana Kannan4f947382014-11-04 17:06:47 +00001876 break;
1877 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001878 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001879 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1880
1881 wm[0] = (sskpd >> 56) & 0xFF;
1882 if (wm[0] == 0)
1883 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001884 wm[1] = (sskpd >> 4) & 0xFF;
1885 wm[2] = (sskpd >> 12) & 0xFF;
1886 wm[3] = (sskpd >> 20) & 0x1FF;
1887 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001888 } else if (INTEL_INFO(dev)->gen >= 6) {
1889 uint32_t sskpd = I915_READ(MCH_SSKPD);
1890
1891 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1892 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1893 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1894 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001895 } else if (INTEL_INFO(dev)->gen >= 5) {
1896 uint32_t mltr = I915_READ(MLTR_ILK);
1897
1898 /* ILK primary LP0 latency is 700 ns */
1899 wm[0] = 7;
1900 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1901 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001902 }
1903}
1904
Ville Syrjälä53615a52013-08-01 16:18:50 +03001905static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1906{
1907 /* ILK sprite LP0 latency is 1300 ns */
1908 if (INTEL_INFO(dev)->gen == 5)
1909 wm[0] = 13;
1910}
1911
1912static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1913{
1914 /* ILK cursor LP0 latency is 1300 ns */
1915 if (INTEL_INFO(dev)->gen == 5)
1916 wm[0] = 13;
1917
1918 /* WaDoubleCursorLP3Latency:ivb */
1919 if (IS_IVYBRIDGE(dev))
1920 wm[3] *= 2;
1921}
1922
Damien Lespiau546c81f2014-05-13 15:30:26 +01001923int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001924{
1925 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001926 if (IS_GEN9(dev))
1927 return 7;
1928 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001929 return 4;
1930 else if (INTEL_INFO(dev)->gen >= 6)
1931 return 3;
1932 else
1933 return 2;
1934}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001935
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001936static void intel_print_wm_latency(struct drm_device *dev,
1937 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001938 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001939{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001940 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001941
1942 for (level = 0; level <= max_level; level++) {
1943 unsigned int latency = wm[level];
1944
1945 if (latency == 0) {
1946 DRM_ERROR("%s WM%d latency not provided\n",
1947 name, level);
1948 continue;
1949 }
1950
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001951 /*
1952 * - latencies are in us on gen9.
1953 * - before then, WM1+ latency values are in 0.5us units
1954 */
1955 if (IS_GEN9(dev))
1956 latency *= 10;
1957 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001958 latency *= 5;
1959
1960 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1961 name, level, wm[level],
1962 latency / 10, latency % 10);
1963 }
1964}
1965
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001966static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1967 uint16_t wm[5], uint16_t min)
1968{
1969 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1970
1971 if (wm[0] >= min)
1972 return false;
1973
1974 wm[0] = max(wm[0], min);
1975 for (level = 1; level <= max_level; level++)
1976 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1977
1978 return true;
1979}
1980
1981static void snb_wm_latency_quirk(struct drm_device *dev)
1982{
1983 struct drm_i915_private *dev_priv = dev->dev_private;
1984 bool changed;
1985
1986 /*
1987 * The BIOS provided WM memory latency values are often
1988 * inadequate for high resolution displays. Adjust them.
1989 */
1990 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1991 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1992 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1993
1994 if (!changed)
1995 return;
1996
1997 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1998 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1999 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2000 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2001}
2002
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002003static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002004{
2005 struct drm_i915_private *dev_priv = dev->dev_private;
2006
2007 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2008
2009 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2010 sizeof(dev_priv->wm.pri_latency));
2011 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2012 sizeof(dev_priv->wm.pri_latency));
2013
2014 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2015 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002016
2017 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2018 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2019 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002020
2021 if (IS_GEN6(dev))
2022 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002023}
2024
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002025static void skl_setup_wm_latency(struct drm_device *dev)
2026{
2027 struct drm_i915_private *dev_priv = dev->dev_private;
2028
2029 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2030 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2031}
2032
Imre Deak820c1982013-12-17 14:46:36 +02002033static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002034 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002035{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002036 struct drm_device *dev = crtc->dev;
2037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2038 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002039 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002040
Matt Roper3ef00282015-03-09 10:19:24 -07002041 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002042 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002043
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002044 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002045 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002046 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roperc9f038a2015-03-09 11:06:02 -07002047
2048 if (crtc->primary->state->fb) {
2049 p->pri.enabled = true;
2050 p->pri.bytes_per_pixel =
2051 crtc->primary->state->fb->bits_per_pixel / 8;
2052 } else {
2053 p->pri.enabled = false;
2054 p->pri.bytes_per_pixel = 0;
2055 }
2056
2057 if (crtc->cursor->state->fb) {
2058 p->cur.enabled = true;
2059 p->cur.bytes_per_pixel = 4;
2060 } else {
2061 p->cur.enabled = false;
2062 p->cur.bytes_per_pixel = 0;
2063 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002064 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08002065 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002066
Matt Roperaf2b6532014-04-01 15:22:32 -07002067 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002068 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002069
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002070 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002071 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002072 break;
2073 }
2074 }
2075}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002076
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002077static void ilk_compute_wm_config(struct drm_device *dev,
2078 struct intel_wm_config *config)
2079{
2080 struct intel_crtc *intel_crtc;
2081
2082 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002083 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002084 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2085
2086 if (!wm->pipe_enabled)
2087 continue;
2088
2089 config->sprites_enabled |= wm->sprites_enabled;
2090 config->sprites_scaled |= wm->sprites_scaled;
2091 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002092 }
2093}
2094
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002095/* Compute new watermarks for the pipe */
2096static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002097 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002098 struct intel_pipe_wm *pipe_wm)
2099{
2100 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002101 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002102 int level, max_level = ilk_wm_max_level(dev);
2103 /* LP0 watermark maximums depend on this pipe alone */
2104 struct intel_wm_config config = {
2105 .num_pipes_active = 1,
2106 .sprites_enabled = params->spr.enabled,
2107 .sprites_scaled = params->spr.scaled,
2108 };
Imre Deak820c1982013-12-17 14:46:36 +02002109 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002110
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002111 pipe_wm->pipe_enabled = params->active;
2112 pipe_wm->sprites_enabled = params->spr.enabled;
2113 pipe_wm->sprites_scaled = params->spr.scaled;
2114
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002115 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2116 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2117 max_level = 1;
2118
2119 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2120 if (params->spr.scaled)
2121 max_level = 0;
2122
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002123 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002124
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002125 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002126 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002127
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002128 /* LP0 watermarks always use 1/2 DDB partitioning */
2129 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2130
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002131 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002132 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2133 return false;
2134
2135 ilk_compute_wm_reg_maximums(dev, 1, &max);
2136
2137 for (level = 1; level <= max_level; level++) {
2138 struct intel_wm_level wm = {};
2139
2140 ilk_compute_wm_level(dev_priv, level, params, &wm);
2141
2142 /*
2143 * Disable any watermark level that exceeds the
2144 * register maximums since such watermarks are
2145 * always invalid.
2146 */
2147 if (!ilk_validate_wm_level(level, &max, &wm))
2148 break;
2149
2150 pipe_wm->wm[level] = wm;
2151 }
2152
2153 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002154}
2155
2156/*
2157 * Merge the watermarks from all active pipes for a specific level.
2158 */
2159static void ilk_merge_wm_level(struct drm_device *dev,
2160 int level,
2161 struct intel_wm_level *ret_wm)
2162{
2163 const struct intel_crtc *intel_crtc;
2164
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002165 ret_wm->enable = true;
2166
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002167 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002168 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2169 const struct intel_wm_level *wm = &active->wm[level];
2170
2171 if (!active->pipe_enabled)
2172 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002173
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002174 /*
2175 * The watermark values may have been used in the past,
2176 * so we must maintain them in the registers for some
2177 * time even if the level is now disabled.
2178 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002179 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002180 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002181
2182 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2183 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2184 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2185 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2186 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002187}
2188
2189/*
2190 * Merge all low power watermarks for all active pipes.
2191 */
2192static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002193 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002194 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002195 struct intel_pipe_wm *merged)
2196{
2197 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002198 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002199
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002200 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2201 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2202 config->num_pipes_active > 1)
2203 return;
2204
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002205 /* ILK: FBC WM must be disabled always */
2206 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002207
2208 /* merge each WM1+ level */
2209 for (level = 1; level <= max_level; level++) {
2210 struct intel_wm_level *wm = &merged->wm[level];
2211
2212 ilk_merge_wm_level(dev, level, wm);
2213
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002214 if (level > last_enabled_level)
2215 wm->enable = false;
2216 else if (!ilk_validate_wm_level(level, max, wm))
2217 /* make sure all following levels get disabled */
2218 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002219
2220 /*
2221 * The spec says it is preferred to disable
2222 * FBC WMs instead of disabling a WM level.
2223 */
2224 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002225 if (wm->enable)
2226 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002227 wm->fbc_val = 0;
2228 }
2229 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002230
2231 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2232 /*
2233 * FIXME this is racy. FBC might get enabled later.
2234 * What we should check here is whether FBC can be
2235 * enabled sometime later.
2236 */
2237 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2238 for (level = 2; level <= max_level; level++) {
2239 struct intel_wm_level *wm = &merged->wm[level];
2240
2241 wm->enable = false;
2242 }
2243 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002244}
2245
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002246static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2247{
2248 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2249 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2250}
2251
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002252/* The value we need to program into the WM_LPx latency field */
2253static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2254{
2255 struct drm_i915_private *dev_priv = dev->dev_private;
2256
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002257 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002258 return 2 * level;
2259 else
2260 return dev_priv->wm.pri_latency[level];
2261}
2262
Imre Deak820c1982013-12-17 14:46:36 +02002263static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002264 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002265 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002266 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002267{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002268 struct intel_crtc *intel_crtc;
2269 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002270
Ville Syrjälä0362c782013-10-09 19:17:57 +03002271 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002272 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002273
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002274 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002275 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002276 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002277
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002278 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002279
Ville Syrjälä0362c782013-10-09 19:17:57 +03002280 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002281
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002282 /*
2283 * Maintain the watermark values even if the level is
2284 * disabled. Doing otherwise could cause underruns.
2285 */
2286 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002287 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002288 (r->pri_val << WM1_LP_SR_SHIFT) |
2289 r->cur_val;
2290
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002291 if (r->enable)
2292 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2293
Ville Syrjälä416f4722013-11-02 21:07:46 -07002294 if (INTEL_INFO(dev)->gen >= 8)
2295 results->wm_lp[wm_lp - 1] |=
2296 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2297 else
2298 results->wm_lp[wm_lp - 1] |=
2299 r->fbc_val << WM1_LP_FBC_SHIFT;
2300
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002301 /*
2302 * Always set WM1S_LP_EN when spr_val != 0, even if the
2303 * level is disabled. Doing otherwise could cause underruns.
2304 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002305 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2306 WARN_ON(wm_lp != 1);
2307 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2308 } else
2309 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002310 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002311
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002312 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002313 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002314 enum pipe pipe = intel_crtc->pipe;
2315 const struct intel_wm_level *r =
2316 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002317
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002318 if (WARN_ON(!r->enable))
2319 continue;
2320
2321 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2322
2323 results->wm_pipe[pipe] =
2324 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2325 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2326 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002327 }
2328}
2329
Paulo Zanoni861f3382013-05-31 10:19:21 -03002330/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2331 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002332static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002333 struct intel_pipe_wm *r1,
2334 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002335{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002336 int level, max_level = ilk_wm_max_level(dev);
2337 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002338
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002339 for (level = 1; level <= max_level; level++) {
2340 if (r1->wm[level].enable)
2341 level1 = level;
2342 if (r2->wm[level].enable)
2343 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002344 }
2345
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002346 if (level1 == level2) {
2347 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002348 return r2;
2349 else
2350 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002351 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002352 return r1;
2353 } else {
2354 return r2;
2355 }
2356}
2357
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002358/* dirty bits used to track which watermarks need changes */
2359#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2360#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2361#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2362#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2363#define WM_DIRTY_FBC (1 << 24)
2364#define WM_DIRTY_DDB (1 << 25)
2365
Damien Lespiau055e3932014-08-18 13:49:10 +01002366static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002367 const struct ilk_wm_values *old,
2368 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002369{
2370 unsigned int dirty = 0;
2371 enum pipe pipe;
2372 int wm_lp;
2373
Damien Lespiau055e3932014-08-18 13:49:10 +01002374 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002375 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2376 dirty |= WM_DIRTY_LINETIME(pipe);
2377 /* Must disable LP1+ watermarks too */
2378 dirty |= WM_DIRTY_LP_ALL;
2379 }
2380
2381 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2382 dirty |= WM_DIRTY_PIPE(pipe);
2383 /* Must disable LP1+ watermarks too */
2384 dirty |= WM_DIRTY_LP_ALL;
2385 }
2386 }
2387
2388 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2389 dirty |= WM_DIRTY_FBC;
2390 /* Must disable LP1+ watermarks too */
2391 dirty |= WM_DIRTY_LP_ALL;
2392 }
2393
2394 if (old->partitioning != new->partitioning) {
2395 dirty |= WM_DIRTY_DDB;
2396 /* Must disable LP1+ watermarks too */
2397 dirty |= WM_DIRTY_LP_ALL;
2398 }
2399
2400 /* LP1+ watermarks already deemed dirty, no need to continue */
2401 if (dirty & WM_DIRTY_LP_ALL)
2402 return dirty;
2403
2404 /* Find the lowest numbered LP1+ watermark in need of an update... */
2405 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2406 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2407 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2408 break;
2409 }
2410
2411 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2412 for (; wm_lp <= 3; wm_lp++)
2413 dirty |= WM_DIRTY_LP(wm_lp);
2414
2415 return dirty;
2416}
2417
Ville Syrjälä8553c182013-12-05 15:51:39 +02002418static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2419 unsigned int dirty)
2420{
Imre Deak820c1982013-12-17 14:46:36 +02002421 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002422 bool changed = false;
2423
2424 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2425 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2426 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2427 changed = true;
2428 }
2429 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2430 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2431 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2432 changed = true;
2433 }
2434 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2435 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2436 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2437 changed = true;
2438 }
2439
2440 /*
2441 * Don't touch WM1S_LP_EN here.
2442 * Doing so could cause underruns.
2443 */
2444
2445 return changed;
2446}
2447
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002448/*
2449 * The spec says we shouldn't write when we don't need, because every write
2450 * causes WMs to be re-evaluated, expending some power.
2451 */
Imre Deak820c1982013-12-17 14:46:36 +02002452static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2453 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002454{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002455 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002456 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002457 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002458 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002459
Damien Lespiau055e3932014-08-18 13:49:10 +01002460 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002461 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002462 return;
2463
Ville Syrjälä8553c182013-12-05 15:51:39 +02002464 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002465
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002466 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002467 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002468 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002469 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002470 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002471 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2472
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002473 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002474 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002475 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002477 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002478 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2479
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002480 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002481 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002482 val = I915_READ(WM_MISC);
2483 if (results->partitioning == INTEL_DDB_PART_1_2)
2484 val &= ~WM_MISC_DATA_PARTITION_5_6;
2485 else
2486 val |= WM_MISC_DATA_PARTITION_5_6;
2487 I915_WRITE(WM_MISC, val);
2488 } else {
2489 val = I915_READ(DISP_ARB_CTL2);
2490 if (results->partitioning == INTEL_DDB_PART_1_2)
2491 val &= ~DISP_DATA_PARTITION_5_6;
2492 else
2493 val |= DISP_DATA_PARTITION_5_6;
2494 I915_WRITE(DISP_ARB_CTL2, val);
2495 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002496 }
2497
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002498 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002499 val = I915_READ(DISP_ARB_CTL);
2500 if (results->enable_fbc_wm)
2501 val &= ~DISP_FBC_WM_DIS;
2502 else
2503 val |= DISP_FBC_WM_DIS;
2504 I915_WRITE(DISP_ARB_CTL, val);
2505 }
2506
Imre Deak954911e2013-12-17 14:46:34 +02002507 if (dirty & WM_DIRTY_LP(1) &&
2508 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2509 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2510
2511 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002512 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2513 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2514 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2515 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2516 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002518 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002520 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002522 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002523 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002524
2525 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002526}
2527
Ville Syrjälä8553c182013-12-05 15:51:39 +02002528static bool ilk_disable_lp_wm(struct drm_device *dev)
2529{
2530 struct drm_i915_private *dev_priv = dev->dev_private;
2531
2532 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2533}
2534
Damien Lespiaub9cec072014-11-04 17:06:43 +00002535/*
2536 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2537 * different active planes.
2538 */
2539
2540#define SKL_DDB_SIZE 896 /* in blocks */
Damien Lespiau43d735a2015-03-17 11:39:34 +02002541#define BXT_DDB_SIZE 512
Damien Lespiaub9cec072014-11-04 17:06:43 +00002542
2543static void
2544skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2545 struct drm_crtc *for_crtc,
2546 const struct intel_wm_config *config,
2547 const struct skl_pipe_wm_parameters *params,
2548 struct skl_ddb_entry *alloc /* out */)
2549{
2550 struct drm_crtc *crtc;
2551 unsigned int pipe_size, ddb_size;
2552 int nth_active_pipe;
2553
2554 if (!params->active) {
2555 alloc->start = 0;
2556 alloc->end = 0;
2557 return;
2558 }
2559
Damien Lespiau43d735a2015-03-17 11:39:34 +02002560 if (IS_BROXTON(dev))
2561 ddb_size = BXT_DDB_SIZE;
2562 else
2563 ddb_size = SKL_DDB_SIZE;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002564
2565 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2566
2567 nth_active_pipe = 0;
2568 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002569 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002570 continue;
2571
2572 if (crtc == for_crtc)
2573 break;
2574
2575 nth_active_pipe++;
2576 }
2577
2578 pipe_size = ddb_size / config->num_pipes_active;
2579 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002580 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002581}
2582
2583static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2584{
2585 if (config->num_pipes_active == 1)
2586 return 32;
2587
2588 return 8;
2589}
2590
Damien Lespiaua269c582014-11-04 17:06:49 +00002591static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2592{
2593 entry->start = reg & 0x3ff;
2594 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002595 if (entry->end)
2596 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002597}
2598
Damien Lespiau08db6652014-11-04 17:06:52 +00002599void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2600 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002601{
Damien Lespiaua269c582014-11-04 17:06:49 +00002602 enum pipe pipe;
2603 int plane;
2604 u32 val;
2605
2606 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002607 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002608 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2609 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2610 val);
2611 }
2612
2613 val = I915_READ(CUR_BUF_CFG(pipe));
2614 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2615 }
2616}
2617
Damien Lespiaub9cec072014-11-04 17:06:43 +00002618static unsigned int
2619skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2620{
2621 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2622}
2623
2624/*
2625 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2626 * a 8192x4096@32bpp framebuffer:
2627 * 3 * 4096 * 8192 * 4 < 2^32
2628 */
2629static unsigned int
2630skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2631 const struct skl_pipe_wm_parameters *params)
2632{
2633 unsigned int total_data_rate = 0;
2634 int plane;
2635
2636 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2637 const struct intel_plane_wm_parameters *p;
2638
2639 p = &params->plane[plane];
2640 if (!p->enabled)
2641 continue;
2642
2643 total_data_rate += skl_plane_relative_data_rate(p);
2644 }
2645
2646 return total_data_rate;
2647}
2648
2649static void
2650skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2651 const struct intel_wm_config *config,
2652 const struct skl_pipe_wm_parameters *params,
2653 struct skl_ddb_allocation *ddb /* out */)
2654{
2655 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002656 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2658 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002659 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002660 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002661 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002662 unsigned int total_data_rate;
2663 int plane;
2664
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002665 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2666 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002667 if (alloc_size == 0) {
2668 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2669 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2670 return;
2671 }
2672
2673 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002674 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2675 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002676
2677 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002678 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002679
Damien Lespiau80958152015-02-09 13:35:10 +00002680 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002681 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002682 const struct intel_plane_wm_parameters *p;
2683
2684 p = &params->plane[plane];
2685 if (!p->enabled)
2686 continue;
2687
2688 minimum[plane] = 8;
2689 alloc_size -= minimum[plane];
2690 }
2691
Damien Lespiaub9cec072014-11-04 17:06:43 +00002692 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002693 * 2. Distribute the remaining space in proportion to the amount of
2694 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002695 *
2696 * FIXME: we may not allocate every single block here.
2697 */
2698 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2699
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002700 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002701 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2702 const struct intel_plane_wm_parameters *p;
2703 unsigned int data_rate;
2704 uint16_t plane_blocks;
2705
2706 p = &params->plane[plane];
2707 if (!p->enabled)
2708 continue;
2709
2710 data_rate = skl_plane_relative_data_rate(p);
2711
2712 /*
2713 * promote the expression to 64 bits to avoid overflowing, the
2714 * result is < available as data_rate / total_data_rate < 1
2715 */
Damien Lespiau80958152015-02-09 13:35:10 +00002716 plane_blocks = minimum[plane];
2717 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2718 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002719
2720 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002721 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002722
2723 start += plane_blocks;
2724 }
2725
2726}
2727
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002728static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002729{
2730 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002731 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002732}
2733
2734/*
2735 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2736 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2737 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2738 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2739*/
2740static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2741 uint32_t latency)
2742{
2743 uint32_t wm_intermediate_val, ret;
2744
2745 if (latency == 0)
2746 return UINT_MAX;
2747
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002748 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002749 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2750
2751 return ret;
2752}
2753
2754static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2755 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002756 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002757{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002758 uint32_t ret;
2759 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2760 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002761
2762 if (latency == 0)
2763 return UINT_MAX;
2764
2765 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002766
2767 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2768 tiling == I915_FORMAT_MOD_Yf_TILED) {
2769 plane_bytes_per_line *= 4;
2770 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2771 plane_blocks_per_line /= 4;
2772 } else {
2773 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2774 }
2775
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002776 wm_intermediate_val = latency * pixel_rate;
2777 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002778 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002779
2780 return ret;
2781}
2782
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002783static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2784 const struct intel_crtc *intel_crtc)
2785{
2786 struct drm_device *dev = intel_crtc->base.dev;
2787 struct drm_i915_private *dev_priv = dev->dev_private;
2788 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2789 enum pipe pipe = intel_crtc->pipe;
2790
2791 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2792 sizeof(new_ddb->plane[pipe])))
2793 return true;
2794
2795 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2796 sizeof(new_ddb->cursor[pipe])))
2797 return true;
2798
2799 return false;
2800}
2801
2802static void skl_compute_wm_global_parameters(struct drm_device *dev,
2803 struct intel_wm_config *config)
2804{
2805 struct drm_crtc *crtc;
2806 struct drm_plane *plane;
2807
2808 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07002809 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002810
2811 /* FIXME: I don't think we need those two global parameters on SKL */
2812 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2813 struct intel_plane *intel_plane = to_intel_plane(plane);
2814
2815 config->sprites_enabled |= intel_plane->wm.enabled;
2816 config->sprites_scaled |= intel_plane->wm.scaled;
2817 }
2818}
2819
2820static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2821 struct skl_pipe_wm_parameters *p)
2822{
2823 struct drm_device *dev = crtc->dev;
2824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2825 enum pipe pipe = intel_crtc->pipe;
2826 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002827 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002828 int i = 1; /* Index for sprite planes start */
2829
Matt Roper3ef00282015-03-09 10:19:24 -07002830 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002831 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002832 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2833 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002834
Matt Roperc9f038a2015-03-09 11:06:02 -07002835 fb = crtc->primary->state->fb;
2836 if (fb) {
2837 p->plane[0].enabled = true;
2838 p->plane[0].bytes_per_pixel = fb->bits_per_pixel / 8;
2839 p->plane[0].tiling = fb->modifier[0];
2840 } else {
2841 p->plane[0].enabled = false;
2842 p->plane[0].bytes_per_pixel = 0;
2843 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2844 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002845 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2846 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00002847 p->plane[0].rotation = crtc->primary->state->rotation;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002848
Matt Roperc9f038a2015-03-09 11:06:02 -07002849 fb = crtc->cursor->state->fb;
2850 if (fb) {
2851 p->cursor.enabled = true;
2852 p->cursor.bytes_per_pixel = fb->bits_per_pixel / 8;
2853 p->cursor.horiz_pixels = crtc->cursor->state->crtc_w;
2854 p->cursor.vert_pixels = crtc->cursor->state->crtc_h;
2855 } else {
2856 p->cursor.enabled = false;
2857 p->cursor.bytes_per_pixel = 0;
2858 p->cursor.horiz_pixels = 64;
2859 p->cursor.vert_pixels = 64;
2860 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002861 }
2862
2863 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2864 struct intel_plane *intel_plane = to_intel_plane(plane);
2865
Sonika Jindala712f8e2014-12-09 10:59:15 +05302866 if (intel_plane->pipe == pipe &&
2867 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002868 p->plane[i++] = intel_plane->wm;
2869 }
2870}
2871
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002872static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2873 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002874 struct intel_plane_wm_parameters *p_params,
2875 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002876 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002877 uint16_t *out_blocks, /* out */
2878 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002879{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002880 uint32_t latency = dev_priv->wm.skl_latency[level];
2881 uint32_t method1, method2;
2882 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2883 uint32_t res_blocks, res_lines;
2884 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002885
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002886 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002887 return false;
2888
2889 method1 = skl_wm_method1(p->pixel_rate,
2890 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002891 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002892 method2 = skl_wm_method2(p->pixel_rate,
2893 p->pipe_htotal,
2894 p_params->horiz_pixels,
2895 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002896 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002897 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002898
2899 plane_bytes_per_line = p_params->horiz_pixels *
2900 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002901 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002902
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002903 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2904 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00002905 uint32_t min_scanlines = 4;
2906 uint32_t y_tile_minimum;
2907 if (intel_rotation_90_or_270(p_params->rotation)) {
2908 switch (p_params->bytes_per_pixel) {
2909 case 1:
2910 min_scanlines = 16;
2911 break;
2912 case 2:
2913 min_scanlines = 8;
2914 break;
2915 case 8:
2916 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08002917 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00002918 }
2919 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002920 selected_result = max(method2, y_tile_minimum);
2921 } else {
2922 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2923 selected_result = min(method1, method2);
2924 else
2925 selected_result = method1;
2926 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002927
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002928 res_blocks = selected_result + 1;
2929 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002930
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002931 if (level >= 1 && level <= 7) {
2932 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2933 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2934 res_lines += 4;
2935 else
2936 res_blocks++;
2937 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002938
2939 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002940 return false;
2941
2942 *out_blocks = res_blocks;
2943 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002944
2945 return true;
2946}
2947
2948static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2949 struct skl_ddb_allocation *ddb,
2950 struct skl_pipe_wm_parameters *p,
2951 enum pipe pipe,
2952 int level,
2953 int num_planes,
2954 struct skl_wm_level *result)
2955{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002956 uint16_t ddb_blocks;
2957 int i;
2958
2959 for (i = 0; i < num_planes; i++) {
2960 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2961
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002962 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2963 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002964 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002965 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002966 &result->plane_res_b[i],
2967 &result->plane_res_l[i]);
2968 }
2969
2970 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002971 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2972 ddb_blocks, level,
2973 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002974 &result->cursor_res_l);
2975}
2976
Damien Lespiau407b50f2014-11-04 17:06:57 +00002977static uint32_t
2978skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2979{
Matt Roper3ef00282015-03-09 10:19:24 -07002980 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002981 return 0;
2982
2983 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2984
2985}
2986
2987static void skl_compute_transition_wm(struct drm_crtc *crtc,
2988 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002989 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002990{
Damien Lespiau9414f562014-11-04 17:06:58 +00002991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2992 int i;
2993
Damien Lespiau407b50f2014-11-04 17:06:57 +00002994 if (!params->active)
2995 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002996
2997 /* Until we know more, just disable transition WMs */
2998 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2999 trans_wm->plane_en[i] = false;
3000 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003001}
3002
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003003static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3004 struct skl_ddb_allocation *ddb,
3005 struct skl_pipe_wm_parameters *params,
3006 struct skl_pipe_wm *pipe_wm)
3007{
3008 struct drm_device *dev = crtc->dev;
3009 const struct drm_i915_private *dev_priv = dev->dev_private;
3010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3011 int level, max_level = ilk_wm_max_level(dev);
3012
3013 for (level = 0; level <= max_level; level++) {
3014 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3015 level, intel_num_planes(intel_crtc),
3016 &pipe_wm->wm[level]);
3017 }
3018 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3019
Damien Lespiau9414f562014-11-04 17:06:58 +00003020 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003021}
3022
3023static void skl_compute_wm_results(struct drm_device *dev,
3024 struct skl_pipe_wm_parameters *p,
3025 struct skl_pipe_wm *p_wm,
3026 struct skl_wm_values *r,
3027 struct intel_crtc *intel_crtc)
3028{
3029 int level, max_level = ilk_wm_max_level(dev);
3030 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003031 uint32_t temp;
3032 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003033
3034 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003035 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3036 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003037
3038 temp |= p_wm->wm[level].plane_res_l[i] <<
3039 PLANE_WM_LINES_SHIFT;
3040 temp |= p_wm->wm[level].plane_res_b[i];
3041 if (p_wm->wm[level].plane_en[i])
3042 temp |= PLANE_WM_EN;
3043
3044 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003045 }
3046
3047 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003048
3049 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3050 temp |= p_wm->wm[level].cursor_res_b;
3051
3052 if (p_wm->wm[level].cursor_en)
3053 temp |= PLANE_WM_EN;
3054
3055 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003056
3057 }
3058
Damien Lespiau9414f562014-11-04 17:06:58 +00003059 /* transition WMs */
3060 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3061 temp = 0;
3062 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3063 temp |= p_wm->trans_wm.plane_res_b[i];
3064 if (p_wm->trans_wm.plane_en[i])
3065 temp |= PLANE_WM_EN;
3066
3067 r->plane_trans[pipe][i] = temp;
3068 }
3069
3070 temp = 0;
3071 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3072 temp |= p_wm->trans_wm.cursor_res_b;
3073 if (p_wm->trans_wm.cursor_en)
3074 temp |= PLANE_WM_EN;
3075
3076 r->cursor_trans[pipe] = temp;
3077
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003078 r->wm_linetime[pipe] = p_wm->linetime;
3079}
3080
Damien Lespiau16160e32014-11-04 17:06:53 +00003081static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3082 const struct skl_ddb_entry *entry)
3083{
3084 if (entry->end)
3085 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3086 else
3087 I915_WRITE(reg, 0);
3088}
3089
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003090static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3091 const struct skl_wm_values *new)
3092{
3093 struct drm_device *dev = dev_priv->dev;
3094 struct intel_crtc *crtc;
3095
3096 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3097 int i, level, max_level = ilk_wm_max_level(dev);
3098 enum pipe pipe = crtc->pipe;
3099
Damien Lespiau5d374d92014-11-04 17:07:00 +00003100 if (!new->dirty[pipe])
3101 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003102
Damien Lespiau5d374d92014-11-04 17:07:00 +00003103 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3104
3105 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003106 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003107 I915_WRITE(PLANE_WM(pipe, i, level),
3108 new->plane[pipe][i][level]);
3109 I915_WRITE(CUR_WM(pipe, level),
3110 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003111 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003112 for (i = 0; i < intel_num_planes(crtc); i++)
3113 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3114 new->plane_trans[pipe][i]);
3115 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3116
3117 for (i = 0; i < intel_num_planes(crtc); i++)
3118 skl_ddb_entry_write(dev_priv,
3119 PLANE_BUF_CFG(pipe, i),
3120 &new->ddb.plane[pipe][i]);
3121
3122 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3123 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003124 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003125}
3126
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003127/*
3128 * When setting up a new DDB allocation arrangement, we need to correctly
3129 * sequence the times at which the new allocations for the pipes are taken into
3130 * account or we'll have pipes fetching from space previously allocated to
3131 * another pipe.
3132 *
3133 * Roughly the sequence looks like:
3134 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3135 * overlapping with a previous light-up pipe (another way to put it is:
3136 * pipes with their new allocation strickly included into their old ones).
3137 * 2. re-allocate the other pipes that get their allocation reduced
3138 * 3. allocate the pipes having their allocation increased
3139 *
3140 * Steps 1. and 2. are here to take care of the following case:
3141 * - Initially DDB looks like this:
3142 * | B | C |
3143 * - enable pipe A.
3144 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3145 * allocation
3146 * | A | B | C |
3147 *
3148 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3149 */
3150
Damien Lespiaud21b7952014-11-04 17:07:03 +00003151static void
3152skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003153{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003154 int plane;
3155
Damien Lespiaud21b7952014-11-04 17:07:03 +00003156 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3157
Damien Lespiaudd740782015-02-28 14:54:08 +00003158 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003159 I915_WRITE(PLANE_SURF(pipe, plane),
3160 I915_READ(PLANE_SURF(pipe, plane)));
3161 }
3162 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3163}
3164
3165static bool
3166skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3167 const struct skl_ddb_allocation *new,
3168 enum pipe pipe)
3169{
3170 uint16_t old_size, new_size;
3171
3172 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3173 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3174
3175 return old_size != new_size &&
3176 new->pipe[pipe].start >= old->pipe[pipe].start &&
3177 new->pipe[pipe].end <= old->pipe[pipe].end;
3178}
3179
3180static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3181 struct skl_wm_values *new_values)
3182{
3183 struct drm_device *dev = dev_priv->dev;
3184 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3185 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3186 struct intel_crtc *crtc;
3187 enum pipe pipe;
3188
3189 new_ddb = &new_values->ddb;
3190 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3191
3192 /*
3193 * First pass: flush the pipes with the new allocation contained into
3194 * the old space.
3195 *
3196 * We'll wait for the vblank on those pipes to ensure we can safely
3197 * re-allocate the freed space without this pipe fetching from it.
3198 */
3199 for_each_intel_crtc(dev, crtc) {
3200 if (!crtc->active)
3201 continue;
3202
3203 pipe = crtc->pipe;
3204
3205 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3206 continue;
3207
Damien Lespiaud21b7952014-11-04 17:07:03 +00003208 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003209 intel_wait_for_vblank(dev, pipe);
3210
3211 reallocated[pipe] = true;
3212 }
3213
3214
3215 /*
3216 * Second pass: flush the pipes that are having their allocation
3217 * reduced, but overlapping with a previous allocation.
3218 *
3219 * Here as well we need to wait for the vblank to make sure the freed
3220 * space is not used anymore.
3221 */
3222 for_each_intel_crtc(dev, crtc) {
3223 if (!crtc->active)
3224 continue;
3225
3226 pipe = crtc->pipe;
3227
3228 if (reallocated[pipe])
3229 continue;
3230
3231 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3232 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003233 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003234 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303235 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003236 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003237 }
3238
3239 /*
3240 * Third pass: flush the pipes that got more space allocated.
3241 *
3242 * We don't need to actively wait for the update here, next vblank
3243 * will just get more DDB space with the correct WM values.
3244 */
3245 for_each_intel_crtc(dev, crtc) {
3246 if (!crtc->active)
3247 continue;
3248
3249 pipe = crtc->pipe;
3250
3251 /*
3252 * At this point, only the pipes more space than before are
3253 * left to re-allocate.
3254 */
3255 if (reallocated[pipe])
3256 continue;
3257
Damien Lespiaud21b7952014-11-04 17:07:03 +00003258 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003259 }
3260}
3261
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003262static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3263 struct skl_pipe_wm_parameters *params,
3264 struct intel_wm_config *config,
3265 struct skl_ddb_allocation *ddb, /* out */
3266 struct skl_pipe_wm *pipe_wm /* out */)
3267{
3268 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3269
3270 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003271 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003272 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3273
3274 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3275 return false;
3276
3277 intel_crtc->wm.skl_active = *pipe_wm;
3278 return true;
3279}
3280
3281static void skl_update_other_pipe_wm(struct drm_device *dev,
3282 struct drm_crtc *crtc,
3283 struct intel_wm_config *config,
3284 struct skl_wm_values *r)
3285{
3286 struct intel_crtc *intel_crtc;
3287 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3288
3289 /*
3290 * If the WM update hasn't changed the allocation for this_crtc (the
3291 * crtc we are currently computing the new WM values for), other
3292 * enabled crtcs will keep the same allocation and we don't need to
3293 * recompute anything for them.
3294 */
3295 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3296 return;
3297
3298 /*
3299 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3300 * other active pipes need new DDB allocation and WM values.
3301 */
3302 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3303 base.head) {
3304 struct skl_pipe_wm_parameters params = {};
3305 struct skl_pipe_wm pipe_wm = {};
3306 bool wm_changed;
3307
3308 if (this_crtc->pipe == intel_crtc->pipe)
3309 continue;
3310
3311 if (!intel_crtc->active)
3312 continue;
3313
3314 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3315 &params, config,
3316 &r->ddb, &pipe_wm);
3317
3318 /*
3319 * If we end up re-computing the other pipe WM values, it's
3320 * because it was really needed, so we expect the WM values to
3321 * be different.
3322 */
3323 WARN_ON(!wm_changed);
3324
3325 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3326 r->dirty[intel_crtc->pipe] = true;
3327 }
3328}
3329
3330static void skl_update_wm(struct drm_crtc *crtc)
3331{
3332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3333 struct drm_device *dev = crtc->dev;
3334 struct drm_i915_private *dev_priv = dev->dev_private;
3335 struct skl_pipe_wm_parameters params = {};
3336 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3337 struct skl_pipe_wm pipe_wm = {};
3338 struct intel_wm_config config = {};
3339
3340 memset(results, 0, sizeof(*results));
3341
3342 skl_compute_wm_global_parameters(dev, &config);
3343
3344 if (!skl_update_pipe_wm(crtc, &params, &config,
3345 &results->ddb, &pipe_wm))
3346 return;
3347
3348 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3349 results->dirty[intel_crtc->pipe] = true;
3350
3351 skl_update_other_pipe_wm(dev, crtc, &config, results);
3352 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003353 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003354
3355 /* store the new configuration */
3356 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003357}
3358
3359static void
3360skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3361 uint32_t sprite_width, uint32_t sprite_height,
3362 int pixel_size, bool enabled, bool scaled)
3363{
3364 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003365 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003366
3367 intel_plane->wm.enabled = enabled;
3368 intel_plane->wm.scaled = scaled;
3369 intel_plane->wm.horiz_pixels = sprite_width;
3370 intel_plane->wm.vert_pixels = sprite_height;
3371 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003372 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3373 /*
3374 * Framebuffer can be NULL on plane disable, but it does not
3375 * matter for watermarks if we assume no tiling in that case.
3376 */
3377 if (fb)
3378 intel_plane->wm.tiling = fb->modifier[0];
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003379 intel_plane->wm.rotation = plane->state->rotation;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003380
3381 skl_update_wm(crtc);
3382}
3383
Imre Deak820c1982013-12-17 14:46:36 +02003384static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003385{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003386 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003387 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003388 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003389 struct ilk_wm_maximums max;
3390 struct ilk_pipe_wm_parameters params = {};
3391 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003392 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003393 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003394 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003395 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003396
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003397 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003398
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003399 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3400
3401 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3402 return;
3403
3404 intel_crtc->wm.active = pipe_wm;
3405
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003406 ilk_compute_wm_config(dev, &config);
3407
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003408 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003409 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003410
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003411 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003412 if (INTEL_INFO(dev)->gen >= 7 &&
3413 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003414 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003415 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003416
Imre Deak820c1982013-12-17 14:46:36 +02003417 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003418 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003419 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003420 }
3421
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003422 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003423 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003424
Imre Deak820c1982013-12-17 14:46:36 +02003425 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003426
Imre Deak820c1982013-12-17 14:46:36 +02003427 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003428}
3429
Damien Lespiaued57cb82014-07-15 09:21:24 +02003430static void
3431ilk_update_sprite_wm(struct drm_plane *plane,
3432 struct drm_crtc *crtc,
3433 uint32_t sprite_width, uint32_t sprite_height,
3434 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003435{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003436 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003437 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003438
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003439 intel_plane->wm.enabled = enabled;
3440 intel_plane->wm.scaled = scaled;
3441 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003442 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003443 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003444
Ville Syrjälä8553c182013-12-05 15:51:39 +02003445 /*
3446 * IVB workaround: must disable low power watermarks for at least
3447 * one frame before enabling scaling. LP watermarks can be re-enabled
3448 * when scaling is disabled.
3449 *
3450 * WaCxSRDisabledForSpriteScaling:ivb
3451 */
3452 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3453 intel_wait_for_vblank(dev, intel_plane->pipe);
3454
Imre Deak820c1982013-12-17 14:46:36 +02003455 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003456}
3457
Pradeep Bhat30789992014-11-04 17:06:45 +00003458static void skl_pipe_wm_active_state(uint32_t val,
3459 struct skl_pipe_wm *active,
3460 bool is_transwm,
3461 bool is_cursor,
3462 int i,
3463 int level)
3464{
3465 bool is_enabled = (val & PLANE_WM_EN) != 0;
3466
3467 if (!is_transwm) {
3468 if (!is_cursor) {
3469 active->wm[level].plane_en[i] = is_enabled;
3470 active->wm[level].plane_res_b[i] =
3471 val & PLANE_WM_BLOCKS_MASK;
3472 active->wm[level].plane_res_l[i] =
3473 (val >> PLANE_WM_LINES_SHIFT) &
3474 PLANE_WM_LINES_MASK;
3475 } else {
3476 active->wm[level].cursor_en = is_enabled;
3477 active->wm[level].cursor_res_b =
3478 val & PLANE_WM_BLOCKS_MASK;
3479 active->wm[level].cursor_res_l =
3480 (val >> PLANE_WM_LINES_SHIFT) &
3481 PLANE_WM_LINES_MASK;
3482 }
3483 } else {
3484 if (!is_cursor) {
3485 active->trans_wm.plane_en[i] = is_enabled;
3486 active->trans_wm.plane_res_b[i] =
3487 val & PLANE_WM_BLOCKS_MASK;
3488 active->trans_wm.plane_res_l[i] =
3489 (val >> PLANE_WM_LINES_SHIFT) &
3490 PLANE_WM_LINES_MASK;
3491 } else {
3492 active->trans_wm.cursor_en = is_enabled;
3493 active->trans_wm.cursor_res_b =
3494 val & PLANE_WM_BLOCKS_MASK;
3495 active->trans_wm.cursor_res_l =
3496 (val >> PLANE_WM_LINES_SHIFT) &
3497 PLANE_WM_LINES_MASK;
3498 }
3499 }
3500}
3501
3502static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3503{
3504 struct drm_device *dev = crtc->dev;
3505 struct drm_i915_private *dev_priv = dev->dev_private;
3506 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3508 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3509 enum pipe pipe = intel_crtc->pipe;
3510 int level, i, max_level;
3511 uint32_t temp;
3512
3513 max_level = ilk_wm_max_level(dev);
3514
3515 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3516
3517 for (level = 0; level <= max_level; level++) {
3518 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3519 hw->plane[pipe][i][level] =
3520 I915_READ(PLANE_WM(pipe, i, level));
3521 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3522 }
3523
3524 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3525 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3526 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3527
Matt Roper3ef00282015-03-09 10:19:24 -07003528 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003529 return;
3530
3531 hw->dirty[pipe] = true;
3532
3533 active->linetime = hw->wm_linetime[pipe];
3534
3535 for (level = 0; level <= max_level; level++) {
3536 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3537 temp = hw->plane[pipe][i][level];
3538 skl_pipe_wm_active_state(temp, active, false,
3539 false, i, level);
3540 }
3541 temp = hw->cursor[pipe][level];
3542 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3543 }
3544
3545 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3546 temp = hw->plane_trans[pipe][i];
3547 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3548 }
3549
3550 temp = hw->cursor_trans[pipe];
3551 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3552}
3553
3554void skl_wm_get_hw_state(struct drm_device *dev)
3555{
Damien Lespiaua269c582014-11-04 17:06:49 +00003556 struct drm_i915_private *dev_priv = dev->dev_private;
3557 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003558 struct drm_crtc *crtc;
3559
Damien Lespiaua269c582014-11-04 17:06:49 +00003560 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003561 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3562 skl_pipe_wm_get_hw_state(crtc);
3563}
3564
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003565static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3566{
3567 struct drm_device *dev = crtc->dev;
3568 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003569 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003570 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3571 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3572 enum pipe pipe = intel_crtc->pipe;
3573 static const unsigned int wm0_pipe_reg[] = {
3574 [PIPE_A] = WM0_PIPEA_ILK,
3575 [PIPE_B] = WM0_PIPEB_ILK,
3576 [PIPE_C] = WM0_PIPEC_IVB,
3577 };
3578
3579 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003580 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003581 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003582
Matt Roper3ef00282015-03-09 10:19:24 -07003583 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003584
3585 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003586 u32 tmp = hw->wm_pipe[pipe];
3587
3588 /*
3589 * For active pipes LP0 watermark is marked as
3590 * enabled, and LP1+ watermaks as disabled since
3591 * we can't really reverse compute them in case
3592 * multiple pipes are active.
3593 */
3594 active->wm[0].enable = true;
3595 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3596 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3597 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3598 active->linetime = hw->wm_linetime[pipe];
3599 } else {
3600 int level, max_level = ilk_wm_max_level(dev);
3601
3602 /*
3603 * For inactive pipes, all watermark levels
3604 * should be marked as enabled but zeroed,
3605 * which is what we'd compute them to.
3606 */
3607 for (level = 0; level <= max_level; level++)
3608 active->wm[level].enable = true;
3609 }
3610}
3611
3612void ilk_wm_get_hw_state(struct drm_device *dev)
3613{
3614 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003615 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003616 struct drm_crtc *crtc;
3617
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003618 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003619 ilk_pipe_wm_get_hw_state(crtc);
3620
3621 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3622 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3623 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3624
3625 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003626 if (INTEL_INFO(dev)->gen >= 7) {
3627 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3628 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3629 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003630
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003631 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003632 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3633 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3634 else if (IS_IVYBRIDGE(dev))
3635 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3636 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003637
3638 hw->enable_fbc_wm =
3639 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3640}
3641
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003642/**
3643 * intel_update_watermarks - update FIFO watermark values based on current modes
3644 *
3645 * Calculate watermark values for the various WM regs based on current mode
3646 * and plane configuration.
3647 *
3648 * There are several cases to deal with here:
3649 * - normal (i.e. non-self-refresh)
3650 * - self-refresh (SR) mode
3651 * - lines are large relative to FIFO size (buffer can hold up to 2)
3652 * - lines are small relative to FIFO size (buffer can hold more than 2
3653 * lines), so need to account for TLB latency
3654 *
3655 * The normal calculation is:
3656 * watermark = dotclock * bytes per pixel * latency
3657 * where latency is platform & configuration dependent (we assume pessimal
3658 * values here).
3659 *
3660 * The SR calculation is:
3661 * watermark = (trunc(latency/line time)+1) * surface width *
3662 * bytes per pixel
3663 * where
3664 * line time = htotal / dotclock
3665 * surface width = hdisplay for normal plane and 64 for cursor
3666 * and latency is assumed to be high, as above.
3667 *
3668 * The final value programmed to the register should always be rounded up,
3669 * and include an extra 2 entries to account for clock crossings.
3670 *
3671 * We don't use the sprite, so we can ignore that. And on Crestline we have
3672 * to set the non-SR watermarks to 8.
3673 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003674void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003675{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003676 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003677
3678 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003679 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003680}
3681
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003682void intel_update_sprite_watermarks(struct drm_plane *plane,
3683 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003684 uint32_t sprite_width,
3685 uint32_t sprite_height,
3686 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003687 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003688{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003689 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003690
3691 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003692 dev_priv->display.update_sprite_wm(plane, crtc,
3693 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003694 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003695}
3696
Daniel Vetter92703882012-08-09 16:46:01 +02003697/**
3698 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003699 */
3700DEFINE_SPINLOCK(mchdev_lock);
3701
3702/* Global for IPS driver to get at the current i915 device. Protected by
3703 * mchdev_lock. */
3704static struct drm_i915_private *i915_mch_dev;
3705
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003706bool ironlake_set_drps(struct drm_device *dev, u8 val)
3707{
3708 struct drm_i915_private *dev_priv = dev->dev_private;
3709 u16 rgvswctl;
3710
Daniel Vetter92703882012-08-09 16:46:01 +02003711 assert_spin_locked(&mchdev_lock);
3712
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003713 rgvswctl = I915_READ16(MEMSWCTL);
3714 if (rgvswctl & MEMCTL_CMD_STS) {
3715 DRM_DEBUG("gpu busy, RCS change rejected\n");
3716 return false; /* still busy with another command */
3717 }
3718
3719 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3720 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3721 I915_WRITE16(MEMSWCTL, rgvswctl);
3722 POSTING_READ16(MEMSWCTL);
3723
3724 rgvswctl |= MEMCTL_CMD_STS;
3725 I915_WRITE16(MEMSWCTL, rgvswctl);
3726
3727 return true;
3728}
3729
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003730static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003731{
3732 struct drm_i915_private *dev_priv = dev->dev_private;
3733 u32 rgvmodectl = I915_READ(MEMMODECTL);
3734 u8 fmax, fmin, fstart, vstart;
3735
Daniel Vetter92703882012-08-09 16:46:01 +02003736 spin_lock_irq(&mchdev_lock);
3737
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003738 /* Enable temp reporting */
3739 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3740 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3741
3742 /* 100ms RC evaluation intervals */
3743 I915_WRITE(RCUPEI, 100000);
3744 I915_WRITE(RCDNEI, 100000);
3745
3746 /* Set max/min thresholds to 90ms and 80ms respectively */
3747 I915_WRITE(RCBMAXAVG, 90000);
3748 I915_WRITE(RCBMINAVG, 80000);
3749
3750 I915_WRITE(MEMIHYST, 1);
3751
3752 /* Set up min, max, and cur for interrupt handling */
3753 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3754 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3755 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3756 MEMMODE_FSTART_SHIFT;
3757
3758 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3759 PXVFREQ_PX_SHIFT;
3760
Daniel Vetter20e4d402012-08-08 23:35:39 +02003761 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3762 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003763
Daniel Vetter20e4d402012-08-08 23:35:39 +02003764 dev_priv->ips.max_delay = fstart;
3765 dev_priv->ips.min_delay = fmin;
3766 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003767
3768 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3769 fmax, fmin, fstart);
3770
3771 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3772
3773 /*
3774 * Interrupts will be enabled in ironlake_irq_postinstall
3775 */
3776
3777 I915_WRITE(VIDSTART, vstart);
3778 POSTING_READ(VIDSTART);
3779
3780 rgvmodectl |= MEMMODE_SWMODE_EN;
3781 I915_WRITE(MEMMODECTL, rgvmodectl);
3782
Daniel Vetter92703882012-08-09 16:46:01 +02003783 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003784 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003785 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003786
3787 ironlake_set_drps(dev, fstart);
3788
Daniel Vetter20e4d402012-08-08 23:35:39 +02003789 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003790 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003791 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3792 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003793 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003794
3795 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003796}
3797
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003798static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003799{
3800 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003801 u16 rgvswctl;
3802
3803 spin_lock_irq(&mchdev_lock);
3804
3805 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003806
3807 /* Ack interrupts, disable EFC interrupt */
3808 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3809 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3810 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3811 I915_WRITE(DEIIR, DE_PCU_EVENT);
3812 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3813
3814 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003815 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003816 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003817 rgvswctl |= MEMCTL_CMD_STS;
3818 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003819 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003820
Daniel Vetter92703882012-08-09 16:46:01 +02003821 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003822}
3823
Daniel Vetteracbe9472012-07-26 11:50:05 +02003824/* There's a funny hw issue where the hw returns all 0 when reading from
3825 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3826 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3827 * all limits and the gpu stuck at whatever frequency it is at atm).
3828 */
Akash Goel74ef1172015-03-06 11:07:19 +05303829static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003830{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003831 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003832
Daniel Vetter20b46e52012-07-26 11:16:14 +02003833 /* Only set the down limit when we've reached the lowest level to avoid
3834 * getting more interrupts, otherwise leave this clear. This prevents a
3835 * race in the hw when coming out of rc6: There's a tiny window where
3836 * the hw runs at the minimal clock before selecting the desired
3837 * frequency, if the down threshold expires in that window we will not
3838 * receive a down interrupt. */
Akash Goel74ef1172015-03-06 11:07:19 +05303839 if (IS_GEN9(dev_priv->dev)) {
3840 limits = (dev_priv->rps.max_freq_softlimit) << 23;
3841 if (val <= dev_priv->rps.min_freq_softlimit)
3842 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
3843 } else {
3844 limits = dev_priv->rps.max_freq_softlimit << 24;
3845 if (val <= dev_priv->rps.min_freq_softlimit)
3846 limits |= dev_priv->rps.min_freq_softlimit << 16;
3847 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02003848
3849 return limits;
3850}
3851
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003852static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3853{
3854 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05303855 u32 threshold_up = 0, threshold_down = 0; /* in % */
3856 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003857
3858 new_power = dev_priv->rps.power;
3859 switch (dev_priv->rps.power) {
3860 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003861 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003862 new_power = BETWEEN;
3863 break;
3864
3865 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003866 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003867 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003868 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003869 new_power = HIGH_POWER;
3870 break;
3871
3872 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003873 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 +01003874 new_power = BETWEEN;
3875 break;
3876 }
3877 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00003878 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003879 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00003880 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003881 new_power = HIGH_POWER;
3882 if (new_power == dev_priv->rps.power)
3883 return;
3884
3885 /* Note the units here are not exactly 1us, but 1280ns. */
3886 switch (new_power) {
3887 case LOW_POWER:
3888 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05303889 ei_up = 16000;
3890 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003891
3892 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05303893 ei_down = 32000;
3894 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003895 break;
3896
3897 case BETWEEN:
3898 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05303899 ei_up = 13000;
3900 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003901
3902 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05303903 ei_down = 32000;
3904 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003905 break;
3906
3907 case HIGH_POWER:
3908 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05303909 ei_up = 10000;
3910 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003911
3912 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05303913 ei_down = 32000;
3914 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003915 break;
3916 }
3917
Akash Goel8a586432015-03-06 11:07:18 +05303918 I915_WRITE(GEN6_RP_UP_EI,
3919 GT_INTERVAL_FROM_US(dev_priv, ei_up));
3920 I915_WRITE(GEN6_RP_UP_THRESHOLD,
3921 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
3922
3923 I915_WRITE(GEN6_RP_DOWN_EI,
3924 GT_INTERVAL_FROM_US(dev_priv, ei_down));
3925 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
3926 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
3927
3928 I915_WRITE(GEN6_RP_CONTROL,
3929 GEN6_RP_MEDIA_TURBO |
3930 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3931 GEN6_RP_MEDIA_IS_GFX |
3932 GEN6_RP_ENABLE |
3933 GEN6_RP_UP_BUSY_AVG |
3934 GEN6_RP_DOWN_IDLE_AVG);
3935
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003936 dev_priv->rps.power = new_power;
3937 dev_priv->rps.last_adj = 0;
3938}
3939
Chris Wilson2876ce72014-03-28 08:03:34 +00003940static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3941{
3942 u32 mask = 0;
3943
3944 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00003945 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00003946 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00003947 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00003948
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003949 mask &= dev_priv->pm_rps_events;
3950
Imre Deak59d02a12014-12-19 19:33:26 +02003951 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003952}
3953
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003954/* gen6_set_rps is called to update the frequency request, but should also be
3955 * called when the range (min_delay and max_delay) is modified so that we can
3956 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003957static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003958{
3959 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003960
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003961 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00003962 WARN_ON(val > dev_priv->rps.max_freq);
3963 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02003964
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003965 /* min/max delay may still have been modified so be sure to
3966 * write the limits value.
3967 */
3968 if (val != dev_priv->rps.cur_freq) {
3969 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003970
Akash Goel57041952015-03-06 11:07:17 +05303971 if (IS_GEN9(dev))
3972 I915_WRITE(GEN6_RPNSWREQ,
3973 GEN9_FREQUENCY(val));
3974 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003975 I915_WRITE(GEN6_RPNSWREQ,
3976 HSW_FREQUENCY(val));
3977 else
3978 I915_WRITE(GEN6_RPNSWREQ,
3979 GEN6_FREQUENCY(val) |
3980 GEN6_OFFSET(0) |
3981 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003982 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003983
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003984 /* Make sure we continue to get interrupts
3985 * until we hit the minimum or maximum frequencies.
3986 */
Akash Goel74ef1172015-03-06 11:07:19 +05303987 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003988 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003989
Ben Widawskyd5570a72012-09-07 19:43:41 -07003990 POSTING_READ(GEN6_RPNSWREQ);
3991
Ben Widawskyb39fb292014-03-19 18:31:11 -07003992 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003993 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003994}
3995
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003996static void valleyview_set_rps(struct drm_device *dev, u8 val)
3997{
3998 struct drm_i915_private *dev_priv = dev->dev_private;
3999
4000 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004001 WARN_ON(val > dev_priv->rps.max_freq);
4002 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004003
4004 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4005 "Odd GPU freq value\n"))
4006 val &= ~1;
4007
4008 if (val != dev_priv->rps.cur_freq)
4009 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
4010
4011 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4012
4013 dev_priv->rps.cur_freq = val;
4014 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4015}
4016
Deepak S76c3552f2014-01-30 23:08:16 +05304017/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
4018 *
4019 * * If Gfx is Idle, then
4020 * 1. Mask Turbo interrupts
4021 * 2. Bring up Gfx clock
4022 * 3. Change the freq to Rpn and wait till P-Unit updates freq
4023 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
4024 * 5. Unmask Turbo interrupts
4025*/
4026static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4027{
Deepak S5549d252014-06-28 11:26:11 +05304028 struct drm_device *dev = dev_priv->dev;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004029 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304030
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004031 /* CHV and latest VLV don't need to force the gfx clock */
4032 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Chris Wilsonaed242f2015-03-18 09:48:21 +00004033 valleyview_set_rps(dev_priv->dev, val);
Deepak S5549d252014-06-28 11:26:11 +05304034 return;
4035 }
4036
Deepak S76c3552f2014-01-30 23:08:16 +05304037 /*
4038 * When we are idle. Drop to min voltage state.
4039 */
4040
Chris Wilsonaed242f2015-03-18 09:48:21 +00004041 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304042 return;
4043
4044 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02004045 I915_WRITE(GEN6_PMINTRMSK,
4046 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05304047
Imre Deak650ad972014-04-18 16:35:02 +03004048 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05304049
Chris Wilsonaed242f2015-03-18 09:48:21 +00004050 dev_priv->rps.cur_freq = val;
Deepak S76c3552f2014-01-30 23:08:16 +05304051
Chris Wilsonaed242f2015-03-18 09:48:21 +00004052 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Deepak S76c3552f2014-01-30 23:08:16 +05304053
4054 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02004055 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05304056 DRM_ERROR("timed out waiting for Punit\n");
4057
Imre Deak650ad972014-04-18 16:35:02 +03004058 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05304059
Chris Wilsonaed242f2015-03-18 09:48:21 +00004060 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Deepak S76c3552f2014-01-30 23:08:16 +05304061}
4062
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004063void gen6_rps_busy(struct drm_i915_private *dev_priv)
4064{
4065 mutex_lock(&dev_priv->rps.hw_lock);
4066 if (dev_priv->rps.enabled) {
4067 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4068 gen6_rps_reset_ei(dev_priv);
4069 I915_WRITE(GEN6_PMINTRMSK,
4070 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4071 }
4072 mutex_unlock(&dev_priv->rps.hw_lock);
4073}
4074
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004075void gen6_rps_idle(struct drm_i915_private *dev_priv)
4076{
Damien Lespiau691bb712013-12-12 14:36:36 +00004077 struct drm_device *dev = dev_priv->dev;
4078
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004079 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004080 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004081 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304082 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004083 else
Chris Wilsonaed242f2015-03-18 09:48:21 +00004084 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004085 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004086 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004087 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004088 mutex_unlock(&dev_priv->rps.hw_lock);
4089}
4090
4091void gen6_rps_boost(struct drm_i915_private *dev_priv)
4092{
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004093 u32 val;
4094
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004095 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004096 val = dev_priv->rps.max_freq_softlimit;
4097 if (dev_priv->rps.enabled &&
4098 dev_priv->mm.busy &&
4099 dev_priv->rps.cur_freq < val) {
4100 intel_set_rps(dev_priv->dev, val);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004101 dev_priv->rps.last_adj = 0;
4102 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004103 mutex_unlock(&dev_priv->rps.hw_lock);
4104}
4105
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004106void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004107{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004108 if (IS_VALLEYVIEW(dev))
4109 valleyview_set_rps(dev, val);
4110 else
4111 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004112}
4113
Zhe Wang20e49362014-11-04 17:07:05 +00004114static void gen9_disable_rps(struct drm_device *dev)
4115{
4116 struct drm_i915_private *dev_priv = dev->dev_private;
4117
4118 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004119 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004120}
4121
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004122static void gen6_disable_rps(struct drm_device *dev)
4123{
4124 struct drm_i915_private *dev_priv = dev->dev_private;
4125
4126 I915_WRITE(GEN6_RC_CONTROL, 0);
4127 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004128}
4129
Deepak S38807742014-05-23 21:00:15 +05304130static void cherryview_disable_rps(struct drm_device *dev)
4131{
4132 struct drm_i915_private *dev_priv = dev->dev_private;
4133
4134 I915_WRITE(GEN6_RC_CONTROL, 0);
4135}
4136
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004137static void valleyview_disable_rps(struct drm_device *dev)
4138{
4139 struct drm_i915_private *dev_priv = dev->dev_private;
4140
Deepak S98a2e5f2014-08-18 10:35:27 -07004141 /* we're doing forcewake before Disabling RC6,
4142 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004143 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004144
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004145 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004146
Mika Kuoppala59bad942015-01-16 11:34:40 +02004147 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004148}
4149
Ben Widawskydc39fff2013-10-18 12:32:07 -07004150static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4151{
Imre Deak91ca6892014-04-14 20:24:25 +03004152 if (IS_VALLEYVIEW(dev)) {
4153 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4154 mode = GEN6_RC_CTL_RC6_ENABLE;
4155 else
4156 mode = 0;
4157 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004158 if (HAS_RC6p(dev))
4159 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4160 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4161 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4162 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4163
4164 else
4165 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4166 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004167}
4168
Imre Deake6069ca2014-04-18 16:01:02 +03004169static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004170{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004171 /* No RC6 before Ironlake */
4172 if (INTEL_INFO(dev)->gen < 5)
4173 return 0;
4174
Imre Deake6069ca2014-04-18 16:01:02 +03004175 /* RC6 is only on Ironlake mobile not on desktop */
4176 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4177 return 0;
4178
Daniel Vetter456470e2012-08-08 23:35:40 +02004179 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004180 if (enable_rc6 >= 0) {
4181 int mask;
4182
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004183 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004184 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4185 INTEL_RC6pp_ENABLE;
4186 else
4187 mask = INTEL_RC6_ENABLE;
4188
4189 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004190 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4191 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004192
4193 return enable_rc6 & mask;
4194 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004195
Chris Wilson6567d742012-11-10 10:00:06 +00004196 /* Disable RC6 on Ironlake */
4197 if (INTEL_INFO(dev)->gen == 5)
4198 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004199
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004200 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004201 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004202
4203 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004204}
4205
Imre Deake6069ca2014-04-18 16:01:02 +03004206int intel_enable_rc6(const struct drm_device *dev)
4207{
4208 return i915.enable_rc6;
4209}
4210
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004211static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004212{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004213 struct drm_i915_private *dev_priv = dev->dev_private;
4214 uint32_t rp_state_cap;
4215 u32 ddcc_status = 0;
4216 int ret;
4217
4218 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004219 /* All of these values are in units of 50MHz */
4220 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004221 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004222 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004223 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004224 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Akash Goelcee991c2015-03-06 11:07:16 +05304225 if (IS_SKYLAKE(dev)) {
4226 /* Store the frequency values in 16.66 MHZ units, which is
4227 the natural hardware unit for SKL */
4228 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4229 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4230 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4231 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004232 /* hw_max = RP0 until we check for overclocking */
4233 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4234
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004235 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4236 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4237 ret = sandybridge_pcode_read(dev_priv,
4238 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4239 &ddcc_status);
4240 if (0 == ret)
4241 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004242 clamp_t(u8,
4243 ((ddcc_status >> 8) & 0xff),
4244 dev_priv->rps.min_freq,
4245 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004246 }
4247
Chris Wilsonaed242f2015-03-18 09:48:21 +00004248 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4249
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004250 /* Preserve min/max settings in case of re-init */
4251 if (dev_priv->rps.max_freq_softlimit == 0)
4252 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4253
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004254 if (dev_priv->rps.min_freq_softlimit == 0) {
4255 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4256 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004257 /* max(RPe, 450 MHz) */
4258 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004259 else
4260 dev_priv->rps.min_freq_softlimit =
4261 dev_priv->rps.min_freq;
4262 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004263}
4264
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004265/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004266static void gen9_enable_rps(struct drm_device *dev)
4267{
4268 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004269
4270 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4271
Damien Lespiauba1c5542015-01-16 18:07:26 +00004272 gen6_init_rps_frequencies(dev);
4273
Akash Goel0beb0592015-03-06 11:07:20 +05304274 /* Program defaults and thresholds for RPS*/
4275 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4276 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004277
Akash Goel0beb0592015-03-06 11:07:20 +05304278 /* 1 second timeout*/
4279 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4280 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4281
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004282 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004283
Akash Goel0beb0592015-03-06 11:07:20 +05304284 /* Leaning on the below call to gen6_set_rps to program/setup the
4285 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4286 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4287 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4288 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004289
4290 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4291}
4292
4293static void gen9_enable_rc6(struct drm_device *dev)
4294{
4295 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004296 struct intel_engine_cs *ring;
4297 uint32_t rc6_mask = 0;
4298 int unused;
4299
4300 /* 1a: Software RC state - RC0 */
4301 I915_WRITE(GEN6_RC_STATE, 0);
4302
4303 /* 1b: Get forcewake during program sequence. Although the driver
4304 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004305 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004306
4307 /* 2a: Disable RC states. */
4308 I915_WRITE(GEN6_RC_CONTROL, 0);
4309
4310 /* 2b: Program RC6 thresholds.*/
4311 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4312 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4313 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4314 for_each_ring(ring, dev_priv, unused)
4315 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4316 I915_WRITE(GEN6_RC_SLEEP, 0);
4317 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4318
Zhe Wang38c23522015-01-20 12:23:04 +00004319 /* 2c: Program Coarse Power Gating Policies. */
4320 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4321 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4322
Zhe Wang20e49362014-11-04 17:07:05 +00004323 /* 3a: Enable RC6 */
4324 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4325 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4326 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4327 "on" : "off");
4328 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4329 GEN6_RC_CTL_EI_MODE(1) |
4330 rc6_mask);
4331
Zhe Wang38c23522015-01-20 12:23:04 +00004332 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4333 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4334
Mika Kuoppala59bad942015-01-16 11:34:40 +02004335 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004336
4337}
4338
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004339static void gen8_enable_rps(struct drm_device *dev)
4340{
4341 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004342 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004343 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004344 int unused;
4345
4346 /* 1a: Software RC state - RC0 */
4347 I915_WRITE(GEN6_RC_STATE, 0);
4348
4349 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4350 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004351 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004352
4353 /* 2a: Disable RC states. */
4354 I915_WRITE(GEN6_RC_CONTROL, 0);
4355
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004356 /* Initialize rps frequencies */
4357 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004358
4359 /* 2b: Program RC6 thresholds.*/
4360 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4361 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4362 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4363 for_each_ring(ring, dev_priv, unused)
4364 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4365 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004366 if (IS_BROADWELL(dev))
4367 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4368 else
4369 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004370
4371 /* 3: Enable RC6 */
4372 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4373 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004374 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004375 if (IS_BROADWELL(dev))
4376 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4377 GEN7_RC_CTL_TO_MODE |
4378 rc6_mask);
4379 else
4380 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4381 GEN6_RC_CTL_EI_MODE(1) |
4382 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004383
4384 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004385 I915_WRITE(GEN6_RPNSWREQ,
4386 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4387 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4388 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004389 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4390 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004391
Daniel Vetter7526ed72014-09-29 15:07:19 +02004392 /* Docs recommend 900MHz, and 300 MHz respectively */
4393 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4394 dev_priv->rps.max_freq_softlimit << 24 |
4395 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004396
Daniel Vetter7526ed72014-09-29 15:07:19 +02004397 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4398 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4399 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4400 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004401
Daniel Vetter7526ed72014-09-29 15:07:19 +02004402 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004403
4404 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004405 I915_WRITE(GEN6_RP_CONTROL,
4406 GEN6_RP_MEDIA_TURBO |
4407 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4408 GEN6_RP_MEDIA_IS_GFX |
4409 GEN6_RP_ENABLE |
4410 GEN6_RP_UP_BUSY_AVG |
4411 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004412
Daniel Vetter7526ed72014-09-29 15:07:19 +02004413 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004414
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004415 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004416 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004417
Mika Kuoppala59bad942015-01-16 11:34:40 +02004418 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004419}
4420
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004421static void gen6_enable_rps(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;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004424 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004425 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004426 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004427 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004428 int i, ret;
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
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004432 /* Here begins a magic sequence of register writes to enable
4433 * auto-downclocking.
4434 *
4435 * Perhaps there might be some value in exposing these to
4436 * userspace...
4437 */
4438 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004439
4440 /* Clear the DBG now so we don't confuse earlier errors */
4441 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4442 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4443 I915_WRITE(GTFIFODBG, gtfifodbg);
4444 }
4445
Mika Kuoppala59bad942015-01-16 11:34:40 +02004446 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004447
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004448 /* Initialize rps frequencies */
4449 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004450
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004451 /* disable the counters and set deterministic thresholds */
4452 I915_WRITE(GEN6_RC_CONTROL, 0);
4453
4454 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4455 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4456 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4457 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4458 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4459
Chris Wilsonb4519512012-05-11 14:29:30 +01004460 for_each_ring(ring, dev_priv, i)
4461 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004462
4463 I915_WRITE(GEN6_RC_SLEEP, 0);
4464 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004465 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004466 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4467 else
4468 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004469 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004470 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4471
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004472 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004473 rc6_mode = intel_enable_rc6(dev_priv->dev);
4474 if (rc6_mode & INTEL_RC6_ENABLE)
4475 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4476
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004477 /* We don't use those on Haswell */
4478 if (!IS_HASWELL(dev)) {
4479 if (rc6_mode & INTEL_RC6p_ENABLE)
4480 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004481
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004482 if (rc6_mode & INTEL_RC6pp_ENABLE)
4483 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4484 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004485
Ben Widawskydc39fff2013-10-18 12:32:07 -07004486 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004487
4488 I915_WRITE(GEN6_RC_CONTROL,
4489 rc6_mask |
4490 GEN6_RC_CTL_EI_MODE(1) |
4491 GEN6_RC_CTL_HW_ENABLE);
4492
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004493 /* Power down if completely idle for over 50ms */
4494 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004495 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004496
Ben Widawsky42c05262012-09-26 10:34:00 -07004497 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004498 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004499 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004500
4501 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4502 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4503 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004504 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004505 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004506 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004507 }
4508
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004509 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004510 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004511
Ben Widawsky31643d52012-09-26 10:34:01 -07004512 rc6vids = 0;
4513 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4514 if (IS_GEN6(dev) && ret) {
4515 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4516 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4517 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4518 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4519 rc6vids &= 0xffff00;
4520 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4521 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4522 if (ret)
4523 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4524 }
4525
Mika Kuoppala59bad942015-01-16 11:34:40 +02004526 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004527}
4528
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004529static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004530{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004531 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004532 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004533 unsigned int gpu_freq;
4534 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004535 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004536 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004537
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004538 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004539
Ben Widawskyeda79642013-10-07 17:15:48 -03004540 policy = cpufreq_cpu_get(0);
4541 if (policy) {
4542 max_ia_freq = policy->cpuinfo.max_freq;
4543 cpufreq_cpu_put(policy);
4544 } else {
4545 /*
4546 * Default to measured freq if none found, PCU will ensure we
4547 * don't go over
4548 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004549 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004550 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004551
4552 /* Convert from kHz to MHz */
4553 max_ia_freq /= 1000;
4554
Ben Widawsky153b4b952013-10-22 22:05:09 -07004555 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004556 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4557 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004558
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004559 /*
4560 * For each potential GPU frequency, load a ring frequency we'd like
4561 * to use for memory access. We do this by specifying the IA frequency
4562 * the PCU should use as a reference to determine the ring frequency.
4563 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004564 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004565 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004566 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004567 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004568
Ben Widawsky46c764d2013-11-02 21:07:49 -07004569 if (INTEL_INFO(dev)->gen >= 8) {
4570 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4571 ring_freq = max(min_ring_freq, gpu_freq);
4572 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004573 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004574 ring_freq = max(min_ring_freq, ring_freq);
4575 /* leave ia_freq as the default, chosen by cpufreq */
4576 } else {
4577 /* On older processors, there is no separate ring
4578 * clock domain, so in order to boost the bandwidth
4579 * of the ring, we need to upclock the CPU (ia_freq).
4580 *
4581 * For GPU frequencies less than 750MHz,
4582 * just use the lowest ring freq.
4583 */
4584 if (gpu_freq < min_freq)
4585 ia_freq = 800;
4586 else
4587 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4588 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4589 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004590
Ben Widawsky42c05262012-09-26 10:34:00 -07004591 sandybridge_pcode_write(dev_priv,
4592 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004593 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4594 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4595 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004596 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004597}
4598
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004599void gen6_update_ring_freq(struct drm_device *dev)
4600{
4601 struct drm_i915_private *dev_priv = dev->dev_private;
4602
4603 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4604 return;
4605
4606 mutex_lock(&dev_priv->rps.hw_lock);
4607 __gen6_update_ring_freq(dev);
4608 mutex_unlock(&dev_priv->rps.hw_lock);
4609}
4610
Ville Syrjälä03af2042014-06-28 02:03:53 +03004611static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304612{
Deepak S095acd52015-01-17 11:05:59 +05304613 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304614 u32 val, rp0;
4615
Deepak S095acd52015-01-17 11:05:59 +05304616 if (dev->pdev->revision >= 0x20) {
4617 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304618
Deepak S095acd52015-01-17 11:05:59 +05304619 switch (INTEL_INFO(dev)->eu_total) {
4620 case 8:
4621 /* (2 * 4) config */
4622 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4623 break;
4624 case 12:
4625 /* (2 * 6) config */
4626 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4627 break;
4628 case 16:
4629 /* (2 * 8) config */
4630 default:
4631 /* Setting (2 * 8) Min RP0 for any other combination */
4632 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4633 break;
4634 }
4635 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4636 } else {
4637 /* For pre-production hardware */
4638 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4639 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4640 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4641 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304642 return rp0;
4643}
4644
4645static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4646{
4647 u32 val, rpe;
4648
4649 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4650 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4651
4652 return rpe;
4653}
4654
Deepak S7707df42014-07-12 18:46:14 +05304655static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4656{
Deepak S095acd52015-01-17 11:05:59 +05304657 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304658 u32 val, rp1;
4659
Deepak S095acd52015-01-17 11:05:59 +05304660 if (dev->pdev->revision >= 0x20) {
4661 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4662 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4663 } else {
4664 /* For pre-production hardware */
4665 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4666 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4667 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4668 }
Deepak S7707df42014-07-12 18:46:14 +05304669 return rp1;
4670}
4671
Ville Syrjälä03af2042014-06-28 02:03:53 +03004672static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304673{
Deepak S095acd52015-01-17 11:05:59 +05304674 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304675 u32 val, rpn;
4676
Deepak S095acd52015-01-17 11:05:59 +05304677 if (dev->pdev->revision >= 0x20) {
4678 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4679 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4680 FB_GFX_FREQ_FUSE_MASK);
4681 } else { /* For pre-production hardware */
4682 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4683 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4684 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4685 }
4686
Deepak S2b6b3a02014-05-27 15:59:30 +05304687 return rpn;
4688}
4689
Deepak Sf8f2b002014-07-10 13:16:21 +05304690static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4691{
4692 u32 val, rp1;
4693
4694 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4695
4696 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4697
4698 return rp1;
4699}
4700
Ville Syrjälä03af2042014-06-28 02:03:53 +03004701static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004702{
4703 u32 val, rp0;
4704
Jani Nikula64936252013-05-22 15:36:20 +03004705 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004706
4707 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4708 /* Clamp to max */
4709 rp0 = min_t(u32, rp0, 0xea);
4710
4711 return rp0;
4712}
4713
4714static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4715{
4716 u32 val, rpe;
4717
Jani Nikula64936252013-05-22 15:36:20 +03004718 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004719 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004720 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004721 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4722
4723 return rpe;
4724}
4725
Ville Syrjälä03af2042014-06-28 02:03:53 +03004726static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004727{
Jani Nikula64936252013-05-22 15:36:20 +03004728 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004729}
4730
Imre Deakae484342014-03-31 15:10:44 +03004731/* Check that the pctx buffer wasn't move under us. */
4732static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4733{
4734 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4735
4736 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4737 dev_priv->vlv_pctx->stolen->start);
4738}
4739
Deepak S38807742014-05-23 21:00:15 +05304740
4741/* Check that the pcbr address is not empty. */
4742static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4743{
4744 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4745
4746 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4747}
4748
4749static void cherryview_setup_pctx(struct drm_device *dev)
4750{
4751 struct drm_i915_private *dev_priv = dev->dev_private;
4752 unsigned long pctx_paddr, paddr;
4753 struct i915_gtt *gtt = &dev_priv->gtt;
4754 u32 pcbr;
4755 int pctx_size = 32*1024;
4756
4757 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4758
4759 pcbr = I915_READ(VLV_PCBR);
4760 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004761 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304762 paddr = (dev_priv->mm.stolen_base +
4763 (gtt->stolen_size - pctx_size));
4764
4765 pctx_paddr = (paddr & (~4095));
4766 I915_WRITE(VLV_PCBR, pctx_paddr);
4767 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004768
4769 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304770}
4771
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004772static void valleyview_setup_pctx(struct drm_device *dev)
4773{
4774 struct drm_i915_private *dev_priv = dev->dev_private;
4775 struct drm_i915_gem_object *pctx;
4776 unsigned long pctx_paddr;
4777 u32 pcbr;
4778 int pctx_size = 24*1024;
4779
Imre Deak17b0c1f2014-02-11 21:39:06 +02004780 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4781
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004782 pcbr = I915_READ(VLV_PCBR);
4783 if (pcbr) {
4784 /* BIOS set it up already, grab the pre-alloc'd space */
4785 int pcbr_offset;
4786
4787 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4788 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4789 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004790 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004791 pctx_size);
4792 goto out;
4793 }
4794
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004795 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4796
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004797 /*
4798 * From the Gunit register HAS:
4799 * The Gfx driver is expected to program this register and ensure
4800 * proper allocation within Gfx stolen memory. For example, this
4801 * register should be programmed such than the PCBR range does not
4802 * overlap with other ranges, such as the frame buffer, protected
4803 * memory, or any other relevant ranges.
4804 */
4805 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4806 if (!pctx) {
4807 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4808 return;
4809 }
4810
4811 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4812 I915_WRITE(VLV_PCBR, pctx_paddr);
4813
4814out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004815 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004816 dev_priv->vlv_pctx = pctx;
4817}
4818
Imre Deakae484342014-03-31 15:10:44 +03004819static void valleyview_cleanup_pctx(struct drm_device *dev)
4820{
4821 struct drm_i915_private *dev_priv = dev->dev_private;
4822
4823 if (WARN_ON(!dev_priv->vlv_pctx))
4824 return;
4825
4826 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4827 dev_priv->vlv_pctx = NULL;
4828}
4829
Imre Deak4e805192014-04-14 20:24:41 +03004830static void valleyview_init_gt_powersave(struct drm_device *dev)
4831{
4832 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004833 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004834
4835 valleyview_setup_pctx(dev);
4836
4837 mutex_lock(&dev_priv->rps.hw_lock);
4838
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004839 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4840 switch ((val >> 6) & 3) {
4841 case 0:
4842 case 1:
4843 dev_priv->mem_freq = 800;
4844 break;
4845 case 2:
4846 dev_priv->mem_freq = 1066;
4847 break;
4848 case 3:
4849 dev_priv->mem_freq = 1333;
4850 break;
4851 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004852 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004853
Imre Deak4e805192014-04-14 20:24:41 +03004854 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4855 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4856 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004857 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004858 dev_priv->rps.max_freq);
4859
4860 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4861 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004862 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004863 dev_priv->rps.efficient_freq);
4864
Deepak Sf8f2b002014-07-10 13:16:21 +05304865 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4866 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004867 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304868 dev_priv->rps.rp1_freq);
4869
Imre Deak4e805192014-04-14 20:24:41 +03004870 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4871 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004872 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004873 dev_priv->rps.min_freq);
4874
Chris Wilsonaed242f2015-03-18 09:48:21 +00004875 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4876
Imre Deak4e805192014-04-14 20:24:41 +03004877 /* Preserve min/max settings in case of re-init */
4878 if (dev_priv->rps.max_freq_softlimit == 0)
4879 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4880
4881 if (dev_priv->rps.min_freq_softlimit == 0)
4882 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4883
4884 mutex_unlock(&dev_priv->rps.hw_lock);
4885}
4886
Deepak S38807742014-05-23 21:00:15 +05304887static void cherryview_init_gt_powersave(struct drm_device *dev)
4888{
Deepak S2b6b3a02014-05-27 15:59:30 +05304889 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004890 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304891
Deepak S38807742014-05-23 21:00:15 +05304892 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304893
4894 mutex_lock(&dev_priv->rps.hw_lock);
4895
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004896 mutex_lock(&dev_priv->dpio_lock);
4897 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4898 mutex_unlock(&dev_priv->dpio_lock);
4899
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004900 switch ((val >> 2) & 0x7) {
4901 case 0:
4902 case 1:
4903 dev_priv->rps.cz_freq = 200;
4904 dev_priv->mem_freq = 1600;
4905 break;
4906 case 2:
4907 dev_priv->rps.cz_freq = 267;
4908 dev_priv->mem_freq = 1600;
4909 break;
4910 case 3:
4911 dev_priv->rps.cz_freq = 333;
4912 dev_priv->mem_freq = 2000;
4913 break;
4914 case 4:
4915 dev_priv->rps.cz_freq = 320;
4916 dev_priv->mem_freq = 1600;
4917 break;
4918 case 5:
4919 dev_priv->rps.cz_freq = 400;
4920 dev_priv->mem_freq = 1600;
4921 break;
4922 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004923 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004924
Deepak S2b6b3a02014-05-27 15:59:30 +05304925 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4926 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4927 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004928 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304929 dev_priv->rps.max_freq);
4930
4931 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4932 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004933 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304934 dev_priv->rps.efficient_freq);
4935
Deepak S7707df42014-07-12 18:46:14 +05304936 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4937 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004938 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304939 dev_priv->rps.rp1_freq);
4940
Deepak S2b6b3a02014-05-27 15:59:30 +05304941 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4942 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004943 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304944 dev_priv->rps.min_freq);
4945
Ville Syrjälä1c147622014-08-18 14:42:43 +03004946 WARN_ONCE((dev_priv->rps.max_freq |
4947 dev_priv->rps.efficient_freq |
4948 dev_priv->rps.rp1_freq |
4949 dev_priv->rps.min_freq) & 1,
4950 "Odd GPU freq values\n");
4951
Chris Wilsonaed242f2015-03-18 09:48:21 +00004952 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4953
Deepak S2b6b3a02014-05-27 15:59:30 +05304954 /* Preserve min/max settings in case of re-init */
4955 if (dev_priv->rps.max_freq_softlimit == 0)
4956 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4957
4958 if (dev_priv->rps.min_freq_softlimit == 0)
4959 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4960
4961 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304962}
4963
Imre Deak4e805192014-04-14 20:24:41 +03004964static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4965{
4966 valleyview_cleanup_pctx(dev);
4967}
4968
Deepak S38807742014-05-23 21:00:15 +05304969static void cherryview_enable_rps(struct drm_device *dev)
4970{
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304973 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304974 int i;
4975
4976 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4977
4978 gtfifodbg = I915_READ(GTFIFODBG);
4979 if (gtfifodbg) {
4980 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4981 gtfifodbg);
4982 I915_WRITE(GTFIFODBG, gtfifodbg);
4983 }
4984
4985 cherryview_check_pctx(dev_priv);
4986
4987 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4988 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004989 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304990
Ville Syrjälä160614a2015-01-19 13:50:47 +02004991 /* Disable RC states. */
4992 I915_WRITE(GEN6_RC_CONTROL, 0);
4993
Deepak S38807742014-05-23 21:00:15 +05304994 /* 2a: Program RC6 thresholds.*/
4995 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4996 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4997 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4998
4999 for_each_ring(ring, dev_priv, i)
5000 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5001 I915_WRITE(GEN6_RC_SLEEP, 0);
5002
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005003 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
5004 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05305005
5006 /* allows RC6 residency counter to work */
5007 I915_WRITE(VLV_COUNTER_CONTROL,
5008 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5009 VLV_MEDIA_RC6_COUNT_EN |
5010 VLV_RENDER_RC6_COUNT_EN));
5011
5012 /* For now we assume BIOS is allocating and populating the PCBR */
5013 pcbr = I915_READ(VLV_PCBR);
5014
Deepak S38807742014-05-23 21:00:15 +05305015 /* 3: Enable RC6 */
5016 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5017 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005018 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305019
5020 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5021
Deepak S2b6b3a02014-05-27 15:59:30 +05305022 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005023 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305024 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5025 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5026 I915_WRITE(GEN6_RP_UP_EI, 66000);
5027 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5028
5029 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5030
5031 /* 5: Enable RPS */
5032 I915_WRITE(GEN6_RP_CONTROL,
5033 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005034 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305035 GEN6_RP_ENABLE |
5036 GEN6_RP_UP_BUSY_AVG |
5037 GEN6_RP_DOWN_IDLE_AVG);
5038
5039 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5040
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005041 /* RPS code assumes GPLL is used */
5042 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5043
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005044 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05305045 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5046
5047 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5048 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005049 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305050 dev_priv->rps.cur_freq);
5051
5052 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005053 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305054 dev_priv->rps.efficient_freq);
5055
5056 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5057
Mika Kuoppala59bad942015-01-16 11:34:40 +02005058 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305059}
5060
Jesse Barnes0a073b82013-04-17 15:54:58 -07005061static void valleyview_enable_rps(struct drm_device *dev)
5062{
5063 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005064 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005065 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005066 int i;
5067
5068 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5069
Imre Deakae484342014-03-31 15:10:44 +03005070 valleyview_check_pctx(dev_priv);
5071
Jesse Barnes0a073b82013-04-17 15:54:58 -07005072 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005073 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5074 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005075 I915_WRITE(GTFIFODBG, gtfifodbg);
5076 }
5077
Deepak Sc8d9a592013-11-23 14:55:42 +05305078 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005079 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005080
Ville Syrjälä160614a2015-01-19 13:50:47 +02005081 /* Disable RC states. */
5082 I915_WRITE(GEN6_RC_CONTROL, 0);
5083
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005084 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005085 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5086 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5087 I915_WRITE(GEN6_RP_UP_EI, 66000);
5088 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5089
5090 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5091
5092 I915_WRITE(GEN6_RP_CONTROL,
5093 GEN6_RP_MEDIA_TURBO |
5094 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5095 GEN6_RP_MEDIA_IS_GFX |
5096 GEN6_RP_ENABLE |
5097 GEN6_RP_UP_BUSY_AVG |
5098 GEN6_RP_DOWN_IDLE_CONT);
5099
5100 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5101 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5102 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5103
5104 for_each_ring(ring, dev_priv, i)
5105 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5106
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005107 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005108
5109 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005110 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005111 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5112 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005113 VLV_MEDIA_RC6_COUNT_EN |
5114 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005115
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005116 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005117 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005118
5119 intel_print_rc6_info(dev, rc6_mode);
5120
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005121 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005122
Jani Nikula64936252013-05-22 15:36:20 +03005123 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005124
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005125 /* RPS code assumes GPLL is used */
5126 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5127
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005128 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07005129 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5130
Ben Widawskyb39fb292014-03-19 18:31:11 -07005131 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005132 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005133 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005134 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005135
Ville Syrjälä73008b92013-06-25 19:21:01 +03005136 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005137 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005138 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005139
Ben Widawskyb39fb292014-03-19 18:31:11 -07005140 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005141
Mika Kuoppala59bad942015-01-16 11:34:40 +02005142 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005143}
5144
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005145static unsigned long intel_pxfreq(u32 vidfreq)
5146{
5147 unsigned long freq;
5148 int div = (vidfreq & 0x3f0000) >> 16;
5149 int post = (vidfreq & 0x3000) >> 12;
5150 int pre = (vidfreq & 0x7);
5151
5152 if (!pre)
5153 return 0;
5154
5155 freq = ((div * 133333) / ((1<<post) * pre));
5156
5157 return freq;
5158}
5159
Daniel Vettereb48eb02012-04-26 23:28:12 +02005160static const struct cparams {
5161 u16 i;
5162 u16 t;
5163 u16 m;
5164 u16 c;
5165} cparams[] = {
5166 { 1, 1333, 301, 28664 },
5167 { 1, 1066, 294, 24460 },
5168 { 1, 800, 294, 25192 },
5169 { 0, 1333, 276, 27605 },
5170 { 0, 1066, 276, 27605 },
5171 { 0, 800, 231, 23784 },
5172};
5173
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005174static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005175{
5176 u64 total_count, diff, ret;
5177 u32 count1, count2, count3, m = 0, c = 0;
5178 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5179 int i;
5180
Daniel Vetter02d71952012-08-09 16:44:54 +02005181 assert_spin_locked(&mchdev_lock);
5182
Daniel Vetter20e4d402012-08-08 23:35:39 +02005183 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005184
5185 /* Prevent division-by-zero if we are asking too fast.
5186 * Also, we don't get interesting results if we are polling
5187 * faster than once in 10ms, so just return the saved value
5188 * in such cases.
5189 */
5190 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005191 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005192
5193 count1 = I915_READ(DMIEC);
5194 count2 = I915_READ(DDREC);
5195 count3 = I915_READ(CSIEC);
5196
5197 total_count = count1 + count2 + count3;
5198
5199 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005200 if (total_count < dev_priv->ips.last_count1) {
5201 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005202 diff += total_count;
5203 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005204 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005205 }
5206
5207 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005208 if (cparams[i].i == dev_priv->ips.c_m &&
5209 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005210 m = cparams[i].m;
5211 c = cparams[i].c;
5212 break;
5213 }
5214 }
5215
5216 diff = div_u64(diff, diff1);
5217 ret = ((m * diff) + c);
5218 ret = div_u64(ret, 10);
5219
Daniel Vetter20e4d402012-08-08 23:35:39 +02005220 dev_priv->ips.last_count1 = total_count;
5221 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005222
Daniel Vetter20e4d402012-08-08 23:35:39 +02005223 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005224
5225 return ret;
5226}
5227
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005228unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5229{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005230 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005231 unsigned long val;
5232
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005233 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005234 return 0;
5235
5236 spin_lock_irq(&mchdev_lock);
5237
5238 val = __i915_chipset_val(dev_priv);
5239
5240 spin_unlock_irq(&mchdev_lock);
5241
5242 return val;
5243}
5244
Daniel Vettereb48eb02012-04-26 23:28:12 +02005245unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5246{
5247 unsigned long m, x, b;
5248 u32 tsfs;
5249
5250 tsfs = I915_READ(TSFS);
5251
5252 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5253 x = I915_READ8(TR1);
5254
5255 b = tsfs & TSFS_INTR_MASK;
5256
5257 return ((m * x) / 127) - b;
5258}
5259
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005260static int _pxvid_to_vd(u8 pxvid)
5261{
5262 if (pxvid == 0)
5263 return 0;
5264
5265 if (pxvid >= 8 && pxvid < 31)
5266 pxvid = 31;
5267
5268 return (pxvid + 2) * 125;
5269}
5270
5271static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005272{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005273 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005274 const int vd = _pxvid_to_vd(pxvid);
5275 const int vm = vd - 1125;
5276
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005277 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005278 return vm > 0 ? vm : 0;
5279
5280 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005281}
5282
Daniel Vetter02d71952012-08-09 16:44:54 +02005283static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005284{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005285 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005286 u32 count;
5287
Daniel Vetter02d71952012-08-09 16:44:54 +02005288 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005289
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005290 now = ktime_get_raw_ns();
5291 diffms = now - dev_priv->ips.last_time2;
5292 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005293
5294 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005295 if (!diffms)
5296 return;
5297
5298 count = I915_READ(GFXEC);
5299
Daniel Vetter20e4d402012-08-08 23:35:39 +02005300 if (count < dev_priv->ips.last_count2) {
5301 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005302 diff += count;
5303 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005304 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005305 }
5306
Daniel Vetter20e4d402012-08-08 23:35:39 +02005307 dev_priv->ips.last_count2 = count;
5308 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005309
5310 /* More magic constants... */
5311 diff = diff * 1181;
5312 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005313 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005314}
5315
Daniel Vetter02d71952012-08-09 16:44:54 +02005316void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5317{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005318 struct drm_device *dev = dev_priv->dev;
5319
5320 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005321 return;
5322
Daniel Vetter92703882012-08-09 16:46:01 +02005323 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005324
5325 __i915_update_gfx_val(dev_priv);
5326
Daniel Vetter92703882012-08-09 16:46:01 +02005327 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005328}
5329
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005330static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005331{
5332 unsigned long t, corr, state1, corr2, state2;
5333 u32 pxvid, ext_v;
5334
Daniel Vetter02d71952012-08-09 16:44:54 +02005335 assert_spin_locked(&mchdev_lock);
5336
Ben Widawskyb39fb292014-03-19 18:31:11 -07005337 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005338 pxvid = (pxvid >> 24) & 0x7f;
5339 ext_v = pvid_to_extvid(dev_priv, pxvid);
5340
5341 state1 = ext_v;
5342
5343 t = i915_mch_val(dev_priv);
5344
5345 /* Revel in the empirically derived constants */
5346
5347 /* Correction factor in 1/100000 units */
5348 if (t > 80)
5349 corr = ((t * 2349) + 135940);
5350 else if (t >= 50)
5351 corr = ((t * 964) + 29317);
5352 else /* < 50 */
5353 corr = ((t * 301) + 1004);
5354
5355 corr = corr * ((150142 * state1) / 10000 - 78642);
5356 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005357 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005358
5359 state2 = (corr2 * state1) / 10000;
5360 state2 /= 100; /* convert to mW */
5361
Daniel Vetter02d71952012-08-09 16:44:54 +02005362 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005363
Daniel Vetter20e4d402012-08-08 23:35:39 +02005364 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005365}
5366
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005367unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5368{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005369 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005370 unsigned long val;
5371
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005372 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005373 return 0;
5374
5375 spin_lock_irq(&mchdev_lock);
5376
5377 val = __i915_gfx_val(dev_priv);
5378
5379 spin_unlock_irq(&mchdev_lock);
5380
5381 return val;
5382}
5383
Daniel Vettereb48eb02012-04-26 23:28:12 +02005384/**
5385 * i915_read_mch_val - return value for IPS use
5386 *
5387 * Calculate and return a value for the IPS driver to use when deciding whether
5388 * we have thermal and power headroom to increase CPU or GPU power budget.
5389 */
5390unsigned long i915_read_mch_val(void)
5391{
5392 struct drm_i915_private *dev_priv;
5393 unsigned long chipset_val, graphics_val, ret = 0;
5394
Daniel Vetter92703882012-08-09 16:46:01 +02005395 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005396 if (!i915_mch_dev)
5397 goto out_unlock;
5398 dev_priv = i915_mch_dev;
5399
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005400 chipset_val = __i915_chipset_val(dev_priv);
5401 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005402
5403 ret = chipset_val + graphics_val;
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_read_mch_val);
5411
5412/**
5413 * i915_gpu_raise - raise GPU frequency limit
5414 *
5415 * Raise the limit; IPS indicates we have thermal headroom.
5416 */
5417bool i915_gpu_raise(void)
5418{
5419 struct drm_i915_private *dev_priv;
5420 bool ret = true;
5421
Daniel Vetter92703882012-08-09 16:46:01 +02005422 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005423 if (!i915_mch_dev) {
5424 ret = false;
5425 goto out_unlock;
5426 }
5427 dev_priv = i915_mch_dev;
5428
Daniel Vetter20e4d402012-08-08 23:35:39 +02005429 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5430 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005431
5432out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005433 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005434
5435 return ret;
5436}
5437EXPORT_SYMBOL_GPL(i915_gpu_raise);
5438
5439/**
5440 * i915_gpu_lower - lower GPU frequency limit
5441 *
5442 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5443 * frequency maximum.
5444 */
5445bool i915_gpu_lower(void)
5446{
5447 struct drm_i915_private *dev_priv;
5448 bool ret = true;
5449
Daniel Vetter92703882012-08-09 16:46:01 +02005450 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005451 if (!i915_mch_dev) {
5452 ret = false;
5453 goto out_unlock;
5454 }
5455 dev_priv = i915_mch_dev;
5456
Daniel Vetter20e4d402012-08-08 23:35:39 +02005457 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5458 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005459
5460out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005461 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005462
5463 return ret;
5464}
5465EXPORT_SYMBOL_GPL(i915_gpu_lower);
5466
5467/**
5468 * i915_gpu_busy - indicate GPU business to IPS
5469 *
5470 * Tell the IPS driver whether or not the GPU is busy.
5471 */
5472bool i915_gpu_busy(void)
5473{
5474 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005475 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005476 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005477 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005478
Daniel Vetter92703882012-08-09 16:46:01 +02005479 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005480 if (!i915_mch_dev)
5481 goto out_unlock;
5482 dev_priv = i915_mch_dev;
5483
Chris Wilsonf047e392012-07-21 12:31:41 +01005484 for_each_ring(ring, dev_priv, i)
5485 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005486
5487out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005488 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005489
5490 return ret;
5491}
5492EXPORT_SYMBOL_GPL(i915_gpu_busy);
5493
5494/**
5495 * i915_gpu_turbo_disable - disable graphics turbo
5496 *
5497 * Disable graphics turbo by resetting the max frequency and setting the
5498 * current frequency to the default.
5499 */
5500bool i915_gpu_turbo_disable(void)
5501{
5502 struct drm_i915_private *dev_priv;
5503 bool ret = true;
5504
Daniel Vetter92703882012-08-09 16:46:01 +02005505 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005506 if (!i915_mch_dev) {
5507 ret = false;
5508 goto out_unlock;
5509 }
5510 dev_priv = i915_mch_dev;
5511
Daniel Vetter20e4d402012-08-08 23:35:39 +02005512 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005513
Daniel Vetter20e4d402012-08-08 23:35:39 +02005514 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005515 ret = false;
5516
5517out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005518 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005519
5520 return ret;
5521}
5522EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5523
5524/**
5525 * Tells the intel_ips driver that the i915 driver is now loaded, if
5526 * IPS got loaded first.
5527 *
5528 * This awkward dance is so that neither module has to depend on the
5529 * other in order for IPS to do the appropriate communication of
5530 * GPU turbo limits to i915.
5531 */
5532static void
5533ips_ping_for_i915_load(void)
5534{
5535 void (*link)(void);
5536
5537 link = symbol_get(ips_link_to_i915_driver);
5538 if (link) {
5539 link();
5540 symbol_put(ips_link_to_i915_driver);
5541 }
5542}
5543
5544void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5545{
Daniel Vetter02d71952012-08-09 16:44:54 +02005546 /* We only register the i915 ips part with intel-ips once everything is
5547 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005548 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005549 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005550 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005551
5552 ips_ping_for_i915_load();
5553}
5554
5555void intel_gpu_ips_teardown(void)
5556{
Daniel Vetter92703882012-08-09 16:46:01 +02005557 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005558 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005559 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005560}
Deepak S76c3552f2014-01-30 23:08:16 +05305561
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005562static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005563{
5564 struct drm_i915_private *dev_priv = dev->dev_private;
5565 u32 lcfuse;
5566 u8 pxw[16];
5567 int i;
5568
5569 /* Disable to program */
5570 I915_WRITE(ECR, 0);
5571 POSTING_READ(ECR);
5572
5573 /* Program energy weights for various events */
5574 I915_WRITE(SDEW, 0x15040d00);
5575 I915_WRITE(CSIEW0, 0x007f0000);
5576 I915_WRITE(CSIEW1, 0x1e220004);
5577 I915_WRITE(CSIEW2, 0x04000004);
5578
5579 for (i = 0; i < 5; i++)
5580 I915_WRITE(PEW + (i * 4), 0);
5581 for (i = 0; i < 3; i++)
5582 I915_WRITE(DEW + (i * 4), 0);
5583
5584 /* Program P-state weights to account for frequency power adjustment */
5585 for (i = 0; i < 16; i++) {
5586 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5587 unsigned long freq = intel_pxfreq(pxvidfreq);
5588 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5589 PXVFREQ_PX_SHIFT;
5590 unsigned long val;
5591
5592 val = vid * vid;
5593 val *= (freq / 1000);
5594 val *= 255;
5595 val /= (127*127*900);
5596 if (val > 0xff)
5597 DRM_ERROR("bad pxval: %ld\n", val);
5598 pxw[i] = val;
5599 }
5600 /* Render standby states get 0 weight */
5601 pxw[14] = 0;
5602 pxw[15] = 0;
5603
5604 for (i = 0; i < 4; i++) {
5605 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5606 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5607 I915_WRITE(PXW + (i * 4), val);
5608 }
5609
5610 /* Adjust magic regs to magic values (more experimental results) */
5611 I915_WRITE(OGW0, 0);
5612 I915_WRITE(OGW1, 0);
5613 I915_WRITE(EG0, 0x00007f00);
5614 I915_WRITE(EG1, 0x0000000e);
5615 I915_WRITE(EG2, 0x000e0000);
5616 I915_WRITE(EG3, 0x68000300);
5617 I915_WRITE(EG4, 0x42000000);
5618 I915_WRITE(EG5, 0x00140031);
5619 I915_WRITE(EG6, 0);
5620 I915_WRITE(EG7, 0);
5621
5622 for (i = 0; i < 8; i++)
5623 I915_WRITE(PXWL + (i * 4), 0);
5624
5625 /* Enable PMON + select events */
5626 I915_WRITE(ECR, 0x80000019);
5627
5628 lcfuse = I915_READ(LCFUSE02);
5629
Daniel Vetter20e4d402012-08-08 23:35:39 +02005630 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005631}
5632
Imre Deakae484342014-03-31 15:10:44 +03005633void intel_init_gt_powersave(struct drm_device *dev)
5634{
Imre Deake6069ca2014-04-18 16:01:02 +03005635 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5636
Deepak S38807742014-05-23 21:00:15 +05305637 if (IS_CHERRYVIEW(dev))
5638 cherryview_init_gt_powersave(dev);
5639 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005640 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005641}
5642
5643void intel_cleanup_gt_powersave(struct drm_device *dev)
5644{
Deepak S38807742014-05-23 21:00:15 +05305645 if (IS_CHERRYVIEW(dev))
5646 return;
5647 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005648 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005649}
5650
Imre Deakdbea3ce2014-12-15 18:59:28 +02005651static void gen6_suspend_rps(struct drm_device *dev)
5652{
5653 struct drm_i915_private *dev_priv = dev->dev_private;
5654
5655 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5656
Akash Goel4c2a8892015-03-06 11:07:24 +05305657 gen6_disable_rps_interrupts(dev);
Imre Deakdbea3ce2014-12-15 18:59:28 +02005658}
5659
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005660/**
5661 * intel_suspend_gt_powersave - suspend PM work and helper threads
5662 * @dev: drm device
5663 *
5664 * We don't want to disable RC6 or other features here, we just want
5665 * to make sure any work we've queued has finished and won't bother
5666 * us while we're suspended.
5667 */
5668void intel_suspend_gt_powersave(struct drm_device *dev)
5669{
5670 struct drm_i915_private *dev_priv = dev->dev_private;
5671
Imre Deakd4d70aa2014-11-19 15:30:04 +02005672 if (INTEL_INFO(dev)->gen < 6)
5673 return;
5674
Imre Deakdbea3ce2014-12-15 18:59:28 +02005675 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305676
5677 /* Force GPU to min freq during suspend */
5678 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005679}
5680
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005681void intel_disable_gt_powersave(struct drm_device *dev)
5682{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005683 struct drm_i915_private *dev_priv = dev->dev_private;
5684
Daniel Vetter930ebb42012-06-29 23:32:16 +02005685 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005686 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305687 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005688 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005689
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005690 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005691 if (INTEL_INFO(dev)->gen >= 9)
5692 gen9_disable_rps(dev);
5693 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305694 cherryview_disable_rps(dev);
5695 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005696 valleyview_disable_rps(dev);
5697 else
5698 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005699
Chris Wilsonc0951f02013-10-10 21:58:50 +01005700 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005701 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005702 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005703}
5704
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005705static void intel_gen6_powersave_work(struct work_struct *work)
5706{
5707 struct drm_i915_private *dev_priv =
5708 container_of(work, struct drm_i915_private,
5709 rps.delayed_resume_work.work);
5710 struct drm_device *dev = dev_priv->dev;
5711
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005712 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005713
Akash Goel4c2a8892015-03-06 11:07:24 +05305714 gen6_reset_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02005715
Deepak S38807742014-05-23 21:00:15 +05305716 if (IS_CHERRYVIEW(dev)) {
5717 cherryview_enable_rps(dev);
5718 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005719 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005720 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005721 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005722 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005723 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005724 } else if (IS_BROADWELL(dev)) {
5725 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005726 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005727 } else {
5728 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005729 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005730 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00005731
5732 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
5733 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
5734
5735 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
5736 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
5737
Chris Wilsonc0951f02013-10-10 21:58:50 +01005738 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005739
Akash Goel4c2a8892015-03-06 11:07:24 +05305740 gen6_enable_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02005741
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005742 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005743
5744 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005745}
5746
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005747void intel_enable_gt_powersave(struct drm_device *dev)
5748{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005749 struct drm_i915_private *dev_priv = dev->dev_private;
5750
Yu Zhangf61018b2015-02-10 19:05:52 +08005751 /* Powersaving is controlled by the host when inside a VM */
5752 if (intel_vgpu_active(dev))
5753 return;
5754
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005755 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005756 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005757 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005758 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005759 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305760 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005761 /*
5762 * PCU communication is slow and this doesn't need to be
5763 * done at any specific time, so do this out of our fast path
5764 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005765 *
5766 * We depend on the HW RC6 power context save/restore
5767 * mechanism when entering D3 through runtime PM suspend. So
5768 * disable RPM until RPS/RC6 is properly setup. We can only
5769 * get here via the driver load/system resume/runtime resume
5770 * paths, so the _noresume version is enough (and in case of
5771 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005772 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005773 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5774 round_jiffies_up_relative(HZ)))
5775 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005776 }
5777}
5778
Imre Deakc6df39b2014-04-14 20:24:29 +03005779void intel_reset_gt_powersave(struct drm_device *dev)
5780{
5781 struct drm_i915_private *dev_priv = dev->dev_private;
5782
Imre Deakdbea3ce2014-12-15 18:59:28 +02005783 if (INTEL_INFO(dev)->gen < 6)
5784 return;
5785
5786 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005787 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005788}
5789
Daniel Vetter3107bd42012-10-31 22:52:31 +01005790static void ibx_init_clock_gating(struct drm_device *dev)
5791{
5792 struct drm_i915_private *dev_priv = dev->dev_private;
5793
5794 /*
5795 * On Ibex Peak and Cougar Point, we need to disable clock
5796 * gating for the panel power sequencer or it will fail to
5797 * start up when no ports are active.
5798 */
5799 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5800}
5801
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005802static void g4x_disable_trickle_feed(struct drm_device *dev)
5803{
5804 struct drm_i915_private *dev_priv = dev->dev_private;
5805 int pipe;
5806
Damien Lespiau055e3932014-08-18 13:49:10 +01005807 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005808 I915_WRITE(DSPCNTR(pipe),
5809 I915_READ(DSPCNTR(pipe)) |
5810 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005811 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005812 }
5813}
5814
Ville Syrjälä017636c2013-12-05 15:51:37 +02005815static void ilk_init_lp_watermarks(struct drm_device *dev)
5816{
5817 struct drm_i915_private *dev_priv = dev->dev_private;
5818
5819 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5820 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5821 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5822
5823 /*
5824 * Don't touch WM1S_LP_EN here.
5825 * Doing so could cause underruns.
5826 */
5827}
5828
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005829static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005830{
5831 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005832 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005833
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005834 /*
5835 * Required for FBC
5836 * WaFbcDisableDpfcClockGating:ilk
5837 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005838 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5839 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5840 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005841
5842 I915_WRITE(PCH_3DCGDIS0,
5843 MARIUNIT_CLOCK_GATE_DISABLE |
5844 SVSMUNIT_CLOCK_GATE_DISABLE);
5845 I915_WRITE(PCH_3DCGDIS1,
5846 VFMUNIT_CLOCK_GATE_DISABLE);
5847
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005848 /*
5849 * According to the spec the following bits should be set in
5850 * order to enable memory self-refresh
5851 * The bit 22/21 of 0x42004
5852 * The bit 5 of 0x42020
5853 * The bit 15 of 0x45000
5854 */
5855 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5856 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5857 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005858 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005859 I915_WRITE(DISP_ARB_CTL,
5860 (I915_READ(DISP_ARB_CTL) |
5861 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005862
5863 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005864
5865 /*
5866 * Based on the document from hardware guys the following bits
5867 * should be set unconditionally in order to enable FBC.
5868 * The bit 22 of 0x42000
5869 * The bit 22 of 0x42004
5870 * The bit 7,8,9 of 0x42020.
5871 */
5872 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005873 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005874 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5875 I915_READ(ILK_DISPLAY_CHICKEN1) |
5876 ILK_FBCQ_DIS);
5877 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5878 I915_READ(ILK_DISPLAY_CHICKEN2) |
5879 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005880 }
5881
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005882 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5883
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005884 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5885 I915_READ(ILK_DISPLAY_CHICKEN2) |
5886 ILK_ELPIN_409_SELECT);
5887 I915_WRITE(_3D_CHICKEN2,
5888 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5889 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005890
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005891 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005892 I915_WRITE(CACHE_MODE_0,
5893 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005894
Akash Goel4e046322014-04-04 17:14:38 +05305895 /* WaDisable_RenderCache_OperationalFlush:ilk */
5896 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5897
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005898 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005899
Daniel Vetter3107bd42012-10-31 22:52:31 +01005900 ibx_init_clock_gating(dev);
5901}
5902
5903static void cpt_init_clock_gating(struct drm_device *dev)
5904{
5905 struct drm_i915_private *dev_priv = dev->dev_private;
5906 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005907 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005908
5909 /*
5910 * On Ibex Peak and Cougar Point, we need to disable clock
5911 * gating for the panel power sequencer or it will fail to
5912 * start up when no ports are active.
5913 */
Jesse Barnescd664072013-10-02 10:34:19 -07005914 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5915 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5916 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005917 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5918 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005919 /* The below fixes the weird display corruption, a few pixels shifted
5920 * downward, on (only) LVDS of some HP laptops with IVY.
5921 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005922 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005923 val = I915_READ(TRANS_CHICKEN2(pipe));
5924 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5925 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005926 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005927 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005928 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5929 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5930 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005931 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5932 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005933 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005934 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005935 I915_WRITE(TRANS_CHICKEN1(pipe),
5936 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5937 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005938}
5939
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005940static void gen6_check_mch_setup(struct drm_device *dev)
5941{
5942 struct drm_i915_private *dev_priv = dev->dev_private;
5943 uint32_t tmp;
5944
5945 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005946 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5947 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5948 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005949}
5950
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005951static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005952{
5953 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005954 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005955
Damien Lespiau231e54f2012-10-19 17:55:41 +01005956 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005957
5958 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5959 I915_READ(ILK_DISPLAY_CHICKEN2) |
5960 ILK_ELPIN_409_SELECT);
5961
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005962 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005963 I915_WRITE(_3D_CHICKEN,
5964 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5965
Akash Goel4e046322014-04-04 17:14:38 +05305966 /* WaDisable_RenderCache_OperationalFlush:snb */
5967 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5968
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005969 /*
5970 * BSpec recoomends 8x4 when MSAA is used,
5971 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005972 *
5973 * Note that PS/WM thread counts depend on the WIZ hashing
5974 * disable bit, which we don't touch here, but it's good
5975 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005976 */
5977 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005978 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005979
Ville Syrjälä017636c2013-12-05 15:51:37 +02005980 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005981
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005982 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005983 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005984
5985 I915_WRITE(GEN6_UCGCTL1,
5986 I915_READ(GEN6_UCGCTL1) |
5987 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5988 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5989
5990 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5991 * gating disable must be set. Failure to set it results in
5992 * flickering pixels due to Z write ordering failures after
5993 * some amount of runtime in the Mesa "fire" demo, and Unigine
5994 * Sanctuary and Tropics, and apparently anything else with
5995 * alpha test or pixel discard.
5996 *
5997 * According to the spec, bit 11 (RCCUNIT) must also be set,
5998 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005999 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006000 * WaDisableRCCUnitClockGating:snb
6001 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006002 */
6003 I915_WRITE(GEN6_UCGCTL2,
6004 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6005 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6006
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006007 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006008 I915_WRITE(_3D_CHICKEN3,
6009 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006010
6011 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006012 * Bspec says:
6013 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6014 * 3DSTATE_SF number of SF output attributes is more than 16."
6015 */
6016 I915_WRITE(_3D_CHICKEN3,
6017 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6018
6019 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006020 * According to the spec the following bits should be
6021 * set in order to enable memory self-refresh and fbc:
6022 * The bit21 and bit22 of 0x42000
6023 * The bit21 and bit22 of 0x42004
6024 * The bit5 and bit7 of 0x42020
6025 * The bit14 of 0x70180
6026 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006027 *
6028 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006029 */
6030 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6031 I915_READ(ILK_DISPLAY_CHICKEN1) |
6032 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6033 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6034 I915_READ(ILK_DISPLAY_CHICKEN2) |
6035 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006036 I915_WRITE(ILK_DSPCLK_GATE_D,
6037 I915_READ(ILK_DSPCLK_GATE_D) |
6038 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6039 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006040
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006041 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006042
Daniel Vetter3107bd42012-10-31 22:52:31 +01006043 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006044
6045 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006046}
6047
6048static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6049{
6050 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6051
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006052 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006053 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006054 *
6055 * This actually overrides the dispatch
6056 * mode for all thread types.
6057 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006058 reg &= ~GEN7_FF_SCHED_MASK;
6059 reg |= GEN7_FF_TS_SCHED_HW;
6060 reg |= GEN7_FF_VS_SCHED_HW;
6061 reg |= GEN7_FF_DS_SCHED_HW;
6062
6063 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6064}
6065
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006066static void lpt_init_clock_gating(struct drm_device *dev)
6067{
6068 struct drm_i915_private *dev_priv = dev->dev_private;
6069
6070 /*
6071 * TODO: this bit should only be enabled when really needed, then
6072 * disabled when not needed anymore in order to save power.
6073 */
6074 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6075 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6076 I915_READ(SOUTH_DSPCLK_GATE_D) |
6077 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006078
6079 /* WADPOClockGatingDisable:hsw */
6080 I915_WRITE(_TRANSA_CHICKEN1,
6081 I915_READ(_TRANSA_CHICKEN1) |
6082 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006083}
6084
Imre Deak7d708ee2013-04-17 14:04:50 +03006085static void lpt_suspend_hw(struct drm_device *dev)
6086{
6087 struct drm_i915_private *dev_priv = dev->dev_private;
6088
6089 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6090 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6091
6092 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6093 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6094 }
6095}
6096
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006097static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006098{
6099 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006100 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006101
6102 I915_WRITE(WM3_LP_ILK, 0);
6103 I915_WRITE(WM2_LP_ILK, 0);
6104 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006105
Ben Widawskyab57fff2013-12-12 15:28:04 -08006106 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006107 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006108
Ben Widawskyab57fff2013-12-12 15:28:04 -08006109 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006110 I915_WRITE(CHICKEN_PAR1_1,
6111 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6112
Ben Widawskyab57fff2013-12-12 15:28:04 -08006113 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006114 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006115 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006116 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006117 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006118 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006119
Ben Widawskyab57fff2013-12-12 15:28:04 -08006120 /* WaVSRefCountFullforceMissDisable:bdw */
6121 /* WaDSRefCountFullforceMissDisable:bdw */
6122 I915_WRITE(GEN7_FF_THREAD_MODE,
6123 I915_READ(GEN7_FF_THREAD_MODE) &
6124 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006125
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006126 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6127 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006128
6129 /* WaDisableSDEUnitClockGating:bdw */
6130 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6131 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006132
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006133 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006134}
6135
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006136static void haswell_init_clock_gating(struct drm_device *dev)
6137{
6138 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006139
Ville Syrjälä017636c2013-12-05 15:51:37 +02006140 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006141
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006142 /* L3 caching of data atomics doesn't work -- disable it. */
6143 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6144 I915_WRITE(HSW_ROW_CHICKEN3,
6145 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6146
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006147 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006148 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6149 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6150 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6151
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006152 /* WaVSRefCountFullforceMissDisable:hsw */
6153 I915_WRITE(GEN7_FF_THREAD_MODE,
6154 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006155
Akash Goel4e046322014-04-04 17:14:38 +05306156 /* WaDisable_RenderCache_OperationalFlush:hsw */
6157 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6158
Chia-I Wufe27c602014-01-28 13:29:33 +08006159 /* enable HiZ Raw Stall Optimization */
6160 I915_WRITE(CACHE_MODE_0_GEN7,
6161 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6162
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006163 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006164 I915_WRITE(CACHE_MODE_1,
6165 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006166
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006167 /*
6168 * BSpec recommends 8x4 when MSAA is used,
6169 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006170 *
6171 * Note that PS/WM thread counts depend on the WIZ hashing
6172 * disable bit, which we don't touch here, but it's good
6173 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006174 */
6175 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006176 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006177
Kenneth Graunke94411592014-12-31 16:23:00 -08006178 /* WaSampleCChickenBitEnable:hsw */
6179 I915_WRITE(HALF_SLICE_CHICKEN3,
6180 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6181
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006182 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006183 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6184
Paulo Zanoni90a88642013-05-03 17:23:45 -03006185 /* WaRsPkgCStateDisplayPMReq:hsw */
6186 I915_WRITE(CHICKEN_PAR1_1,
6187 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006188
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006189 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006190}
6191
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006192static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006193{
6194 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006195 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006196
Ville Syrjälä017636c2013-12-05 15:51:37 +02006197 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006198
Damien Lespiau231e54f2012-10-19 17:55:41 +01006199 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006200
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006201 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006202 I915_WRITE(_3D_CHICKEN3,
6203 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6204
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006205 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006206 I915_WRITE(IVB_CHICKEN3,
6207 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6208 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6209
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006210 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006211 if (IS_IVB_GT1(dev))
6212 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6213 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006214
Akash Goel4e046322014-04-04 17:14:38 +05306215 /* WaDisable_RenderCache_OperationalFlush:ivb */
6216 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6217
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006218 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006219 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6220 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6221
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006222 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006223 I915_WRITE(GEN7_L3CNTLREG1,
6224 GEN7_WA_FOR_GEN7_L3_CONTROL);
6225 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006226 GEN7_WA_L3_CHICKEN_MODE);
6227 if (IS_IVB_GT1(dev))
6228 I915_WRITE(GEN7_ROW_CHICKEN2,
6229 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006230 else {
6231 /* must write both registers */
6232 I915_WRITE(GEN7_ROW_CHICKEN2,
6233 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006234 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6235 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006236 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006237
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006238 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006239 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6240 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6241
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006242 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006243 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006244 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006245 */
6246 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006247 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006248
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006249 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006250 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6251 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6252 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6253
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006254 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006255
6256 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006257
Chris Wilson22721342014-03-04 09:41:43 +00006258 if (0) { /* causes HiZ corruption on ivb:gt1 */
6259 /* enable HiZ Raw Stall Optimization */
6260 I915_WRITE(CACHE_MODE_0_GEN7,
6261 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6262 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006263
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006264 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006265 I915_WRITE(CACHE_MODE_1,
6266 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006267
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006268 /*
6269 * BSpec recommends 8x4 when MSAA is used,
6270 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006271 *
6272 * Note that PS/WM thread counts depend on the WIZ hashing
6273 * disable bit, which we don't touch here, but it's good
6274 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006275 */
6276 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006277 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006278
Ben Widawsky20848222012-05-04 18:58:59 -07006279 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6280 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6281 snpcr |= GEN6_MBC_SNPCR_MED;
6282 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006283
Ben Widawskyab5c6082013-04-05 13:12:41 -07006284 if (!HAS_PCH_NOP(dev))
6285 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006286
6287 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006288}
6289
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006290static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6291{
6292 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6293
6294 /*
6295 * Disable trickle feed and enable pnd deadline calculation
6296 */
6297 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6298 I915_WRITE(CBR1_VLV, 0);
6299}
6300
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006301static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006302{
6303 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006304
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006305 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006306
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006307 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006308 I915_WRITE(_3D_CHICKEN3,
6309 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6310
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006311 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006312 I915_WRITE(IVB_CHICKEN3,
6313 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6314 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6315
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006316 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006317 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006318 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006319 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6320 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006321
Akash Goel4e046322014-04-04 17:14:38 +05306322 /* WaDisable_RenderCache_OperationalFlush:vlv */
6323 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6324
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006325 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006326 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6327 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6328
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006329 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006330 I915_WRITE(GEN7_ROW_CHICKEN2,
6331 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6332
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006333 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006334 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6335 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6336 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6337
Ville Syrjälä46680e02014-01-22 21:33:01 +02006338 gen7_setup_fixed_func_scheduler(dev_priv);
6339
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006340 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006341 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006342 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006343 */
6344 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006345 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006346
Akash Goelc98f5062014-03-24 23:00:07 +05306347 /* WaDisableL3Bank2xClockGate:vlv
6348 * Disabling L3 clock gating- MMIO 940c[25] = 1
6349 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6350 I915_WRITE(GEN7_UCGCTL4,
6351 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006352
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006353 /*
6354 * BSpec says this must be set, even though
6355 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6356 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006357 I915_WRITE(CACHE_MODE_1,
6358 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006359
6360 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006361 * BSpec recommends 8x4 when MSAA is used,
6362 * however in practice 16x4 seems fastest.
6363 *
6364 * Note that PS/WM thread counts depend on the WIZ hashing
6365 * disable bit, which we don't touch here, but it's good
6366 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6367 */
6368 I915_WRITE(GEN7_GT_MODE,
6369 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6370
6371 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006372 * WaIncreaseL3CreditsForVLVB0:vlv
6373 * This is the hardware default actually.
6374 */
6375 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6376
6377 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006378 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006379 * Disable clock gating on th GCFG unit to prevent a delay
6380 * in the reporting of vblank events.
6381 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006382 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006383}
6384
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006385static void cherryview_init_clock_gating(struct drm_device *dev)
6386{
6387 struct drm_i915_private *dev_priv = dev->dev_private;
6388
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006389 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006390
Ville Syrjälä232ce332014-04-09 13:28:35 +03006391 /* WaVSRefCountFullforceMissDisable:chv */
6392 /* WaDSRefCountFullforceMissDisable:chv */
6393 I915_WRITE(GEN7_FF_THREAD_MODE,
6394 I915_READ(GEN7_FF_THREAD_MODE) &
6395 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006396
6397 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6398 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6399 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006400
6401 /* WaDisableCSUnitClockGating:chv */
6402 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6403 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006404
6405 /* WaDisableSDEUnitClockGating:chv */
6406 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6407 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006408}
6409
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006410static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006411{
6412 struct drm_i915_private *dev_priv = dev->dev_private;
6413 uint32_t dspclk_gate;
6414
6415 I915_WRITE(RENCLK_GATE_D1, 0);
6416 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6417 GS_UNIT_CLOCK_GATE_DISABLE |
6418 CL_UNIT_CLOCK_GATE_DISABLE);
6419 I915_WRITE(RAMCLK_GATE_D, 0);
6420 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6421 OVRUNIT_CLOCK_GATE_DISABLE |
6422 OVCUNIT_CLOCK_GATE_DISABLE;
6423 if (IS_GM45(dev))
6424 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6425 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006426
6427 /* WaDisableRenderCachePipelinedFlush */
6428 I915_WRITE(CACHE_MODE_0,
6429 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006430
Akash Goel4e046322014-04-04 17:14:38 +05306431 /* WaDisable_RenderCache_OperationalFlush:g4x */
6432 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6433
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006434 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006435}
6436
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006437static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006438{
6439 struct drm_i915_private *dev_priv = dev->dev_private;
6440
6441 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6442 I915_WRITE(RENCLK_GATE_D2, 0);
6443 I915_WRITE(DSPCLK_GATE_D, 0);
6444 I915_WRITE(RAMCLK_GATE_D, 0);
6445 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006446 I915_WRITE(MI_ARB_STATE,
6447 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306448
6449 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6450 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006451}
6452
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006453static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006454{
6455 struct drm_i915_private *dev_priv = dev->dev_private;
6456
6457 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6458 I965_RCC_CLOCK_GATE_DISABLE |
6459 I965_RCPB_CLOCK_GATE_DISABLE |
6460 I965_ISC_CLOCK_GATE_DISABLE |
6461 I965_FBC_CLOCK_GATE_DISABLE);
6462 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006463 I915_WRITE(MI_ARB_STATE,
6464 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306465
6466 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6467 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006468}
6469
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006470static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006471{
6472 struct drm_i915_private *dev_priv = dev->dev_private;
6473 u32 dstate = I915_READ(D_STATE);
6474
6475 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6476 DSTATE_DOT_CLOCK_GATING;
6477 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006478
6479 if (IS_PINEVIEW(dev))
6480 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006481
6482 /* IIR "flip pending" means done if this bit is set */
6483 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006484
6485 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006486 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006487
6488 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6489 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006490
6491 I915_WRITE(MI_ARB_STATE,
6492 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006493}
6494
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006495static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006496{
6497 struct drm_i915_private *dev_priv = dev->dev_private;
6498
6499 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006500
6501 /* interrupts should cause a wake up from C3 */
6502 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6503 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006504
6505 I915_WRITE(MEM_MODE,
6506 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006507}
6508
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006509static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006510{
6511 struct drm_i915_private *dev_priv = dev->dev_private;
6512
6513 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006514
6515 I915_WRITE(MEM_MODE,
6516 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6517 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006518}
6519
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006520void intel_init_clock_gating(struct drm_device *dev)
6521{
6522 struct drm_i915_private *dev_priv = dev->dev_private;
6523
Damien Lespiauc57e3552015-02-09 19:33:05 +00006524 if (dev_priv->display.init_clock_gating)
6525 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006526}
6527
Imre Deak7d708ee2013-04-17 14:04:50 +03006528void intel_suspend_hw(struct drm_device *dev)
6529{
6530 if (HAS_PCH_LPT(dev))
6531 lpt_suspend_hw(dev);
6532}
6533
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006534/* Set up chip specific power management-related functions */
6535void intel_init_pm(struct drm_device *dev)
6536{
6537 struct drm_i915_private *dev_priv = dev->dev_private;
6538
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006539 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006540
Daniel Vetterc921aba2012-04-26 23:28:17 +02006541 /* For cxsr */
6542 if (IS_PINEVIEW(dev))
6543 i915_pineview_get_mem_freq(dev);
6544 else if (IS_GEN5(dev))
6545 i915_ironlake_get_mem_freq(dev);
6546
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006547 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006548 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006549 skl_setup_wm_latency(dev);
6550
Damien Lespiau45db2192015-02-09 19:33:09 +00006551 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006552 dev_priv->display.update_wm = skl_update_wm;
6553 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306554 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006555 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006556
Ville Syrjäläbd602542014-01-07 16:14:10 +02006557 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6558 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6559 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6560 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6561 dev_priv->display.update_wm = ilk_update_wm;
6562 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6563 } else {
6564 DRM_DEBUG_KMS("Failed to read display plane latency. "
6565 "Disable CxSR\n");
6566 }
6567
6568 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006569 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006570 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006571 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006572 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006573 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006574 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006575 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006576 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006577 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006578 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläae801522015-03-05 21:19:49 +02006579 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306580 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006581 dev_priv->display.init_clock_gating =
6582 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006583 } else if (IS_VALLEYVIEW(dev)) {
6584 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306585 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006586 dev_priv->display.init_clock_gating =
6587 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006588 } else if (IS_PINEVIEW(dev)) {
6589 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6590 dev_priv->is_ddr3,
6591 dev_priv->fsb_freq,
6592 dev_priv->mem_freq)) {
6593 DRM_INFO("failed to find known CxSR latency "
6594 "(found ddr%s fsb freq %d, mem freq %d), "
6595 "disabling CxSR\n",
6596 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6597 dev_priv->fsb_freq, dev_priv->mem_freq);
6598 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006599 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006600 dev_priv->display.update_wm = NULL;
6601 } else
6602 dev_priv->display.update_wm = pineview_update_wm;
6603 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6604 } else if (IS_G4X(dev)) {
6605 dev_priv->display.update_wm = g4x_update_wm;
6606 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6607 } else if (IS_GEN4(dev)) {
6608 dev_priv->display.update_wm = i965_update_wm;
6609 if (IS_CRESTLINE(dev))
6610 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6611 else if (IS_BROADWATER(dev))
6612 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6613 } else if (IS_GEN3(dev)) {
6614 dev_priv->display.update_wm = i9xx_update_wm;
6615 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6616 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006617 } else if (IS_GEN2(dev)) {
6618 if (INTEL_INFO(dev)->num_pipes == 1) {
6619 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006620 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006621 } else {
6622 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006623 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006624 }
6625
6626 if (IS_I85X(dev) || IS_I865G(dev))
6627 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6628 else
6629 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6630 } else {
6631 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006632 }
6633}
6634
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006635int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006636{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006637 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006638
6639 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6640 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6641 return -EAGAIN;
6642 }
6643
6644 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006645 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006646 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6647
6648 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6649 500)) {
6650 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6651 return -ETIMEDOUT;
6652 }
6653
6654 *val = I915_READ(GEN6_PCODE_DATA);
6655 I915_WRITE(GEN6_PCODE_DATA, 0);
6656
6657 return 0;
6658}
6659
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006660int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006661{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006662 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006663
6664 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6665 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6666 return -EAGAIN;
6667 }
6668
6669 I915_WRITE(GEN6_PCODE_DATA, val);
6670 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6671
6672 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6673 500)) {
6674 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6675 return -ETIMEDOUT;
6676 }
6677
6678 I915_WRITE(GEN6_PCODE_DATA, 0);
6679
6680 return 0;
6681}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006682
Ville Syrjälädd06f882014-11-10 22:55:12 +02006683static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006684{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006685 switch (czclk_freq) {
6686 case 200:
6687 return 10;
6688 case 267:
6689 return 12;
6690 case 320:
6691 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006692 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006693 case 400:
6694 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006695 default:
6696 return -1;
6697 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006698}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006699
Ville Syrjälädd06f882014-11-10 22:55:12 +02006700static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6701{
6702 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6703
6704 div = vlv_gpu_freq_div(czclk_freq);
6705 if (div < 0)
6706 return div;
6707
6708 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006709}
6710
Fengguang Wub55dd642014-07-12 11:21:39 +02006711static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006712{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006713 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006714
Ville Syrjälädd06f882014-11-10 22:55:12 +02006715 mul = vlv_gpu_freq_div(czclk_freq);
6716 if (mul < 0)
6717 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006718
Ville Syrjälädd06f882014-11-10 22:55:12 +02006719 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006720}
6721
Fengguang Wub55dd642014-07-12 11:21:39 +02006722static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306723{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006724 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306725
Ville Syrjälädd06f882014-11-10 22:55:12 +02006726 div = vlv_gpu_freq_div(czclk_freq) / 2;
6727 if (div < 0)
6728 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306729
Ville Syrjälädd06f882014-11-10 22:55:12 +02006730 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306731}
6732
Fengguang Wub55dd642014-07-12 11:21:39 +02006733static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306734{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006735 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306736
Ville Syrjälädd06f882014-11-10 22:55:12 +02006737 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6738 if (mul < 0)
6739 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306740
Ville Syrjälä1c147622014-08-18 14:42:43 +03006741 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006742 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306743}
6744
Ville Syrjälä616bc822015-01-23 21:04:25 +02006745int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6746{
Akash Goel80b6dda2015-03-06 11:07:15 +05306747 if (IS_GEN9(dev_priv->dev))
6748 return (val * GT_FREQUENCY_MULTIPLIER) / GEN9_FREQ_SCALER;
6749 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006750 return chv_gpu_freq(dev_priv, val);
6751 else if (IS_VALLEYVIEW(dev_priv->dev))
6752 return byt_gpu_freq(dev_priv, val);
6753 else
6754 return val * GT_FREQUENCY_MULTIPLIER;
6755}
6756
Ville Syrjälä616bc822015-01-23 21:04:25 +02006757int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6758{
Akash Goel80b6dda2015-03-06 11:07:15 +05306759 if (IS_GEN9(dev_priv->dev))
6760 return (val * GEN9_FREQ_SCALER) / GT_FREQUENCY_MULTIPLIER;
6761 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006762 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306763 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006764 return byt_freq_opcode(dev_priv, val);
6765 else
6766 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306767}
6768
Daniel Vetterf742a552013-12-06 10:17:53 +01006769void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006770{
6771 struct drm_i915_private *dev_priv = dev->dev_private;
6772
Daniel Vetterf742a552013-12-06 10:17:53 +01006773 mutex_init(&dev_priv->rps.hw_lock);
6774
Chris Wilson907b28c2013-07-19 20:36:52 +01006775 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6776 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006777
Paulo Zanoni33688d92014-03-07 20:08:19 -03006778 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006779}