blob: ac7777217440cd5c721b4cb11fab92934f8ab59a [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
Imre Deak5209b1f2014-07-01 12:36:17 +0300304void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300305{
Imre Deak5209b1f2014-07-01 12:36:17 +0300306 struct drm_device *dev = dev_priv->dev;
307 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300308
Imre Deak5209b1f2014-07-01 12:36:17 +0300309 if (IS_VALLEYVIEW(dev)) {
310 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200311 if (IS_CHERRYVIEW(dev))
312 chv_set_memory_pm5(dev_priv, enable);
Imre Deak5209b1f2014-07-01 12:36:17 +0300313 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
314 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
315 } else if (IS_PINEVIEW(dev)) {
316 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
317 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
318 I915_WRITE(DSPFW3, val);
319 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
320 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
321 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
322 I915_WRITE(FW_BLC_SELF, val);
323 } else if (IS_I915GM(dev)) {
324 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
325 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
326 I915_WRITE(INSTPM, val);
327 } else {
328 return;
329 }
330
331 DRM_DEBUG_KMS("memory self-refresh is %s\n",
332 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300333}
334
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200335
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300336/*
337 * Latency for FIFO fetches is dependent on several factors:
338 * - memory configuration (speed, channels)
339 * - chipset
340 * - current MCH state
341 * It can be fairly high in some situations, so here we assume a fairly
342 * pessimal value. It's a tradeoff between extra memory fetches (if we
343 * set this value too high, the FIFO will fetch frequently to stay full)
344 * and power consumption (set it too low to save power and we might see
345 * FIFO underruns and display "flicker").
346 *
347 * A value of 5us seems to be a good balance; safe for very low end
348 * platforms but not overly aggressive on lower latency configs.
349 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100350static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300351
Ville Syrjäläb5004722015-03-05 21:19:47 +0200352#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
353 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
354
355static int vlv_get_fifo_size(struct drm_device *dev,
356 enum pipe pipe, int plane)
357{
358 struct drm_i915_private *dev_priv = dev->dev_private;
359 int sprite0_start, sprite1_start, size;
360
361 switch (pipe) {
362 uint32_t dsparb, dsparb2, dsparb3;
363 case PIPE_A:
364 dsparb = I915_READ(DSPARB);
365 dsparb2 = I915_READ(DSPARB2);
366 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
367 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
368 break;
369 case PIPE_B:
370 dsparb = I915_READ(DSPARB);
371 dsparb2 = I915_READ(DSPARB2);
372 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
373 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
374 break;
375 case PIPE_C:
376 dsparb2 = I915_READ(DSPARB2);
377 dsparb3 = I915_READ(DSPARB3);
378 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
379 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
380 break;
381 default:
382 return 0;
383 }
384
385 switch (plane) {
386 case 0:
387 size = sprite0_start;
388 break;
389 case 1:
390 size = sprite1_start - sprite0_start;
391 break;
392 case 2:
393 size = 512 - 1 - sprite1_start;
394 break;
395 default:
396 return 0;
397 }
398
399 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
400 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
401 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
402 size);
403
404 return size;
405}
406
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300407static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300408{
409 struct drm_i915_private *dev_priv = dev->dev_private;
410 uint32_t dsparb = I915_READ(DSPARB);
411 int size;
412
413 size = dsparb & 0x7f;
414 if (plane)
415 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
416
417 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
418 plane ? "B" : "A", size);
419
420 return size;
421}
422
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200423static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300424{
425 struct drm_i915_private *dev_priv = dev->dev_private;
426 uint32_t dsparb = I915_READ(DSPARB);
427 int size;
428
429 size = dsparb & 0x1ff;
430 if (plane)
431 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
432 size >>= 1; /* Convert to cachelines */
433
434 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
435 plane ? "B" : "A", size);
436
437 return size;
438}
439
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300440static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300441{
442 struct drm_i915_private *dev_priv = dev->dev_private;
443 uint32_t dsparb = I915_READ(DSPARB);
444 int size;
445
446 size = dsparb & 0x7f;
447 size >>= 2; /* Convert to cachelines */
448
449 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
450 plane ? "B" : "A",
451 size);
452
453 return size;
454}
455
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300456/* Pineview has different values for various configs */
457static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300458 .fifo_size = PINEVIEW_DISPLAY_FIFO,
459 .max_wm = PINEVIEW_MAX_WM,
460 .default_wm = PINEVIEW_DFT_WM,
461 .guard_size = PINEVIEW_GUARD_WM,
462 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300463};
464static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300465 .fifo_size = PINEVIEW_DISPLAY_FIFO,
466 .max_wm = PINEVIEW_MAX_WM,
467 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
468 .guard_size = PINEVIEW_GUARD_WM,
469 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300470};
471static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300472 .fifo_size = PINEVIEW_CURSOR_FIFO,
473 .max_wm = PINEVIEW_CURSOR_MAX_WM,
474 .default_wm = PINEVIEW_CURSOR_DFT_WM,
475 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
476 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300477};
478static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300479 .fifo_size = PINEVIEW_CURSOR_FIFO,
480 .max_wm = PINEVIEW_CURSOR_MAX_WM,
481 .default_wm = PINEVIEW_CURSOR_DFT_WM,
482 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
483 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300484};
485static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300486 .fifo_size = G4X_FIFO_SIZE,
487 .max_wm = G4X_MAX_WM,
488 .default_wm = G4X_MAX_WM,
489 .guard_size = 2,
490 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300491};
492static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300493 .fifo_size = I965_CURSOR_FIFO,
494 .max_wm = I965_CURSOR_MAX_WM,
495 .default_wm = I965_CURSOR_DFT_WM,
496 .guard_size = 2,
497 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300498};
499static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300500 .fifo_size = VALLEYVIEW_FIFO_SIZE,
501 .max_wm = VALLEYVIEW_MAX_WM,
502 .default_wm = VALLEYVIEW_MAX_WM,
503 .guard_size = 2,
504 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300505};
506static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300507 .fifo_size = I965_CURSOR_FIFO,
508 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
509 .default_wm = I965_CURSOR_DFT_WM,
510 .guard_size = 2,
511 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300512};
513static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300514 .fifo_size = I965_CURSOR_FIFO,
515 .max_wm = I965_CURSOR_MAX_WM,
516 .default_wm = I965_CURSOR_DFT_WM,
517 .guard_size = 2,
518 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300519};
520static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300521 .fifo_size = I945_FIFO_SIZE,
522 .max_wm = I915_MAX_WM,
523 .default_wm = 1,
524 .guard_size = 2,
525 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526};
527static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300528 .fifo_size = I915_FIFO_SIZE,
529 .max_wm = I915_MAX_WM,
530 .default_wm = 1,
531 .guard_size = 2,
532 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300534static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300535 .fifo_size = I855GM_FIFO_SIZE,
536 .max_wm = I915_MAX_WM,
537 .default_wm = 1,
538 .guard_size = 2,
539 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300541static const struct intel_watermark_params i830_bc_wm_info = {
542 .fifo_size = I855GM_FIFO_SIZE,
543 .max_wm = I915_MAX_WM/2,
544 .default_wm = 1,
545 .guard_size = 2,
546 .cacheline_size = I830_FIFO_LINE_SIZE,
547};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200548static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300549 .fifo_size = I830_FIFO_SIZE,
550 .max_wm = I915_MAX_WM,
551 .default_wm = 1,
552 .guard_size = 2,
553 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300554};
555
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300556/**
557 * intel_calculate_wm - calculate watermark level
558 * @clock_in_khz: pixel clock
559 * @wm: chip FIFO params
560 * @pixel_size: display pixel size
561 * @latency_ns: memory latency for the platform
562 *
563 * Calculate the watermark level (the level at which the display plane will
564 * start fetching from memory again). Each chip has a different display
565 * FIFO size and allocation, so the caller needs to figure that out and pass
566 * in the correct intel_watermark_params structure.
567 *
568 * As the pixel clock runs, the FIFO will be drained at a rate that depends
569 * on the pixel size. When it reaches the watermark level, it'll start
570 * fetching FIFO line sized based chunks from memory until the FIFO fills
571 * past the watermark point. If the FIFO drains completely, a FIFO underrun
572 * will occur, and a display engine hang could result.
573 */
574static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
575 const struct intel_watermark_params *wm,
576 int fifo_size,
577 int pixel_size,
578 unsigned long latency_ns)
579{
580 long entries_required, wm_size;
581
582 /*
583 * Note: we need to make sure we don't overflow for various clock &
584 * latency values.
585 * clocks go from a few thousand to several hundred thousand.
586 * latency is usually a few thousand
587 */
588 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
589 1000;
590 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
591
592 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
593
594 wm_size = fifo_size - (entries_required + wm->guard_size);
595
596 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
597
598 /* Don't promote wm_size to unsigned... */
599 if (wm_size > (long)wm->max_wm)
600 wm_size = wm->max_wm;
601 if (wm_size <= 0)
602 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300603
604 /*
605 * Bspec seems to indicate that the value shouldn't be lower than
606 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
607 * Lets go for 8 which is the burst size since certain platforms
608 * already use a hardcoded 8 (which is what the spec says should be
609 * done).
610 */
611 if (wm_size <= 8)
612 wm_size = 8;
613
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614 return wm_size;
615}
616
617static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
618{
619 struct drm_crtc *crtc, *enabled = NULL;
620
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100621 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000622 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623 if (enabled)
624 return NULL;
625 enabled = crtc;
626 }
627 }
628
629 return enabled;
630}
631
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300632static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300633{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300634 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300635 struct drm_i915_private *dev_priv = dev->dev_private;
636 struct drm_crtc *crtc;
637 const struct cxsr_latency *latency;
638 u32 reg;
639 unsigned long wm;
640
641 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
642 dev_priv->fsb_freq, dev_priv->mem_freq);
643 if (!latency) {
644 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300645 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300646 return;
647 }
648
649 crtc = single_enabled_crtc(dev);
650 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100651 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800652 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100653 int clock;
654
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200655 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100656 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300657
658 /* Display SR */
659 wm = intel_calculate_wm(clock, &pineview_display_wm,
660 pineview_display_wm.fifo_size,
661 pixel_size, latency->display_sr);
662 reg = I915_READ(DSPFW1);
663 reg &= ~DSPFW_SR_MASK;
664 reg |= wm << DSPFW_SR_SHIFT;
665 I915_WRITE(DSPFW1, reg);
666 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
667
668 /* cursor SR */
669 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
670 pineview_display_wm.fifo_size,
671 pixel_size, latency->cursor_sr);
672 reg = I915_READ(DSPFW3);
673 reg &= ~DSPFW_CURSOR_SR_MASK;
674 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
675 I915_WRITE(DSPFW3, reg);
676
677 /* Display HPLL off SR */
678 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
679 pineview_display_hplloff_wm.fifo_size,
680 pixel_size, latency->display_hpll_disable);
681 reg = I915_READ(DSPFW3);
682 reg &= ~DSPFW_HPLL_SR_MASK;
683 reg |= wm & DSPFW_HPLL_SR_MASK;
684 I915_WRITE(DSPFW3, reg);
685
686 /* cursor HPLL off SR */
687 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
688 pineview_display_hplloff_wm.fifo_size,
689 pixel_size, latency->cursor_hpll_disable);
690 reg = I915_READ(DSPFW3);
691 reg &= ~DSPFW_HPLL_CURSOR_MASK;
692 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
693 I915_WRITE(DSPFW3, reg);
694 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
695
Imre Deak5209b1f2014-07-01 12:36:17 +0300696 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300697 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300698 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300699 }
700}
701
702static bool g4x_compute_wm0(struct drm_device *dev,
703 int plane,
704 const struct intel_watermark_params *display,
705 int display_latency_ns,
706 const struct intel_watermark_params *cursor,
707 int cursor_latency_ns,
708 int *plane_wm,
709 int *cursor_wm)
710{
711 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300712 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300713 int htotal, hdisplay, clock, pixel_size;
714 int line_time_us, line_count;
715 int entries, tlb_miss;
716
717 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000718 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300719 *cursor_wm = cursor->guard_size;
720 *plane_wm = display->guard_size;
721 return false;
722 }
723
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200724 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100725 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800726 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200727 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800728 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300729
730 /* Use the small buffer method to calculate plane watermark */
731 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
732 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
733 if (tlb_miss > 0)
734 entries += tlb_miss;
735 entries = DIV_ROUND_UP(entries, display->cacheline_size);
736 *plane_wm = entries + display->guard_size;
737 if (*plane_wm > (int)display->max_wm)
738 *plane_wm = display->max_wm;
739
740 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200741 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300742 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800743 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300744 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
745 if (tlb_miss > 0)
746 entries += tlb_miss;
747 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
748 *cursor_wm = entries + cursor->guard_size;
749 if (*cursor_wm > (int)cursor->max_wm)
750 *cursor_wm = (int)cursor->max_wm;
751
752 return true;
753}
754
755/*
756 * Check the wm result.
757 *
758 * If any calculated watermark values is larger than the maximum value that
759 * can be programmed into the associated watermark register, that watermark
760 * must be disabled.
761 */
762static bool g4x_check_srwm(struct drm_device *dev,
763 int display_wm, int cursor_wm,
764 const struct intel_watermark_params *display,
765 const struct intel_watermark_params *cursor)
766{
767 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
768 display_wm, cursor_wm);
769
770 if (display_wm > display->max_wm) {
771 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
772 display_wm, display->max_wm);
773 return false;
774 }
775
776 if (cursor_wm > cursor->max_wm) {
777 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
778 cursor_wm, cursor->max_wm);
779 return false;
780 }
781
782 if (!(display_wm || cursor_wm)) {
783 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
784 return false;
785 }
786
787 return true;
788}
789
790static bool g4x_compute_srwm(struct drm_device *dev,
791 int plane,
792 int latency_ns,
793 const struct intel_watermark_params *display,
794 const struct intel_watermark_params *cursor,
795 int *display_wm, int *cursor_wm)
796{
797 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300798 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300799 int hdisplay, htotal, pixel_size, clock;
800 unsigned long line_time_us;
801 int line_count, line_size;
802 int small, large;
803 int entries;
804
805 if (!latency_ns) {
806 *display_wm = *cursor_wm = 0;
807 return false;
808 }
809
810 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200811 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100812 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800813 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200814 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800815 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300816
Ville Syrjälä922044c2014-02-14 14:18:57 +0200817 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300818 line_count = (latency_ns / line_time_us + 1000) / 1000;
819 line_size = hdisplay * pixel_size;
820
821 /* Use the minimum of the small and large buffer method for primary */
822 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
823 large = line_count * line_size;
824
825 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
826 *display_wm = entries + display->guard_size;
827
828 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800829 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300830 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
831 *cursor_wm = entries + cursor->guard_size;
832
833 return g4x_check_srwm(dev,
834 *display_wm, *cursor_wm,
835 display, cursor);
836}
837
Ville Syrjälä15665972015-03-10 16:16:28 +0200838#define FW_WM(value, plane) \
839 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
840#define FW_WM_VLV(value, plane) \
841 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
842
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200843static void vlv_write_wm_values(struct intel_crtc *crtc,
844 const struct vlv_wm_values *wm)
845{
846 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
847 enum pipe pipe = crtc->pipe;
848
849 I915_WRITE(VLV_DDL(pipe),
850 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
851 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
852 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
853 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
854
Ville Syrjäläae801522015-03-05 21:19:49 +0200855 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200856 FW_WM(wm->sr.plane, SR) |
857 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
858 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
859 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200860 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200861 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
862 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
863 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200864 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200865 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200866
867 if (IS_CHERRYVIEW(dev_priv)) {
868 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200869 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
870 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200871 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200872 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
873 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200874 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200875 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
876 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200877 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200878 FW_WM(wm->sr.plane >> 9, SR_HI) |
879 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
880 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
881 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
882 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
883 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
884 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
885 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
886 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
887 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200888 } else {
889 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200890 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
891 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200892 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200893 FW_WM(wm->sr.plane >> 9, SR_HI) |
894 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
895 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
896 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
897 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
898 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
899 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200900 }
901
902 POSTING_READ(DSPFW1);
903
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200904 dev_priv->wm.vlv = *wm;
905}
906
Ville Syrjälä15665972015-03-10 16:16:28 +0200907#undef FW_WM
908#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,
1166 (plane_sr << DSPFW_SR_SHIFT) |
1167 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1168 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001169 (planea_wm << DSPFW_PLANEA_SHIFT));
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) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001172 (cursora_wm << DSPFW_CURSORA_SHIFT));
1173 /* 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)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001176 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
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... */
1242 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001243 (8 << DSPFW_CURSORB_SHIFT) |
1244 (8 << DSPFW_PLANEB_SHIFT) |
1245 (8 << DSPFW_PLANEA_SHIFT));
1246 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1247 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001248 /* update cursor SR watermark */
1249 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
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ä46ba6142013-09-10 11:40:40 +03001255static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001256{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001257 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001258 struct drm_i915_private *dev_priv = dev->dev_private;
1259 const struct intel_watermark_params *wm_info;
1260 uint32_t fwater_lo;
1261 uint32_t fwater_hi;
1262 int cwm, srwm = 1;
1263 int fifo_size;
1264 int planea_wm, planeb_wm;
1265 struct drm_crtc *crtc, *enabled = NULL;
1266
1267 if (IS_I945GM(dev))
1268 wm_info = &i945_wm_info;
1269 else if (!IS_GEN2(dev))
1270 wm_info = &i915_wm_info;
1271 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001272 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001273
1274 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1275 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001276 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001277 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001278 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001279 if (IS_GEN2(dev))
1280 cpp = 4;
1281
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001282 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001283 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001284 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001285 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001286 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001287 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001288 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001289 if (planea_wm > (long)wm_info->max_wm)
1290 planea_wm = wm_info->max_wm;
1291 }
1292
1293 if (IS_GEN2(dev))
1294 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001295
1296 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1297 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001298 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001299 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001300 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001301 if (IS_GEN2(dev))
1302 cpp = 4;
1303
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001304 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001305 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001306 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001307 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001308 if (enabled == NULL)
1309 enabled = crtc;
1310 else
1311 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001312 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001313 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001314 if (planeb_wm > (long)wm_info->max_wm)
1315 planeb_wm = wm_info->max_wm;
1316 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001317
1318 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1319
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001320 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001321 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001322
Matt Roper59bea882015-02-27 10:12:01 -08001323 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001324
1325 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001326 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001327 enabled = NULL;
1328 }
1329
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001330 /*
1331 * Overlay gets an aggressive default since video jitter is bad.
1332 */
1333 cwm = 2;
1334
1335 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001336 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001337
1338 /* Calc sr entries for one plane configs */
1339 if (HAS_FW_BLC(dev) && enabled) {
1340 /* self-refresh has much higher latency */
1341 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001342 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001343 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001344 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001345 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001346 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001347 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001348 unsigned long line_time_us;
1349 int entries;
1350
Ville Syrjälä922044c2014-02-14 14:18:57 +02001351 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001352
1353 /* Use ns/us then divide to preserve precision */
1354 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1355 pixel_size * hdisplay;
1356 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1357 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1358 srwm = wm_info->fifo_size - entries;
1359 if (srwm < 0)
1360 srwm = 1;
1361
1362 if (IS_I945G(dev) || IS_I945GM(dev))
1363 I915_WRITE(FW_BLC_SELF,
1364 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1365 else if (IS_I915GM(dev))
1366 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1367 }
1368
1369 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1370 planea_wm, planeb_wm, cwm, srwm);
1371
1372 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1373 fwater_hi = (cwm & 0x1f);
1374
1375 /* Set request length to 8 cachelines per fetch */
1376 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1377 fwater_hi = fwater_hi | (1 << 8);
1378
1379 I915_WRITE(FW_BLC, fwater_lo);
1380 I915_WRITE(FW_BLC2, fwater_hi);
1381
Imre Deak5209b1f2014-07-01 12:36:17 +03001382 if (enabled)
1383 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384}
1385
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001386static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001387{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001388 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001389 struct drm_i915_private *dev_priv = dev->dev_private;
1390 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001391 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001392 uint32_t fwater_lo;
1393 int planea_wm;
1394
1395 crtc = single_enabled_crtc(dev);
1396 if (crtc == NULL)
1397 return;
1398
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001399 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001400 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001401 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001402 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001403 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001404 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1405 fwater_lo |= (3<<8) | planea_wm;
1406
1407 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1408
1409 I915_WRITE(FW_BLC, fwater_lo);
1410}
1411
Ville Syrjälä36587292013-07-05 11:57:16 +03001412static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1413 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001414{
1415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001416 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001417
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001418 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001419
1420 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1421 * adjust the pixel_rate here. */
1422
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001423 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001424 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001425 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001426
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001427 pipe_w = intel_crtc->config->pipe_src_w;
1428 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001429 pfit_w = (pfit_size >> 16) & 0xFFFF;
1430 pfit_h = pfit_size & 0xFFFF;
1431 if (pipe_w < pfit_w)
1432 pipe_w = pfit_w;
1433 if (pipe_h < pfit_h)
1434 pipe_h = pfit_h;
1435
1436 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1437 pfit_w * pfit_h);
1438 }
1439
1440 return pixel_rate;
1441}
1442
Ville Syrjälä37126462013-08-01 16:18:55 +03001443/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001444static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001445 uint32_t latency)
1446{
1447 uint64_t ret;
1448
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001449 if (WARN(latency == 0, "Latency value missing\n"))
1450 return UINT_MAX;
1451
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001452 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1453 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1454
1455 return ret;
1456}
1457
Ville Syrjälä37126462013-08-01 16:18:55 +03001458/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001459static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001460 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1461 uint32_t latency)
1462{
1463 uint32_t ret;
1464
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001465 if (WARN(latency == 0, "Latency value missing\n"))
1466 return UINT_MAX;
1467
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001468 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1469 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1470 ret = DIV_ROUND_UP(ret, 64) + 2;
1471 return ret;
1472}
1473
Ville Syrjälä23297042013-07-05 11:57:17 +03001474static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001475 uint8_t bytes_per_pixel)
1476{
1477 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1478}
1479
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001480struct skl_pipe_wm_parameters {
1481 bool active;
1482 uint32_t pipe_htotal;
1483 uint32_t pixel_rate; /* in KHz */
1484 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1485 struct intel_plane_wm_parameters cursor;
1486};
1487
Imre Deak820c1982013-12-17 14:46:36 +02001488struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001489 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001490 uint32_t pipe_htotal;
1491 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001492 struct intel_plane_wm_parameters pri;
1493 struct intel_plane_wm_parameters spr;
1494 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001495};
1496
Imre Deak820c1982013-12-17 14:46:36 +02001497struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001498 uint16_t pri;
1499 uint16_t spr;
1500 uint16_t cur;
1501 uint16_t fbc;
1502};
1503
Ville Syrjälä240264f2013-08-07 13:29:12 +03001504/* used in computing the new watermarks state */
1505struct intel_wm_config {
1506 unsigned int num_pipes_active;
1507 bool sprites_enabled;
1508 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001509};
1510
Ville Syrjälä37126462013-08-01 16:18:55 +03001511/*
1512 * For both WM_PIPE and WM_LP.
1513 * mem_value must be in 0.1us units.
1514 */
Imre Deak820c1982013-12-17 14:46:36 +02001515static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001516 uint32_t mem_value,
1517 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001518{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001519 uint32_t method1, method2;
1520
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001521 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001522 return 0;
1523
Ville Syrjälä23297042013-07-05 11:57:17 +03001524 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001525 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001526 mem_value);
1527
1528 if (!is_lp)
1529 return method1;
1530
Ville Syrjälä23297042013-07-05 11:57:17 +03001531 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001532 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001533 params->pri.horiz_pixels,
1534 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001535 mem_value);
1536
1537 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001538}
1539
Ville Syrjälä37126462013-08-01 16:18:55 +03001540/*
1541 * For both WM_PIPE and WM_LP.
1542 * mem_value must be in 0.1us units.
1543 */
Imre Deak820c1982013-12-17 14:46:36 +02001544static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001545 uint32_t mem_value)
1546{
1547 uint32_t method1, method2;
1548
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001549 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001550 return 0;
1551
Ville Syrjälä23297042013-07-05 11:57:17 +03001552 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001553 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001554 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001555 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001556 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001557 params->spr.horiz_pixels,
1558 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001559 mem_value);
1560 return min(method1, method2);
1561}
1562
Ville Syrjälä37126462013-08-01 16:18:55 +03001563/*
1564 * For both WM_PIPE and WM_LP.
1565 * mem_value must be in 0.1us units.
1566 */
Imre Deak820c1982013-12-17 14:46:36 +02001567static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001568 uint32_t mem_value)
1569{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001570 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001571 return 0;
1572
Ville Syrjälä23297042013-07-05 11:57:17 +03001573 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001574 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001575 params->cur.horiz_pixels,
1576 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001577 mem_value);
1578}
1579
Paulo Zanonicca32e92013-05-31 11:45:06 -03001580/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001581static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001582 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001583{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001584 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001585 return 0;
1586
Ville Syrjälä23297042013-07-05 11:57:17 +03001587 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001588 params->pri.horiz_pixels,
1589 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001590}
1591
Ville Syrjälä158ae642013-08-07 13:28:19 +03001592static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1593{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001594 if (INTEL_INFO(dev)->gen >= 8)
1595 return 3072;
1596 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001597 return 768;
1598 else
1599 return 512;
1600}
1601
Ville Syrjälä4e975082014-03-07 18:32:11 +02001602static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1603 int level, bool is_sprite)
1604{
1605 if (INTEL_INFO(dev)->gen >= 8)
1606 /* BDW primary/sprite plane watermarks */
1607 return level == 0 ? 255 : 2047;
1608 else if (INTEL_INFO(dev)->gen >= 7)
1609 /* IVB/HSW primary/sprite plane watermarks */
1610 return level == 0 ? 127 : 1023;
1611 else if (!is_sprite)
1612 /* ILK/SNB primary plane watermarks */
1613 return level == 0 ? 127 : 511;
1614 else
1615 /* ILK/SNB sprite plane watermarks */
1616 return level == 0 ? 63 : 255;
1617}
1618
1619static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1620 int level)
1621{
1622 if (INTEL_INFO(dev)->gen >= 7)
1623 return level == 0 ? 63 : 255;
1624 else
1625 return level == 0 ? 31 : 63;
1626}
1627
1628static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1629{
1630 if (INTEL_INFO(dev)->gen >= 8)
1631 return 31;
1632 else
1633 return 15;
1634}
1635
Ville Syrjälä158ae642013-08-07 13:28:19 +03001636/* Calculate the maximum primary/sprite plane watermark */
1637static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1638 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001639 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001640 enum intel_ddb_partitioning ddb_partitioning,
1641 bool is_sprite)
1642{
1643 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001644
1645 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001646 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001647 return 0;
1648
1649 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001650 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001651 fifo_size /= INTEL_INFO(dev)->num_pipes;
1652
1653 /*
1654 * For some reason the non self refresh
1655 * FIFO size is only half of the self
1656 * refresh FIFO size on ILK/SNB.
1657 */
1658 if (INTEL_INFO(dev)->gen <= 6)
1659 fifo_size /= 2;
1660 }
1661
Ville Syrjälä240264f2013-08-07 13:29:12 +03001662 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001663 /* level 0 is always calculated with 1:1 split */
1664 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1665 if (is_sprite)
1666 fifo_size *= 5;
1667 fifo_size /= 6;
1668 } else {
1669 fifo_size /= 2;
1670 }
1671 }
1672
1673 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001674 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001675}
1676
1677/* Calculate the maximum cursor plane watermark */
1678static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001679 int level,
1680 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001681{
1682 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001683 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001684 return 64;
1685
1686 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001687 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001688}
1689
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001690static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001691 int level,
1692 const struct intel_wm_config *config,
1693 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001694 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001695{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001696 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1697 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1698 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001699 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001700}
1701
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001702static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1703 int level,
1704 struct ilk_wm_maximums *max)
1705{
1706 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1707 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1708 max->cur = ilk_cursor_wm_reg_max(dev, level);
1709 max->fbc = ilk_fbc_wm_reg_max(dev);
1710}
1711
Ville Syrjäläd9395652013-10-09 19:18:10 +03001712static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001713 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001714 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001715{
1716 bool ret;
1717
1718 /* already determined to be invalid? */
1719 if (!result->enable)
1720 return false;
1721
1722 result->enable = result->pri_val <= max->pri &&
1723 result->spr_val <= max->spr &&
1724 result->cur_val <= max->cur;
1725
1726 ret = result->enable;
1727
1728 /*
1729 * HACK until we can pre-compute everything,
1730 * and thus fail gracefully if LP0 watermarks
1731 * are exceeded...
1732 */
1733 if (level == 0 && !result->enable) {
1734 if (result->pri_val > max->pri)
1735 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1736 level, result->pri_val, max->pri);
1737 if (result->spr_val > max->spr)
1738 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1739 level, result->spr_val, max->spr);
1740 if (result->cur_val > max->cur)
1741 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1742 level, result->cur_val, max->cur);
1743
1744 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1745 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1746 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1747 result->enable = true;
1748 }
1749
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001750 return ret;
1751}
1752
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001753static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001754 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001755 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001756 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001757{
1758 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1759 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1760 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1761
1762 /* WM1+ latency values stored in 0.5us units */
1763 if (level > 0) {
1764 pri_latency *= 5;
1765 spr_latency *= 5;
1766 cur_latency *= 5;
1767 }
1768
1769 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1770 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1771 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1772 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1773 result->enable = true;
1774}
1775
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001776static uint32_t
1777hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001778{
1779 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001781 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001782 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001783
Matt Roper3ef00282015-03-09 10:19:24 -07001784 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001785 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001786
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001787 /* The WM are computed with base on how long it takes to fill a single
1788 * row at the given clock rate, multiplied by 8.
1789 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001790 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1791 mode->crtc_clock);
1792 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001793 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001794
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001795 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1796 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001797}
1798
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001799static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001800{
1801 struct drm_i915_private *dev_priv = dev->dev_private;
1802
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001803 if (IS_GEN9(dev)) {
1804 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001805 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001806 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001807
1808 /* read the first set of memory latencies[0:3] */
1809 val = 0; /* data0 to be programmed to 0 for first set */
1810 mutex_lock(&dev_priv->rps.hw_lock);
1811 ret = sandybridge_pcode_read(dev_priv,
1812 GEN9_PCODE_READ_MEM_LATENCY,
1813 &val);
1814 mutex_unlock(&dev_priv->rps.hw_lock);
1815
1816 if (ret) {
1817 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1818 return;
1819 }
1820
1821 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1822 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1823 GEN9_MEM_LATENCY_LEVEL_MASK;
1824 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1825 GEN9_MEM_LATENCY_LEVEL_MASK;
1826 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1827 GEN9_MEM_LATENCY_LEVEL_MASK;
1828
1829 /* read the second set of memory latencies[4:7] */
1830 val = 1; /* data0 to be programmed to 1 for second set */
1831 mutex_lock(&dev_priv->rps.hw_lock);
1832 ret = sandybridge_pcode_read(dev_priv,
1833 GEN9_PCODE_READ_MEM_LATENCY,
1834 &val);
1835 mutex_unlock(&dev_priv->rps.hw_lock);
1836 if (ret) {
1837 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1838 return;
1839 }
1840
1841 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1842 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1843 GEN9_MEM_LATENCY_LEVEL_MASK;
1844 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1845 GEN9_MEM_LATENCY_LEVEL_MASK;
1846 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1847 GEN9_MEM_LATENCY_LEVEL_MASK;
1848
Vandana Kannan367294b2014-11-04 17:06:46 +00001849 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001850 * WaWmMemoryReadLatency:skl
1851 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001852 * punit doesn't take into account the read latency so we need
1853 * to add 2us to the various latency levels we retrieve from
1854 * the punit.
1855 * - W0 is a bit special in that it's the only level that
1856 * can't be disabled if we want to have display working, so
1857 * we always add 2us there.
1858 * - For levels >=1, punit returns 0us latency when they are
1859 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001860 *
1861 * Additionally, if a level n (n > 1) has a 0us latency, all
1862 * levels m (m >= n) need to be disabled. We make sure to
1863 * sanitize the values out of the punit to satisfy this
1864 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001865 */
1866 wm[0] += 2;
1867 for (level = 1; level <= max_level; level++)
1868 if (wm[level] != 0)
1869 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001870 else {
1871 for (i = level + 1; i <= max_level; i++)
1872 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001873
Vandana Kannan4f947382014-11-04 17:06:47 +00001874 break;
1875 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001876 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001877 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1878
1879 wm[0] = (sskpd >> 56) & 0xFF;
1880 if (wm[0] == 0)
1881 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001882 wm[1] = (sskpd >> 4) & 0xFF;
1883 wm[2] = (sskpd >> 12) & 0xFF;
1884 wm[3] = (sskpd >> 20) & 0x1FF;
1885 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001886 } else if (INTEL_INFO(dev)->gen >= 6) {
1887 uint32_t sskpd = I915_READ(MCH_SSKPD);
1888
1889 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1890 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1891 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1892 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001893 } else if (INTEL_INFO(dev)->gen >= 5) {
1894 uint32_t mltr = I915_READ(MLTR_ILK);
1895
1896 /* ILK primary LP0 latency is 700 ns */
1897 wm[0] = 7;
1898 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1899 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001900 }
1901}
1902
Ville Syrjälä53615a52013-08-01 16:18:50 +03001903static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1904{
1905 /* ILK sprite LP0 latency is 1300 ns */
1906 if (INTEL_INFO(dev)->gen == 5)
1907 wm[0] = 13;
1908}
1909
1910static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1911{
1912 /* ILK cursor LP0 latency is 1300 ns */
1913 if (INTEL_INFO(dev)->gen == 5)
1914 wm[0] = 13;
1915
1916 /* WaDoubleCursorLP3Latency:ivb */
1917 if (IS_IVYBRIDGE(dev))
1918 wm[3] *= 2;
1919}
1920
Damien Lespiau546c81f2014-05-13 15:30:26 +01001921int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001922{
1923 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001924 if (IS_GEN9(dev))
1925 return 7;
1926 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001927 return 4;
1928 else if (INTEL_INFO(dev)->gen >= 6)
1929 return 3;
1930 else
1931 return 2;
1932}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001933
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001934static void intel_print_wm_latency(struct drm_device *dev,
1935 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001936 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001937{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001938 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001939
1940 for (level = 0; level <= max_level; level++) {
1941 unsigned int latency = wm[level];
1942
1943 if (latency == 0) {
1944 DRM_ERROR("%s WM%d latency not provided\n",
1945 name, level);
1946 continue;
1947 }
1948
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001949 /*
1950 * - latencies are in us on gen9.
1951 * - before then, WM1+ latency values are in 0.5us units
1952 */
1953 if (IS_GEN9(dev))
1954 latency *= 10;
1955 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001956 latency *= 5;
1957
1958 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1959 name, level, wm[level],
1960 latency / 10, latency % 10);
1961 }
1962}
1963
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001964static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1965 uint16_t wm[5], uint16_t min)
1966{
1967 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1968
1969 if (wm[0] >= min)
1970 return false;
1971
1972 wm[0] = max(wm[0], min);
1973 for (level = 1; level <= max_level; level++)
1974 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1975
1976 return true;
1977}
1978
1979static void snb_wm_latency_quirk(struct drm_device *dev)
1980{
1981 struct drm_i915_private *dev_priv = dev->dev_private;
1982 bool changed;
1983
1984 /*
1985 * The BIOS provided WM memory latency values are often
1986 * inadequate for high resolution displays. Adjust them.
1987 */
1988 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1989 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1990 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1991
1992 if (!changed)
1993 return;
1994
1995 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1996 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1997 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1998 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1999}
2000
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002001static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002002{
2003 struct drm_i915_private *dev_priv = dev->dev_private;
2004
2005 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2006
2007 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2008 sizeof(dev_priv->wm.pri_latency));
2009 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2010 sizeof(dev_priv->wm.pri_latency));
2011
2012 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2013 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002014
2015 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2016 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2017 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002018
2019 if (IS_GEN6(dev))
2020 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002021}
2022
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002023static void skl_setup_wm_latency(struct drm_device *dev)
2024{
2025 struct drm_i915_private *dev_priv = dev->dev_private;
2026
2027 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2028 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2029}
2030
Imre Deak820c1982013-12-17 14:46:36 +02002031static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002032 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002033{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002034 struct drm_device *dev = crtc->dev;
2035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2036 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002037 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002038
Matt Roper3ef00282015-03-09 10:19:24 -07002039 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002040 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002041
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002042 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002043 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002044 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roperc9f038a2015-03-09 11:06:02 -07002045
2046 if (crtc->primary->state->fb) {
2047 p->pri.enabled = true;
2048 p->pri.bytes_per_pixel =
2049 crtc->primary->state->fb->bits_per_pixel / 8;
2050 } else {
2051 p->pri.enabled = false;
2052 p->pri.bytes_per_pixel = 0;
2053 }
2054
2055 if (crtc->cursor->state->fb) {
2056 p->cur.enabled = true;
2057 p->cur.bytes_per_pixel = 4;
2058 } else {
2059 p->cur.enabled = false;
2060 p->cur.bytes_per_pixel = 0;
2061 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002062 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08002063 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002064
Matt Roperaf2b6532014-04-01 15:22:32 -07002065 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002066 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002067
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002068 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002069 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002070 break;
2071 }
2072 }
2073}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002074
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002075static void ilk_compute_wm_config(struct drm_device *dev,
2076 struct intel_wm_config *config)
2077{
2078 struct intel_crtc *intel_crtc;
2079
2080 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002081 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002082 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2083
2084 if (!wm->pipe_enabled)
2085 continue;
2086
2087 config->sprites_enabled |= wm->sprites_enabled;
2088 config->sprites_scaled |= wm->sprites_scaled;
2089 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002090 }
2091}
2092
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002093/* Compute new watermarks for the pipe */
2094static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002095 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002096 struct intel_pipe_wm *pipe_wm)
2097{
2098 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002099 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002100 int level, max_level = ilk_wm_max_level(dev);
2101 /* LP0 watermark maximums depend on this pipe alone */
2102 struct intel_wm_config config = {
2103 .num_pipes_active = 1,
2104 .sprites_enabled = params->spr.enabled,
2105 .sprites_scaled = params->spr.scaled,
2106 };
Imre Deak820c1982013-12-17 14:46:36 +02002107 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002108
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002109 pipe_wm->pipe_enabled = params->active;
2110 pipe_wm->sprites_enabled = params->spr.enabled;
2111 pipe_wm->sprites_scaled = params->spr.scaled;
2112
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002113 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2114 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2115 max_level = 1;
2116
2117 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2118 if (params->spr.scaled)
2119 max_level = 0;
2120
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002121 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002122
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002123 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002124 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002125
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002126 /* LP0 watermarks always use 1/2 DDB partitioning */
2127 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2128
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002129 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002130 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2131 return false;
2132
2133 ilk_compute_wm_reg_maximums(dev, 1, &max);
2134
2135 for (level = 1; level <= max_level; level++) {
2136 struct intel_wm_level wm = {};
2137
2138 ilk_compute_wm_level(dev_priv, level, params, &wm);
2139
2140 /*
2141 * Disable any watermark level that exceeds the
2142 * register maximums since such watermarks are
2143 * always invalid.
2144 */
2145 if (!ilk_validate_wm_level(level, &max, &wm))
2146 break;
2147
2148 pipe_wm->wm[level] = wm;
2149 }
2150
2151 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002152}
2153
2154/*
2155 * Merge the watermarks from all active pipes for a specific level.
2156 */
2157static void ilk_merge_wm_level(struct drm_device *dev,
2158 int level,
2159 struct intel_wm_level *ret_wm)
2160{
2161 const struct intel_crtc *intel_crtc;
2162
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002163 ret_wm->enable = true;
2164
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002165 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002166 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2167 const struct intel_wm_level *wm = &active->wm[level];
2168
2169 if (!active->pipe_enabled)
2170 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002171
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002172 /*
2173 * The watermark values may have been used in the past,
2174 * so we must maintain them in the registers for some
2175 * time even if the level is now disabled.
2176 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002177 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002178 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002179
2180 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2181 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2182 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2183 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2184 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002185}
2186
2187/*
2188 * Merge all low power watermarks for all active pipes.
2189 */
2190static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002191 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002192 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002193 struct intel_pipe_wm *merged)
2194{
2195 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002196 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002197
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002198 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2199 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2200 config->num_pipes_active > 1)
2201 return;
2202
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002203 /* ILK: FBC WM must be disabled always */
2204 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002205
2206 /* merge each WM1+ level */
2207 for (level = 1; level <= max_level; level++) {
2208 struct intel_wm_level *wm = &merged->wm[level];
2209
2210 ilk_merge_wm_level(dev, level, wm);
2211
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002212 if (level > last_enabled_level)
2213 wm->enable = false;
2214 else if (!ilk_validate_wm_level(level, max, wm))
2215 /* make sure all following levels get disabled */
2216 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002217
2218 /*
2219 * The spec says it is preferred to disable
2220 * FBC WMs instead of disabling a WM level.
2221 */
2222 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002223 if (wm->enable)
2224 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002225 wm->fbc_val = 0;
2226 }
2227 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002228
2229 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2230 /*
2231 * FIXME this is racy. FBC might get enabled later.
2232 * What we should check here is whether FBC can be
2233 * enabled sometime later.
2234 */
2235 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2236 for (level = 2; level <= max_level; level++) {
2237 struct intel_wm_level *wm = &merged->wm[level];
2238
2239 wm->enable = false;
2240 }
2241 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002242}
2243
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002244static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2245{
2246 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2247 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2248}
2249
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002250/* The value we need to program into the WM_LPx latency field */
2251static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2252{
2253 struct drm_i915_private *dev_priv = dev->dev_private;
2254
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002255 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002256 return 2 * level;
2257 else
2258 return dev_priv->wm.pri_latency[level];
2259}
2260
Imre Deak820c1982013-12-17 14:46:36 +02002261static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002262 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002263 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002264 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002265{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002266 struct intel_crtc *intel_crtc;
2267 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002268
Ville Syrjälä0362c782013-10-09 19:17:57 +03002269 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002270 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002271
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002272 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002273 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002274 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002275
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002276 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002277
Ville Syrjälä0362c782013-10-09 19:17:57 +03002278 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002279
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002280 /*
2281 * Maintain the watermark values even if the level is
2282 * disabled. Doing otherwise could cause underruns.
2283 */
2284 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002285 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002286 (r->pri_val << WM1_LP_SR_SHIFT) |
2287 r->cur_val;
2288
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002289 if (r->enable)
2290 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2291
Ville Syrjälä416f4722013-11-02 21:07:46 -07002292 if (INTEL_INFO(dev)->gen >= 8)
2293 results->wm_lp[wm_lp - 1] |=
2294 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2295 else
2296 results->wm_lp[wm_lp - 1] |=
2297 r->fbc_val << WM1_LP_FBC_SHIFT;
2298
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002299 /*
2300 * Always set WM1S_LP_EN when spr_val != 0, even if the
2301 * level is disabled. Doing otherwise could cause underruns.
2302 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002303 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2304 WARN_ON(wm_lp != 1);
2305 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2306 } else
2307 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002308 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002309
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002310 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002311 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002312 enum pipe pipe = intel_crtc->pipe;
2313 const struct intel_wm_level *r =
2314 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002315
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002316 if (WARN_ON(!r->enable))
2317 continue;
2318
2319 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2320
2321 results->wm_pipe[pipe] =
2322 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2323 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2324 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002325 }
2326}
2327
Paulo Zanoni861f3382013-05-31 10:19:21 -03002328/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2329 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002330static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002331 struct intel_pipe_wm *r1,
2332 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002333{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002334 int level, max_level = ilk_wm_max_level(dev);
2335 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002336
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002337 for (level = 1; level <= max_level; level++) {
2338 if (r1->wm[level].enable)
2339 level1 = level;
2340 if (r2->wm[level].enable)
2341 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002342 }
2343
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002344 if (level1 == level2) {
2345 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002346 return r2;
2347 else
2348 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002349 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002350 return r1;
2351 } else {
2352 return r2;
2353 }
2354}
2355
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002356/* dirty bits used to track which watermarks need changes */
2357#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2358#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2359#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2360#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2361#define WM_DIRTY_FBC (1 << 24)
2362#define WM_DIRTY_DDB (1 << 25)
2363
Damien Lespiau055e3932014-08-18 13:49:10 +01002364static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002365 const struct ilk_wm_values *old,
2366 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002367{
2368 unsigned int dirty = 0;
2369 enum pipe pipe;
2370 int wm_lp;
2371
Damien Lespiau055e3932014-08-18 13:49:10 +01002372 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002373 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2374 dirty |= WM_DIRTY_LINETIME(pipe);
2375 /* Must disable LP1+ watermarks too */
2376 dirty |= WM_DIRTY_LP_ALL;
2377 }
2378
2379 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2380 dirty |= WM_DIRTY_PIPE(pipe);
2381 /* Must disable LP1+ watermarks too */
2382 dirty |= WM_DIRTY_LP_ALL;
2383 }
2384 }
2385
2386 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2387 dirty |= WM_DIRTY_FBC;
2388 /* Must disable LP1+ watermarks too */
2389 dirty |= WM_DIRTY_LP_ALL;
2390 }
2391
2392 if (old->partitioning != new->partitioning) {
2393 dirty |= WM_DIRTY_DDB;
2394 /* Must disable LP1+ watermarks too */
2395 dirty |= WM_DIRTY_LP_ALL;
2396 }
2397
2398 /* LP1+ watermarks already deemed dirty, no need to continue */
2399 if (dirty & WM_DIRTY_LP_ALL)
2400 return dirty;
2401
2402 /* Find the lowest numbered LP1+ watermark in need of an update... */
2403 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2404 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2405 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2406 break;
2407 }
2408
2409 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2410 for (; wm_lp <= 3; wm_lp++)
2411 dirty |= WM_DIRTY_LP(wm_lp);
2412
2413 return dirty;
2414}
2415
Ville Syrjälä8553c182013-12-05 15:51:39 +02002416static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2417 unsigned int dirty)
2418{
Imre Deak820c1982013-12-17 14:46:36 +02002419 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002420 bool changed = false;
2421
2422 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2423 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2424 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2425 changed = true;
2426 }
2427 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2428 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2429 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2430 changed = true;
2431 }
2432 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2433 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2434 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2435 changed = true;
2436 }
2437
2438 /*
2439 * Don't touch WM1S_LP_EN here.
2440 * Doing so could cause underruns.
2441 */
2442
2443 return changed;
2444}
2445
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002446/*
2447 * The spec says we shouldn't write when we don't need, because every write
2448 * causes WMs to be re-evaluated, expending some power.
2449 */
Imre Deak820c1982013-12-17 14:46:36 +02002450static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2451 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002452{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002453 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002454 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002455 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002456 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002457
Damien Lespiau055e3932014-08-18 13:49:10 +01002458 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002459 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002460 return;
2461
Ville Syrjälä8553c182013-12-05 15:51:39 +02002462 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002463
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002464 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002465 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002466 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002467 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002468 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002469 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2470
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002471 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002472 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002473 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002474 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002475 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002476 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2477
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002478 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002479 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002480 val = I915_READ(WM_MISC);
2481 if (results->partitioning == INTEL_DDB_PART_1_2)
2482 val &= ~WM_MISC_DATA_PARTITION_5_6;
2483 else
2484 val |= WM_MISC_DATA_PARTITION_5_6;
2485 I915_WRITE(WM_MISC, val);
2486 } else {
2487 val = I915_READ(DISP_ARB_CTL2);
2488 if (results->partitioning == INTEL_DDB_PART_1_2)
2489 val &= ~DISP_DATA_PARTITION_5_6;
2490 else
2491 val |= DISP_DATA_PARTITION_5_6;
2492 I915_WRITE(DISP_ARB_CTL2, val);
2493 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002494 }
2495
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002496 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002497 val = I915_READ(DISP_ARB_CTL);
2498 if (results->enable_fbc_wm)
2499 val &= ~DISP_FBC_WM_DIS;
2500 else
2501 val |= DISP_FBC_WM_DIS;
2502 I915_WRITE(DISP_ARB_CTL, val);
2503 }
2504
Imre Deak954911e2013-12-17 14:46:34 +02002505 if (dirty & WM_DIRTY_LP(1) &&
2506 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2507 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2508
2509 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002510 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2511 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2512 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2513 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2514 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002515
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002516 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002517 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002518 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002519 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002520 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002521 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002522
2523 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002524}
2525
Ville Syrjälä8553c182013-12-05 15:51:39 +02002526static bool ilk_disable_lp_wm(struct drm_device *dev)
2527{
2528 struct drm_i915_private *dev_priv = dev->dev_private;
2529
2530 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2531}
2532
Damien Lespiaub9cec072014-11-04 17:06:43 +00002533/*
2534 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2535 * different active planes.
2536 */
2537
2538#define SKL_DDB_SIZE 896 /* in blocks */
2539
2540static void
2541skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2542 struct drm_crtc *for_crtc,
2543 const struct intel_wm_config *config,
2544 const struct skl_pipe_wm_parameters *params,
2545 struct skl_ddb_entry *alloc /* out */)
2546{
2547 struct drm_crtc *crtc;
2548 unsigned int pipe_size, ddb_size;
2549 int nth_active_pipe;
2550
2551 if (!params->active) {
2552 alloc->start = 0;
2553 alloc->end = 0;
2554 return;
2555 }
2556
2557 ddb_size = SKL_DDB_SIZE;
2558
2559 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2560
2561 nth_active_pipe = 0;
2562 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002563 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002564 continue;
2565
2566 if (crtc == for_crtc)
2567 break;
2568
2569 nth_active_pipe++;
2570 }
2571
2572 pipe_size = ddb_size / config->num_pipes_active;
2573 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002574 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002575}
2576
2577static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2578{
2579 if (config->num_pipes_active == 1)
2580 return 32;
2581
2582 return 8;
2583}
2584
Damien Lespiaua269c582014-11-04 17:06:49 +00002585static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2586{
2587 entry->start = reg & 0x3ff;
2588 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002589 if (entry->end)
2590 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002591}
2592
Damien Lespiau08db6652014-11-04 17:06:52 +00002593void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2594 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002595{
Damien Lespiaua269c582014-11-04 17:06:49 +00002596 enum pipe pipe;
2597 int plane;
2598 u32 val;
2599
2600 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002601 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002602 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2603 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2604 val);
2605 }
2606
2607 val = I915_READ(CUR_BUF_CFG(pipe));
2608 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2609 }
2610}
2611
Damien Lespiaub9cec072014-11-04 17:06:43 +00002612static unsigned int
2613skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2614{
2615 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2616}
2617
2618/*
2619 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2620 * a 8192x4096@32bpp framebuffer:
2621 * 3 * 4096 * 8192 * 4 < 2^32
2622 */
2623static unsigned int
2624skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2625 const struct skl_pipe_wm_parameters *params)
2626{
2627 unsigned int total_data_rate = 0;
2628 int plane;
2629
2630 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2631 const struct intel_plane_wm_parameters *p;
2632
2633 p = &params->plane[plane];
2634 if (!p->enabled)
2635 continue;
2636
2637 total_data_rate += skl_plane_relative_data_rate(p);
2638 }
2639
2640 return total_data_rate;
2641}
2642
2643static void
2644skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2645 const struct intel_wm_config *config,
2646 const struct skl_pipe_wm_parameters *params,
2647 struct skl_ddb_allocation *ddb /* out */)
2648{
2649 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002650 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2652 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002653 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002654 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002655 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002656 unsigned int total_data_rate;
2657 int plane;
2658
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002659 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2660 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002661 if (alloc_size == 0) {
2662 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2663 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2664 return;
2665 }
2666
2667 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002668 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2669 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002670
2671 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002672 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002673
Damien Lespiau80958152015-02-09 13:35:10 +00002674 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002675 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002676 const struct intel_plane_wm_parameters *p;
2677
2678 p = &params->plane[plane];
2679 if (!p->enabled)
2680 continue;
2681
2682 minimum[plane] = 8;
2683 alloc_size -= minimum[plane];
2684 }
2685
Damien Lespiaub9cec072014-11-04 17:06:43 +00002686 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002687 * 2. Distribute the remaining space in proportion to the amount of
2688 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002689 *
2690 * FIXME: we may not allocate every single block here.
2691 */
2692 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2693
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002694 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002695 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2696 const struct intel_plane_wm_parameters *p;
2697 unsigned int data_rate;
2698 uint16_t plane_blocks;
2699
2700 p = &params->plane[plane];
2701 if (!p->enabled)
2702 continue;
2703
2704 data_rate = skl_plane_relative_data_rate(p);
2705
2706 /*
2707 * promote the expression to 64 bits to avoid overflowing, the
2708 * result is < available as data_rate / total_data_rate < 1
2709 */
Damien Lespiau80958152015-02-09 13:35:10 +00002710 plane_blocks = minimum[plane];
2711 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2712 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002713
2714 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002715 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002716
2717 start += plane_blocks;
2718 }
2719
2720}
2721
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002722static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002723{
2724 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002725 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002726}
2727
2728/*
2729 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2730 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2731 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2732 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2733*/
2734static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2735 uint32_t latency)
2736{
2737 uint32_t wm_intermediate_val, ret;
2738
2739 if (latency == 0)
2740 return UINT_MAX;
2741
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002742 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002743 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2744
2745 return ret;
2746}
2747
2748static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2749 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002750 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002751{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002752 uint32_t ret;
2753 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2754 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002755
2756 if (latency == 0)
2757 return UINT_MAX;
2758
2759 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002760
2761 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2762 tiling == I915_FORMAT_MOD_Yf_TILED) {
2763 plane_bytes_per_line *= 4;
2764 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2765 plane_blocks_per_line /= 4;
2766 } else {
2767 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2768 }
2769
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002770 wm_intermediate_val = latency * pixel_rate;
2771 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002772 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002773
2774 return ret;
2775}
2776
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002777static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2778 const struct intel_crtc *intel_crtc)
2779{
2780 struct drm_device *dev = intel_crtc->base.dev;
2781 struct drm_i915_private *dev_priv = dev->dev_private;
2782 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2783 enum pipe pipe = intel_crtc->pipe;
2784
2785 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2786 sizeof(new_ddb->plane[pipe])))
2787 return true;
2788
2789 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2790 sizeof(new_ddb->cursor[pipe])))
2791 return true;
2792
2793 return false;
2794}
2795
2796static void skl_compute_wm_global_parameters(struct drm_device *dev,
2797 struct intel_wm_config *config)
2798{
2799 struct drm_crtc *crtc;
2800 struct drm_plane *plane;
2801
2802 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07002803 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002804
2805 /* FIXME: I don't think we need those two global parameters on SKL */
2806 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2807 struct intel_plane *intel_plane = to_intel_plane(plane);
2808
2809 config->sprites_enabled |= intel_plane->wm.enabled;
2810 config->sprites_scaled |= intel_plane->wm.scaled;
2811 }
2812}
2813
2814static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2815 struct skl_pipe_wm_parameters *p)
2816{
2817 struct drm_device *dev = crtc->dev;
2818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2819 enum pipe pipe = intel_crtc->pipe;
2820 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002821 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002822 int i = 1; /* Index for sprite planes start */
2823
Matt Roper3ef00282015-03-09 10:19:24 -07002824 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002825 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002826 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2827 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002828
Matt Roperc9f038a2015-03-09 11:06:02 -07002829 fb = crtc->primary->state->fb;
2830 if (fb) {
2831 p->plane[0].enabled = true;
2832 p->plane[0].bytes_per_pixel = fb->bits_per_pixel / 8;
2833 p->plane[0].tiling = fb->modifier[0];
2834 } else {
2835 p->plane[0].enabled = false;
2836 p->plane[0].bytes_per_pixel = 0;
2837 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2838 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002839 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2840 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002841
Matt Roperc9f038a2015-03-09 11:06:02 -07002842 fb = crtc->cursor->state->fb;
2843 if (fb) {
2844 p->cursor.enabled = true;
2845 p->cursor.bytes_per_pixel = fb->bits_per_pixel / 8;
2846 p->cursor.horiz_pixels = crtc->cursor->state->crtc_w;
2847 p->cursor.vert_pixels = crtc->cursor->state->crtc_h;
2848 } else {
2849 p->cursor.enabled = false;
2850 p->cursor.bytes_per_pixel = 0;
2851 p->cursor.horiz_pixels = 64;
2852 p->cursor.vert_pixels = 64;
2853 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002854 }
2855
2856 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2857 struct intel_plane *intel_plane = to_intel_plane(plane);
2858
Sonika Jindala712f8e2014-12-09 10:59:15 +05302859 if (intel_plane->pipe == pipe &&
2860 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002861 p->plane[i++] = intel_plane->wm;
2862 }
2863}
2864
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002865static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2866 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002867 struct intel_plane_wm_parameters *p_params,
2868 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002869 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002870 uint16_t *out_blocks, /* out */
2871 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002872{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002873 uint32_t latency = dev_priv->wm.skl_latency[level];
2874 uint32_t method1, method2;
2875 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2876 uint32_t res_blocks, res_lines;
2877 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002878
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002879 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002880 return false;
2881
2882 method1 = skl_wm_method1(p->pixel_rate,
2883 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002884 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002885 method2 = skl_wm_method2(p->pixel_rate,
2886 p->pipe_htotal,
2887 p_params->horiz_pixels,
2888 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002889 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002890 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002891
2892 plane_bytes_per_line = p_params->horiz_pixels *
2893 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002894 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002895
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002896 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2897 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2898 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2899 selected_result = max(method2, y_tile_minimum);
2900 } else {
2901 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2902 selected_result = min(method1, method2);
2903 else
2904 selected_result = method1;
2905 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002906
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002907 res_blocks = selected_result + 1;
2908 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002909
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002910 if (level >= 1 && level <= 7) {
2911 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2912 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2913 res_lines += 4;
2914 else
2915 res_blocks++;
2916 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002917
2918 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002919 return false;
2920
2921 *out_blocks = res_blocks;
2922 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002923
2924 return true;
2925}
2926
2927static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2928 struct skl_ddb_allocation *ddb,
2929 struct skl_pipe_wm_parameters *p,
2930 enum pipe pipe,
2931 int level,
2932 int num_planes,
2933 struct skl_wm_level *result)
2934{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002935 uint16_t ddb_blocks;
2936 int i;
2937
2938 for (i = 0; i < num_planes; i++) {
2939 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2940
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002941 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2942 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002943 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002944 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002945 &result->plane_res_b[i],
2946 &result->plane_res_l[i]);
2947 }
2948
2949 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002950 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2951 ddb_blocks, level,
2952 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002953 &result->cursor_res_l);
2954}
2955
Damien Lespiau407b50f2014-11-04 17:06:57 +00002956static uint32_t
2957skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2958{
Matt Roper3ef00282015-03-09 10:19:24 -07002959 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002960 return 0;
2961
2962 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2963
2964}
2965
2966static void skl_compute_transition_wm(struct drm_crtc *crtc,
2967 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002968 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002969{
Damien Lespiau9414f562014-11-04 17:06:58 +00002970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2971 int i;
2972
Damien Lespiau407b50f2014-11-04 17:06:57 +00002973 if (!params->active)
2974 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002975
2976 /* Until we know more, just disable transition WMs */
2977 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2978 trans_wm->plane_en[i] = false;
2979 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002980}
2981
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002982static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2983 struct skl_ddb_allocation *ddb,
2984 struct skl_pipe_wm_parameters *params,
2985 struct skl_pipe_wm *pipe_wm)
2986{
2987 struct drm_device *dev = crtc->dev;
2988 const struct drm_i915_private *dev_priv = dev->dev_private;
2989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2990 int level, max_level = ilk_wm_max_level(dev);
2991
2992 for (level = 0; level <= max_level; level++) {
2993 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2994 level, intel_num_planes(intel_crtc),
2995 &pipe_wm->wm[level]);
2996 }
2997 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2998
Damien Lespiau9414f562014-11-04 17:06:58 +00002999 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003000}
3001
3002static void skl_compute_wm_results(struct drm_device *dev,
3003 struct skl_pipe_wm_parameters *p,
3004 struct skl_pipe_wm *p_wm,
3005 struct skl_wm_values *r,
3006 struct intel_crtc *intel_crtc)
3007{
3008 int level, max_level = ilk_wm_max_level(dev);
3009 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003010 uint32_t temp;
3011 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003012
3013 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003014 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3015 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003016
3017 temp |= p_wm->wm[level].plane_res_l[i] <<
3018 PLANE_WM_LINES_SHIFT;
3019 temp |= p_wm->wm[level].plane_res_b[i];
3020 if (p_wm->wm[level].plane_en[i])
3021 temp |= PLANE_WM_EN;
3022
3023 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003024 }
3025
3026 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003027
3028 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3029 temp |= p_wm->wm[level].cursor_res_b;
3030
3031 if (p_wm->wm[level].cursor_en)
3032 temp |= PLANE_WM_EN;
3033
3034 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003035
3036 }
3037
Damien Lespiau9414f562014-11-04 17:06:58 +00003038 /* transition WMs */
3039 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3040 temp = 0;
3041 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3042 temp |= p_wm->trans_wm.plane_res_b[i];
3043 if (p_wm->trans_wm.plane_en[i])
3044 temp |= PLANE_WM_EN;
3045
3046 r->plane_trans[pipe][i] = temp;
3047 }
3048
3049 temp = 0;
3050 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3051 temp |= p_wm->trans_wm.cursor_res_b;
3052 if (p_wm->trans_wm.cursor_en)
3053 temp |= PLANE_WM_EN;
3054
3055 r->cursor_trans[pipe] = temp;
3056
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003057 r->wm_linetime[pipe] = p_wm->linetime;
3058}
3059
Damien Lespiau16160e32014-11-04 17:06:53 +00003060static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3061 const struct skl_ddb_entry *entry)
3062{
3063 if (entry->end)
3064 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3065 else
3066 I915_WRITE(reg, 0);
3067}
3068
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003069static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3070 const struct skl_wm_values *new)
3071{
3072 struct drm_device *dev = dev_priv->dev;
3073 struct intel_crtc *crtc;
3074
3075 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3076 int i, level, max_level = ilk_wm_max_level(dev);
3077 enum pipe pipe = crtc->pipe;
3078
Damien Lespiau5d374d92014-11-04 17:07:00 +00003079 if (!new->dirty[pipe])
3080 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003081
Damien Lespiau5d374d92014-11-04 17:07:00 +00003082 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3083
3084 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003085 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003086 I915_WRITE(PLANE_WM(pipe, i, level),
3087 new->plane[pipe][i][level]);
3088 I915_WRITE(CUR_WM(pipe, level),
3089 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003090 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003091 for (i = 0; i < intel_num_planes(crtc); i++)
3092 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3093 new->plane_trans[pipe][i]);
3094 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3095
3096 for (i = 0; i < intel_num_planes(crtc); i++)
3097 skl_ddb_entry_write(dev_priv,
3098 PLANE_BUF_CFG(pipe, i),
3099 &new->ddb.plane[pipe][i]);
3100
3101 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3102 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003103 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003104}
3105
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003106/*
3107 * When setting up a new DDB allocation arrangement, we need to correctly
3108 * sequence the times at which the new allocations for the pipes are taken into
3109 * account or we'll have pipes fetching from space previously allocated to
3110 * another pipe.
3111 *
3112 * Roughly the sequence looks like:
3113 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3114 * overlapping with a previous light-up pipe (another way to put it is:
3115 * pipes with their new allocation strickly included into their old ones).
3116 * 2. re-allocate the other pipes that get their allocation reduced
3117 * 3. allocate the pipes having their allocation increased
3118 *
3119 * Steps 1. and 2. are here to take care of the following case:
3120 * - Initially DDB looks like this:
3121 * | B | C |
3122 * - enable pipe A.
3123 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3124 * allocation
3125 * | A | B | C |
3126 *
3127 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3128 */
3129
Damien Lespiaud21b7952014-11-04 17:07:03 +00003130static void
3131skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003132{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003133 int plane;
3134
Damien Lespiaud21b7952014-11-04 17:07:03 +00003135 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3136
Damien Lespiaudd740782015-02-28 14:54:08 +00003137 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003138 I915_WRITE(PLANE_SURF(pipe, plane),
3139 I915_READ(PLANE_SURF(pipe, plane)));
3140 }
3141 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3142}
3143
3144static bool
3145skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3146 const struct skl_ddb_allocation *new,
3147 enum pipe pipe)
3148{
3149 uint16_t old_size, new_size;
3150
3151 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3152 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3153
3154 return old_size != new_size &&
3155 new->pipe[pipe].start >= old->pipe[pipe].start &&
3156 new->pipe[pipe].end <= old->pipe[pipe].end;
3157}
3158
3159static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3160 struct skl_wm_values *new_values)
3161{
3162 struct drm_device *dev = dev_priv->dev;
3163 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3164 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3165 struct intel_crtc *crtc;
3166 enum pipe pipe;
3167
3168 new_ddb = &new_values->ddb;
3169 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3170
3171 /*
3172 * First pass: flush the pipes with the new allocation contained into
3173 * the old space.
3174 *
3175 * We'll wait for the vblank on those pipes to ensure we can safely
3176 * re-allocate the freed space without this pipe fetching from it.
3177 */
3178 for_each_intel_crtc(dev, crtc) {
3179 if (!crtc->active)
3180 continue;
3181
3182 pipe = crtc->pipe;
3183
3184 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3185 continue;
3186
Damien Lespiaud21b7952014-11-04 17:07:03 +00003187 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003188 intel_wait_for_vblank(dev, pipe);
3189
3190 reallocated[pipe] = true;
3191 }
3192
3193
3194 /*
3195 * Second pass: flush the pipes that are having their allocation
3196 * reduced, but overlapping with a previous allocation.
3197 *
3198 * Here as well we need to wait for the vblank to make sure the freed
3199 * space is not used anymore.
3200 */
3201 for_each_intel_crtc(dev, crtc) {
3202 if (!crtc->active)
3203 continue;
3204
3205 pipe = crtc->pipe;
3206
3207 if (reallocated[pipe])
3208 continue;
3209
3210 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3211 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003212 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003213 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303214 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003215 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003216 }
3217
3218 /*
3219 * Third pass: flush the pipes that got more space allocated.
3220 *
3221 * We don't need to actively wait for the update here, next vblank
3222 * will just get more DDB space with the correct WM values.
3223 */
3224 for_each_intel_crtc(dev, crtc) {
3225 if (!crtc->active)
3226 continue;
3227
3228 pipe = crtc->pipe;
3229
3230 /*
3231 * At this point, only the pipes more space than before are
3232 * left to re-allocate.
3233 */
3234 if (reallocated[pipe])
3235 continue;
3236
Damien Lespiaud21b7952014-11-04 17:07:03 +00003237 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003238 }
3239}
3240
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003241static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3242 struct skl_pipe_wm_parameters *params,
3243 struct intel_wm_config *config,
3244 struct skl_ddb_allocation *ddb, /* out */
3245 struct skl_pipe_wm *pipe_wm /* out */)
3246{
3247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3248
3249 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003250 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003251 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3252
3253 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3254 return false;
3255
3256 intel_crtc->wm.skl_active = *pipe_wm;
3257 return true;
3258}
3259
3260static void skl_update_other_pipe_wm(struct drm_device *dev,
3261 struct drm_crtc *crtc,
3262 struct intel_wm_config *config,
3263 struct skl_wm_values *r)
3264{
3265 struct intel_crtc *intel_crtc;
3266 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3267
3268 /*
3269 * If the WM update hasn't changed the allocation for this_crtc (the
3270 * crtc we are currently computing the new WM values for), other
3271 * enabled crtcs will keep the same allocation and we don't need to
3272 * recompute anything for them.
3273 */
3274 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3275 return;
3276
3277 /*
3278 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3279 * other active pipes need new DDB allocation and WM values.
3280 */
3281 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3282 base.head) {
3283 struct skl_pipe_wm_parameters params = {};
3284 struct skl_pipe_wm pipe_wm = {};
3285 bool wm_changed;
3286
3287 if (this_crtc->pipe == intel_crtc->pipe)
3288 continue;
3289
3290 if (!intel_crtc->active)
3291 continue;
3292
3293 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3294 &params, config,
3295 &r->ddb, &pipe_wm);
3296
3297 /*
3298 * If we end up re-computing the other pipe WM values, it's
3299 * because it was really needed, so we expect the WM values to
3300 * be different.
3301 */
3302 WARN_ON(!wm_changed);
3303
3304 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3305 r->dirty[intel_crtc->pipe] = true;
3306 }
3307}
3308
3309static void skl_update_wm(struct drm_crtc *crtc)
3310{
3311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3312 struct drm_device *dev = crtc->dev;
3313 struct drm_i915_private *dev_priv = dev->dev_private;
3314 struct skl_pipe_wm_parameters params = {};
3315 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3316 struct skl_pipe_wm pipe_wm = {};
3317 struct intel_wm_config config = {};
3318
3319 memset(results, 0, sizeof(*results));
3320
3321 skl_compute_wm_global_parameters(dev, &config);
3322
3323 if (!skl_update_pipe_wm(crtc, &params, &config,
3324 &results->ddb, &pipe_wm))
3325 return;
3326
3327 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3328 results->dirty[intel_crtc->pipe] = true;
3329
3330 skl_update_other_pipe_wm(dev, crtc, &config, results);
3331 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003332 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003333
3334 /* store the new configuration */
3335 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003336}
3337
3338static void
3339skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3340 uint32_t sprite_width, uint32_t sprite_height,
3341 int pixel_size, bool enabled, bool scaled)
3342{
3343 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003344 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003345
3346 intel_plane->wm.enabled = enabled;
3347 intel_plane->wm.scaled = scaled;
3348 intel_plane->wm.horiz_pixels = sprite_width;
3349 intel_plane->wm.vert_pixels = sprite_height;
3350 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003351 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3352 /*
3353 * Framebuffer can be NULL on plane disable, but it does not
3354 * matter for watermarks if we assume no tiling in that case.
3355 */
3356 if (fb)
3357 intel_plane->wm.tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003358
3359 skl_update_wm(crtc);
3360}
3361
Imre Deak820c1982013-12-17 14:46:36 +02003362static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003363{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003365 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003366 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003367 struct ilk_wm_maximums max;
3368 struct ilk_pipe_wm_parameters params = {};
3369 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003370 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003371 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003372 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003373 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003374
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003375 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003376
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003377 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3378
3379 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3380 return;
3381
3382 intel_crtc->wm.active = pipe_wm;
3383
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003384 ilk_compute_wm_config(dev, &config);
3385
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003386 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003387 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003388
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003389 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003390 if (INTEL_INFO(dev)->gen >= 7 &&
3391 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003392 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003393 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003394
Imre Deak820c1982013-12-17 14:46:36 +02003395 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003396 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003397 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003398 }
3399
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003400 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003401 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003402
Imre Deak820c1982013-12-17 14:46:36 +02003403 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003404
Imre Deak820c1982013-12-17 14:46:36 +02003405 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003406}
3407
Damien Lespiaued57cb82014-07-15 09:21:24 +02003408static void
3409ilk_update_sprite_wm(struct drm_plane *plane,
3410 struct drm_crtc *crtc,
3411 uint32_t sprite_width, uint32_t sprite_height,
3412 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003413{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003414 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003415 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003416
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003417 intel_plane->wm.enabled = enabled;
3418 intel_plane->wm.scaled = scaled;
3419 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003420 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003421 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003422
Ville Syrjälä8553c182013-12-05 15:51:39 +02003423 /*
3424 * IVB workaround: must disable low power watermarks for at least
3425 * one frame before enabling scaling. LP watermarks can be re-enabled
3426 * when scaling is disabled.
3427 *
3428 * WaCxSRDisabledForSpriteScaling:ivb
3429 */
3430 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3431 intel_wait_for_vblank(dev, intel_plane->pipe);
3432
Imre Deak820c1982013-12-17 14:46:36 +02003433 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003434}
3435
Pradeep Bhat30789992014-11-04 17:06:45 +00003436static void skl_pipe_wm_active_state(uint32_t val,
3437 struct skl_pipe_wm *active,
3438 bool is_transwm,
3439 bool is_cursor,
3440 int i,
3441 int level)
3442{
3443 bool is_enabled = (val & PLANE_WM_EN) != 0;
3444
3445 if (!is_transwm) {
3446 if (!is_cursor) {
3447 active->wm[level].plane_en[i] = is_enabled;
3448 active->wm[level].plane_res_b[i] =
3449 val & PLANE_WM_BLOCKS_MASK;
3450 active->wm[level].plane_res_l[i] =
3451 (val >> PLANE_WM_LINES_SHIFT) &
3452 PLANE_WM_LINES_MASK;
3453 } else {
3454 active->wm[level].cursor_en = is_enabled;
3455 active->wm[level].cursor_res_b =
3456 val & PLANE_WM_BLOCKS_MASK;
3457 active->wm[level].cursor_res_l =
3458 (val >> PLANE_WM_LINES_SHIFT) &
3459 PLANE_WM_LINES_MASK;
3460 }
3461 } else {
3462 if (!is_cursor) {
3463 active->trans_wm.plane_en[i] = is_enabled;
3464 active->trans_wm.plane_res_b[i] =
3465 val & PLANE_WM_BLOCKS_MASK;
3466 active->trans_wm.plane_res_l[i] =
3467 (val >> PLANE_WM_LINES_SHIFT) &
3468 PLANE_WM_LINES_MASK;
3469 } else {
3470 active->trans_wm.cursor_en = is_enabled;
3471 active->trans_wm.cursor_res_b =
3472 val & PLANE_WM_BLOCKS_MASK;
3473 active->trans_wm.cursor_res_l =
3474 (val >> PLANE_WM_LINES_SHIFT) &
3475 PLANE_WM_LINES_MASK;
3476 }
3477 }
3478}
3479
3480static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3481{
3482 struct drm_device *dev = crtc->dev;
3483 struct drm_i915_private *dev_priv = dev->dev_private;
3484 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3486 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3487 enum pipe pipe = intel_crtc->pipe;
3488 int level, i, max_level;
3489 uint32_t temp;
3490
3491 max_level = ilk_wm_max_level(dev);
3492
3493 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3494
3495 for (level = 0; level <= max_level; level++) {
3496 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3497 hw->plane[pipe][i][level] =
3498 I915_READ(PLANE_WM(pipe, i, level));
3499 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3500 }
3501
3502 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3503 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3504 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3505
Matt Roper3ef00282015-03-09 10:19:24 -07003506 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003507 return;
3508
3509 hw->dirty[pipe] = true;
3510
3511 active->linetime = hw->wm_linetime[pipe];
3512
3513 for (level = 0; level <= max_level; level++) {
3514 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3515 temp = hw->plane[pipe][i][level];
3516 skl_pipe_wm_active_state(temp, active, false,
3517 false, i, level);
3518 }
3519 temp = hw->cursor[pipe][level];
3520 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3521 }
3522
3523 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3524 temp = hw->plane_trans[pipe][i];
3525 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3526 }
3527
3528 temp = hw->cursor_trans[pipe];
3529 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3530}
3531
3532void skl_wm_get_hw_state(struct drm_device *dev)
3533{
Damien Lespiaua269c582014-11-04 17:06:49 +00003534 struct drm_i915_private *dev_priv = dev->dev_private;
3535 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003536 struct drm_crtc *crtc;
3537
Damien Lespiaua269c582014-11-04 17:06:49 +00003538 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003539 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3540 skl_pipe_wm_get_hw_state(crtc);
3541}
3542
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003543static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3544{
3545 struct drm_device *dev = crtc->dev;
3546 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003547 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003548 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3549 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3550 enum pipe pipe = intel_crtc->pipe;
3551 static const unsigned int wm0_pipe_reg[] = {
3552 [PIPE_A] = WM0_PIPEA_ILK,
3553 [PIPE_B] = WM0_PIPEB_ILK,
3554 [PIPE_C] = WM0_PIPEC_IVB,
3555 };
3556
3557 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003558 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003559 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003560
Matt Roper3ef00282015-03-09 10:19:24 -07003561 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003562
3563 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003564 u32 tmp = hw->wm_pipe[pipe];
3565
3566 /*
3567 * For active pipes LP0 watermark is marked as
3568 * enabled, and LP1+ watermaks as disabled since
3569 * we can't really reverse compute them in case
3570 * multiple pipes are active.
3571 */
3572 active->wm[0].enable = true;
3573 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3574 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3575 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3576 active->linetime = hw->wm_linetime[pipe];
3577 } else {
3578 int level, max_level = ilk_wm_max_level(dev);
3579
3580 /*
3581 * For inactive pipes, all watermark levels
3582 * should be marked as enabled but zeroed,
3583 * which is what we'd compute them to.
3584 */
3585 for (level = 0; level <= max_level; level++)
3586 active->wm[level].enable = true;
3587 }
3588}
3589
3590void ilk_wm_get_hw_state(struct drm_device *dev)
3591{
3592 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003593 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003594 struct drm_crtc *crtc;
3595
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003596 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003597 ilk_pipe_wm_get_hw_state(crtc);
3598
3599 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3600 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3601 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3602
3603 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003604 if (INTEL_INFO(dev)->gen >= 7) {
3605 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3606 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3607 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003608
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003609 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003610 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3611 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3612 else if (IS_IVYBRIDGE(dev))
3613 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3614 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003615
3616 hw->enable_fbc_wm =
3617 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3618}
3619
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003620/**
3621 * intel_update_watermarks - update FIFO watermark values based on current modes
3622 *
3623 * Calculate watermark values for the various WM regs based on current mode
3624 * and plane configuration.
3625 *
3626 * There are several cases to deal with here:
3627 * - normal (i.e. non-self-refresh)
3628 * - self-refresh (SR) mode
3629 * - lines are large relative to FIFO size (buffer can hold up to 2)
3630 * - lines are small relative to FIFO size (buffer can hold more than 2
3631 * lines), so need to account for TLB latency
3632 *
3633 * The normal calculation is:
3634 * watermark = dotclock * bytes per pixel * latency
3635 * where latency is platform & configuration dependent (we assume pessimal
3636 * values here).
3637 *
3638 * The SR calculation is:
3639 * watermark = (trunc(latency/line time)+1) * surface width *
3640 * bytes per pixel
3641 * where
3642 * line time = htotal / dotclock
3643 * surface width = hdisplay for normal plane and 64 for cursor
3644 * and latency is assumed to be high, as above.
3645 *
3646 * The final value programmed to the register should always be rounded up,
3647 * and include an extra 2 entries to account for clock crossings.
3648 *
3649 * We don't use the sprite, so we can ignore that. And on Crestline we have
3650 * to set the non-SR watermarks to 8.
3651 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003652void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003653{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003654 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003655
3656 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003657 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003658}
3659
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003660void intel_update_sprite_watermarks(struct drm_plane *plane,
3661 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003662 uint32_t sprite_width,
3663 uint32_t sprite_height,
3664 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003665 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003666{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003667 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003668
3669 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003670 dev_priv->display.update_sprite_wm(plane, crtc,
3671 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003672 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003673}
3674
Daniel Vetter92703882012-08-09 16:46:01 +02003675/**
3676 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003677 */
3678DEFINE_SPINLOCK(mchdev_lock);
3679
3680/* Global for IPS driver to get at the current i915 device. Protected by
3681 * mchdev_lock. */
3682static struct drm_i915_private *i915_mch_dev;
3683
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003684bool ironlake_set_drps(struct drm_device *dev, u8 val)
3685{
3686 struct drm_i915_private *dev_priv = dev->dev_private;
3687 u16 rgvswctl;
3688
Daniel Vetter92703882012-08-09 16:46:01 +02003689 assert_spin_locked(&mchdev_lock);
3690
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003691 rgvswctl = I915_READ16(MEMSWCTL);
3692 if (rgvswctl & MEMCTL_CMD_STS) {
3693 DRM_DEBUG("gpu busy, RCS change rejected\n");
3694 return false; /* still busy with another command */
3695 }
3696
3697 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3698 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3699 I915_WRITE16(MEMSWCTL, rgvswctl);
3700 POSTING_READ16(MEMSWCTL);
3701
3702 rgvswctl |= MEMCTL_CMD_STS;
3703 I915_WRITE16(MEMSWCTL, rgvswctl);
3704
3705 return true;
3706}
3707
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003708static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003709{
3710 struct drm_i915_private *dev_priv = dev->dev_private;
3711 u32 rgvmodectl = I915_READ(MEMMODECTL);
3712 u8 fmax, fmin, fstart, vstart;
3713
Daniel Vetter92703882012-08-09 16:46:01 +02003714 spin_lock_irq(&mchdev_lock);
3715
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003716 /* Enable temp reporting */
3717 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3718 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3719
3720 /* 100ms RC evaluation intervals */
3721 I915_WRITE(RCUPEI, 100000);
3722 I915_WRITE(RCDNEI, 100000);
3723
3724 /* Set max/min thresholds to 90ms and 80ms respectively */
3725 I915_WRITE(RCBMAXAVG, 90000);
3726 I915_WRITE(RCBMINAVG, 80000);
3727
3728 I915_WRITE(MEMIHYST, 1);
3729
3730 /* Set up min, max, and cur for interrupt handling */
3731 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3732 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3733 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3734 MEMMODE_FSTART_SHIFT;
3735
3736 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3737 PXVFREQ_PX_SHIFT;
3738
Daniel Vetter20e4d402012-08-08 23:35:39 +02003739 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3740 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003741
Daniel Vetter20e4d402012-08-08 23:35:39 +02003742 dev_priv->ips.max_delay = fstart;
3743 dev_priv->ips.min_delay = fmin;
3744 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003745
3746 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3747 fmax, fmin, fstart);
3748
3749 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3750
3751 /*
3752 * Interrupts will be enabled in ironlake_irq_postinstall
3753 */
3754
3755 I915_WRITE(VIDSTART, vstart);
3756 POSTING_READ(VIDSTART);
3757
3758 rgvmodectl |= MEMMODE_SWMODE_EN;
3759 I915_WRITE(MEMMODECTL, rgvmodectl);
3760
Daniel Vetter92703882012-08-09 16:46:01 +02003761 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003762 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003763 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003764
3765 ironlake_set_drps(dev, fstart);
3766
Daniel Vetter20e4d402012-08-08 23:35:39 +02003767 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003768 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003769 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3770 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003771 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003772
3773 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003774}
3775
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003776static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003777{
3778 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003779 u16 rgvswctl;
3780
3781 spin_lock_irq(&mchdev_lock);
3782
3783 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003784
3785 /* Ack interrupts, disable EFC interrupt */
3786 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3787 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3788 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3789 I915_WRITE(DEIIR, DE_PCU_EVENT);
3790 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3791
3792 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003793 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003794 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003795 rgvswctl |= MEMCTL_CMD_STS;
3796 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003797 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003798
Daniel Vetter92703882012-08-09 16:46:01 +02003799 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003800}
3801
Daniel Vetteracbe9472012-07-26 11:50:05 +02003802/* There's a funny hw issue where the hw returns all 0 when reading from
3803 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3804 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3805 * all limits and the gpu stuck at whatever frequency it is at atm).
3806 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003807static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003808{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003809 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003810
Daniel Vetter20b46e52012-07-26 11:16:14 +02003811 /* Only set the down limit when we've reached the lowest level to avoid
3812 * getting more interrupts, otherwise leave this clear. This prevents a
3813 * race in the hw when coming out of rc6: There's a tiny window where
3814 * the hw runs at the minimal clock before selecting the desired
3815 * frequency, if the down threshold expires in that window we will not
3816 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003817 limits = dev_priv->rps.max_freq_softlimit << 24;
3818 if (val <= dev_priv->rps.min_freq_softlimit)
3819 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003820
3821 return limits;
3822}
3823
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003824static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3825{
3826 int new_power;
3827
3828 new_power = dev_priv->rps.power;
3829 switch (dev_priv->rps.power) {
3830 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003831 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003832 new_power = BETWEEN;
3833 break;
3834
3835 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003836 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003837 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003838 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003839 new_power = HIGH_POWER;
3840 break;
3841
3842 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003843 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 +01003844 new_power = BETWEEN;
3845 break;
3846 }
3847 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003848 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003849 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003850 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003851 new_power = HIGH_POWER;
3852 if (new_power == dev_priv->rps.power)
3853 return;
3854
3855 /* Note the units here are not exactly 1us, but 1280ns. */
3856 switch (new_power) {
3857 case LOW_POWER:
3858 /* Upclock if more than 95% busy over 16ms */
3859 I915_WRITE(GEN6_RP_UP_EI, 12500);
3860 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3861
3862 /* Downclock if less than 85% busy over 32ms */
3863 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3864 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3865
3866 I915_WRITE(GEN6_RP_CONTROL,
3867 GEN6_RP_MEDIA_TURBO |
3868 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3869 GEN6_RP_MEDIA_IS_GFX |
3870 GEN6_RP_ENABLE |
3871 GEN6_RP_UP_BUSY_AVG |
3872 GEN6_RP_DOWN_IDLE_AVG);
3873 break;
3874
3875 case BETWEEN:
3876 /* Upclock if more than 90% busy over 13ms */
3877 I915_WRITE(GEN6_RP_UP_EI, 10250);
3878 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3879
3880 /* Downclock if less than 75% busy over 32ms */
3881 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3882 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3883
3884 I915_WRITE(GEN6_RP_CONTROL,
3885 GEN6_RP_MEDIA_TURBO |
3886 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3887 GEN6_RP_MEDIA_IS_GFX |
3888 GEN6_RP_ENABLE |
3889 GEN6_RP_UP_BUSY_AVG |
3890 GEN6_RP_DOWN_IDLE_AVG);
3891 break;
3892
3893 case HIGH_POWER:
3894 /* Upclock if more than 85% busy over 10ms */
3895 I915_WRITE(GEN6_RP_UP_EI, 8000);
3896 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3897
3898 /* Downclock if less than 60% busy over 32ms */
3899 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3900 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3901
3902 I915_WRITE(GEN6_RP_CONTROL,
3903 GEN6_RP_MEDIA_TURBO |
3904 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3905 GEN6_RP_MEDIA_IS_GFX |
3906 GEN6_RP_ENABLE |
3907 GEN6_RP_UP_BUSY_AVG |
3908 GEN6_RP_DOWN_IDLE_AVG);
3909 break;
3910 }
3911
3912 dev_priv->rps.power = new_power;
3913 dev_priv->rps.last_adj = 0;
3914}
3915
Chris Wilson2876ce72014-03-28 08:03:34 +00003916static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3917{
3918 u32 mask = 0;
3919
3920 if (val > dev_priv->rps.min_freq_softlimit)
3921 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3922 if (val < dev_priv->rps.max_freq_softlimit)
3923 mask |= GEN6_PM_RP_UP_THRESHOLD;
3924
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003925 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3926 mask &= dev_priv->pm_rps_events;
3927
Imre Deak59d02a12014-12-19 19:33:26 +02003928 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003929}
3930
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003931/* gen6_set_rps is called to update the frequency request, but should also be
3932 * called when the range (min_delay and max_delay) is modified so that we can
3933 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003934static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003935{
3936 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003937
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003938 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003939 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3940 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003941
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003942 /* min/max delay may still have been modified so be sure to
3943 * write the limits value.
3944 */
3945 if (val != dev_priv->rps.cur_freq) {
3946 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003947
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003948 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003949 I915_WRITE(GEN6_RPNSWREQ,
3950 HSW_FREQUENCY(val));
3951 else
3952 I915_WRITE(GEN6_RPNSWREQ,
3953 GEN6_FREQUENCY(val) |
3954 GEN6_OFFSET(0) |
3955 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003956 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003957
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003958 /* Make sure we continue to get interrupts
3959 * until we hit the minimum or maximum frequencies.
3960 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003961 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003962 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003963
Ben Widawskyd5570a72012-09-07 19:43:41 -07003964 POSTING_READ(GEN6_RPNSWREQ);
3965
Ben Widawskyb39fb292014-03-19 18:31:11 -07003966 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003967 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003968}
3969
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003970static void valleyview_set_rps(struct drm_device *dev, u8 val)
3971{
3972 struct drm_i915_private *dev_priv = dev->dev_private;
3973
3974 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3975 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3976 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3977
3978 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3979 "Odd GPU freq value\n"))
3980 val &= ~1;
3981
3982 if (val != dev_priv->rps.cur_freq)
3983 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3984
3985 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3986
3987 dev_priv->rps.cur_freq = val;
3988 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3989}
3990
Deepak S76c3552f2014-01-30 23:08:16 +05303991/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3992 *
3993 * * If Gfx is Idle, then
3994 * 1. Mask Turbo interrupts
3995 * 2. Bring up Gfx clock
3996 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3997 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3998 * 5. Unmask Turbo interrupts
3999*/
4000static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4001{
Deepak S5549d252014-06-28 11:26:11 +05304002 struct drm_device *dev = dev_priv->dev;
4003
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004004 /* CHV and latest VLV don't need to force the gfx clock */
4005 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05304006 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4007 return;
4008 }
4009
Deepak S76c3552f2014-01-30 23:08:16 +05304010 /*
4011 * When we are idle. Drop to min voltage state.
4012 */
4013
Ben Widawskyb39fb292014-03-19 18:31:11 -07004014 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05304015 return;
4016
4017 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02004018 I915_WRITE(GEN6_PMINTRMSK,
4019 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05304020
Imre Deak650ad972014-04-18 16:35:02 +03004021 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05304022
Ben Widawskyb39fb292014-03-19 18:31:11 -07004023 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05304024
4025 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07004026 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05304027
4028 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02004029 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05304030 DRM_ERROR("timed out waiting for Punit\n");
4031
Imre Deak650ad972014-04-18 16:35:02 +03004032 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05304033
Chris Wilson2876ce72014-03-28 08:03:34 +00004034 I915_WRITE(GEN6_PMINTRMSK,
4035 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05304036}
4037
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004038void gen6_rps_idle(struct drm_i915_private *dev_priv)
4039{
Damien Lespiau691bb712013-12-12 14:36:36 +00004040 struct drm_device *dev = dev_priv->dev;
4041
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004042 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004043 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004044 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304045 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004046 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07004047 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004048 dev_priv->rps.last_adj = 0;
4049 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004050 mutex_unlock(&dev_priv->rps.hw_lock);
4051}
4052
4053void gen6_rps_boost(struct drm_i915_private *dev_priv)
4054{
4055 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004056 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004057 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004058 dev_priv->rps.last_adj = 0;
4059 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004060 mutex_unlock(&dev_priv->rps.hw_lock);
4061}
4062
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004063void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004064{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004065 if (IS_VALLEYVIEW(dev))
4066 valleyview_set_rps(dev, val);
4067 else
4068 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004069}
4070
Zhe Wang20e49362014-11-04 17:07:05 +00004071static void gen9_disable_rps(struct drm_device *dev)
4072{
4073 struct drm_i915_private *dev_priv = dev->dev_private;
4074
4075 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004076 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004077}
4078
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004079static void gen6_disable_rps(struct drm_device *dev)
4080{
4081 struct drm_i915_private *dev_priv = dev->dev_private;
4082
4083 I915_WRITE(GEN6_RC_CONTROL, 0);
4084 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004085}
4086
Deepak S38807742014-05-23 21:00:15 +05304087static void cherryview_disable_rps(struct drm_device *dev)
4088{
4089 struct drm_i915_private *dev_priv = dev->dev_private;
4090
4091 I915_WRITE(GEN6_RC_CONTROL, 0);
4092}
4093
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004094static void valleyview_disable_rps(struct drm_device *dev)
4095{
4096 struct drm_i915_private *dev_priv = dev->dev_private;
4097
Deepak S98a2e5f2014-08-18 10:35:27 -07004098 /* we're doing forcewake before Disabling RC6,
4099 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004100 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004101
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004102 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004103
Mika Kuoppala59bad942015-01-16 11:34:40 +02004104 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004105}
4106
Ben Widawskydc39fff2013-10-18 12:32:07 -07004107static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4108{
Imre Deak91ca6892014-04-14 20:24:25 +03004109 if (IS_VALLEYVIEW(dev)) {
4110 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4111 mode = GEN6_RC_CTL_RC6_ENABLE;
4112 else
4113 mode = 0;
4114 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004115 if (HAS_RC6p(dev))
4116 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4117 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4118 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4119 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4120
4121 else
4122 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4123 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004124}
4125
Imre Deake6069ca2014-04-18 16:01:02 +03004126static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004127{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004128 /* No RC6 before Ironlake */
4129 if (INTEL_INFO(dev)->gen < 5)
4130 return 0;
4131
Imre Deake6069ca2014-04-18 16:01:02 +03004132 /* RC6 is only on Ironlake mobile not on desktop */
4133 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4134 return 0;
4135
Daniel Vetter456470e2012-08-08 23:35:40 +02004136 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004137 if (enable_rc6 >= 0) {
4138 int mask;
4139
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004140 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004141 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4142 INTEL_RC6pp_ENABLE;
4143 else
4144 mask = INTEL_RC6_ENABLE;
4145
4146 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004147 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4148 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004149
4150 return enable_rc6 & mask;
4151 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004152
Chris Wilson6567d742012-11-10 10:00:06 +00004153 /* Disable RC6 on Ironlake */
4154 if (INTEL_INFO(dev)->gen == 5)
4155 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004156
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004157 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004158 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004159
4160 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004161}
4162
Imre Deake6069ca2014-04-18 16:01:02 +03004163int intel_enable_rc6(const struct drm_device *dev)
4164{
4165 return i915.enable_rc6;
4166}
4167
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004168static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004169{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 uint32_t rp_state_cap;
4172 u32 ddcc_status = 0;
4173 int ret;
4174
4175 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004176 /* All of these values are in units of 50MHz */
4177 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004178 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004179 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004180 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004181 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004182 /* hw_max = RP0 until we check for overclocking */
4183 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4184
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004185 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4186 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4187 ret = sandybridge_pcode_read(dev_priv,
4188 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4189 &ddcc_status);
4190 if (0 == ret)
4191 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004192 clamp_t(u8,
4193 ((ddcc_status >> 8) & 0xff),
4194 dev_priv->rps.min_freq,
4195 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004196 }
4197
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004198 /* Preserve min/max settings in case of re-init */
4199 if (dev_priv->rps.max_freq_softlimit == 0)
4200 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4201
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004202 if (dev_priv->rps.min_freq_softlimit == 0) {
4203 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4204 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004205 /* max(RPe, 450 MHz) */
4206 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004207 else
4208 dev_priv->rps.min_freq_softlimit =
4209 dev_priv->rps.min_freq;
4210 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004211}
4212
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004213/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004214static void gen9_enable_rps(struct drm_device *dev)
4215{
4216 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004217
4218 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4219
Damien Lespiauba1c5542015-01-16 18:07:26 +00004220 gen6_init_rps_frequencies(dev);
4221
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004222 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4223 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4224
4225 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4226 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4227 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4228 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4229 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4230 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4231 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4232 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4233 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4234 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4235 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4236 GEN6_RP_DOWN_IDLE_AVG);
4237
4238 gen6_enable_rps_interrupts(dev);
4239
4240 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4241}
4242
4243static void gen9_enable_rc6(struct drm_device *dev)
4244{
4245 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004246 struct intel_engine_cs *ring;
4247 uint32_t rc6_mask = 0;
4248 int unused;
4249
4250 /* 1a: Software RC state - RC0 */
4251 I915_WRITE(GEN6_RC_STATE, 0);
4252
4253 /* 1b: Get forcewake during program sequence. Although the driver
4254 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004255 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004256
4257 /* 2a: Disable RC states. */
4258 I915_WRITE(GEN6_RC_CONTROL, 0);
4259
4260 /* 2b: Program RC6 thresholds.*/
4261 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4262 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4263 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4264 for_each_ring(ring, dev_priv, unused)
4265 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4266 I915_WRITE(GEN6_RC_SLEEP, 0);
4267 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4268
Zhe Wang38c23522015-01-20 12:23:04 +00004269 /* 2c: Program Coarse Power Gating Policies. */
4270 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4271 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4272
Zhe Wang20e49362014-11-04 17:07:05 +00004273 /* 3a: Enable RC6 */
4274 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4275 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4276 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4277 "on" : "off");
4278 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4279 GEN6_RC_CTL_EI_MODE(1) |
4280 rc6_mask);
4281
Zhe Wang38c23522015-01-20 12:23:04 +00004282 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4283 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4284
Mika Kuoppala59bad942015-01-16 11:34:40 +02004285 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004286
4287}
4288
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004289static void gen8_enable_rps(struct drm_device *dev)
4290{
4291 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004292 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004293 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004294 int unused;
4295
4296 /* 1a: Software RC state - RC0 */
4297 I915_WRITE(GEN6_RC_STATE, 0);
4298
4299 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4300 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004301 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004302
4303 /* 2a: Disable RC states. */
4304 I915_WRITE(GEN6_RC_CONTROL, 0);
4305
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004306 /* Initialize rps frequencies */
4307 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004308
4309 /* 2b: Program RC6 thresholds.*/
4310 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4311 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4312 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4313 for_each_ring(ring, dev_priv, unused)
4314 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4315 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004316 if (IS_BROADWELL(dev))
4317 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4318 else
4319 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004320
4321 /* 3: Enable RC6 */
4322 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4323 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004324 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004325 if (IS_BROADWELL(dev))
4326 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4327 GEN7_RC_CTL_TO_MODE |
4328 rc6_mask);
4329 else
4330 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4331 GEN6_RC_CTL_EI_MODE(1) |
4332 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004333
4334 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004335 I915_WRITE(GEN6_RPNSWREQ,
4336 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4337 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4338 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004339 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4340 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004341
Daniel Vetter7526ed72014-09-29 15:07:19 +02004342 /* Docs recommend 900MHz, and 300 MHz respectively */
4343 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4344 dev_priv->rps.max_freq_softlimit << 24 |
4345 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004346
Daniel Vetter7526ed72014-09-29 15:07:19 +02004347 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4348 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4349 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4350 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004351
Daniel Vetter7526ed72014-09-29 15:07:19 +02004352 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004353
4354 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004355 I915_WRITE(GEN6_RP_CONTROL,
4356 GEN6_RP_MEDIA_TURBO |
4357 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4358 GEN6_RP_MEDIA_IS_GFX |
4359 GEN6_RP_ENABLE |
4360 GEN6_RP_UP_BUSY_AVG |
4361 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004362
Daniel Vetter7526ed72014-09-29 15:07:19 +02004363 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004364
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004365 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4366 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004367
Mika Kuoppala59bad942015-01-16 11:34:40 +02004368 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004369}
4370
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004371static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004372{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004373 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004374 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004375 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004376 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004377 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004378 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004379
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004380 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004381
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004382 /* Here begins a magic sequence of register writes to enable
4383 * auto-downclocking.
4384 *
4385 * Perhaps there might be some value in exposing these to
4386 * userspace...
4387 */
4388 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004389
4390 /* Clear the DBG now so we don't confuse earlier errors */
4391 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4392 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4393 I915_WRITE(GTFIFODBG, gtfifodbg);
4394 }
4395
Mika Kuoppala59bad942015-01-16 11:34:40 +02004396 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004397
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004398 /* Initialize rps frequencies */
4399 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004400
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004401 /* disable the counters and set deterministic thresholds */
4402 I915_WRITE(GEN6_RC_CONTROL, 0);
4403
4404 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4405 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4406 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4407 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4408 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4409
Chris Wilsonb4519512012-05-11 14:29:30 +01004410 for_each_ring(ring, dev_priv, i)
4411 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004412
4413 I915_WRITE(GEN6_RC_SLEEP, 0);
4414 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004415 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004416 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4417 else
4418 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004419 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004420 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4421
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004422 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004423 rc6_mode = intel_enable_rc6(dev_priv->dev);
4424 if (rc6_mode & INTEL_RC6_ENABLE)
4425 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4426
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004427 /* We don't use those on Haswell */
4428 if (!IS_HASWELL(dev)) {
4429 if (rc6_mode & INTEL_RC6p_ENABLE)
4430 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004431
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004432 if (rc6_mode & INTEL_RC6pp_ENABLE)
4433 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4434 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004435
Ben Widawskydc39fff2013-10-18 12:32:07 -07004436 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004437
4438 I915_WRITE(GEN6_RC_CONTROL,
4439 rc6_mask |
4440 GEN6_RC_CTL_EI_MODE(1) |
4441 GEN6_RC_CTL_HW_ENABLE);
4442
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004443 /* Power down if completely idle for over 50ms */
4444 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004445 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004446
Ben Widawsky42c05262012-09-26 10:34:00 -07004447 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004448 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004449 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004450
4451 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4452 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4453 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004454 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004455 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004456 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004457 }
4458
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004459 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004460 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004461
Ben Widawsky31643d52012-09-26 10:34:01 -07004462 rc6vids = 0;
4463 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4464 if (IS_GEN6(dev) && ret) {
4465 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4466 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4467 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4468 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4469 rc6vids &= 0xffff00;
4470 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4471 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4472 if (ret)
4473 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4474 }
4475
Mika Kuoppala59bad942015-01-16 11:34:40 +02004476 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004477}
4478
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004479static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004480{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004481 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004482 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004483 unsigned int gpu_freq;
4484 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004485 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004486 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004487
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004488 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004489
Ben Widawskyeda79642013-10-07 17:15:48 -03004490 policy = cpufreq_cpu_get(0);
4491 if (policy) {
4492 max_ia_freq = policy->cpuinfo.max_freq;
4493 cpufreq_cpu_put(policy);
4494 } else {
4495 /*
4496 * Default to measured freq if none found, PCU will ensure we
4497 * don't go over
4498 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004499 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004500 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004501
4502 /* Convert from kHz to MHz */
4503 max_ia_freq /= 1000;
4504
Ben Widawsky153b4b952013-10-22 22:05:09 -07004505 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004506 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4507 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004508
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004509 /*
4510 * For each potential GPU frequency, load a ring frequency we'd like
4511 * to use for memory access. We do this by specifying the IA frequency
4512 * the PCU should use as a reference to determine the ring frequency.
4513 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004514 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004515 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004516 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004517 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004518
Ben Widawsky46c764d2013-11-02 21:07:49 -07004519 if (INTEL_INFO(dev)->gen >= 8) {
4520 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4521 ring_freq = max(min_ring_freq, gpu_freq);
4522 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004523 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004524 ring_freq = max(min_ring_freq, ring_freq);
4525 /* leave ia_freq as the default, chosen by cpufreq */
4526 } else {
4527 /* On older processors, there is no separate ring
4528 * clock domain, so in order to boost the bandwidth
4529 * of the ring, we need to upclock the CPU (ia_freq).
4530 *
4531 * For GPU frequencies less than 750MHz,
4532 * just use the lowest ring freq.
4533 */
4534 if (gpu_freq < min_freq)
4535 ia_freq = 800;
4536 else
4537 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4538 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4539 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004540
Ben Widawsky42c05262012-09-26 10:34:00 -07004541 sandybridge_pcode_write(dev_priv,
4542 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004543 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4544 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4545 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004546 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004547}
4548
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004549void gen6_update_ring_freq(struct drm_device *dev)
4550{
4551 struct drm_i915_private *dev_priv = dev->dev_private;
4552
4553 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4554 return;
4555
4556 mutex_lock(&dev_priv->rps.hw_lock);
4557 __gen6_update_ring_freq(dev);
4558 mutex_unlock(&dev_priv->rps.hw_lock);
4559}
4560
Ville Syrjälä03af2042014-06-28 02:03:53 +03004561static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304562{
Deepak S095acd52015-01-17 11:05:59 +05304563 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304564 u32 val, rp0;
4565
Deepak S095acd52015-01-17 11:05:59 +05304566 if (dev->pdev->revision >= 0x20) {
4567 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304568
Deepak S095acd52015-01-17 11:05:59 +05304569 switch (INTEL_INFO(dev)->eu_total) {
4570 case 8:
4571 /* (2 * 4) config */
4572 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4573 break;
4574 case 12:
4575 /* (2 * 6) config */
4576 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4577 break;
4578 case 16:
4579 /* (2 * 8) config */
4580 default:
4581 /* Setting (2 * 8) Min RP0 for any other combination */
4582 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4583 break;
4584 }
4585 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4586 } else {
4587 /* For pre-production hardware */
4588 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4589 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4590 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4591 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304592 return rp0;
4593}
4594
4595static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4596{
4597 u32 val, rpe;
4598
4599 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4600 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4601
4602 return rpe;
4603}
4604
Deepak S7707df42014-07-12 18:46:14 +05304605static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4606{
Deepak S095acd52015-01-17 11:05:59 +05304607 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304608 u32 val, rp1;
4609
Deepak S095acd52015-01-17 11:05:59 +05304610 if (dev->pdev->revision >= 0x20) {
4611 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4612 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4613 } else {
4614 /* For pre-production hardware */
4615 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4616 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4617 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4618 }
Deepak S7707df42014-07-12 18:46:14 +05304619 return rp1;
4620}
4621
Ville Syrjälä03af2042014-06-28 02:03:53 +03004622static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304623{
Deepak S095acd52015-01-17 11:05:59 +05304624 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304625 u32 val, rpn;
4626
Deepak S095acd52015-01-17 11:05:59 +05304627 if (dev->pdev->revision >= 0x20) {
4628 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4629 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4630 FB_GFX_FREQ_FUSE_MASK);
4631 } else { /* For pre-production hardware */
4632 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4633 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4634 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4635 }
4636
Deepak S2b6b3a02014-05-27 15:59:30 +05304637 return rpn;
4638}
4639
Deepak Sf8f2b002014-07-10 13:16:21 +05304640static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4641{
4642 u32 val, rp1;
4643
4644 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4645
4646 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4647
4648 return rp1;
4649}
4650
Ville Syrjälä03af2042014-06-28 02:03:53 +03004651static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004652{
4653 u32 val, rp0;
4654
Jani Nikula64936252013-05-22 15:36:20 +03004655 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004656
4657 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4658 /* Clamp to max */
4659 rp0 = min_t(u32, rp0, 0xea);
4660
4661 return rp0;
4662}
4663
4664static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4665{
4666 u32 val, rpe;
4667
Jani Nikula64936252013-05-22 15:36:20 +03004668 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004669 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004670 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004671 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4672
4673 return rpe;
4674}
4675
Ville Syrjälä03af2042014-06-28 02:03:53 +03004676static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004677{
Jani Nikula64936252013-05-22 15:36:20 +03004678 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004679}
4680
Imre Deakae484342014-03-31 15:10:44 +03004681/* Check that the pctx buffer wasn't move under us. */
4682static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4683{
4684 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4685
4686 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4687 dev_priv->vlv_pctx->stolen->start);
4688}
4689
Deepak S38807742014-05-23 21:00:15 +05304690
4691/* Check that the pcbr address is not empty. */
4692static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4693{
4694 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4695
4696 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4697}
4698
4699static void cherryview_setup_pctx(struct drm_device *dev)
4700{
4701 struct drm_i915_private *dev_priv = dev->dev_private;
4702 unsigned long pctx_paddr, paddr;
4703 struct i915_gtt *gtt = &dev_priv->gtt;
4704 u32 pcbr;
4705 int pctx_size = 32*1024;
4706
4707 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4708
4709 pcbr = I915_READ(VLV_PCBR);
4710 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004711 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304712 paddr = (dev_priv->mm.stolen_base +
4713 (gtt->stolen_size - pctx_size));
4714
4715 pctx_paddr = (paddr & (~4095));
4716 I915_WRITE(VLV_PCBR, pctx_paddr);
4717 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004718
4719 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304720}
4721
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004722static void valleyview_setup_pctx(struct drm_device *dev)
4723{
4724 struct drm_i915_private *dev_priv = dev->dev_private;
4725 struct drm_i915_gem_object *pctx;
4726 unsigned long pctx_paddr;
4727 u32 pcbr;
4728 int pctx_size = 24*1024;
4729
Imre Deak17b0c1f2014-02-11 21:39:06 +02004730 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4731
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004732 pcbr = I915_READ(VLV_PCBR);
4733 if (pcbr) {
4734 /* BIOS set it up already, grab the pre-alloc'd space */
4735 int pcbr_offset;
4736
4737 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4738 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4739 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004740 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004741 pctx_size);
4742 goto out;
4743 }
4744
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004745 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4746
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004747 /*
4748 * From the Gunit register HAS:
4749 * The Gfx driver is expected to program this register and ensure
4750 * proper allocation within Gfx stolen memory. For example, this
4751 * register should be programmed such than the PCBR range does not
4752 * overlap with other ranges, such as the frame buffer, protected
4753 * memory, or any other relevant ranges.
4754 */
4755 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4756 if (!pctx) {
4757 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4758 return;
4759 }
4760
4761 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4762 I915_WRITE(VLV_PCBR, pctx_paddr);
4763
4764out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004765 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004766 dev_priv->vlv_pctx = pctx;
4767}
4768
Imre Deakae484342014-03-31 15:10:44 +03004769static void valleyview_cleanup_pctx(struct drm_device *dev)
4770{
4771 struct drm_i915_private *dev_priv = dev->dev_private;
4772
4773 if (WARN_ON(!dev_priv->vlv_pctx))
4774 return;
4775
4776 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4777 dev_priv->vlv_pctx = NULL;
4778}
4779
Imre Deak4e805192014-04-14 20:24:41 +03004780static void valleyview_init_gt_powersave(struct drm_device *dev)
4781{
4782 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004783 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004784
4785 valleyview_setup_pctx(dev);
4786
4787 mutex_lock(&dev_priv->rps.hw_lock);
4788
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004789 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4790 switch ((val >> 6) & 3) {
4791 case 0:
4792 case 1:
4793 dev_priv->mem_freq = 800;
4794 break;
4795 case 2:
4796 dev_priv->mem_freq = 1066;
4797 break;
4798 case 3:
4799 dev_priv->mem_freq = 1333;
4800 break;
4801 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004802 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004803
Imre Deak4e805192014-04-14 20:24:41 +03004804 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4805 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4806 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004807 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004808 dev_priv->rps.max_freq);
4809
4810 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4811 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004812 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004813 dev_priv->rps.efficient_freq);
4814
Deepak Sf8f2b002014-07-10 13:16:21 +05304815 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4816 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004817 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304818 dev_priv->rps.rp1_freq);
4819
Imre Deak4e805192014-04-14 20:24:41 +03004820 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4821 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004822 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004823 dev_priv->rps.min_freq);
4824
4825 /* Preserve min/max settings in case of re-init */
4826 if (dev_priv->rps.max_freq_softlimit == 0)
4827 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4828
4829 if (dev_priv->rps.min_freq_softlimit == 0)
4830 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4831
4832 mutex_unlock(&dev_priv->rps.hw_lock);
4833}
4834
Deepak S38807742014-05-23 21:00:15 +05304835static void cherryview_init_gt_powersave(struct drm_device *dev)
4836{
Deepak S2b6b3a02014-05-27 15:59:30 +05304837 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004838 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304839
Deepak S38807742014-05-23 21:00:15 +05304840 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304841
4842 mutex_lock(&dev_priv->rps.hw_lock);
4843
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004844 mutex_lock(&dev_priv->dpio_lock);
4845 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4846 mutex_unlock(&dev_priv->dpio_lock);
4847
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004848 switch ((val >> 2) & 0x7) {
4849 case 0:
4850 case 1:
4851 dev_priv->rps.cz_freq = 200;
4852 dev_priv->mem_freq = 1600;
4853 break;
4854 case 2:
4855 dev_priv->rps.cz_freq = 267;
4856 dev_priv->mem_freq = 1600;
4857 break;
4858 case 3:
4859 dev_priv->rps.cz_freq = 333;
4860 dev_priv->mem_freq = 2000;
4861 break;
4862 case 4:
4863 dev_priv->rps.cz_freq = 320;
4864 dev_priv->mem_freq = 1600;
4865 break;
4866 case 5:
4867 dev_priv->rps.cz_freq = 400;
4868 dev_priv->mem_freq = 1600;
4869 break;
4870 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004871 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004872
Deepak S2b6b3a02014-05-27 15:59:30 +05304873 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4874 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4875 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004876 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304877 dev_priv->rps.max_freq);
4878
4879 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4880 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004881 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304882 dev_priv->rps.efficient_freq);
4883
Deepak S7707df42014-07-12 18:46:14 +05304884 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4885 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004886 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304887 dev_priv->rps.rp1_freq);
4888
Deepak S2b6b3a02014-05-27 15:59:30 +05304889 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4890 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004891 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304892 dev_priv->rps.min_freq);
4893
Ville Syrjälä1c147622014-08-18 14:42:43 +03004894 WARN_ONCE((dev_priv->rps.max_freq |
4895 dev_priv->rps.efficient_freq |
4896 dev_priv->rps.rp1_freq |
4897 dev_priv->rps.min_freq) & 1,
4898 "Odd GPU freq values\n");
4899
Deepak S2b6b3a02014-05-27 15:59:30 +05304900 /* Preserve min/max settings in case of re-init */
4901 if (dev_priv->rps.max_freq_softlimit == 0)
4902 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4903
4904 if (dev_priv->rps.min_freq_softlimit == 0)
4905 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4906
4907 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304908}
4909
Imre Deak4e805192014-04-14 20:24:41 +03004910static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4911{
4912 valleyview_cleanup_pctx(dev);
4913}
4914
Deepak S38807742014-05-23 21:00:15 +05304915static void cherryview_enable_rps(struct drm_device *dev)
4916{
4917 struct drm_i915_private *dev_priv = dev->dev_private;
4918 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304919 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304920 int i;
4921
4922 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4923
4924 gtfifodbg = I915_READ(GTFIFODBG);
4925 if (gtfifodbg) {
4926 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4927 gtfifodbg);
4928 I915_WRITE(GTFIFODBG, gtfifodbg);
4929 }
4930
4931 cherryview_check_pctx(dev_priv);
4932
4933 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4934 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004935 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304936
Ville Syrjälä160614a2015-01-19 13:50:47 +02004937 /* Disable RC states. */
4938 I915_WRITE(GEN6_RC_CONTROL, 0);
4939
Deepak S38807742014-05-23 21:00:15 +05304940 /* 2a: Program RC6 thresholds.*/
4941 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4942 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4943 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4944
4945 for_each_ring(ring, dev_priv, i)
4946 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4947 I915_WRITE(GEN6_RC_SLEEP, 0);
4948
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004949 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4950 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304951
4952 /* allows RC6 residency counter to work */
4953 I915_WRITE(VLV_COUNTER_CONTROL,
4954 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4955 VLV_MEDIA_RC6_COUNT_EN |
4956 VLV_RENDER_RC6_COUNT_EN));
4957
4958 /* For now we assume BIOS is allocating and populating the PCBR */
4959 pcbr = I915_READ(VLV_PCBR);
4960
Deepak S38807742014-05-23 21:00:15 +05304961 /* 3: Enable RC6 */
4962 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4963 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004964 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304965
4966 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4967
Deepak S2b6b3a02014-05-27 15:59:30 +05304968 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004969 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304970 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4971 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4972 I915_WRITE(GEN6_RP_UP_EI, 66000);
4973 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4974
4975 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4976
4977 /* 5: Enable RPS */
4978 I915_WRITE(GEN6_RP_CONTROL,
4979 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004980 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304981 GEN6_RP_ENABLE |
4982 GEN6_RP_UP_BUSY_AVG |
4983 GEN6_RP_DOWN_IDLE_AVG);
4984
4985 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4986
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004987 /* RPS code assumes GPLL is used */
4988 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4989
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004990 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304991 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4992
4993 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4994 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004995 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304996 dev_priv->rps.cur_freq);
4997
4998 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004999 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305000 dev_priv->rps.efficient_freq);
5001
5002 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5003
Mika Kuoppala59bad942015-01-16 11:34:40 +02005004 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305005}
5006
Jesse Barnes0a073b82013-04-17 15:54:58 -07005007static void valleyview_enable_rps(struct drm_device *dev)
5008{
5009 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005010 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005011 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005012 int i;
5013
5014 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5015
Imre Deakae484342014-03-31 15:10:44 +03005016 valleyview_check_pctx(dev_priv);
5017
Jesse Barnes0a073b82013-04-17 15:54:58 -07005018 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005019 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5020 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005021 I915_WRITE(GTFIFODBG, gtfifodbg);
5022 }
5023
Deepak Sc8d9a592013-11-23 14:55:42 +05305024 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005025 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005026
Ville Syrjälä160614a2015-01-19 13:50:47 +02005027 /* Disable RC states. */
5028 I915_WRITE(GEN6_RC_CONTROL, 0);
5029
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005030 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005031 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5032 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5033 I915_WRITE(GEN6_RP_UP_EI, 66000);
5034 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5035
5036 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5037
5038 I915_WRITE(GEN6_RP_CONTROL,
5039 GEN6_RP_MEDIA_TURBO |
5040 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5041 GEN6_RP_MEDIA_IS_GFX |
5042 GEN6_RP_ENABLE |
5043 GEN6_RP_UP_BUSY_AVG |
5044 GEN6_RP_DOWN_IDLE_CONT);
5045
5046 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5047 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5048 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5049
5050 for_each_ring(ring, dev_priv, i)
5051 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5052
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005053 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005054
5055 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005056 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005057 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5058 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005059 VLV_MEDIA_RC6_COUNT_EN |
5060 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005061
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005062 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005063 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005064
5065 intel_print_rc6_info(dev, rc6_mode);
5066
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005067 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005068
Jani Nikula64936252013-05-22 15:36:20 +03005069 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005070
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005071 /* RPS code assumes GPLL is used */
5072 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5073
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005074 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07005075 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5076
Ben Widawskyb39fb292014-03-19 18:31:11 -07005077 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005078 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005079 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005080 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005081
Ville Syrjälä73008b92013-06-25 19:21:01 +03005082 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005083 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005084 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005085
Ben Widawskyb39fb292014-03-19 18:31:11 -07005086 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005087
Mika Kuoppala59bad942015-01-16 11:34:40 +02005088 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005089}
5090
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005091static unsigned long intel_pxfreq(u32 vidfreq)
5092{
5093 unsigned long freq;
5094 int div = (vidfreq & 0x3f0000) >> 16;
5095 int post = (vidfreq & 0x3000) >> 12;
5096 int pre = (vidfreq & 0x7);
5097
5098 if (!pre)
5099 return 0;
5100
5101 freq = ((div * 133333) / ((1<<post) * pre));
5102
5103 return freq;
5104}
5105
Daniel Vettereb48eb02012-04-26 23:28:12 +02005106static const struct cparams {
5107 u16 i;
5108 u16 t;
5109 u16 m;
5110 u16 c;
5111} cparams[] = {
5112 { 1, 1333, 301, 28664 },
5113 { 1, 1066, 294, 24460 },
5114 { 1, 800, 294, 25192 },
5115 { 0, 1333, 276, 27605 },
5116 { 0, 1066, 276, 27605 },
5117 { 0, 800, 231, 23784 },
5118};
5119
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005120static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005121{
5122 u64 total_count, diff, ret;
5123 u32 count1, count2, count3, m = 0, c = 0;
5124 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5125 int i;
5126
Daniel Vetter02d71952012-08-09 16:44:54 +02005127 assert_spin_locked(&mchdev_lock);
5128
Daniel Vetter20e4d402012-08-08 23:35:39 +02005129 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005130
5131 /* Prevent division-by-zero if we are asking too fast.
5132 * Also, we don't get interesting results if we are polling
5133 * faster than once in 10ms, so just return the saved value
5134 * in such cases.
5135 */
5136 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005137 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005138
5139 count1 = I915_READ(DMIEC);
5140 count2 = I915_READ(DDREC);
5141 count3 = I915_READ(CSIEC);
5142
5143 total_count = count1 + count2 + count3;
5144
5145 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005146 if (total_count < dev_priv->ips.last_count1) {
5147 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005148 diff += total_count;
5149 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005150 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005151 }
5152
5153 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005154 if (cparams[i].i == dev_priv->ips.c_m &&
5155 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005156 m = cparams[i].m;
5157 c = cparams[i].c;
5158 break;
5159 }
5160 }
5161
5162 diff = div_u64(diff, diff1);
5163 ret = ((m * diff) + c);
5164 ret = div_u64(ret, 10);
5165
Daniel Vetter20e4d402012-08-08 23:35:39 +02005166 dev_priv->ips.last_count1 = total_count;
5167 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005168
Daniel Vetter20e4d402012-08-08 23:35:39 +02005169 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005170
5171 return ret;
5172}
5173
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005174unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5175{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005176 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005177 unsigned long val;
5178
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005179 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005180 return 0;
5181
5182 spin_lock_irq(&mchdev_lock);
5183
5184 val = __i915_chipset_val(dev_priv);
5185
5186 spin_unlock_irq(&mchdev_lock);
5187
5188 return val;
5189}
5190
Daniel Vettereb48eb02012-04-26 23:28:12 +02005191unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5192{
5193 unsigned long m, x, b;
5194 u32 tsfs;
5195
5196 tsfs = I915_READ(TSFS);
5197
5198 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5199 x = I915_READ8(TR1);
5200
5201 b = tsfs & TSFS_INTR_MASK;
5202
5203 return ((m * x) / 127) - b;
5204}
5205
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005206static int _pxvid_to_vd(u8 pxvid)
5207{
5208 if (pxvid == 0)
5209 return 0;
5210
5211 if (pxvid >= 8 && pxvid < 31)
5212 pxvid = 31;
5213
5214 return (pxvid + 2) * 125;
5215}
5216
5217static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005218{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005219 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005220 const int vd = _pxvid_to_vd(pxvid);
5221 const int vm = vd - 1125;
5222
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005223 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005224 return vm > 0 ? vm : 0;
5225
5226 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005227}
5228
Daniel Vetter02d71952012-08-09 16:44:54 +02005229static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005230{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005231 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005232 u32 count;
5233
Daniel Vetter02d71952012-08-09 16:44:54 +02005234 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005235
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005236 now = ktime_get_raw_ns();
5237 diffms = now - dev_priv->ips.last_time2;
5238 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005239
5240 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005241 if (!diffms)
5242 return;
5243
5244 count = I915_READ(GFXEC);
5245
Daniel Vetter20e4d402012-08-08 23:35:39 +02005246 if (count < dev_priv->ips.last_count2) {
5247 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005248 diff += count;
5249 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005250 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005251 }
5252
Daniel Vetter20e4d402012-08-08 23:35:39 +02005253 dev_priv->ips.last_count2 = count;
5254 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005255
5256 /* More magic constants... */
5257 diff = diff * 1181;
5258 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005259 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005260}
5261
Daniel Vetter02d71952012-08-09 16:44:54 +02005262void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5263{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005264 struct drm_device *dev = dev_priv->dev;
5265
5266 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005267 return;
5268
Daniel Vetter92703882012-08-09 16:46:01 +02005269 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005270
5271 __i915_update_gfx_val(dev_priv);
5272
Daniel Vetter92703882012-08-09 16:46:01 +02005273 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005274}
5275
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005276static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005277{
5278 unsigned long t, corr, state1, corr2, state2;
5279 u32 pxvid, ext_v;
5280
Daniel Vetter02d71952012-08-09 16:44:54 +02005281 assert_spin_locked(&mchdev_lock);
5282
Ben Widawskyb39fb292014-03-19 18:31:11 -07005283 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005284 pxvid = (pxvid >> 24) & 0x7f;
5285 ext_v = pvid_to_extvid(dev_priv, pxvid);
5286
5287 state1 = ext_v;
5288
5289 t = i915_mch_val(dev_priv);
5290
5291 /* Revel in the empirically derived constants */
5292
5293 /* Correction factor in 1/100000 units */
5294 if (t > 80)
5295 corr = ((t * 2349) + 135940);
5296 else if (t >= 50)
5297 corr = ((t * 964) + 29317);
5298 else /* < 50 */
5299 corr = ((t * 301) + 1004);
5300
5301 corr = corr * ((150142 * state1) / 10000 - 78642);
5302 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005303 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005304
5305 state2 = (corr2 * state1) / 10000;
5306 state2 /= 100; /* convert to mW */
5307
Daniel Vetter02d71952012-08-09 16:44:54 +02005308 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005309
Daniel Vetter20e4d402012-08-08 23:35:39 +02005310 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005311}
5312
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005313unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5314{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005315 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005316 unsigned long val;
5317
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005318 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005319 return 0;
5320
5321 spin_lock_irq(&mchdev_lock);
5322
5323 val = __i915_gfx_val(dev_priv);
5324
5325 spin_unlock_irq(&mchdev_lock);
5326
5327 return val;
5328}
5329
Daniel Vettereb48eb02012-04-26 23:28:12 +02005330/**
5331 * i915_read_mch_val - return value for IPS use
5332 *
5333 * Calculate and return a value for the IPS driver to use when deciding whether
5334 * we have thermal and power headroom to increase CPU or GPU power budget.
5335 */
5336unsigned long i915_read_mch_val(void)
5337{
5338 struct drm_i915_private *dev_priv;
5339 unsigned long chipset_val, graphics_val, ret = 0;
5340
Daniel Vetter92703882012-08-09 16:46:01 +02005341 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005342 if (!i915_mch_dev)
5343 goto out_unlock;
5344 dev_priv = i915_mch_dev;
5345
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005346 chipset_val = __i915_chipset_val(dev_priv);
5347 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005348
5349 ret = chipset_val + graphics_val;
5350
5351out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005352 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005353
5354 return ret;
5355}
5356EXPORT_SYMBOL_GPL(i915_read_mch_val);
5357
5358/**
5359 * i915_gpu_raise - raise GPU frequency limit
5360 *
5361 * Raise the limit; IPS indicates we have thermal headroom.
5362 */
5363bool i915_gpu_raise(void)
5364{
5365 struct drm_i915_private *dev_priv;
5366 bool ret = true;
5367
Daniel Vetter92703882012-08-09 16:46:01 +02005368 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005369 if (!i915_mch_dev) {
5370 ret = false;
5371 goto out_unlock;
5372 }
5373 dev_priv = i915_mch_dev;
5374
Daniel Vetter20e4d402012-08-08 23:35:39 +02005375 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5376 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005377
5378out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005379 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005380
5381 return ret;
5382}
5383EXPORT_SYMBOL_GPL(i915_gpu_raise);
5384
5385/**
5386 * i915_gpu_lower - lower GPU frequency limit
5387 *
5388 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5389 * frequency maximum.
5390 */
5391bool i915_gpu_lower(void)
5392{
5393 struct drm_i915_private *dev_priv;
5394 bool ret = true;
5395
Daniel Vetter92703882012-08-09 16:46:01 +02005396 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005397 if (!i915_mch_dev) {
5398 ret = false;
5399 goto out_unlock;
5400 }
5401 dev_priv = i915_mch_dev;
5402
Daniel Vetter20e4d402012-08-08 23:35:39 +02005403 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5404 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005405
5406out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005407 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005408
5409 return ret;
5410}
5411EXPORT_SYMBOL_GPL(i915_gpu_lower);
5412
5413/**
5414 * i915_gpu_busy - indicate GPU business to IPS
5415 *
5416 * Tell the IPS driver whether or not the GPU is busy.
5417 */
5418bool i915_gpu_busy(void)
5419{
5420 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005421 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005422 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005423 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005424
Daniel Vetter92703882012-08-09 16:46:01 +02005425 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005426 if (!i915_mch_dev)
5427 goto out_unlock;
5428 dev_priv = i915_mch_dev;
5429
Chris Wilsonf047e392012-07-21 12:31:41 +01005430 for_each_ring(ring, dev_priv, i)
5431 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005432
5433out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005434 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005435
5436 return ret;
5437}
5438EXPORT_SYMBOL_GPL(i915_gpu_busy);
5439
5440/**
5441 * i915_gpu_turbo_disable - disable graphics turbo
5442 *
5443 * Disable graphics turbo by resetting the max frequency and setting the
5444 * current frequency to the default.
5445 */
5446bool i915_gpu_turbo_disable(void)
5447{
5448 struct drm_i915_private *dev_priv;
5449 bool ret = true;
5450
Daniel Vetter92703882012-08-09 16:46:01 +02005451 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005452 if (!i915_mch_dev) {
5453 ret = false;
5454 goto out_unlock;
5455 }
5456 dev_priv = i915_mch_dev;
5457
Daniel Vetter20e4d402012-08-08 23:35:39 +02005458 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005459
Daniel Vetter20e4d402012-08-08 23:35:39 +02005460 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005461 ret = false;
5462
5463out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005464 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005465
5466 return ret;
5467}
5468EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5469
5470/**
5471 * Tells the intel_ips driver that the i915 driver is now loaded, if
5472 * IPS got loaded first.
5473 *
5474 * This awkward dance is so that neither module has to depend on the
5475 * other in order for IPS to do the appropriate communication of
5476 * GPU turbo limits to i915.
5477 */
5478static void
5479ips_ping_for_i915_load(void)
5480{
5481 void (*link)(void);
5482
5483 link = symbol_get(ips_link_to_i915_driver);
5484 if (link) {
5485 link();
5486 symbol_put(ips_link_to_i915_driver);
5487 }
5488}
5489
5490void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5491{
Daniel Vetter02d71952012-08-09 16:44:54 +02005492 /* We only register the i915 ips part with intel-ips once everything is
5493 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005494 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005495 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005496 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005497
5498 ips_ping_for_i915_load();
5499}
5500
5501void intel_gpu_ips_teardown(void)
5502{
Daniel Vetter92703882012-08-09 16:46:01 +02005503 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005504 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005505 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005506}
Deepak S76c3552f2014-01-30 23:08:16 +05305507
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005508static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005509{
5510 struct drm_i915_private *dev_priv = dev->dev_private;
5511 u32 lcfuse;
5512 u8 pxw[16];
5513 int i;
5514
5515 /* Disable to program */
5516 I915_WRITE(ECR, 0);
5517 POSTING_READ(ECR);
5518
5519 /* Program energy weights for various events */
5520 I915_WRITE(SDEW, 0x15040d00);
5521 I915_WRITE(CSIEW0, 0x007f0000);
5522 I915_WRITE(CSIEW1, 0x1e220004);
5523 I915_WRITE(CSIEW2, 0x04000004);
5524
5525 for (i = 0; i < 5; i++)
5526 I915_WRITE(PEW + (i * 4), 0);
5527 for (i = 0; i < 3; i++)
5528 I915_WRITE(DEW + (i * 4), 0);
5529
5530 /* Program P-state weights to account for frequency power adjustment */
5531 for (i = 0; i < 16; i++) {
5532 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5533 unsigned long freq = intel_pxfreq(pxvidfreq);
5534 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5535 PXVFREQ_PX_SHIFT;
5536 unsigned long val;
5537
5538 val = vid * vid;
5539 val *= (freq / 1000);
5540 val *= 255;
5541 val /= (127*127*900);
5542 if (val > 0xff)
5543 DRM_ERROR("bad pxval: %ld\n", val);
5544 pxw[i] = val;
5545 }
5546 /* Render standby states get 0 weight */
5547 pxw[14] = 0;
5548 pxw[15] = 0;
5549
5550 for (i = 0; i < 4; i++) {
5551 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5552 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5553 I915_WRITE(PXW + (i * 4), val);
5554 }
5555
5556 /* Adjust magic regs to magic values (more experimental results) */
5557 I915_WRITE(OGW0, 0);
5558 I915_WRITE(OGW1, 0);
5559 I915_WRITE(EG0, 0x00007f00);
5560 I915_WRITE(EG1, 0x0000000e);
5561 I915_WRITE(EG2, 0x000e0000);
5562 I915_WRITE(EG3, 0x68000300);
5563 I915_WRITE(EG4, 0x42000000);
5564 I915_WRITE(EG5, 0x00140031);
5565 I915_WRITE(EG6, 0);
5566 I915_WRITE(EG7, 0);
5567
5568 for (i = 0; i < 8; i++)
5569 I915_WRITE(PXWL + (i * 4), 0);
5570
5571 /* Enable PMON + select events */
5572 I915_WRITE(ECR, 0x80000019);
5573
5574 lcfuse = I915_READ(LCFUSE02);
5575
Daniel Vetter20e4d402012-08-08 23:35:39 +02005576 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005577}
5578
Imre Deakae484342014-03-31 15:10:44 +03005579void intel_init_gt_powersave(struct drm_device *dev)
5580{
Imre Deake6069ca2014-04-18 16:01:02 +03005581 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5582
Deepak S38807742014-05-23 21:00:15 +05305583 if (IS_CHERRYVIEW(dev))
5584 cherryview_init_gt_powersave(dev);
5585 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005586 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005587}
5588
5589void intel_cleanup_gt_powersave(struct drm_device *dev)
5590{
Deepak S38807742014-05-23 21:00:15 +05305591 if (IS_CHERRYVIEW(dev))
5592 return;
5593 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005594 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005595}
5596
Imre Deakdbea3ce2014-12-15 18:59:28 +02005597static void gen6_suspend_rps(struct drm_device *dev)
5598{
5599 struct drm_i915_private *dev_priv = dev->dev_private;
5600
5601 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5602
5603 /*
5604 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5605 * is added for it.
5606 */
5607 if (INTEL_INFO(dev)->gen < 9)
5608 gen6_disable_rps_interrupts(dev);
5609}
5610
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005611/**
5612 * intel_suspend_gt_powersave - suspend PM work and helper threads
5613 * @dev: drm device
5614 *
5615 * We don't want to disable RC6 or other features here, we just want
5616 * to make sure any work we've queued has finished and won't bother
5617 * us while we're suspended.
5618 */
5619void intel_suspend_gt_powersave(struct drm_device *dev)
5620{
5621 struct drm_i915_private *dev_priv = dev->dev_private;
5622
Imre Deakd4d70aa2014-11-19 15:30:04 +02005623 if (INTEL_INFO(dev)->gen < 6)
5624 return;
5625
Imre Deakdbea3ce2014-12-15 18:59:28 +02005626 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305627
5628 /* Force GPU to min freq during suspend */
5629 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005630}
5631
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005632void intel_disable_gt_powersave(struct drm_device *dev)
5633{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005634 struct drm_i915_private *dev_priv = dev->dev_private;
5635
Daniel Vetter930ebb42012-06-29 23:32:16 +02005636 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005637 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305638 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005639 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005640
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005641 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005642 if (INTEL_INFO(dev)->gen >= 9)
5643 gen9_disable_rps(dev);
5644 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305645 cherryview_disable_rps(dev);
5646 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005647 valleyview_disable_rps(dev);
5648 else
5649 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005650
Chris Wilsonc0951f02013-10-10 21:58:50 +01005651 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005652 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005653 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005654}
5655
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005656static void intel_gen6_powersave_work(struct work_struct *work)
5657{
5658 struct drm_i915_private *dev_priv =
5659 container_of(work, struct drm_i915_private,
5660 rps.delayed_resume_work.work);
5661 struct drm_device *dev = dev_priv->dev;
5662
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005663 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005664
Imre Deak3cc134e2014-11-19 15:30:03 +02005665 /*
5666 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5667 * added for it.
5668 */
5669 if (INTEL_INFO(dev)->gen < 9)
5670 gen6_reset_rps_interrupts(dev);
5671
Deepak S38807742014-05-23 21:00:15 +05305672 if (IS_CHERRYVIEW(dev)) {
5673 cherryview_enable_rps(dev);
5674 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005675 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005676 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005677 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005678 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005679 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005680 } else if (IS_BROADWELL(dev)) {
5681 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005682 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005683 } else {
5684 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005685 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005686 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005687 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005688
5689 if (INTEL_INFO(dev)->gen < 9)
5690 gen6_enable_rps_interrupts(dev);
5691
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005692 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005693
5694 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005695}
5696
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005697void intel_enable_gt_powersave(struct drm_device *dev)
5698{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005699 struct drm_i915_private *dev_priv = dev->dev_private;
5700
Yu Zhangf61018b2015-02-10 19:05:52 +08005701 /* Powersaving is controlled by the host when inside a VM */
5702 if (intel_vgpu_active(dev))
5703 return;
5704
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005705 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005706 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005707 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005708 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005709 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305710 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005711 /*
5712 * PCU communication is slow and this doesn't need to be
5713 * done at any specific time, so do this out of our fast path
5714 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005715 *
5716 * We depend on the HW RC6 power context save/restore
5717 * mechanism when entering D3 through runtime PM suspend. So
5718 * disable RPM until RPS/RC6 is properly setup. We can only
5719 * get here via the driver load/system resume/runtime resume
5720 * paths, so the _noresume version is enough (and in case of
5721 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005722 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005723 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5724 round_jiffies_up_relative(HZ)))
5725 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005726 }
5727}
5728
Imre Deakc6df39b2014-04-14 20:24:29 +03005729void intel_reset_gt_powersave(struct drm_device *dev)
5730{
5731 struct drm_i915_private *dev_priv = dev->dev_private;
5732
Imre Deakdbea3ce2014-12-15 18:59:28 +02005733 if (INTEL_INFO(dev)->gen < 6)
5734 return;
5735
5736 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005737 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005738}
5739
Daniel Vetter3107bd42012-10-31 22:52:31 +01005740static void ibx_init_clock_gating(struct drm_device *dev)
5741{
5742 struct drm_i915_private *dev_priv = dev->dev_private;
5743
5744 /*
5745 * On Ibex Peak and Cougar Point, we need to disable clock
5746 * gating for the panel power sequencer or it will fail to
5747 * start up when no ports are active.
5748 */
5749 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5750}
5751
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005752static void g4x_disable_trickle_feed(struct drm_device *dev)
5753{
5754 struct drm_i915_private *dev_priv = dev->dev_private;
5755 int pipe;
5756
Damien Lespiau055e3932014-08-18 13:49:10 +01005757 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005758 I915_WRITE(DSPCNTR(pipe),
5759 I915_READ(DSPCNTR(pipe)) |
5760 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005761 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005762 }
5763}
5764
Ville Syrjälä017636c2013-12-05 15:51:37 +02005765static void ilk_init_lp_watermarks(struct drm_device *dev)
5766{
5767 struct drm_i915_private *dev_priv = dev->dev_private;
5768
5769 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5770 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5771 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5772
5773 /*
5774 * Don't touch WM1S_LP_EN here.
5775 * Doing so could cause underruns.
5776 */
5777}
5778
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005779static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005780{
5781 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005782 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005783
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005784 /*
5785 * Required for FBC
5786 * WaFbcDisableDpfcClockGating:ilk
5787 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005788 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5789 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5790 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005791
5792 I915_WRITE(PCH_3DCGDIS0,
5793 MARIUNIT_CLOCK_GATE_DISABLE |
5794 SVSMUNIT_CLOCK_GATE_DISABLE);
5795 I915_WRITE(PCH_3DCGDIS1,
5796 VFMUNIT_CLOCK_GATE_DISABLE);
5797
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005798 /*
5799 * According to the spec the following bits should be set in
5800 * order to enable memory self-refresh
5801 * The bit 22/21 of 0x42004
5802 * The bit 5 of 0x42020
5803 * The bit 15 of 0x45000
5804 */
5805 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5806 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5807 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005808 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005809 I915_WRITE(DISP_ARB_CTL,
5810 (I915_READ(DISP_ARB_CTL) |
5811 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005812
5813 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005814
5815 /*
5816 * Based on the document from hardware guys the following bits
5817 * should be set unconditionally in order to enable FBC.
5818 * The bit 22 of 0x42000
5819 * The bit 22 of 0x42004
5820 * The bit 7,8,9 of 0x42020.
5821 */
5822 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005823 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005824 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5825 I915_READ(ILK_DISPLAY_CHICKEN1) |
5826 ILK_FBCQ_DIS);
5827 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5828 I915_READ(ILK_DISPLAY_CHICKEN2) |
5829 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005830 }
5831
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005832 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5833
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005834 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5835 I915_READ(ILK_DISPLAY_CHICKEN2) |
5836 ILK_ELPIN_409_SELECT);
5837 I915_WRITE(_3D_CHICKEN2,
5838 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5839 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005840
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005841 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005842 I915_WRITE(CACHE_MODE_0,
5843 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005844
Akash Goel4e046322014-04-04 17:14:38 +05305845 /* WaDisable_RenderCache_OperationalFlush:ilk */
5846 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5847
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005848 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005849
Daniel Vetter3107bd42012-10-31 22:52:31 +01005850 ibx_init_clock_gating(dev);
5851}
5852
5853static void cpt_init_clock_gating(struct drm_device *dev)
5854{
5855 struct drm_i915_private *dev_priv = dev->dev_private;
5856 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005857 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005858
5859 /*
5860 * On Ibex Peak and Cougar Point, we need to disable clock
5861 * gating for the panel power sequencer or it will fail to
5862 * start up when no ports are active.
5863 */
Jesse Barnescd664072013-10-02 10:34:19 -07005864 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5865 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5866 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005867 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5868 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005869 /* The below fixes the weird display corruption, a few pixels shifted
5870 * downward, on (only) LVDS of some HP laptops with IVY.
5871 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005872 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005873 val = I915_READ(TRANS_CHICKEN2(pipe));
5874 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5875 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005876 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005877 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005878 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5879 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5880 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005881 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5882 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005883 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005884 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005885 I915_WRITE(TRANS_CHICKEN1(pipe),
5886 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5887 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005888}
5889
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005890static void gen6_check_mch_setup(struct drm_device *dev)
5891{
5892 struct drm_i915_private *dev_priv = dev->dev_private;
5893 uint32_t tmp;
5894
5895 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005896 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5897 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5898 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005899}
5900
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005901static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005902{
5903 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005904 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005905
Damien Lespiau231e54f2012-10-19 17:55:41 +01005906 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005907
5908 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5909 I915_READ(ILK_DISPLAY_CHICKEN2) |
5910 ILK_ELPIN_409_SELECT);
5911
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005912 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005913 I915_WRITE(_3D_CHICKEN,
5914 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5915
Akash Goel4e046322014-04-04 17:14:38 +05305916 /* WaDisable_RenderCache_OperationalFlush:snb */
5917 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5918
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005919 /*
5920 * BSpec recoomends 8x4 when MSAA is used,
5921 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005922 *
5923 * Note that PS/WM thread counts depend on the WIZ hashing
5924 * disable bit, which we don't touch here, but it's good
5925 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005926 */
5927 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005928 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005929
Ville Syrjälä017636c2013-12-05 15:51:37 +02005930 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005931
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005932 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005933 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005934
5935 I915_WRITE(GEN6_UCGCTL1,
5936 I915_READ(GEN6_UCGCTL1) |
5937 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5938 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5939
5940 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5941 * gating disable must be set. Failure to set it results in
5942 * flickering pixels due to Z write ordering failures after
5943 * some amount of runtime in the Mesa "fire" demo, and Unigine
5944 * Sanctuary and Tropics, and apparently anything else with
5945 * alpha test or pixel discard.
5946 *
5947 * According to the spec, bit 11 (RCCUNIT) must also be set,
5948 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005949 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005950 * WaDisableRCCUnitClockGating:snb
5951 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005952 */
5953 I915_WRITE(GEN6_UCGCTL2,
5954 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5955 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5956
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005957 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005958 I915_WRITE(_3D_CHICKEN3,
5959 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005960
5961 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005962 * Bspec says:
5963 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5964 * 3DSTATE_SF number of SF output attributes is more than 16."
5965 */
5966 I915_WRITE(_3D_CHICKEN3,
5967 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5968
5969 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005970 * According to the spec the following bits should be
5971 * set in order to enable memory self-refresh and fbc:
5972 * The bit21 and bit22 of 0x42000
5973 * The bit21 and bit22 of 0x42004
5974 * The bit5 and bit7 of 0x42020
5975 * The bit14 of 0x70180
5976 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005977 *
5978 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005979 */
5980 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5981 I915_READ(ILK_DISPLAY_CHICKEN1) |
5982 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5983 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5984 I915_READ(ILK_DISPLAY_CHICKEN2) |
5985 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005986 I915_WRITE(ILK_DSPCLK_GATE_D,
5987 I915_READ(ILK_DSPCLK_GATE_D) |
5988 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5989 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005990
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005991 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005992
Daniel Vetter3107bd42012-10-31 22:52:31 +01005993 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005994
5995 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005996}
5997
5998static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5999{
6000 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6001
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006002 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006003 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006004 *
6005 * This actually overrides the dispatch
6006 * mode for all thread types.
6007 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006008 reg &= ~GEN7_FF_SCHED_MASK;
6009 reg |= GEN7_FF_TS_SCHED_HW;
6010 reg |= GEN7_FF_VS_SCHED_HW;
6011 reg |= GEN7_FF_DS_SCHED_HW;
6012
6013 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6014}
6015
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006016static void lpt_init_clock_gating(struct drm_device *dev)
6017{
6018 struct drm_i915_private *dev_priv = dev->dev_private;
6019
6020 /*
6021 * TODO: this bit should only be enabled when really needed, then
6022 * disabled when not needed anymore in order to save power.
6023 */
6024 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6025 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6026 I915_READ(SOUTH_DSPCLK_GATE_D) |
6027 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006028
6029 /* WADPOClockGatingDisable:hsw */
6030 I915_WRITE(_TRANSA_CHICKEN1,
6031 I915_READ(_TRANSA_CHICKEN1) |
6032 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006033}
6034
Imre Deak7d708ee2013-04-17 14:04:50 +03006035static void lpt_suspend_hw(struct drm_device *dev)
6036{
6037 struct drm_i915_private *dev_priv = dev->dev_private;
6038
6039 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6040 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6041
6042 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6043 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6044 }
6045}
6046
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006047static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006048{
6049 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006050 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006051
6052 I915_WRITE(WM3_LP_ILK, 0);
6053 I915_WRITE(WM2_LP_ILK, 0);
6054 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006055
Ben Widawskyab57fff2013-12-12 15:28:04 -08006056 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006057 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006058
Ben Widawskyab57fff2013-12-12 15:28:04 -08006059 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006060 I915_WRITE(CHICKEN_PAR1_1,
6061 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6062
Ben Widawskyab57fff2013-12-12 15:28:04 -08006063 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006064 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006065 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006066 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006067 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006068 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006069
Ben Widawskyab57fff2013-12-12 15:28:04 -08006070 /* WaVSRefCountFullforceMissDisable:bdw */
6071 /* WaDSRefCountFullforceMissDisable:bdw */
6072 I915_WRITE(GEN7_FF_THREAD_MODE,
6073 I915_READ(GEN7_FF_THREAD_MODE) &
6074 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006075
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006076 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6077 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006078
6079 /* WaDisableSDEUnitClockGating:bdw */
6080 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6081 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006082
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006083 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006084}
6085
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006086static void haswell_init_clock_gating(struct drm_device *dev)
6087{
6088 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006089
Ville Syrjälä017636c2013-12-05 15:51:37 +02006090 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006091
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006092 /* L3 caching of data atomics doesn't work -- disable it. */
6093 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6094 I915_WRITE(HSW_ROW_CHICKEN3,
6095 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6096
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006097 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006098 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6099 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6100 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6101
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006102 /* WaVSRefCountFullforceMissDisable:hsw */
6103 I915_WRITE(GEN7_FF_THREAD_MODE,
6104 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006105
Akash Goel4e046322014-04-04 17:14:38 +05306106 /* WaDisable_RenderCache_OperationalFlush:hsw */
6107 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6108
Chia-I Wufe27c602014-01-28 13:29:33 +08006109 /* enable HiZ Raw Stall Optimization */
6110 I915_WRITE(CACHE_MODE_0_GEN7,
6111 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6112
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006113 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006114 I915_WRITE(CACHE_MODE_1,
6115 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006116
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006117 /*
6118 * BSpec recommends 8x4 when MSAA is used,
6119 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006120 *
6121 * Note that PS/WM thread counts depend on the WIZ hashing
6122 * disable bit, which we don't touch here, but it's good
6123 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006124 */
6125 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006126 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006127
Kenneth Graunke94411592014-12-31 16:23:00 -08006128 /* WaSampleCChickenBitEnable:hsw */
6129 I915_WRITE(HALF_SLICE_CHICKEN3,
6130 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6131
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006132 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006133 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6134
Paulo Zanoni90a88642013-05-03 17:23:45 -03006135 /* WaRsPkgCStateDisplayPMReq:hsw */
6136 I915_WRITE(CHICKEN_PAR1_1,
6137 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006138
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006139 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006140}
6141
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006142static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006143{
6144 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006145 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006146
Ville Syrjälä017636c2013-12-05 15:51:37 +02006147 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006148
Damien Lespiau231e54f2012-10-19 17:55:41 +01006149 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006150
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006151 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006152 I915_WRITE(_3D_CHICKEN3,
6153 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6154
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006155 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006156 I915_WRITE(IVB_CHICKEN3,
6157 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6158 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6159
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006160 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006161 if (IS_IVB_GT1(dev))
6162 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6163 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006164
Akash Goel4e046322014-04-04 17:14:38 +05306165 /* WaDisable_RenderCache_OperationalFlush:ivb */
6166 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6167
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006168 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006169 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6170 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6171
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006172 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006173 I915_WRITE(GEN7_L3CNTLREG1,
6174 GEN7_WA_FOR_GEN7_L3_CONTROL);
6175 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006176 GEN7_WA_L3_CHICKEN_MODE);
6177 if (IS_IVB_GT1(dev))
6178 I915_WRITE(GEN7_ROW_CHICKEN2,
6179 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006180 else {
6181 /* must write both registers */
6182 I915_WRITE(GEN7_ROW_CHICKEN2,
6183 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006184 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6185 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006186 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006187
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006188 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006189 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6190 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6191
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006192 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006193 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006194 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006195 */
6196 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006197 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006198
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006199 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006200 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6201 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6202 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6203
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006204 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006205
6206 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006207
Chris Wilson22721342014-03-04 09:41:43 +00006208 if (0) { /* causes HiZ corruption on ivb:gt1 */
6209 /* enable HiZ Raw Stall Optimization */
6210 I915_WRITE(CACHE_MODE_0_GEN7,
6211 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6212 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006213
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006214 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006215 I915_WRITE(CACHE_MODE_1,
6216 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006217
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006218 /*
6219 * BSpec recommends 8x4 when MSAA is used,
6220 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006221 *
6222 * Note that PS/WM thread counts depend on the WIZ hashing
6223 * disable bit, which we don't touch here, but it's good
6224 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006225 */
6226 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006227 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006228
Ben Widawsky20848222012-05-04 18:58:59 -07006229 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6230 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6231 snpcr |= GEN6_MBC_SNPCR_MED;
6232 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006233
Ben Widawskyab5c6082013-04-05 13:12:41 -07006234 if (!HAS_PCH_NOP(dev))
6235 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006236
6237 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006238}
6239
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006240static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6241{
6242 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6243
6244 /*
6245 * Disable trickle feed and enable pnd deadline calculation
6246 */
6247 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6248 I915_WRITE(CBR1_VLV, 0);
6249}
6250
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006251static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006252{
6253 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006254
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006255 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006256
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006257 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006258 I915_WRITE(_3D_CHICKEN3,
6259 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6260
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006261 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006262 I915_WRITE(IVB_CHICKEN3,
6263 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6264 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6265
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006266 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006267 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006268 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006269 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6270 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006271
Akash Goel4e046322014-04-04 17:14:38 +05306272 /* WaDisable_RenderCache_OperationalFlush:vlv */
6273 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6274
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006275 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006276 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6277 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6278
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006279 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006280 I915_WRITE(GEN7_ROW_CHICKEN2,
6281 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6282
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006283 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006284 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6285 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6286 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6287
Ville Syrjälä46680e02014-01-22 21:33:01 +02006288 gen7_setup_fixed_func_scheduler(dev_priv);
6289
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006290 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006291 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006292 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006293 */
6294 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006295 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006296
Akash Goelc98f5062014-03-24 23:00:07 +05306297 /* WaDisableL3Bank2xClockGate:vlv
6298 * Disabling L3 clock gating- MMIO 940c[25] = 1
6299 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6300 I915_WRITE(GEN7_UCGCTL4,
6301 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006302
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006303 /*
6304 * BSpec says this must be set, even though
6305 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6306 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006307 I915_WRITE(CACHE_MODE_1,
6308 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006309
6310 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006311 * BSpec recommends 8x4 when MSAA is used,
6312 * however in practice 16x4 seems fastest.
6313 *
6314 * Note that PS/WM thread counts depend on the WIZ hashing
6315 * disable bit, which we don't touch here, but it's good
6316 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6317 */
6318 I915_WRITE(GEN7_GT_MODE,
6319 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6320
6321 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006322 * WaIncreaseL3CreditsForVLVB0:vlv
6323 * This is the hardware default actually.
6324 */
6325 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6326
6327 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006328 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006329 * Disable clock gating on th GCFG unit to prevent a delay
6330 * in the reporting of vblank events.
6331 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006332 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006333}
6334
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006335static void cherryview_init_clock_gating(struct drm_device *dev)
6336{
6337 struct drm_i915_private *dev_priv = dev->dev_private;
6338
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006339 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006340
Ville Syrjälä232ce332014-04-09 13:28:35 +03006341 /* WaVSRefCountFullforceMissDisable:chv */
6342 /* WaDSRefCountFullforceMissDisable:chv */
6343 I915_WRITE(GEN7_FF_THREAD_MODE,
6344 I915_READ(GEN7_FF_THREAD_MODE) &
6345 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006346
6347 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6348 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6349 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006350
6351 /* WaDisableCSUnitClockGating:chv */
6352 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6353 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006354
6355 /* WaDisableSDEUnitClockGating:chv */
6356 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6357 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006358}
6359
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006360static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006361{
6362 struct drm_i915_private *dev_priv = dev->dev_private;
6363 uint32_t dspclk_gate;
6364
6365 I915_WRITE(RENCLK_GATE_D1, 0);
6366 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6367 GS_UNIT_CLOCK_GATE_DISABLE |
6368 CL_UNIT_CLOCK_GATE_DISABLE);
6369 I915_WRITE(RAMCLK_GATE_D, 0);
6370 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6371 OVRUNIT_CLOCK_GATE_DISABLE |
6372 OVCUNIT_CLOCK_GATE_DISABLE;
6373 if (IS_GM45(dev))
6374 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6375 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006376
6377 /* WaDisableRenderCachePipelinedFlush */
6378 I915_WRITE(CACHE_MODE_0,
6379 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006380
Akash Goel4e046322014-04-04 17:14:38 +05306381 /* WaDisable_RenderCache_OperationalFlush:g4x */
6382 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6383
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006384 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006385}
6386
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006387static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006388{
6389 struct drm_i915_private *dev_priv = dev->dev_private;
6390
6391 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6392 I915_WRITE(RENCLK_GATE_D2, 0);
6393 I915_WRITE(DSPCLK_GATE_D, 0);
6394 I915_WRITE(RAMCLK_GATE_D, 0);
6395 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006396 I915_WRITE(MI_ARB_STATE,
6397 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306398
6399 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6400 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006401}
6402
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006403static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006404{
6405 struct drm_i915_private *dev_priv = dev->dev_private;
6406
6407 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6408 I965_RCC_CLOCK_GATE_DISABLE |
6409 I965_RCPB_CLOCK_GATE_DISABLE |
6410 I965_ISC_CLOCK_GATE_DISABLE |
6411 I965_FBC_CLOCK_GATE_DISABLE);
6412 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006413 I915_WRITE(MI_ARB_STATE,
6414 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306415
6416 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6417 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006418}
6419
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006420static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006421{
6422 struct drm_i915_private *dev_priv = dev->dev_private;
6423 u32 dstate = I915_READ(D_STATE);
6424
6425 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6426 DSTATE_DOT_CLOCK_GATING;
6427 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006428
6429 if (IS_PINEVIEW(dev))
6430 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006431
6432 /* IIR "flip pending" means done if this bit is set */
6433 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006434
6435 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006436 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006437
6438 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6439 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006440
6441 I915_WRITE(MI_ARB_STATE,
6442 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006443}
6444
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006445static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006446{
6447 struct drm_i915_private *dev_priv = dev->dev_private;
6448
6449 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006450
6451 /* interrupts should cause a wake up from C3 */
6452 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6453 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006454
6455 I915_WRITE(MEM_MODE,
6456 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006457}
6458
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006459static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006460{
6461 struct drm_i915_private *dev_priv = dev->dev_private;
6462
6463 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006464
6465 I915_WRITE(MEM_MODE,
6466 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6467 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006468}
6469
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006470void intel_init_clock_gating(struct drm_device *dev)
6471{
6472 struct drm_i915_private *dev_priv = dev->dev_private;
6473
Damien Lespiauc57e3552015-02-09 19:33:05 +00006474 if (dev_priv->display.init_clock_gating)
6475 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006476}
6477
Imre Deak7d708ee2013-04-17 14:04:50 +03006478void intel_suspend_hw(struct drm_device *dev)
6479{
6480 if (HAS_PCH_LPT(dev))
6481 lpt_suspend_hw(dev);
6482}
6483
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006484/* Set up chip specific power management-related functions */
6485void intel_init_pm(struct drm_device *dev)
6486{
6487 struct drm_i915_private *dev_priv = dev->dev_private;
6488
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006489 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006490
Daniel Vetterc921aba2012-04-26 23:28:17 +02006491 /* For cxsr */
6492 if (IS_PINEVIEW(dev))
6493 i915_pineview_get_mem_freq(dev);
6494 else if (IS_GEN5(dev))
6495 i915_ironlake_get_mem_freq(dev);
6496
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006497 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006498 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006499 skl_setup_wm_latency(dev);
6500
Damien Lespiau45db2192015-02-09 19:33:09 +00006501 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006502 dev_priv->display.update_wm = skl_update_wm;
6503 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306504 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006505 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006506
Ville Syrjäläbd602542014-01-07 16:14:10 +02006507 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6508 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6509 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6510 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6511 dev_priv->display.update_wm = ilk_update_wm;
6512 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6513 } else {
6514 DRM_DEBUG_KMS("Failed to read display plane latency. "
6515 "Disable CxSR\n");
6516 }
6517
6518 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006519 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006520 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006521 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006522 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006523 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006524 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006525 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006526 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006527 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006528 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläae801522015-03-05 21:19:49 +02006529 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306530 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006531 dev_priv->display.init_clock_gating =
6532 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006533 } else if (IS_VALLEYVIEW(dev)) {
6534 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306535 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006536 dev_priv->display.init_clock_gating =
6537 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006538 } else if (IS_PINEVIEW(dev)) {
6539 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6540 dev_priv->is_ddr3,
6541 dev_priv->fsb_freq,
6542 dev_priv->mem_freq)) {
6543 DRM_INFO("failed to find known CxSR latency "
6544 "(found ddr%s fsb freq %d, mem freq %d), "
6545 "disabling CxSR\n",
6546 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6547 dev_priv->fsb_freq, dev_priv->mem_freq);
6548 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006549 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006550 dev_priv->display.update_wm = NULL;
6551 } else
6552 dev_priv->display.update_wm = pineview_update_wm;
6553 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6554 } else if (IS_G4X(dev)) {
6555 dev_priv->display.update_wm = g4x_update_wm;
6556 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6557 } else if (IS_GEN4(dev)) {
6558 dev_priv->display.update_wm = i965_update_wm;
6559 if (IS_CRESTLINE(dev))
6560 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6561 else if (IS_BROADWATER(dev))
6562 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6563 } else if (IS_GEN3(dev)) {
6564 dev_priv->display.update_wm = i9xx_update_wm;
6565 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6566 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006567 } else if (IS_GEN2(dev)) {
6568 if (INTEL_INFO(dev)->num_pipes == 1) {
6569 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006570 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006571 } else {
6572 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006573 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006574 }
6575
6576 if (IS_I85X(dev) || IS_I865G(dev))
6577 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6578 else
6579 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6580 } else {
6581 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006582 }
6583}
6584
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006585int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006586{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006587 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006588
6589 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6590 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6591 return -EAGAIN;
6592 }
6593
6594 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006595 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006596 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6597
6598 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6599 500)) {
6600 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6601 return -ETIMEDOUT;
6602 }
6603
6604 *val = I915_READ(GEN6_PCODE_DATA);
6605 I915_WRITE(GEN6_PCODE_DATA, 0);
6606
6607 return 0;
6608}
6609
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006610int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006611{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006612 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006613
6614 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6615 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6616 return -EAGAIN;
6617 }
6618
6619 I915_WRITE(GEN6_PCODE_DATA, val);
6620 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6621
6622 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6623 500)) {
6624 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6625 return -ETIMEDOUT;
6626 }
6627
6628 I915_WRITE(GEN6_PCODE_DATA, 0);
6629
6630 return 0;
6631}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006632
Ville Syrjälädd06f882014-11-10 22:55:12 +02006633static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006634{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006635 switch (czclk_freq) {
6636 case 200:
6637 return 10;
6638 case 267:
6639 return 12;
6640 case 320:
6641 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006642 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006643 case 400:
6644 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006645 default:
6646 return -1;
6647 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006648}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006649
Ville Syrjälädd06f882014-11-10 22:55:12 +02006650static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6651{
6652 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6653
6654 div = vlv_gpu_freq_div(czclk_freq);
6655 if (div < 0)
6656 return div;
6657
6658 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006659}
6660
Fengguang Wub55dd642014-07-12 11:21:39 +02006661static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006662{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006663 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006664
Ville Syrjälädd06f882014-11-10 22:55:12 +02006665 mul = vlv_gpu_freq_div(czclk_freq);
6666 if (mul < 0)
6667 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006668
Ville Syrjälädd06f882014-11-10 22:55:12 +02006669 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006670}
6671
Fengguang Wub55dd642014-07-12 11:21:39 +02006672static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306673{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006674 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306675
Ville Syrjälädd06f882014-11-10 22:55:12 +02006676 div = vlv_gpu_freq_div(czclk_freq) / 2;
6677 if (div < 0)
6678 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306679
Ville Syrjälädd06f882014-11-10 22:55:12 +02006680 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306681}
6682
Fengguang Wub55dd642014-07-12 11:21:39 +02006683static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306684{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006685 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306686
Ville Syrjälädd06f882014-11-10 22:55:12 +02006687 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6688 if (mul < 0)
6689 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306690
Ville Syrjälä1c147622014-08-18 14:42:43 +03006691 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006692 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306693}
6694
Ville Syrjälä616bc822015-01-23 21:04:25 +02006695int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6696{
6697 if (IS_CHERRYVIEW(dev_priv->dev))
6698 return chv_gpu_freq(dev_priv, val);
6699 else if (IS_VALLEYVIEW(dev_priv->dev))
6700 return byt_gpu_freq(dev_priv, val);
6701 else
6702 return val * GT_FREQUENCY_MULTIPLIER;
6703}
6704
Ville Syrjälä616bc822015-01-23 21:04:25 +02006705int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6706{
Deepak S22b1b2f2014-07-12 14:54:33 +05306707 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006708 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306709 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006710 return byt_freq_opcode(dev_priv, val);
6711 else
6712 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306713}
6714
Daniel Vetterf742a552013-12-06 10:17:53 +01006715void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006716{
6717 struct drm_i915_private *dev_priv = dev->dev_private;
6718
Daniel Vetterf742a552013-12-06 10:17:53 +01006719 mutex_init(&dev_priv->rps.hw_lock);
6720
Chris Wilson907b28c2013-07-19 20:36:52 +01006721 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6722 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006723
Paulo Zanoni33688d92014-03-07 20:08:19 -03006724 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006725}