blob: 3b3bf86767833be28ee2ad8de10843df38a748a0 [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ä0018fda2015-03-05 21:19:45 +0200838static void vlv_write_wm_values(struct intel_crtc *crtc,
839 const struct vlv_wm_values *wm)
840{
841 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
842 enum pipe pipe = crtc->pipe;
843
844 I915_WRITE(VLV_DDL(pipe),
845 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
846 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
847 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
848 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
849
Ville Syrjäläae801522015-03-05 21:19:49 +0200850 I915_WRITE(DSPFW1,
851 ((wm->sr.plane << DSPFW_SR_SHIFT) & DSPFW_SR_MASK) |
852 ((wm->pipe[PIPE_B].cursor << DSPFW_CURSORB_SHIFT) & DSPFW_CURSORB_MASK) |
853 ((wm->pipe[PIPE_B].primary << DSPFW_PLANEB_SHIFT) & DSPFW_PLANEB_MASK_VLV) |
854 ((wm->pipe[PIPE_A].primary << DSPFW_PLANEA_SHIFT) & DSPFW_PLANEA_MASK_VLV));
855 I915_WRITE(DSPFW2,
856 ((wm->pipe[PIPE_A].sprite[1] << DSPFW_SPRITEB_SHIFT) & DSPFW_SPRITEB_MASK_VLV) |
857 ((wm->pipe[PIPE_A].cursor << DSPFW_CURSORA_SHIFT) & DSPFW_CURSORA_MASK) |
858 ((wm->pipe[PIPE_A].sprite[0] << DSPFW_SPRITEA_SHIFT) & DSPFW_SPRITEA_MASK_VLV));
859 I915_WRITE(DSPFW3,
860 ((wm->sr.cursor << DSPFW_CURSOR_SR_SHIFT) & DSPFW_CURSOR_SR_MASK));
861
862 if (IS_CHERRYVIEW(dev_priv)) {
863 I915_WRITE(DSPFW7_CHV,
864 ((wm->pipe[PIPE_B].sprite[1] << DSPFW_SPRITED_SHIFT) & DSPFW_SPRITED_MASK) |
865 ((wm->pipe[PIPE_B].sprite[0] << DSPFW_SPRITEC_SHIFT) & DSPFW_SPRITEC_MASK));
866 I915_WRITE(DSPFW8_CHV,
867 ((wm->pipe[PIPE_C].sprite[1] << DSPFW_SPRITEF_SHIFT) & DSPFW_SPRITEF_MASK) |
868 ((wm->pipe[PIPE_C].sprite[0] << DSPFW_SPRITEE_SHIFT) & DSPFW_SPRITEE_MASK));
869 I915_WRITE(DSPFW9_CHV,
870 ((wm->pipe[PIPE_C].primary << DSPFW_PLANEC_SHIFT) & DSPFW_PLANEC_MASK) |
871 ((wm->pipe[PIPE_C].cursor << DSPFW_CURSORC_SHIFT) & DSPFW_CURSORC_MASK));
872 I915_WRITE(DSPHOWM,
873 (((wm->sr.plane >> 9) << DSPFW_SR_HI_SHIFT) & DSPFW_SR_HI_MASK) |
874 (((wm->pipe[PIPE_C].sprite[1] >> 8) << DSPFW_SPRITEF_HI_SHIFT) & DSPFW_SPRITEF_HI_MASK) |
875 (((wm->pipe[PIPE_C].sprite[0] >> 8) << DSPFW_SPRITEE_HI_SHIFT) & DSPFW_SPRITEE_HI_MASK) |
876 (((wm->pipe[PIPE_C].primary >> 8) << DSPFW_PLANEC_HI_SHIFT) & DSPFW_PLANEC_HI_MASK) |
877 (((wm->pipe[PIPE_B].sprite[1] >> 8) << DSPFW_SPRITED_HI_SHIFT) & DSPFW_SPRITED_HI_MASK) |
878 (((wm->pipe[PIPE_B].sprite[0] >> 8) << DSPFW_SPRITEC_HI_SHIFT) & DSPFW_SPRITEC_HI_MASK) |
879 (((wm->pipe[PIPE_B].primary >> 8) << DSPFW_PLANEB_HI_SHIFT) & DSPFW_PLANEB_HI_MASK) |
880 (((wm->pipe[PIPE_A].sprite[1] >> 8) << DSPFW_SPRITEB_HI_SHIFT) & DSPFW_SPRITEB_HI_MASK) |
881 (((wm->pipe[PIPE_A].sprite[0] >> 8) << DSPFW_SPRITEA_HI_SHIFT) & DSPFW_SPRITEA_HI_MASK) |
882 (((wm->pipe[PIPE_A].primary >> 8) << DSPFW_PLANEA_HI_SHIFT) & DSPFW_PLANEA_HI_MASK));
883 } else {
884 I915_WRITE(DSPFW7,
885 ((wm->pipe[PIPE_B].sprite[1] << DSPFW_SPRITED_SHIFT) & DSPFW_SPRITED_MASK) |
886 ((wm->pipe[PIPE_B].sprite[0] << DSPFW_SPRITEC_SHIFT) & DSPFW_SPRITEC_MASK));
887 I915_WRITE(DSPHOWM,
888 (((wm->sr.plane >> 9) << DSPFW_SR_HI_SHIFT) & DSPFW_SR_HI_MASK) |
889 (((wm->pipe[PIPE_B].sprite[1] >> 8) << DSPFW_SPRITED_HI_SHIFT) & DSPFW_SPRITED_HI_MASK) |
890 (((wm->pipe[PIPE_B].sprite[0] >> 8) << DSPFW_SPRITEC_HI_SHIFT) & DSPFW_SPRITEC_HI_MASK) |
891 (((wm->pipe[PIPE_B].primary >> 8) << DSPFW_PLANEB_HI_SHIFT) & DSPFW_PLANEB_HI_MASK) |
892 (((wm->pipe[PIPE_A].sprite[1] >> 8) << DSPFW_SPRITEB_HI_SHIFT) & DSPFW_SPRITEB_HI_MASK) |
893 (((wm->pipe[PIPE_A].sprite[0] >> 8) << DSPFW_SPRITEA_HI_SHIFT) & DSPFW_SPRITEA_HI_MASK) |
894 (((wm->pipe[PIPE_A].primary >> 8) << DSPFW_PLANEA_HI_SHIFT) & DSPFW_PLANEA_HI_MASK));
895 }
896
897 POSTING_READ(DSPFW1);
898
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200899 dev_priv->wm.vlv = *wm;
900}
901
Ville Syrjälä341c5262015-03-05 21:19:44 +0200902static uint8_t vlv_compute_drain_latency(struct drm_crtc *crtc,
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200903 struct drm_plane *plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300904{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700905 struct drm_device *dev = crtc->dev;
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200906 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
907 int entries, prec_mult, drain_latency, pixel_size;
908 int clock = intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä341c5262015-03-05 21:19:44 +0200909 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300910
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200911 /*
912 * FIXME the plane might have an fb
913 * but be invisible (eg. due to clipping)
914 */
915 if (!intel_crtc->active || !plane->state->fb)
916 return 0;
917
Gajanan Bhat0948c262014-08-07 01:58:24 +0530918 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200919 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300920
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200921 pixel_size = drm_format_plane_cpp(plane->state->fb->pixel_format, 0);
922
Gajanan Bhat0948c262014-08-07 01:58:24 +0530923 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200924 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300925
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530926 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200927
Ville Syrjälä341c5262015-03-05 21:19:44 +0200928 prec_mult = high_precision;
929 drain_latency = 64 * prec_mult * 4 / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300930
Ville Syrjälä341c5262015-03-05 21:19:44 +0200931 if (drain_latency > DRAIN_LATENCY_MASK) {
932 prec_mult /= 2;
933 drain_latency = 64 * prec_mult * 4 / entries;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200934 }
935
Ville Syrjälä341c5262015-03-05 21:19:44 +0200936 if (drain_latency > DRAIN_LATENCY_MASK)
937 drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938
Ville Syrjälä341c5262015-03-05 21:19:44 +0200939 return drain_latency | (prec_mult == high_precision ?
940 DDL_PRECISION_HIGH : DDL_PRECISION_LOW);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300941}
942
Ville Syrjäläae801522015-03-05 21:19:49 +0200943static int vlv_compute_wm(struct intel_crtc *crtc,
944 struct intel_plane *plane,
945 int fifo_size)
946{
947 int clock, entries, pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948
Ville Syrjäläae801522015-03-05 21:19:49 +0200949 /*
950 * FIXME the plane might have an fb
951 * but be invisible (eg. due to clipping)
952 */
953 if (!crtc->active || !plane->base.state->fb)
954 return 0;
955
956 pixel_size = drm_format_plane_cpp(plane->base.state->fb->pixel_format, 0);
957 clock = crtc->config->base.adjusted_mode.crtc_clock;
958
959 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
960
961 /*
962 * Set up the watermark such that we don't start issuing memory
963 * requests until we are within PND's max deadline value (256us).
964 * Idea being to be idle as long as possible while still taking
965 * advatange of PND's deadline scheduling. The limit of 8
966 * cachelines (used when the FIFO will anyway drain in less time
967 * than 256us) should match what we would be done if trickle
968 * feed were enabled.
969 */
970 return fifo_size - clamp(DIV_ROUND_UP(256 * entries, 64), 0, fifo_size - 8);
971}
972
973static bool vlv_compute_sr_wm(struct drm_device *dev,
974 struct vlv_wm_values *wm)
975{
976 struct drm_i915_private *dev_priv = to_i915(dev);
977 struct drm_crtc *crtc;
978 enum pipe pipe = INVALID_PIPE;
979 int num_planes = 0;
980 int fifo_size = 0;
981 struct intel_plane *plane;
982
983 wm->sr.cursor = wm->sr.plane = 0;
984
985 crtc = single_enabled_crtc(dev);
986 /* maxfifo not supported on pipe C */
987 if (crtc && to_intel_crtc(crtc)->pipe != PIPE_C) {
988 pipe = to_intel_crtc(crtc)->pipe;
989 num_planes = !!wm->pipe[pipe].primary +
990 !!wm->pipe[pipe].sprite[0] +
991 !!wm->pipe[pipe].sprite[1];
992 fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
993 }
994
995 if (fifo_size == 0 || num_planes > 1)
996 return false;
997
998 wm->sr.cursor = vlv_compute_wm(to_intel_crtc(crtc),
999 to_intel_plane(crtc->cursor), 0x3f);
1000
1001 list_for_each_entry(plane, &dev->mode_config.plane_list, base.head) {
1002 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1003 continue;
1004
1005 if (plane->pipe != pipe)
1006 continue;
1007
1008 wm->sr.plane = vlv_compute_wm(to_intel_crtc(crtc),
1009 plane, fifo_size);
1010 if (wm->sr.plane != 0)
1011 break;
1012 }
1013
1014 return true;
1015}
1016
1017static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001018{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001019 struct drm_device *dev = crtc->dev;
1020 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301022 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläae801522015-03-05 21:19:49 +02001023 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001024 struct vlv_wm_values wm = dev_priv->wm.vlv;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001025
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001026 wm.ddl[pipe].primary = vlv_compute_drain_latency(crtc, crtc->primary);
Ville Syrjäläae801522015-03-05 21:19:49 +02001027 wm.pipe[pipe].primary = vlv_compute_wm(intel_crtc,
1028 to_intel_plane(crtc->primary),
1029 vlv_get_fifo_size(dev, pipe, 0));
1030
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001031 wm.ddl[pipe].cursor = vlv_compute_drain_latency(crtc, crtc->cursor);
Ville Syrjäläae801522015-03-05 21:19:49 +02001032 wm.pipe[pipe].cursor = vlv_compute_wm(intel_crtc,
1033 to_intel_plane(crtc->cursor),
1034 0x3f);
1035
1036 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1037
1038 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1039 return;
1040
1041 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1042 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1043 wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1044 wm.sr.plane, wm.sr.cursor);
1045
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +02001046 /*
1047 * FIXME DDR DVFS introduces massive memory latencies which
1048 * are not known to system agent so any deadline specified
1049 * by the display may not be respected. To support DDR DVFS
1050 * the watermark code needs to be rewritten to essentially
1051 * bypass deadline mechanism and rely solely on the
1052 * watermarks. For now disable DDR DVFS.
1053 */
1054 if (IS_CHERRYVIEW(dev_priv))
1055 chv_set_memory_dvfs(dev_priv, false);
1056
Ville Syrjäläae801522015-03-05 21:19:49 +02001057 if (!cxsr_enabled)
1058 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301059
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001060 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001061
1062 if (cxsr_enabled)
1063 intel_set_memory_cxsr(dev_priv, true);
1064}
1065
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301066static void valleyview_update_sprite_wm(struct drm_plane *plane,
1067 struct drm_crtc *crtc,
1068 uint32_t sprite_width,
1069 uint32_t sprite_height,
1070 int pixel_size,
1071 bool enabled, bool scaled)
1072{
1073 struct drm_device *dev = crtc->dev;
1074 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1076 enum pipe pipe = intel_crtc->pipe;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301077 int sprite = to_intel_plane(plane)->plane;
Ville Syrjäläae801522015-03-05 21:19:49 +02001078 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001079 struct vlv_wm_values wm = dev_priv->wm.vlv;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301080
Ville Syrjäläae801522015-03-05 21:19:49 +02001081 if (enabled) {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001082 wm.ddl[pipe].sprite[sprite] =
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001083 vlv_compute_drain_latency(crtc, plane);
Ville Syrjäläae801522015-03-05 21:19:49 +02001084
1085 wm.pipe[pipe].sprite[sprite] =
1086 vlv_compute_wm(intel_crtc,
1087 to_intel_plane(plane),
1088 vlv_get_fifo_size(dev, pipe, sprite+1));
1089 } else {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001090 wm.ddl[pipe].sprite[sprite] = 0;
Ville Syrjäläae801522015-03-05 21:19:49 +02001091 wm.pipe[pipe].sprite[sprite] = 0;
1092 }
1093
1094 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1095
1096 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1097 return;
1098
1099 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: sprite %c=%d, "
1100 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1101 sprite_name(pipe, sprite),
1102 wm.pipe[pipe].sprite[sprite],
1103 wm.sr.plane, wm.sr.cursor);
1104
1105 if (!cxsr_enabled)
1106 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301107
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001108 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjäläae801522015-03-05 21:19:49 +02001109
1110 if (cxsr_enabled)
1111 intel_set_memory_cxsr(dev_priv, true);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301112}
1113
Ville Syrjäläae801522015-03-05 21:19:49 +02001114#define single_plane_enabled(mask) is_power_of_2(mask)
1115
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001116static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001117{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001118 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001119 static const int sr_latency_ns = 12000;
1120 struct drm_i915_private *dev_priv = dev->dev_private;
1121 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1122 int plane_sr, cursor_sr;
1123 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001124 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001125
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001126 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001127 &g4x_wm_info, pessimal_latency_ns,
1128 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001129 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001130 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001131
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001132 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001133 &g4x_wm_info, pessimal_latency_ns,
1134 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001135 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001136 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001137
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001138 if (single_plane_enabled(enabled) &&
1139 g4x_compute_srwm(dev, ffs(enabled) - 1,
1140 sr_latency_ns,
1141 &g4x_wm_info,
1142 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001143 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001144 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001145 } else {
Imre Deak98584252014-06-13 14:54:20 +03001146 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001147 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001148 plane_sr = cursor_sr = 0;
1149 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001150
Ville Syrjäläa5043452014-06-28 02:04:18 +03001151 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1152 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001153 planea_wm, cursora_wm,
1154 planeb_wm, cursorb_wm,
1155 plane_sr, cursor_sr);
1156
1157 I915_WRITE(DSPFW1,
1158 (plane_sr << DSPFW_SR_SHIFT) |
1159 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1160 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001161 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001162 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001163 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001164 (cursora_wm << DSPFW_CURSORA_SHIFT));
1165 /* HPLL off in SR has some issues on G4x... disable it */
1166 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001167 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001168 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001169
1170 if (cxsr_enabled)
1171 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001172}
1173
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001174static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001175{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001176 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001177 struct drm_i915_private *dev_priv = dev->dev_private;
1178 struct drm_crtc *crtc;
1179 int srwm = 1;
1180 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001181 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001182
1183 /* Calc sr entries for one plane configs */
1184 crtc = single_enabled_crtc(dev);
1185 if (crtc) {
1186 /* self-refresh has much higher latency */
1187 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001188 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001189 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001190 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001191 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001192 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001193 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001194 unsigned long line_time_us;
1195 int entries;
1196
Ville Syrjälä922044c2014-02-14 14:18:57 +02001197 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001198
1199 /* Use ns/us then divide to preserve precision */
1200 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1201 pixel_size * hdisplay;
1202 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1203 srwm = I965_FIFO_SIZE - entries;
1204 if (srwm < 0)
1205 srwm = 1;
1206 srwm &= 0x1ff;
1207 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1208 entries, srwm);
1209
1210 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001211 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001212 entries = DIV_ROUND_UP(entries,
1213 i965_cursor_wm_info.cacheline_size);
1214 cursor_sr = i965_cursor_wm_info.fifo_size -
1215 (entries + i965_cursor_wm_info.guard_size);
1216
1217 if (cursor_sr > i965_cursor_wm_info.max_wm)
1218 cursor_sr = i965_cursor_wm_info.max_wm;
1219
1220 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1221 "cursor %d\n", srwm, cursor_sr);
1222
Imre Deak98584252014-06-13 14:54:20 +03001223 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001224 } else {
Imre Deak98584252014-06-13 14:54:20 +03001225 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001226 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001227 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001228 }
1229
1230 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1231 srwm);
1232
1233 /* 965 has limitations... */
1234 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001235 (8 << DSPFW_CURSORB_SHIFT) |
1236 (8 << DSPFW_PLANEB_SHIFT) |
1237 (8 << DSPFW_PLANEA_SHIFT));
1238 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1239 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001240 /* update cursor SR watermark */
1241 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001242
1243 if (cxsr_enabled)
1244 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001245}
1246
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001247static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001248{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001249 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001250 struct drm_i915_private *dev_priv = dev->dev_private;
1251 const struct intel_watermark_params *wm_info;
1252 uint32_t fwater_lo;
1253 uint32_t fwater_hi;
1254 int cwm, srwm = 1;
1255 int fifo_size;
1256 int planea_wm, planeb_wm;
1257 struct drm_crtc *crtc, *enabled = NULL;
1258
1259 if (IS_I945GM(dev))
1260 wm_info = &i945_wm_info;
1261 else if (!IS_GEN2(dev))
1262 wm_info = &i915_wm_info;
1263 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001264 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001265
1266 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1267 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001268 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001269 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001270 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001271 if (IS_GEN2(dev))
1272 cpp = 4;
1273
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001274 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001275 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001276 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001277 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001278 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001279 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001280 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001281 if (planea_wm > (long)wm_info->max_wm)
1282 planea_wm = wm_info->max_wm;
1283 }
1284
1285 if (IS_GEN2(dev))
1286 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001287
1288 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1289 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001290 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001291 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001292 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001293 if (IS_GEN2(dev))
1294 cpp = 4;
1295
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001296 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001297 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001298 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001299 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001300 if (enabled == NULL)
1301 enabled = crtc;
1302 else
1303 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001304 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001305 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001306 if (planeb_wm > (long)wm_info->max_wm)
1307 planeb_wm = wm_info->max_wm;
1308 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001309
1310 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1311
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001312 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001313 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001314
Matt Roper59bea882015-02-27 10:12:01 -08001315 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001316
1317 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001318 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001319 enabled = NULL;
1320 }
1321
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001322 /*
1323 * Overlay gets an aggressive default since video jitter is bad.
1324 */
1325 cwm = 2;
1326
1327 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001328 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001329
1330 /* Calc sr entries for one plane configs */
1331 if (HAS_FW_BLC(dev) && enabled) {
1332 /* self-refresh has much higher latency */
1333 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001334 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001335 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001336 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001337 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001338 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001339 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001340 unsigned long line_time_us;
1341 int entries;
1342
Ville Syrjälä922044c2014-02-14 14:18:57 +02001343 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001344
1345 /* Use ns/us then divide to preserve precision */
1346 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1347 pixel_size * hdisplay;
1348 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1349 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1350 srwm = wm_info->fifo_size - entries;
1351 if (srwm < 0)
1352 srwm = 1;
1353
1354 if (IS_I945G(dev) || IS_I945GM(dev))
1355 I915_WRITE(FW_BLC_SELF,
1356 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1357 else if (IS_I915GM(dev))
1358 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1359 }
1360
1361 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1362 planea_wm, planeb_wm, cwm, srwm);
1363
1364 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1365 fwater_hi = (cwm & 0x1f);
1366
1367 /* Set request length to 8 cachelines per fetch */
1368 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1369 fwater_hi = fwater_hi | (1 << 8);
1370
1371 I915_WRITE(FW_BLC, fwater_lo);
1372 I915_WRITE(FW_BLC2, fwater_hi);
1373
Imre Deak5209b1f2014-07-01 12:36:17 +03001374 if (enabled)
1375 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001376}
1377
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001378static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001379{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001380 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001381 struct drm_i915_private *dev_priv = dev->dev_private;
1382 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001383 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384 uint32_t fwater_lo;
1385 int planea_wm;
1386
1387 crtc = single_enabled_crtc(dev);
1388 if (crtc == NULL)
1389 return;
1390
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001391 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001392 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001393 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001394 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001395 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001396 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1397 fwater_lo |= (3<<8) | planea_wm;
1398
1399 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1400
1401 I915_WRITE(FW_BLC, fwater_lo);
1402}
1403
Ville Syrjälä36587292013-07-05 11:57:16 +03001404static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1405 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001406{
1407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001408 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001409
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001410 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001411
1412 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1413 * adjust the pixel_rate here. */
1414
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001415 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001416 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001417 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001418
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001419 pipe_w = intel_crtc->config->pipe_src_w;
1420 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001421 pfit_w = (pfit_size >> 16) & 0xFFFF;
1422 pfit_h = pfit_size & 0xFFFF;
1423 if (pipe_w < pfit_w)
1424 pipe_w = pfit_w;
1425 if (pipe_h < pfit_h)
1426 pipe_h = pfit_h;
1427
1428 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1429 pfit_w * pfit_h);
1430 }
1431
1432 return pixel_rate;
1433}
1434
Ville Syrjälä37126462013-08-01 16:18:55 +03001435/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001436static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001437 uint32_t latency)
1438{
1439 uint64_t ret;
1440
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001441 if (WARN(latency == 0, "Latency value missing\n"))
1442 return UINT_MAX;
1443
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001444 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1445 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1446
1447 return ret;
1448}
1449
Ville Syrjälä37126462013-08-01 16:18:55 +03001450/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001451static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001452 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1453 uint32_t latency)
1454{
1455 uint32_t ret;
1456
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001457 if (WARN(latency == 0, "Latency value missing\n"))
1458 return UINT_MAX;
1459
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001460 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1461 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1462 ret = DIV_ROUND_UP(ret, 64) + 2;
1463 return ret;
1464}
1465
Ville Syrjälä23297042013-07-05 11:57:17 +03001466static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001467 uint8_t bytes_per_pixel)
1468{
1469 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1470}
1471
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001472struct skl_pipe_wm_parameters {
1473 bool active;
1474 uint32_t pipe_htotal;
1475 uint32_t pixel_rate; /* in KHz */
1476 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1477 struct intel_plane_wm_parameters cursor;
1478};
1479
Imre Deak820c1982013-12-17 14:46:36 +02001480struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001481 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001482 uint32_t pipe_htotal;
1483 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001484 struct intel_plane_wm_parameters pri;
1485 struct intel_plane_wm_parameters spr;
1486 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001487};
1488
Imre Deak820c1982013-12-17 14:46:36 +02001489struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001490 uint16_t pri;
1491 uint16_t spr;
1492 uint16_t cur;
1493 uint16_t fbc;
1494};
1495
Ville Syrjälä240264f2013-08-07 13:29:12 +03001496/* used in computing the new watermarks state */
1497struct intel_wm_config {
1498 unsigned int num_pipes_active;
1499 bool sprites_enabled;
1500 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001501};
1502
Ville Syrjälä37126462013-08-01 16:18:55 +03001503/*
1504 * For both WM_PIPE and WM_LP.
1505 * mem_value must be in 0.1us units.
1506 */
Imre Deak820c1982013-12-17 14:46:36 +02001507static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001508 uint32_t mem_value,
1509 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001510{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001511 uint32_t method1, method2;
1512
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001513 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001514 return 0;
1515
Ville Syrjälä23297042013-07-05 11:57:17 +03001516 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001517 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001518 mem_value);
1519
1520 if (!is_lp)
1521 return method1;
1522
Ville Syrjälä23297042013-07-05 11:57:17 +03001523 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001524 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001525 params->pri.horiz_pixels,
1526 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001527 mem_value);
1528
1529 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001530}
1531
Ville Syrjälä37126462013-08-01 16:18:55 +03001532/*
1533 * For both WM_PIPE and WM_LP.
1534 * mem_value must be in 0.1us units.
1535 */
Imre Deak820c1982013-12-17 14:46:36 +02001536static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001537 uint32_t mem_value)
1538{
1539 uint32_t method1, method2;
1540
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001541 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001542 return 0;
1543
Ville Syrjälä23297042013-07-05 11:57:17 +03001544 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001545 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001546 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001547 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001548 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001549 params->spr.horiz_pixels,
1550 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001551 mem_value);
1552 return min(method1, method2);
1553}
1554
Ville Syrjälä37126462013-08-01 16:18:55 +03001555/*
1556 * For both WM_PIPE and WM_LP.
1557 * mem_value must be in 0.1us units.
1558 */
Imre Deak820c1982013-12-17 14:46:36 +02001559static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001560 uint32_t mem_value)
1561{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001562 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001563 return 0;
1564
Ville Syrjälä23297042013-07-05 11:57:17 +03001565 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001566 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001567 params->cur.horiz_pixels,
1568 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001569 mem_value);
1570}
1571
Paulo Zanonicca32e92013-05-31 11:45:06 -03001572/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001573static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001574 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001575{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001576 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001577 return 0;
1578
Ville Syrjälä23297042013-07-05 11:57:17 +03001579 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001580 params->pri.horiz_pixels,
1581 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001582}
1583
Ville Syrjälä158ae642013-08-07 13:28:19 +03001584static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1585{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001586 if (INTEL_INFO(dev)->gen >= 8)
1587 return 3072;
1588 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001589 return 768;
1590 else
1591 return 512;
1592}
1593
Ville Syrjälä4e975082014-03-07 18:32:11 +02001594static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1595 int level, bool is_sprite)
1596{
1597 if (INTEL_INFO(dev)->gen >= 8)
1598 /* BDW primary/sprite plane watermarks */
1599 return level == 0 ? 255 : 2047;
1600 else if (INTEL_INFO(dev)->gen >= 7)
1601 /* IVB/HSW primary/sprite plane watermarks */
1602 return level == 0 ? 127 : 1023;
1603 else if (!is_sprite)
1604 /* ILK/SNB primary plane watermarks */
1605 return level == 0 ? 127 : 511;
1606 else
1607 /* ILK/SNB sprite plane watermarks */
1608 return level == 0 ? 63 : 255;
1609}
1610
1611static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1612 int level)
1613{
1614 if (INTEL_INFO(dev)->gen >= 7)
1615 return level == 0 ? 63 : 255;
1616 else
1617 return level == 0 ? 31 : 63;
1618}
1619
1620static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1621{
1622 if (INTEL_INFO(dev)->gen >= 8)
1623 return 31;
1624 else
1625 return 15;
1626}
1627
Ville Syrjälä158ae642013-08-07 13:28:19 +03001628/* Calculate the maximum primary/sprite plane watermark */
1629static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1630 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001631 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001632 enum intel_ddb_partitioning ddb_partitioning,
1633 bool is_sprite)
1634{
1635 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001636
1637 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001638 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001639 return 0;
1640
1641 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001642 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001643 fifo_size /= INTEL_INFO(dev)->num_pipes;
1644
1645 /*
1646 * For some reason the non self refresh
1647 * FIFO size is only half of the self
1648 * refresh FIFO size on ILK/SNB.
1649 */
1650 if (INTEL_INFO(dev)->gen <= 6)
1651 fifo_size /= 2;
1652 }
1653
Ville Syrjälä240264f2013-08-07 13:29:12 +03001654 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001655 /* level 0 is always calculated with 1:1 split */
1656 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1657 if (is_sprite)
1658 fifo_size *= 5;
1659 fifo_size /= 6;
1660 } else {
1661 fifo_size /= 2;
1662 }
1663 }
1664
1665 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001666 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001667}
1668
1669/* Calculate the maximum cursor plane watermark */
1670static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001671 int level,
1672 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001673{
1674 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001675 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001676 return 64;
1677
1678 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001679 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001680}
1681
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001682static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001683 int level,
1684 const struct intel_wm_config *config,
1685 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001686 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001687{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001688 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1689 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1690 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001691 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001692}
1693
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001694static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1695 int level,
1696 struct ilk_wm_maximums *max)
1697{
1698 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1699 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1700 max->cur = ilk_cursor_wm_reg_max(dev, level);
1701 max->fbc = ilk_fbc_wm_reg_max(dev);
1702}
1703
Ville Syrjäläd9395652013-10-09 19:18:10 +03001704static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001705 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001706 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001707{
1708 bool ret;
1709
1710 /* already determined to be invalid? */
1711 if (!result->enable)
1712 return false;
1713
1714 result->enable = result->pri_val <= max->pri &&
1715 result->spr_val <= max->spr &&
1716 result->cur_val <= max->cur;
1717
1718 ret = result->enable;
1719
1720 /*
1721 * HACK until we can pre-compute everything,
1722 * and thus fail gracefully if LP0 watermarks
1723 * are exceeded...
1724 */
1725 if (level == 0 && !result->enable) {
1726 if (result->pri_val > max->pri)
1727 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1728 level, result->pri_val, max->pri);
1729 if (result->spr_val > max->spr)
1730 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1731 level, result->spr_val, max->spr);
1732 if (result->cur_val > max->cur)
1733 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1734 level, result->cur_val, max->cur);
1735
1736 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1737 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1738 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1739 result->enable = true;
1740 }
1741
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001742 return ret;
1743}
1744
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001745static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001746 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001747 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001748 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001749{
1750 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1751 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1752 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1753
1754 /* WM1+ latency values stored in 0.5us units */
1755 if (level > 0) {
1756 pri_latency *= 5;
1757 spr_latency *= 5;
1758 cur_latency *= 5;
1759 }
1760
1761 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1762 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1763 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1764 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1765 result->enable = true;
1766}
1767
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001768static uint32_t
1769hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001770{
1771 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001773 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001774 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001775
Matt Roper3ef00282015-03-09 10:19:24 -07001776 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001777 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001778
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001779 /* The WM are computed with base on how long it takes to fill a single
1780 * row at the given clock rate, multiplied by 8.
1781 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001782 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1783 mode->crtc_clock);
1784 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001785 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001786
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001787 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1788 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001789}
1790
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001791static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001792{
1793 struct drm_i915_private *dev_priv = dev->dev_private;
1794
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001795 if (IS_GEN9(dev)) {
1796 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001797 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001798 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001799
1800 /* read the first set of memory latencies[0:3] */
1801 val = 0; /* data0 to be programmed to 0 for first set */
1802 mutex_lock(&dev_priv->rps.hw_lock);
1803 ret = sandybridge_pcode_read(dev_priv,
1804 GEN9_PCODE_READ_MEM_LATENCY,
1805 &val);
1806 mutex_unlock(&dev_priv->rps.hw_lock);
1807
1808 if (ret) {
1809 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1810 return;
1811 }
1812
1813 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1814 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1815 GEN9_MEM_LATENCY_LEVEL_MASK;
1816 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1817 GEN9_MEM_LATENCY_LEVEL_MASK;
1818 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1819 GEN9_MEM_LATENCY_LEVEL_MASK;
1820
1821 /* read the second set of memory latencies[4:7] */
1822 val = 1; /* data0 to be programmed to 1 for second set */
1823 mutex_lock(&dev_priv->rps.hw_lock);
1824 ret = sandybridge_pcode_read(dev_priv,
1825 GEN9_PCODE_READ_MEM_LATENCY,
1826 &val);
1827 mutex_unlock(&dev_priv->rps.hw_lock);
1828 if (ret) {
1829 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1830 return;
1831 }
1832
1833 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1834 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1835 GEN9_MEM_LATENCY_LEVEL_MASK;
1836 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1837 GEN9_MEM_LATENCY_LEVEL_MASK;
1838 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1839 GEN9_MEM_LATENCY_LEVEL_MASK;
1840
Vandana Kannan367294b2014-11-04 17:06:46 +00001841 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001842 * WaWmMemoryReadLatency:skl
1843 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001844 * punit doesn't take into account the read latency so we need
1845 * to add 2us to the various latency levels we retrieve from
1846 * the punit.
1847 * - W0 is a bit special in that it's the only level that
1848 * can't be disabled if we want to have display working, so
1849 * we always add 2us there.
1850 * - For levels >=1, punit returns 0us latency when they are
1851 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001852 *
1853 * Additionally, if a level n (n > 1) has a 0us latency, all
1854 * levels m (m >= n) need to be disabled. We make sure to
1855 * sanitize the values out of the punit to satisfy this
1856 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001857 */
1858 wm[0] += 2;
1859 for (level = 1; level <= max_level; level++)
1860 if (wm[level] != 0)
1861 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001862 else {
1863 for (i = level + 1; i <= max_level; i++)
1864 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001865
Vandana Kannan4f947382014-11-04 17:06:47 +00001866 break;
1867 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001868 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001869 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1870
1871 wm[0] = (sskpd >> 56) & 0xFF;
1872 if (wm[0] == 0)
1873 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001874 wm[1] = (sskpd >> 4) & 0xFF;
1875 wm[2] = (sskpd >> 12) & 0xFF;
1876 wm[3] = (sskpd >> 20) & 0x1FF;
1877 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001878 } else if (INTEL_INFO(dev)->gen >= 6) {
1879 uint32_t sskpd = I915_READ(MCH_SSKPD);
1880
1881 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1882 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1883 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1884 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001885 } else if (INTEL_INFO(dev)->gen >= 5) {
1886 uint32_t mltr = I915_READ(MLTR_ILK);
1887
1888 /* ILK primary LP0 latency is 700 ns */
1889 wm[0] = 7;
1890 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1891 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001892 }
1893}
1894
Ville Syrjälä53615a52013-08-01 16:18:50 +03001895static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1896{
1897 /* ILK sprite LP0 latency is 1300 ns */
1898 if (INTEL_INFO(dev)->gen == 5)
1899 wm[0] = 13;
1900}
1901
1902static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1903{
1904 /* ILK cursor LP0 latency is 1300 ns */
1905 if (INTEL_INFO(dev)->gen == 5)
1906 wm[0] = 13;
1907
1908 /* WaDoubleCursorLP3Latency:ivb */
1909 if (IS_IVYBRIDGE(dev))
1910 wm[3] *= 2;
1911}
1912
Damien Lespiau546c81f2014-05-13 15:30:26 +01001913int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001914{
1915 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001916 if (IS_GEN9(dev))
1917 return 7;
1918 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001919 return 4;
1920 else if (INTEL_INFO(dev)->gen >= 6)
1921 return 3;
1922 else
1923 return 2;
1924}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001925
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001926static void intel_print_wm_latency(struct drm_device *dev,
1927 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001928 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001929{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001930 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001931
1932 for (level = 0; level <= max_level; level++) {
1933 unsigned int latency = wm[level];
1934
1935 if (latency == 0) {
1936 DRM_ERROR("%s WM%d latency not provided\n",
1937 name, level);
1938 continue;
1939 }
1940
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001941 /*
1942 * - latencies are in us on gen9.
1943 * - before then, WM1+ latency values are in 0.5us units
1944 */
1945 if (IS_GEN9(dev))
1946 latency *= 10;
1947 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001948 latency *= 5;
1949
1950 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1951 name, level, wm[level],
1952 latency / 10, latency % 10);
1953 }
1954}
1955
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001956static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1957 uint16_t wm[5], uint16_t min)
1958{
1959 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1960
1961 if (wm[0] >= min)
1962 return false;
1963
1964 wm[0] = max(wm[0], min);
1965 for (level = 1; level <= max_level; level++)
1966 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1967
1968 return true;
1969}
1970
1971static void snb_wm_latency_quirk(struct drm_device *dev)
1972{
1973 struct drm_i915_private *dev_priv = dev->dev_private;
1974 bool changed;
1975
1976 /*
1977 * The BIOS provided WM memory latency values are often
1978 * inadequate for high resolution displays. Adjust them.
1979 */
1980 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1981 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1982 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1983
1984 if (!changed)
1985 return;
1986
1987 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1988 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1989 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1990 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1991}
1992
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001993static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001994{
1995 struct drm_i915_private *dev_priv = dev->dev_private;
1996
1997 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1998
1999 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2000 sizeof(dev_priv->wm.pri_latency));
2001 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2002 sizeof(dev_priv->wm.pri_latency));
2003
2004 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2005 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002006
2007 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2008 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2009 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002010
2011 if (IS_GEN6(dev))
2012 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002013}
2014
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002015static void skl_setup_wm_latency(struct drm_device *dev)
2016{
2017 struct drm_i915_private *dev_priv = dev->dev_private;
2018
2019 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2020 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2021}
2022
Imre Deak820c1982013-12-17 14:46:36 +02002023static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002024 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002025{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002026 struct drm_device *dev = crtc->dev;
2027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2028 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002029 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002030
Matt Roper3ef00282015-03-09 10:19:24 -07002031 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002032 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002033
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002034 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002035 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002036 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roperc9f038a2015-03-09 11:06:02 -07002037
2038 if (crtc->primary->state->fb) {
2039 p->pri.enabled = true;
2040 p->pri.bytes_per_pixel =
2041 crtc->primary->state->fb->bits_per_pixel / 8;
2042 } else {
2043 p->pri.enabled = false;
2044 p->pri.bytes_per_pixel = 0;
2045 }
2046
2047 if (crtc->cursor->state->fb) {
2048 p->cur.enabled = true;
2049 p->cur.bytes_per_pixel = 4;
2050 } else {
2051 p->cur.enabled = false;
2052 p->cur.bytes_per_pixel = 0;
2053 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002054 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08002055 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002056
Matt Roperaf2b6532014-04-01 15:22:32 -07002057 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002058 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002059
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002060 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002061 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002062 break;
2063 }
2064 }
2065}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002066
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002067static void ilk_compute_wm_config(struct drm_device *dev,
2068 struct intel_wm_config *config)
2069{
2070 struct intel_crtc *intel_crtc;
2071
2072 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002073 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002074 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2075
2076 if (!wm->pipe_enabled)
2077 continue;
2078
2079 config->sprites_enabled |= wm->sprites_enabled;
2080 config->sprites_scaled |= wm->sprites_scaled;
2081 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002082 }
2083}
2084
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002085/* Compute new watermarks for the pipe */
2086static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002087 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002088 struct intel_pipe_wm *pipe_wm)
2089{
2090 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002091 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002092 int level, max_level = ilk_wm_max_level(dev);
2093 /* LP0 watermark maximums depend on this pipe alone */
2094 struct intel_wm_config config = {
2095 .num_pipes_active = 1,
2096 .sprites_enabled = params->spr.enabled,
2097 .sprites_scaled = params->spr.scaled,
2098 };
Imre Deak820c1982013-12-17 14:46:36 +02002099 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002100
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002101 pipe_wm->pipe_enabled = params->active;
2102 pipe_wm->sprites_enabled = params->spr.enabled;
2103 pipe_wm->sprites_scaled = params->spr.scaled;
2104
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002105 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2106 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2107 max_level = 1;
2108
2109 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2110 if (params->spr.scaled)
2111 max_level = 0;
2112
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002113 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002114
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002115 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002116 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002117
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002118 /* LP0 watermarks always use 1/2 DDB partitioning */
2119 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2120
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002121 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002122 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2123 return false;
2124
2125 ilk_compute_wm_reg_maximums(dev, 1, &max);
2126
2127 for (level = 1; level <= max_level; level++) {
2128 struct intel_wm_level wm = {};
2129
2130 ilk_compute_wm_level(dev_priv, level, params, &wm);
2131
2132 /*
2133 * Disable any watermark level that exceeds the
2134 * register maximums since such watermarks are
2135 * always invalid.
2136 */
2137 if (!ilk_validate_wm_level(level, &max, &wm))
2138 break;
2139
2140 pipe_wm->wm[level] = wm;
2141 }
2142
2143 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002144}
2145
2146/*
2147 * Merge the watermarks from all active pipes for a specific level.
2148 */
2149static void ilk_merge_wm_level(struct drm_device *dev,
2150 int level,
2151 struct intel_wm_level *ret_wm)
2152{
2153 const struct intel_crtc *intel_crtc;
2154
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002155 ret_wm->enable = true;
2156
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002157 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002158 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2159 const struct intel_wm_level *wm = &active->wm[level];
2160
2161 if (!active->pipe_enabled)
2162 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002163
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002164 /*
2165 * The watermark values may have been used in the past,
2166 * so we must maintain them in the registers for some
2167 * time even if the level is now disabled.
2168 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002169 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002170 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002171
2172 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2173 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2174 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2175 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2176 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002177}
2178
2179/*
2180 * Merge all low power watermarks for all active pipes.
2181 */
2182static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002183 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002184 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002185 struct intel_pipe_wm *merged)
2186{
2187 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002188 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002189
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002190 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2191 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2192 config->num_pipes_active > 1)
2193 return;
2194
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002195 /* ILK: FBC WM must be disabled always */
2196 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002197
2198 /* merge each WM1+ level */
2199 for (level = 1; level <= max_level; level++) {
2200 struct intel_wm_level *wm = &merged->wm[level];
2201
2202 ilk_merge_wm_level(dev, level, wm);
2203
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002204 if (level > last_enabled_level)
2205 wm->enable = false;
2206 else if (!ilk_validate_wm_level(level, max, wm))
2207 /* make sure all following levels get disabled */
2208 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002209
2210 /*
2211 * The spec says it is preferred to disable
2212 * FBC WMs instead of disabling a WM level.
2213 */
2214 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002215 if (wm->enable)
2216 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002217 wm->fbc_val = 0;
2218 }
2219 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002220
2221 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2222 /*
2223 * FIXME this is racy. FBC might get enabled later.
2224 * What we should check here is whether FBC can be
2225 * enabled sometime later.
2226 */
2227 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2228 for (level = 2; level <= max_level; level++) {
2229 struct intel_wm_level *wm = &merged->wm[level];
2230
2231 wm->enable = false;
2232 }
2233 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002234}
2235
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002236static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2237{
2238 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2239 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2240}
2241
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002242/* The value we need to program into the WM_LPx latency field */
2243static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2244{
2245 struct drm_i915_private *dev_priv = dev->dev_private;
2246
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002247 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002248 return 2 * level;
2249 else
2250 return dev_priv->wm.pri_latency[level];
2251}
2252
Imre Deak820c1982013-12-17 14:46:36 +02002253static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002254 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002255 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002256 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002257{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002258 struct intel_crtc *intel_crtc;
2259 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002260
Ville Syrjälä0362c782013-10-09 19:17:57 +03002261 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002262 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002263
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002264 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002265 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002266 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002267
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002268 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002269
Ville Syrjälä0362c782013-10-09 19:17:57 +03002270 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002271
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002272 /*
2273 * Maintain the watermark values even if the level is
2274 * disabled. Doing otherwise could cause underruns.
2275 */
2276 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002277 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002278 (r->pri_val << WM1_LP_SR_SHIFT) |
2279 r->cur_val;
2280
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002281 if (r->enable)
2282 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2283
Ville Syrjälä416f4722013-11-02 21:07:46 -07002284 if (INTEL_INFO(dev)->gen >= 8)
2285 results->wm_lp[wm_lp - 1] |=
2286 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2287 else
2288 results->wm_lp[wm_lp - 1] |=
2289 r->fbc_val << WM1_LP_FBC_SHIFT;
2290
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002291 /*
2292 * Always set WM1S_LP_EN when spr_val != 0, even if the
2293 * level is disabled. Doing otherwise could cause underruns.
2294 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002295 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2296 WARN_ON(wm_lp != 1);
2297 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2298 } else
2299 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002300 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002301
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002302 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002303 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002304 enum pipe pipe = intel_crtc->pipe;
2305 const struct intel_wm_level *r =
2306 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002307
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002308 if (WARN_ON(!r->enable))
2309 continue;
2310
2311 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2312
2313 results->wm_pipe[pipe] =
2314 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2315 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2316 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002317 }
2318}
2319
Paulo Zanoni861f3382013-05-31 10:19:21 -03002320/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2321 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002322static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002323 struct intel_pipe_wm *r1,
2324 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002325{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002326 int level, max_level = ilk_wm_max_level(dev);
2327 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002328
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002329 for (level = 1; level <= max_level; level++) {
2330 if (r1->wm[level].enable)
2331 level1 = level;
2332 if (r2->wm[level].enable)
2333 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002334 }
2335
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002336 if (level1 == level2) {
2337 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002338 return r2;
2339 else
2340 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002341 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002342 return r1;
2343 } else {
2344 return r2;
2345 }
2346}
2347
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002348/* dirty bits used to track which watermarks need changes */
2349#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2350#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2351#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2352#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2353#define WM_DIRTY_FBC (1 << 24)
2354#define WM_DIRTY_DDB (1 << 25)
2355
Damien Lespiau055e3932014-08-18 13:49:10 +01002356static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002357 const struct ilk_wm_values *old,
2358 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002359{
2360 unsigned int dirty = 0;
2361 enum pipe pipe;
2362 int wm_lp;
2363
Damien Lespiau055e3932014-08-18 13:49:10 +01002364 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002365 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2366 dirty |= WM_DIRTY_LINETIME(pipe);
2367 /* Must disable LP1+ watermarks too */
2368 dirty |= WM_DIRTY_LP_ALL;
2369 }
2370
2371 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2372 dirty |= WM_DIRTY_PIPE(pipe);
2373 /* Must disable LP1+ watermarks too */
2374 dirty |= WM_DIRTY_LP_ALL;
2375 }
2376 }
2377
2378 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2379 dirty |= WM_DIRTY_FBC;
2380 /* Must disable LP1+ watermarks too */
2381 dirty |= WM_DIRTY_LP_ALL;
2382 }
2383
2384 if (old->partitioning != new->partitioning) {
2385 dirty |= WM_DIRTY_DDB;
2386 /* Must disable LP1+ watermarks too */
2387 dirty |= WM_DIRTY_LP_ALL;
2388 }
2389
2390 /* LP1+ watermarks already deemed dirty, no need to continue */
2391 if (dirty & WM_DIRTY_LP_ALL)
2392 return dirty;
2393
2394 /* Find the lowest numbered LP1+ watermark in need of an update... */
2395 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2396 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2397 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2398 break;
2399 }
2400
2401 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2402 for (; wm_lp <= 3; wm_lp++)
2403 dirty |= WM_DIRTY_LP(wm_lp);
2404
2405 return dirty;
2406}
2407
Ville Syrjälä8553c182013-12-05 15:51:39 +02002408static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2409 unsigned int dirty)
2410{
Imre Deak820c1982013-12-17 14:46:36 +02002411 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002412 bool changed = false;
2413
2414 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2415 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2416 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2417 changed = true;
2418 }
2419 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2420 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2421 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2422 changed = true;
2423 }
2424 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2425 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2426 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2427 changed = true;
2428 }
2429
2430 /*
2431 * Don't touch WM1S_LP_EN here.
2432 * Doing so could cause underruns.
2433 */
2434
2435 return changed;
2436}
2437
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002438/*
2439 * The spec says we shouldn't write when we don't need, because every write
2440 * causes WMs to be re-evaluated, expending some power.
2441 */
Imre Deak820c1982013-12-17 14:46:36 +02002442static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2443 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002444{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002445 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002446 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002447 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002448 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002449
Damien Lespiau055e3932014-08-18 13:49:10 +01002450 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002451 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002452 return;
2453
Ville Syrjälä8553c182013-12-05 15:51:39 +02002454 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002455
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002456 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002457 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002458 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002459 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002460 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002461 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2462
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002463 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002464 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002465 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002466 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002467 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002468 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2469
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002470 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002471 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002472 val = I915_READ(WM_MISC);
2473 if (results->partitioning == INTEL_DDB_PART_1_2)
2474 val &= ~WM_MISC_DATA_PARTITION_5_6;
2475 else
2476 val |= WM_MISC_DATA_PARTITION_5_6;
2477 I915_WRITE(WM_MISC, val);
2478 } else {
2479 val = I915_READ(DISP_ARB_CTL2);
2480 if (results->partitioning == INTEL_DDB_PART_1_2)
2481 val &= ~DISP_DATA_PARTITION_5_6;
2482 else
2483 val |= DISP_DATA_PARTITION_5_6;
2484 I915_WRITE(DISP_ARB_CTL2, val);
2485 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002486 }
2487
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002488 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002489 val = I915_READ(DISP_ARB_CTL);
2490 if (results->enable_fbc_wm)
2491 val &= ~DISP_FBC_WM_DIS;
2492 else
2493 val |= DISP_FBC_WM_DIS;
2494 I915_WRITE(DISP_ARB_CTL, val);
2495 }
2496
Imre Deak954911e2013-12-17 14:46:34 +02002497 if (dirty & WM_DIRTY_LP(1) &&
2498 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2499 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2500
2501 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002502 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2503 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2504 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2505 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2506 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002507
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002508 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002509 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002510 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002511 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002512 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002513 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002514
2515 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002516}
2517
Ville Syrjälä8553c182013-12-05 15:51:39 +02002518static bool ilk_disable_lp_wm(struct drm_device *dev)
2519{
2520 struct drm_i915_private *dev_priv = dev->dev_private;
2521
2522 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2523}
2524
Damien Lespiaub9cec072014-11-04 17:06:43 +00002525/*
2526 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2527 * different active planes.
2528 */
2529
2530#define SKL_DDB_SIZE 896 /* in blocks */
2531
2532static void
2533skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2534 struct drm_crtc *for_crtc,
2535 const struct intel_wm_config *config,
2536 const struct skl_pipe_wm_parameters *params,
2537 struct skl_ddb_entry *alloc /* out */)
2538{
2539 struct drm_crtc *crtc;
2540 unsigned int pipe_size, ddb_size;
2541 int nth_active_pipe;
2542
2543 if (!params->active) {
2544 alloc->start = 0;
2545 alloc->end = 0;
2546 return;
2547 }
2548
2549 ddb_size = SKL_DDB_SIZE;
2550
2551 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2552
2553 nth_active_pipe = 0;
2554 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002555 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002556 continue;
2557
2558 if (crtc == for_crtc)
2559 break;
2560
2561 nth_active_pipe++;
2562 }
2563
2564 pipe_size = ddb_size / config->num_pipes_active;
2565 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002566 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002567}
2568
2569static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2570{
2571 if (config->num_pipes_active == 1)
2572 return 32;
2573
2574 return 8;
2575}
2576
Damien Lespiaua269c582014-11-04 17:06:49 +00002577static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2578{
2579 entry->start = reg & 0x3ff;
2580 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002581 if (entry->end)
2582 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002583}
2584
Damien Lespiau08db6652014-11-04 17:06:52 +00002585void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2586 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002587{
Damien Lespiaua269c582014-11-04 17:06:49 +00002588 enum pipe pipe;
2589 int plane;
2590 u32 val;
2591
2592 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002593 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002594 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2595 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2596 val);
2597 }
2598
2599 val = I915_READ(CUR_BUF_CFG(pipe));
2600 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2601 }
2602}
2603
Damien Lespiaub9cec072014-11-04 17:06:43 +00002604static unsigned int
2605skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2606{
2607 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2608}
2609
2610/*
2611 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2612 * a 8192x4096@32bpp framebuffer:
2613 * 3 * 4096 * 8192 * 4 < 2^32
2614 */
2615static unsigned int
2616skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2617 const struct skl_pipe_wm_parameters *params)
2618{
2619 unsigned int total_data_rate = 0;
2620 int plane;
2621
2622 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2623 const struct intel_plane_wm_parameters *p;
2624
2625 p = &params->plane[plane];
2626 if (!p->enabled)
2627 continue;
2628
2629 total_data_rate += skl_plane_relative_data_rate(p);
2630 }
2631
2632 return total_data_rate;
2633}
2634
2635static void
2636skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2637 const struct intel_wm_config *config,
2638 const struct skl_pipe_wm_parameters *params,
2639 struct skl_ddb_allocation *ddb /* out */)
2640{
2641 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002642 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2644 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002645 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002646 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002647 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002648 unsigned int total_data_rate;
2649 int plane;
2650
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002651 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2652 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002653 if (alloc_size == 0) {
2654 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2655 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2656 return;
2657 }
2658
2659 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002660 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2661 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002662
2663 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002664 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002665
Damien Lespiau80958152015-02-09 13:35:10 +00002666 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002667 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002668 const struct intel_plane_wm_parameters *p;
2669
2670 p = &params->plane[plane];
2671 if (!p->enabled)
2672 continue;
2673
2674 minimum[plane] = 8;
2675 alloc_size -= minimum[plane];
2676 }
2677
Damien Lespiaub9cec072014-11-04 17:06:43 +00002678 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002679 * 2. Distribute the remaining space in proportion to the amount of
2680 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002681 *
2682 * FIXME: we may not allocate every single block here.
2683 */
2684 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2685
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002686 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002687 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2688 const struct intel_plane_wm_parameters *p;
2689 unsigned int data_rate;
2690 uint16_t plane_blocks;
2691
2692 p = &params->plane[plane];
2693 if (!p->enabled)
2694 continue;
2695
2696 data_rate = skl_plane_relative_data_rate(p);
2697
2698 /*
2699 * promote the expression to 64 bits to avoid overflowing, the
2700 * result is < available as data_rate / total_data_rate < 1
2701 */
Damien Lespiau80958152015-02-09 13:35:10 +00002702 plane_blocks = minimum[plane];
2703 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2704 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002705
2706 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002707 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002708
2709 start += plane_blocks;
2710 }
2711
2712}
2713
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002714static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002715{
2716 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002717 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002718}
2719
2720/*
2721 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2722 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2723 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2724 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2725*/
2726static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2727 uint32_t latency)
2728{
2729 uint32_t wm_intermediate_val, ret;
2730
2731 if (latency == 0)
2732 return UINT_MAX;
2733
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002734 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002735 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2736
2737 return ret;
2738}
2739
2740static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2741 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002742 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002743{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002744 uint32_t ret;
2745 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2746 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002747
2748 if (latency == 0)
2749 return UINT_MAX;
2750
2751 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002752
2753 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2754 tiling == I915_FORMAT_MOD_Yf_TILED) {
2755 plane_bytes_per_line *= 4;
2756 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2757 plane_blocks_per_line /= 4;
2758 } else {
2759 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2760 }
2761
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002762 wm_intermediate_val = latency * pixel_rate;
2763 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002764 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002765
2766 return ret;
2767}
2768
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002769static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2770 const struct intel_crtc *intel_crtc)
2771{
2772 struct drm_device *dev = intel_crtc->base.dev;
2773 struct drm_i915_private *dev_priv = dev->dev_private;
2774 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2775 enum pipe pipe = intel_crtc->pipe;
2776
2777 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2778 sizeof(new_ddb->plane[pipe])))
2779 return true;
2780
2781 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2782 sizeof(new_ddb->cursor[pipe])))
2783 return true;
2784
2785 return false;
2786}
2787
2788static void skl_compute_wm_global_parameters(struct drm_device *dev,
2789 struct intel_wm_config *config)
2790{
2791 struct drm_crtc *crtc;
2792 struct drm_plane *plane;
2793
2794 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07002795 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002796
2797 /* FIXME: I don't think we need those two global parameters on SKL */
2798 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2799 struct intel_plane *intel_plane = to_intel_plane(plane);
2800
2801 config->sprites_enabled |= intel_plane->wm.enabled;
2802 config->sprites_scaled |= intel_plane->wm.scaled;
2803 }
2804}
2805
2806static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2807 struct skl_pipe_wm_parameters *p)
2808{
2809 struct drm_device *dev = crtc->dev;
2810 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2811 enum pipe pipe = intel_crtc->pipe;
2812 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002813 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002814 int i = 1; /* Index for sprite planes start */
2815
Matt Roper3ef00282015-03-09 10:19:24 -07002816 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002817 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002818 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2819 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002820
Matt Roperc9f038a2015-03-09 11:06:02 -07002821 fb = crtc->primary->state->fb;
2822 if (fb) {
2823 p->plane[0].enabled = true;
2824 p->plane[0].bytes_per_pixel = fb->bits_per_pixel / 8;
2825 p->plane[0].tiling = fb->modifier[0];
2826 } else {
2827 p->plane[0].enabled = false;
2828 p->plane[0].bytes_per_pixel = 0;
2829 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2830 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002831 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2832 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002833
Matt Roperc9f038a2015-03-09 11:06:02 -07002834 fb = crtc->cursor->state->fb;
2835 if (fb) {
2836 p->cursor.enabled = true;
2837 p->cursor.bytes_per_pixel = fb->bits_per_pixel / 8;
2838 p->cursor.horiz_pixels = crtc->cursor->state->crtc_w;
2839 p->cursor.vert_pixels = crtc->cursor->state->crtc_h;
2840 } else {
2841 p->cursor.enabled = false;
2842 p->cursor.bytes_per_pixel = 0;
2843 p->cursor.horiz_pixels = 64;
2844 p->cursor.vert_pixels = 64;
2845 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002846 }
2847
2848 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2849 struct intel_plane *intel_plane = to_intel_plane(plane);
2850
Sonika Jindala712f8e2014-12-09 10:59:15 +05302851 if (intel_plane->pipe == pipe &&
2852 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002853 p->plane[i++] = intel_plane->wm;
2854 }
2855}
2856
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002857static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2858 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002859 struct intel_plane_wm_parameters *p_params,
2860 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002861 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002862 uint16_t *out_blocks, /* out */
2863 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002864{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002865 uint32_t latency = dev_priv->wm.skl_latency[level];
2866 uint32_t method1, method2;
2867 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2868 uint32_t res_blocks, res_lines;
2869 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002870
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002871 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002872 return false;
2873
2874 method1 = skl_wm_method1(p->pixel_rate,
2875 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002876 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002877 method2 = skl_wm_method2(p->pixel_rate,
2878 p->pipe_htotal,
2879 p_params->horiz_pixels,
2880 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002881 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002882 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002883
2884 plane_bytes_per_line = p_params->horiz_pixels *
2885 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002886 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002887
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002888 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2889 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2890 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2891 selected_result = max(method2, y_tile_minimum);
2892 } else {
2893 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2894 selected_result = min(method1, method2);
2895 else
2896 selected_result = method1;
2897 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002898
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002899 res_blocks = selected_result + 1;
2900 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002901
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002902 if (level >= 1 && level <= 7) {
2903 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2904 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2905 res_lines += 4;
2906 else
2907 res_blocks++;
2908 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002909
2910 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002911 return false;
2912
2913 *out_blocks = res_blocks;
2914 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002915
2916 return true;
2917}
2918
2919static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2920 struct skl_ddb_allocation *ddb,
2921 struct skl_pipe_wm_parameters *p,
2922 enum pipe pipe,
2923 int level,
2924 int num_planes,
2925 struct skl_wm_level *result)
2926{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002927 uint16_t ddb_blocks;
2928 int i;
2929
2930 for (i = 0; i < num_planes; i++) {
2931 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2932
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002933 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2934 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002935 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002936 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002937 &result->plane_res_b[i],
2938 &result->plane_res_l[i]);
2939 }
2940
2941 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002942 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2943 ddb_blocks, level,
2944 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002945 &result->cursor_res_l);
2946}
2947
Damien Lespiau407b50f2014-11-04 17:06:57 +00002948static uint32_t
2949skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2950{
Matt Roper3ef00282015-03-09 10:19:24 -07002951 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002952 return 0;
2953
2954 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2955
2956}
2957
2958static void skl_compute_transition_wm(struct drm_crtc *crtc,
2959 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002960 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002961{
Damien Lespiau9414f562014-11-04 17:06:58 +00002962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2963 int i;
2964
Damien Lespiau407b50f2014-11-04 17:06:57 +00002965 if (!params->active)
2966 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002967
2968 /* Until we know more, just disable transition WMs */
2969 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2970 trans_wm->plane_en[i] = false;
2971 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002972}
2973
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002974static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2975 struct skl_ddb_allocation *ddb,
2976 struct skl_pipe_wm_parameters *params,
2977 struct skl_pipe_wm *pipe_wm)
2978{
2979 struct drm_device *dev = crtc->dev;
2980 const struct drm_i915_private *dev_priv = dev->dev_private;
2981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2982 int level, max_level = ilk_wm_max_level(dev);
2983
2984 for (level = 0; level <= max_level; level++) {
2985 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2986 level, intel_num_planes(intel_crtc),
2987 &pipe_wm->wm[level]);
2988 }
2989 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2990
Damien Lespiau9414f562014-11-04 17:06:58 +00002991 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002992}
2993
2994static void skl_compute_wm_results(struct drm_device *dev,
2995 struct skl_pipe_wm_parameters *p,
2996 struct skl_pipe_wm *p_wm,
2997 struct skl_wm_values *r,
2998 struct intel_crtc *intel_crtc)
2999{
3000 int level, max_level = ilk_wm_max_level(dev);
3001 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003002 uint32_t temp;
3003 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003004
3005 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003006 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3007 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003008
3009 temp |= p_wm->wm[level].plane_res_l[i] <<
3010 PLANE_WM_LINES_SHIFT;
3011 temp |= p_wm->wm[level].plane_res_b[i];
3012 if (p_wm->wm[level].plane_en[i])
3013 temp |= PLANE_WM_EN;
3014
3015 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003016 }
3017
3018 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003019
3020 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3021 temp |= p_wm->wm[level].cursor_res_b;
3022
3023 if (p_wm->wm[level].cursor_en)
3024 temp |= PLANE_WM_EN;
3025
3026 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003027
3028 }
3029
Damien Lespiau9414f562014-11-04 17:06:58 +00003030 /* transition WMs */
3031 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3032 temp = 0;
3033 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3034 temp |= p_wm->trans_wm.plane_res_b[i];
3035 if (p_wm->trans_wm.plane_en[i])
3036 temp |= PLANE_WM_EN;
3037
3038 r->plane_trans[pipe][i] = temp;
3039 }
3040
3041 temp = 0;
3042 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3043 temp |= p_wm->trans_wm.cursor_res_b;
3044 if (p_wm->trans_wm.cursor_en)
3045 temp |= PLANE_WM_EN;
3046
3047 r->cursor_trans[pipe] = temp;
3048
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003049 r->wm_linetime[pipe] = p_wm->linetime;
3050}
3051
Damien Lespiau16160e32014-11-04 17:06:53 +00003052static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3053 const struct skl_ddb_entry *entry)
3054{
3055 if (entry->end)
3056 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3057 else
3058 I915_WRITE(reg, 0);
3059}
3060
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003061static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3062 const struct skl_wm_values *new)
3063{
3064 struct drm_device *dev = dev_priv->dev;
3065 struct intel_crtc *crtc;
3066
3067 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3068 int i, level, max_level = ilk_wm_max_level(dev);
3069 enum pipe pipe = crtc->pipe;
3070
Damien Lespiau5d374d92014-11-04 17:07:00 +00003071 if (!new->dirty[pipe])
3072 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003073
Damien Lespiau5d374d92014-11-04 17:07:00 +00003074 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3075
3076 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003077 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003078 I915_WRITE(PLANE_WM(pipe, i, level),
3079 new->plane[pipe][i][level]);
3080 I915_WRITE(CUR_WM(pipe, level),
3081 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003082 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003083 for (i = 0; i < intel_num_planes(crtc); i++)
3084 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3085 new->plane_trans[pipe][i]);
3086 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3087
3088 for (i = 0; i < intel_num_planes(crtc); i++)
3089 skl_ddb_entry_write(dev_priv,
3090 PLANE_BUF_CFG(pipe, i),
3091 &new->ddb.plane[pipe][i]);
3092
3093 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3094 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003095 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003096}
3097
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003098/*
3099 * When setting up a new DDB allocation arrangement, we need to correctly
3100 * sequence the times at which the new allocations for the pipes are taken into
3101 * account or we'll have pipes fetching from space previously allocated to
3102 * another pipe.
3103 *
3104 * Roughly the sequence looks like:
3105 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3106 * overlapping with a previous light-up pipe (another way to put it is:
3107 * pipes with their new allocation strickly included into their old ones).
3108 * 2. re-allocate the other pipes that get their allocation reduced
3109 * 3. allocate the pipes having their allocation increased
3110 *
3111 * Steps 1. and 2. are here to take care of the following case:
3112 * - Initially DDB looks like this:
3113 * | B | C |
3114 * - enable pipe A.
3115 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3116 * allocation
3117 * | A | B | C |
3118 *
3119 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3120 */
3121
Damien Lespiaud21b7952014-11-04 17:07:03 +00003122static void
3123skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003124{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003125 int plane;
3126
Damien Lespiaud21b7952014-11-04 17:07:03 +00003127 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3128
Damien Lespiaudd740782015-02-28 14:54:08 +00003129 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003130 I915_WRITE(PLANE_SURF(pipe, plane),
3131 I915_READ(PLANE_SURF(pipe, plane)));
3132 }
3133 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3134}
3135
3136static bool
3137skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3138 const struct skl_ddb_allocation *new,
3139 enum pipe pipe)
3140{
3141 uint16_t old_size, new_size;
3142
3143 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3144 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3145
3146 return old_size != new_size &&
3147 new->pipe[pipe].start >= old->pipe[pipe].start &&
3148 new->pipe[pipe].end <= old->pipe[pipe].end;
3149}
3150
3151static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3152 struct skl_wm_values *new_values)
3153{
3154 struct drm_device *dev = dev_priv->dev;
3155 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3156 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3157 struct intel_crtc *crtc;
3158 enum pipe pipe;
3159
3160 new_ddb = &new_values->ddb;
3161 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3162
3163 /*
3164 * First pass: flush the pipes with the new allocation contained into
3165 * the old space.
3166 *
3167 * We'll wait for the vblank on those pipes to ensure we can safely
3168 * re-allocate the freed space without this pipe fetching from it.
3169 */
3170 for_each_intel_crtc(dev, crtc) {
3171 if (!crtc->active)
3172 continue;
3173
3174 pipe = crtc->pipe;
3175
3176 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3177 continue;
3178
Damien Lespiaud21b7952014-11-04 17:07:03 +00003179 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003180 intel_wait_for_vblank(dev, pipe);
3181
3182 reallocated[pipe] = true;
3183 }
3184
3185
3186 /*
3187 * Second pass: flush the pipes that are having their allocation
3188 * reduced, but overlapping with a previous allocation.
3189 *
3190 * Here as well we need to wait for the vblank to make sure the freed
3191 * space is not used anymore.
3192 */
3193 for_each_intel_crtc(dev, crtc) {
3194 if (!crtc->active)
3195 continue;
3196
3197 pipe = crtc->pipe;
3198
3199 if (reallocated[pipe])
3200 continue;
3201
3202 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3203 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003204 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003205 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303206 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003207 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003208 }
3209
3210 /*
3211 * Third pass: flush the pipes that got more space allocated.
3212 *
3213 * We don't need to actively wait for the update here, next vblank
3214 * will just get more DDB space with the correct WM values.
3215 */
3216 for_each_intel_crtc(dev, crtc) {
3217 if (!crtc->active)
3218 continue;
3219
3220 pipe = crtc->pipe;
3221
3222 /*
3223 * At this point, only the pipes more space than before are
3224 * left to re-allocate.
3225 */
3226 if (reallocated[pipe])
3227 continue;
3228
Damien Lespiaud21b7952014-11-04 17:07:03 +00003229 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003230 }
3231}
3232
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003233static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3234 struct skl_pipe_wm_parameters *params,
3235 struct intel_wm_config *config,
3236 struct skl_ddb_allocation *ddb, /* out */
3237 struct skl_pipe_wm *pipe_wm /* out */)
3238{
3239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3240
3241 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003242 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003243 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3244
3245 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3246 return false;
3247
3248 intel_crtc->wm.skl_active = *pipe_wm;
3249 return true;
3250}
3251
3252static void skl_update_other_pipe_wm(struct drm_device *dev,
3253 struct drm_crtc *crtc,
3254 struct intel_wm_config *config,
3255 struct skl_wm_values *r)
3256{
3257 struct intel_crtc *intel_crtc;
3258 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3259
3260 /*
3261 * If the WM update hasn't changed the allocation for this_crtc (the
3262 * crtc we are currently computing the new WM values for), other
3263 * enabled crtcs will keep the same allocation and we don't need to
3264 * recompute anything for them.
3265 */
3266 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3267 return;
3268
3269 /*
3270 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3271 * other active pipes need new DDB allocation and WM values.
3272 */
3273 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3274 base.head) {
3275 struct skl_pipe_wm_parameters params = {};
3276 struct skl_pipe_wm pipe_wm = {};
3277 bool wm_changed;
3278
3279 if (this_crtc->pipe == intel_crtc->pipe)
3280 continue;
3281
3282 if (!intel_crtc->active)
3283 continue;
3284
3285 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3286 &params, config,
3287 &r->ddb, &pipe_wm);
3288
3289 /*
3290 * If we end up re-computing the other pipe WM values, it's
3291 * because it was really needed, so we expect the WM values to
3292 * be different.
3293 */
3294 WARN_ON(!wm_changed);
3295
3296 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3297 r->dirty[intel_crtc->pipe] = true;
3298 }
3299}
3300
3301static void skl_update_wm(struct drm_crtc *crtc)
3302{
3303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3304 struct drm_device *dev = crtc->dev;
3305 struct drm_i915_private *dev_priv = dev->dev_private;
3306 struct skl_pipe_wm_parameters params = {};
3307 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3308 struct skl_pipe_wm pipe_wm = {};
3309 struct intel_wm_config config = {};
3310
3311 memset(results, 0, sizeof(*results));
3312
3313 skl_compute_wm_global_parameters(dev, &config);
3314
3315 if (!skl_update_pipe_wm(crtc, &params, &config,
3316 &results->ddb, &pipe_wm))
3317 return;
3318
3319 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3320 results->dirty[intel_crtc->pipe] = true;
3321
3322 skl_update_other_pipe_wm(dev, crtc, &config, results);
3323 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003324 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003325
3326 /* store the new configuration */
3327 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003328}
3329
3330static void
3331skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3332 uint32_t sprite_width, uint32_t sprite_height,
3333 int pixel_size, bool enabled, bool scaled)
3334{
3335 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003336 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003337
3338 intel_plane->wm.enabled = enabled;
3339 intel_plane->wm.scaled = scaled;
3340 intel_plane->wm.horiz_pixels = sprite_width;
3341 intel_plane->wm.vert_pixels = sprite_height;
3342 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003343 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3344 /*
3345 * Framebuffer can be NULL on plane disable, but it does not
3346 * matter for watermarks if we assume no tiling in that case.
3347 */
3348 if (fb)
3349 intel_plane->wm.tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003350
3351 skl_update_wm(crtc);
3352}
3353
Imre Deak820c1982013-12-17 14:46:36 +02003354static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003355{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003357 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003358 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003359 struct ilk_wm_maximums max;
3360 struct ilk_pipe_wm_parameters params = {};
3361 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003362 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003363 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003364 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003365 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003366
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003367 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003368
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003369 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3370
3371 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3372 return;
3373
3374 intel_crtc->wm.active = pipe_wm;
3375
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003376 ilk_compute_wm_config(dev, &config);
3377
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003378 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003379 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003380
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003381 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003382 if (INTEL_INFO(dev)->gen >= 7 &&
3383 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003384 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003385 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003386
Imre Deak820c1982013-12-17 14:46:36 +02003387 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003388 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003389 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003390 }
3391
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003392 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003393 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003394
Imre Deak820c1982013-12-17 14:46:36 +02003395 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003396
Imre Deak820c1982013-12-17 14:46:36 +02003397 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003398}
3399
Damien Lespiaued57cb82014-07-15 09:21:24 +02003400static void
3401ilk_update_sprite_wm(struct drm_plane *plane,
3402 struct drm_crtc *crtc,
3403 uint32_t sprite_width, uint32_t sprite_height,
3404 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003405{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003406 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003407 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003408
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003409 intel_plane->wm.enabled = enabled;
3410 intel_plane->wm.scaled = scaled;
3411 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003412 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003413 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003414
Ville Syrjälä8553c182013-12-05 15:51:39 +02003415 /*
3416 * IVB workaround: must disable low power watermarks for at least
3417 * one frame before enabling scaling. LP watermarks can be re-enabled
3418 * when scaling is disabled.
3419 *
3420 * WaCxSRDisabledForSpriteScaling:ivb
3421 */
3422 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3423 intel_wait_for_vblank(dev, intel_plane->pipe);
3424
Imre Deak820c1982013-12-17 14:46:36 +02003425 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003426}
3427
Pradeep Bhat30789992014-11-04 17:06:45 +00003428static void skl_pipe_wm_active_state(uint32_t val,
3429 struct skl_pipe_wm *active,
3430 bool is_transwm,
3431 bool is_cursor,
3432 int i,
3433 int level)
3434{
3435 bool is_enabled = (val & PLANE_WM_EN) != 0;
3436
3437 if (!is_transwm) {
3438 if (!is_cursor) {
3439 active->wm[level].plane_en[i] = is_enabled;
3440 active->wm[level].plane_res_b[i] =
3441 val & PLANE_WM_BLOCKS_MASK;
3442 active->wm[level].plane_res_l[i] =
3443 (val >> PLANE_WM_LINES_SHIFT) &
3444 PLANE_WM_LINES_MASK;
3445 } else {
3446 active->wm[level].cursor_en = is_enabled;
3447 active->wm[level].cursor_res_b =
3448 val & PLANE_WM_BLOCKS_MASK;
3449 active->wm[level].cursor_res_l =
3450 (val >> PLANE_WM_LINES_SHIFT) &
3451 PLANE_WM_LINES_MASK;
3452 }
3453 } else {
3454 if (!is_cursor) {
3455 active->trans_wm.plane_en[i] = is_enabled;
3456 active->trans_wm.plane_res_b[i] =
3457 val & PLANE_WM_BLOCKS_MASK;
3458 active->trans_wm.plane_res_l[i] =
3459 (val >> PLANE_WM_LINES_SHIFT) &
3460 PLANE_WM_LINES_MASK;
3461 } else {
3462 active->trans_wm.cursor_en = is_enabled;
3463 active->trans_wm.cursor_res_b =
3464 val & PLANE_WM_BLOCKS_MASK;
3465 active->trans_wm.cursor_res_l =
3466 (val >> PLANE_WM_LINES_SHIFT) &
3467 PLANE_WM_LINES_MASK;
3468 }
3469 }
3470}
3471
3472static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3473{
3474 struct drm_device *dev = crtc->dev;
3475 struct drm_i915_private *dev_priv = dev->dev_private;
3476 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3478 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3479 enum pipe pipe = intel_crtc->pipe;
3480 int level, i, max_level;
3481 uint32_t temp;
3482
3483 max_level = ilk_wm_max_level(dev);
3484
3485 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3486
3487 for (level = 0; level <= max_level; level++) {
3488 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3489 hw->plane[pipe][i][level] =
3490 I915_READ(PLANE_WM(pipe, i, level));
3491 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3492 }
3493
3494 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3495 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3496 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3497
Matt Roper3ef00282015-03-09 10:19:24 -07003498 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003499 return;
3500
3501 hw->dirty[pipe] = true;
3502
3503 active->linetime = hw->wm_linetime[pipe];
3504
3505 for (level = 0; level <= max_level; level++) {
3506 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3507 temp = hw->plane[pipe][i][level];
3508 skl_pipe_wm_active_state(temp, active, false,
3509 false, i, level);
3510 }
3511 temp = hw->cursor[pipe][level];
3512 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3513 }
3514
3515 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3516 temp = hw->plane_trans[pipe][i];
3517 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3518 }
3519
3520 temp = hw->cursor_trans[pipe];
3521 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3522}
3523
3524void skl_wm_get_hw_state(struct drm_device *dev)
3525{
Damien Lespiaua269c582014-11-04 17:06:49 +00003526 struct drm_i915_private *dev_priv = dev->dev_private;
3527 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003528 struct drm_crtc *crtc;
3529
Damien Lespiaua269c582014-11-04 17:06:49 +00003530 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003531 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3532 skl_pipe_wm_get_hw_state(crtc);
3533}
3534
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003535static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3536{
3537 struct drm_device *dev = crtc->dev;
3538 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003539 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3541 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3542 enum pipe pipe = intel_crtc->pipe;
3543 static const unsigned int wm0_pipe_reg[] = {
3544 [PIPE_A] = WM0_PIPEA_ILK,
3545 [PIPE_B] = WM0_PIPEB_ILK,
3546 [PIPE_C] = WM0_PIPEC_IVB,
3547 };
3548
3549 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003550 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003551 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003552
Matt Roper3ef00282015-03-09 10:19:24 -07003553 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003554
3555 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003556 u32 tmp = hw->wm_pipe[pipe];
3557
3558 /*
3559 * For active pipes LP0 watermark is marked as
3560 * enabled, and LP1+ watermaks as disabled since
3561 * we can't really reverse compute them in case
3562 * multiple pipes are active.
3563 */
3564 active->wm[0].enable = true;
3565 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3566 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3567 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3568 active->linetime = hw->wm_linetime[pipe];
3569 } else {
3570 int level, max_level = ilk_wm_max_level(dev);
3571
3572 /*
3573 * For inactive pipes, all watermark levels
3574 * should be marked as enabled but zeroed,
3575 * which is what we'd compute them to.
3576 */
3577 for (level = 0; level <= max_level; level++)
3578 active->wm[level].enable = true;
3579 }
3580}
3581
3582void ilk_wm_get_hw_state(struct drm_device *dev)
3583{
3584 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003585 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003586 struct drm_crtc *crtc;
3587
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003588 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003589 ilk_pipe_wm_get_hw_state(crtc);
3590
3591 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3592 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3593 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3594
3595 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003596 if (INTEL_INFO(dev)->gen >= 7) {
3597 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3598 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3599 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003600
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003601 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003602 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3603 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3604 else if (IS_IVYBRIDGE(dev))
3605 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3606 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003607
3608 hw->enable_fbc_wm =
3609 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3610}
3611
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003612/**
3613 * intel_update_watermarks - update FIFO watermark values based on current modes
3614 *
3615 * Calculate watermark values for the various WM regs based on current mode
3616 * and plane configuration.
3617 *
3618 * There are several cases to deal with here:
3619 * - normal (i.e. non-self-refresh)
3620 * - self-refresh (SR) mode
3621 * - lines are large relative to FIFO size (buffer can hold up to 2)
3622 * - lines are small relative to FIFO size (buffer can hold more than 2
3623 * lines), so need to account for TLB latency
3624 *
3625 * The normal calculation is:
3626 * watermark = dotclock * bytes per pixel * latency
3627 * where latency is platform & configuration dependent (we assume pessimal
3628 * values here).
3629 *
3630 * The SR calculation is:
3631 * watermark = (trunc(latency/line time)+1) * surface width *
3632 * bytes per pixel
3633 * where
3634 * line time = htotal / dotclock
3635 * surface width = hdisplay for normal plane and 64 for cursor
3636 * and latency is assumed to be high, as above.
3637 *
3638 * The final value programmed to the register should always be rounded up,
3639 * and include an extra 2 entries to account for clock crossings.
3640 *
3641 * We don't use the sprite, so we can ignore that. And on Crestline we have
3642 * to set the non-SR watermarks to 8.
3643 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003644void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003645{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003646 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003647
3648 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003649 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003650}
3651
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003652void intel_update_sprite_watermarks(struct drm_plane *plane,
3653 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003654 uint32_t sprite_width,
3655 uint32_t sprite_height,
3656 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003657 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003658{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003659 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003660
3661 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003662 dev_priv->display.update_sprite_wm(plane, crtc,
3663 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003664 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003665}
3666
Daniel Vetter92703882012-08-09 16:46:01 +02003667/**
3668 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003669 */
3670DEFINE_SPINLOCK(mchdev_lock);
3671
3672/* Global for IPS driver to get at the current i915 device. Protected by
3673 * mchdev_lock. */
3674static struct drm_i915_private *i915_mch_dev;
3675
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003676bool ironlake_set_drps(struct drm_device *dev, u8 val)
3677{
3678 struct drm_i915_private *dev_priv = dev->dev_private;
3679 u16 rgvswctl;
3680
Daniel Vetter92703882012-08-09 16:46:01 +02003681 assert_spin_locked(&mchdev_lock);
3682
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003683 rgvswctl = I915_READ16(MEMSWCTL);
3684 if (rgvswctl & MEMCTL_CMD_STS) {
3685 DRM_DEBUG("gpu busy, RCS change rejected\n");
3686 return false; /* still busy with another command */
3687 }
3688
3689 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3690 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3691 I915_WRITE16(MEMSWCTL, rgvswctl);
3692 POSTING_READ16(MEMSWCTL);
3693
3694 rgvswctl |= MEMCTL_CMD_STS;
3695 I915_WRITE16(MEMSWCTL, rgvswctl);
3696
3697 return true;
3698}
3699
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003700static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003701{
3702 struct drm_i915_private *dev_priv = dev->dev_private;
3703 u32 rgvmodectl = I915_READ(MEMMODECTL);
3704 u8 fmax, fmin, fstart, vstart;
3705
Daniel Vetter92703882012-08-09 16:46:01 +02003706 spin_lock_irq(&mchdev_lock);
3707
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003708 /* Enable temp reporting */
3709 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3710 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3711
3712 /* 100ms RC evaluation intervals */
3713 I915_WRITE(RCUPEI, 100000);
3714 I915_WRITE(RCDNEI, 100000);
3715
3716 /* Set max/min thresholds to 90ms and 80ms respectively */
3717 I915_WRITE(RCBMAXAVG, 90000);
3718 I915_WRITE(RCBMINAVG, 80000);
3719
3720 I915_WRITE(MEMIHYST, 1);
3721
3722 /* Set up min, max, and cur for interrupt handling */
3723 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3724 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3725 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3726 MEMMODE_FSTART_SHIFT;
3727
3728 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3729 PXVFREQ_PX_SHIFT;
3730
Daniel Vetter20e4d402012-08-08 23:35:39 +02003731 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3732 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003733
Daniel Vetter20e4d402012-08-08 23:35:39 +02003734 dev_priv->ips.max_delay = fstart;
3735 dev_priv->ips.min_delay = fmin;
3736 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003737
3738 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3739 fmax, fmin, fstart);
3740
3741 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3742
3743 /*
3744 * Interrupts will be enabled in ironlake_irq_postinstall
3745 */
3746
3747 I915_WRITE(VIDSTART, vstart);
3748 POSTING_READ(VIDSTART);
3749
3750 rgvmodectl |= MEMMODE_SWMODE_EN;
3751 I915_WRITE(MEMMODECTL, rgvmodectl);
3752
Daniel Vetter92703882012-08-09 16:46:01 +02003753 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003754 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003755 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003756
3757 ironlake_set_drps(dev, fstart);
3758
Daniel Vetter20e4d402012-08-08 23:35:39 +02003759 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003760 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003761 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3762 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003763 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003764
3765 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003766}
3767
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003768static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003769{
3770 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003771 u16 rgvswctl;
3772
3773 spin_lock_irq(&mchdev_lock);
3774
3775 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003776
3777 /* Ack interrupts, disable EFC interrupt */
3778 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3779 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3780 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3781 I915_WRITE(DEIIR, DE_PCU_EVENT);
3782 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3783
3784 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003785 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003786 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003787 rgvswctl |= MEMCTL_CMD_STS;
3788 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003789 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003790
Daniel Vetter92703882012-08-09 16:46:01 +02003791 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003792}
3793
Daniel Vetteracbe9472012-07-26 11:50:05 +02003794/* There's a funny hw issue where the hw returns all 0 when reading from
3795 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3796 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3797 * all limits and the gpu stuck at whatever frequency it is at atm).
3798 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003799static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003800{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003801 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003802
Daniel Vetter20b46e52012-07-26 11:16:14 +02003803 /* Only set the down limit when we've reached the lowest level to avoid
3804 * getting more interrupts, otherwise leave this clear. This prevents a
3805 * race in the hw when coming out of rc6: There's a tiny window where
3806 * the hw runs at the minimal clock before selecting the desired
3807 * frequency, if the down threshold expires in that window we will not
3808 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003809 limits = dev_priv->rps.max_freq_softlimit << 24;
3810 if (val <= dev_priv->rps.min_freq_softlimit)
3811 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003812
3813 return limits;
3814}
3815
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003816static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3817{
3818 int new_power;
3819
3820 new_power = dev_priv->rps.power;
3821 switch (dev_priv->rps.power) {
3822 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003823 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003824 new_power = BETWEEN;
3825 break;
3826
3827 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003828 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003829 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003830 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003831 new_power = HIGH_POWER;
3832 break;
3833
3834 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003835 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 +01003836 new_power = BETWEEN;
3837 break;
3838 }
3839 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003840 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003841 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003842 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003843 new_power = HIGH_POWER;
3844 if (new_power == dev_priv->rps.power)
3845 return;
3846
3847 /* Note the units here are not exactly 1us, but 1280ns. */
3848 switch (new_power) {
3849 case LOW_POWER:
3850 /* Upclock if more than 95% busy over 16ms */
3851 I915_WRITE(GEN6_RP_UP_EI, 12500);
3852 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3853
3854 /* Downclock if less than 85% busy over 32ms */
3855 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3856 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3857
3858 I915_WRITE(GEN6_RP_CONTROL,
3859 GEN6_RP_MEDIA_TURBO |
3860 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3861 GEN6_RP_MEDIA_IS_GFX |
3862 GEN6_RP_ENABLE |
3863 GEN6_RP_UP_BUSY_AVG |
3864 GEN6_RP_DOWN_IDLE_AVG);
3865 break;
3866
3867 case BETWEEN:
3868 /* Upclock if more than 90% busy over 13ms */
3869 I915_WRITE(GEN6_RP_UP_EI, 10250);
3870 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3871
3872 /* Downclock if less than 75% busy over 32ms */
3873 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3874 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3875
3876 I915_WRITE(GEN6_RP_CONTROL,
3877 GEN6_RP_MEDIA_TURBO |
3878 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3879 GEN6_RP_MEDIA_IS_GFX |
3880 GEN6_RP_ENABLE |
3881 GEN6_RP_UP_BUSY_AVG |
3882 GEN6_RP_DOWN_IDLE_AVG);
3883 break;
3884
3885 case HIGH_POWER:
3886 /* Upclock if more than 85% busy over 10ms */
3887 I915_WRITE(GEN6_RP_UP_EI, 8000);
3888 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3889
3890 /* Downclock if less than 60% busy over 32ms */
3891 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3892 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3893
3894 I915_WRITE(GEN6_RP_CONTROL,
3895 GEN6_RP_MEDIA_TURBO |
3896 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3897 GEN6_RP_MEDIA_IS_GFX |
3898 GEN6_RP_ENABLE |
3899 GEN6_RP_UP_BUSY_AVG |
3900 GEN6_RP_DOWN_IDLE_AVG);
3901 break;
3902 }
3903
3904 dev_priv->rps.power = new_power;
3905 dev_priv->rps.last_adj = 0;
3906}
3907
Chris Wilson2876ce72014-03-28 08:03:34 +00003908static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3909{
3910 u32 mask = 0;
3911
3912 if (val > dev_priv->rps.min_freq_softlimit)
3913 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3914 if (val < dev_priv->rps.max_freq_softlimit)
3915 mask |= GEN6_PM_RP_UP_THRESHOLD;
3916
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003917 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3918 mask &= dev_priv->pm_rps_events;
3919
Imre Deak59d02a12014-12-19 19:33:26 +02003920 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003921}
3922
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003923/* gen6_set_rps is called to update the frequency request, but should also be
3924 * called when the range (min_delay and max_delay) is modified so that we can
3925 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003926static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003927{
3928 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003929
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003930 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003931 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3932 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003933
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003934 /* min/max delay may still have been modified so be sure to
3935 * write the limits value.
3936 */
3937 if (val != dev_priv->rps.cur_freq) {
3938 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003939
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003940 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003941 I915_WRITE(GEN6_RPNSWREQ,
3942 HSW_FREQUENCY(val));
3943 else
3944 I915_WRITE(GEN6_RPNSWREQ,
3945 GEN6_FREQUENCY(val) |
3946 GEN6_OFFSET(0) |
3947 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003948 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003949
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003950 /* Make sure we continue to get interrupts
3951 * until we hit the minimum or maximum frequencies.
3952 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003953 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003954 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003955
Ben Widawskyd5570a72012-09-07 19:43:41 -07003956 POSTING_READ(GEN6_RPNSWREQ);
3957
Ben Widawskyb39fb292014-03-19 18:31:11 -07003958 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003959 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003960}
3961
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003962static void valleyview_set_rps(struct drm_device *dev, u8 val)
3963{
3964 struct drm_i915_private *dev_priv = dev->dev_private;
3965
3966 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3967 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3968 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3969
3970 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3971 "Odd GPU freq value\n"))
3972 val &= ~1;
3973
3974 if (val != dev_priv->rps.cur_freq)
3975 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3976
3977 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3978
3979 dev_priv->rps.cur_freq = val;
3980 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3981}
3982
Deepak S76c3552f2014-01-30 23:08:16 +05303983/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3984 *
3985 * * If Gfx is Idle, then
3986 * 1. Mask Turbo interrupts
3987 * 2. Bring up Gfx clock
3988 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3989 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3990 * 5. Unmask Turbo interrupts
3991*/
3992static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3993{
Deepak S5549d252014-06-28 11:26:11 +05303994 struct drm_device *dev = dev_priv->dev;
3995
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003996 /* CHV and latest VLV don't need to force the gfx clock */
3997 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303998 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3999 return;
4000 }
4001
Deepak S76c3552f2014-01-30 23:08:16 +05304002 /*
4003 * When we are idle. Drop to min voltage state.
4004 */
4005
Ben Widawskyb39fb292014-03-19 18:31:11 -07004006 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05304007 return;
4008
4009 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02004010 I915_WRITE(GEN6_PMINTRMSK,
4011 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05304012
Imre Deak650ad972014-04-18 16:35:02 +03004013 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05304014
Ben Widawskyb39fb292014-03-19 18:31:11 -07004015 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05304016
4017 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07004018 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05304019
4020 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02004021 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05304022 DRM_ERROR("timed out waiting for Punit\n");
4023
Imre Deak650ad972014-04-18 16:35:02 +03004024 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05304025
Chris Wilson2876ce72014-03-28 08:03:34 +00004026 I915_WRITE(GEN6_PMINTRMSK,
4027 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05304028}
4029
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004030void gen6_rps_idle(struct drm_i915_private *dev_priv)
4031{
Damien Lespiau691bb712013-12-12 14:36:36 +00004032 struct drm_device *dev = dev_priv->dev;
4033
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004034 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004035 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004036 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304037 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004038 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07004039 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004040 dev_priv->rps.last_adj = 0;
4041 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004042 mutex_unlock(&dev_priv->rps.hw_lock);
4043}
4044
4045void gen6_rps_boost(struct drm_i915_private *dev_priv)
4046{
4047 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004048 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004049 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004050 dev_priv->rps.last_adj = 0;
4051 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004052 mutex_unlock(&dev_priv->rps.hw_lock);
4053}
4054
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004055void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004056{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004057 if (IS_VALLEYVIEW(dev))
4058 valleyview_set_rps(dev, val);
4059 else
4060 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004061}
4062
Zhe Wang20e49362014-11-04 17:07:05 +00004063static void gen9_disable_rps(struct drm_device *dev)
4064{
4065 struct drm_i915_private *dev_priv = dev->dev_private;
4066
4067 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004068 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004069}
4070
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004071static void gen6_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);
4076 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004077}
4078
Deepak S38807742014-05-23 21:00:15 +05304079static void cherryview_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}
4085
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004086static void valleyview_disable_rps(struct drm_device *dev)
4087{
4088 struct drm_i915_private *dev_priv = dev->dev_private;
4089
Deepak S98a2e5f2014-08-18 10:35:27 -07004090 /* we're doing forcewake before Disabling RC6,
4091 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004092 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004093
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004094 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004095
Mika Kuoppala59bad942015-01-16 11:34:40 +02004096 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004097}
4098
Ben Widawskydc39fff2013-10-18 12:32:07 -07004099static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4100{
Imre Deak91ca6892014-04-14 20:24:25 +03004101 if (IS_VALLEYVIEW(dev)) {
4102 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4103 mode = GEN6_RC_CTL_RC6_ENABLE;
4104 else
4105 mode = 0;
4106 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004107 if (HAS_RC6p(dev))
4108 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4109 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4110 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4111 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4112
4113 else
4114 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4115 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004116}
4117
Imre Deake6069ca2014-04-18 16:01:02 +03004118static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004119{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004120 /* No RC6 before Ironlake */
4121 if (INTEL_INFO(dev)->gen < 5)
4122 return 0;
4123
Imre Deake6069ca2014-04-18 16:01:02 +03004124 /* RC6 is only on Ironlake mobile not on desktop */
4125 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4126 return 0;
4127
Daniel Vetter456470e2012-08-08 23:35:40 +02004128 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004129 if (enable_rc6 >= 0) {
4130 int mask;
4131
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004132 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004133 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4134 INTEL_RC6pp_ENABLE;
4135 else
4136 mask = INTEL_RC6_ENABLE;
4137
4138 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004139 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4140 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004141
4142 return enable_rc6 & mask;
4143 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004144
Chris Wilson6567d742012-11-10 10:00:06 +00004145 /* Disable RC6 on Ironlake */
4146 if (INTEL_INFO(dev)->gen == 5)
4147 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004148
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004149 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004150 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004151
4152 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004153}
4154
Imre Deake6069ca2014-04-18 16:01:02 +03004155int intel_enable_rc6(const struct drm_device *dev)
4156{
4157 return i915.enable_rc6;
4158}
4159
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004160static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004161{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004162 struct drm_i915_private *dev_priv = dev->dev_private;
4163 uint32_t rp_state_cap;
4164 u32 ddcc_status = 0;
4165 int ret;
4166
4167 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004168 /* All of these values are in units of 50MHz */
4169 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004170 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004171 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004172 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004173 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004174 /* hw_max = RP0 until we check for overclocking */
4175 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4176
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004177 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4178 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4179 ret = sandybridge_pcode_read(dev_priv,
4180 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4181 &ddcc_status);
4182 if (0 == ret)
4183 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004184 clamp_t(u8,
4185 ((ddcc_status >> 8) & 0xff),
4186 dev_priv->rps.min_freq,
4187 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004188 }
4189
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004190 /* Preserve min/max settings in case of re-init */
4191 if (dev_priv->rps.max_freq_softlimit == 0)
4192 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4193
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004194 if (dev_priv->rps.min_freq_softlimit == 0) {
4195 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4196 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004197 /* max(RPe, 450 MHz) */
4198 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004199 else
4200 dev_priv->rps.min_freq_softlimit =
4201 dev_priv->rps.min_freq;
4202 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004203}
4204
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004205/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004206static void gen9_enable_rps(struct drm_device *dev)
4207{
4208 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004209
4210 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4211
Damien Lespiauba1c5542015-01-16 18:07:26 +00004212 gen6_init_rps_frequencies(dev);
4213
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004214 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4215 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4216
4217 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4218 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4219 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4220 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4221 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4222 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4223 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4224 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4225 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4226 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4227 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4228 GEN6_RP_DOWN_IDLE_AVG);
4229
4230 gen6_enable_rps_interrupts(dev);
4231
4232 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4233}
4234
4235static void gen9_enable_rc6(struct drm_device *dev)
4236{
4237 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004238 struct intel_engine_cs *ring;
4239 uint32_t rc6_mask = 0;
4240 int unused;
4241
4242 /* 1a: Software RC state - RC0 */
4243 I915_WRITE(GEN6_RC_STATE, 0);
4244
4245 /* 1b: Get forcewake during program sequence. Although the driver
4246 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004247 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004248
4249 /* 2a: Disable RC states. */
4250 I915_WRITE(GEN6_RC_CONTROL, 0);
4251
4252 /* 2b: Program RC6 thresholds.*/
4253 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4254 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4255 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4256 for_each_ring(ring, dev_priv, unused)
4257 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4258 I915_WRITE(GEN6_RC_SLEEP, 0);
4259 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4260
Zhe Wang38c23522015-01-20 12:23:04 +00004261 /* 2c: Program Coarse Power Gating Policies. */
4262 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4263 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4264
Zhe Wang20e49362014-11-04 17:07:05 +00004265 /* 3a: Enable RC6 */
4266 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4267 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4268 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4269 "on" : "off");
4270 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4271 GEN6_RC_CTL_EI_MODE(1) |
4272 rc6_mask);
4273
Zhe Wang38c23522015-01-20 12:23:04 +00004274 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4275 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4276
Mika Kuoppala59bad942015-01-16 11:34:40 +02004277 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004278
4279}
4280
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004281static void gen8_enable_rps(struct drm_device *dev)
4282{
4283 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004284 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004285 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004286 int unused;
4287
4288 /* 1a: Software RC state - RC0 */
4289 I915_WRITE(GEN6_RC_STATE, 0);
4290
4291 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4292 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004293 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004294
4295 /* 2a: Disable RC states. */
4296 I915_WRITE(GEN6_RC_CONTROL, 0);
4297
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004298 /* Initialize rps frequencies */
4299 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004300
4301 /* 2b: Program RC6 thresholds.*/
4302 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4303 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4304 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4305 for_each_ring(ring, dev_priv, unused)
4306 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4307 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004308 if (IS_BROADWELL(dev))
4309 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4310 else
4311 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004312
4313 /* 3: Enable RC6 */
4314 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4315 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004316 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004317 if (IS_BROADWELL(dev))
4318 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4319 GEN7_RC_CTL_TO_MODE |
4320 rc6_mask);
4321 else
4322 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4323 GEN6_RC_CTL_EI_MODE(1) |
4324 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004325
4326 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004327 I915_WRITE(GEN6_RPNSWREQ,
4328 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4329 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4330 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004331 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4332 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004333
Daniel Vetter7526ed72014-09-29 15:07:19 +02004334 /* Docs recommend 900MHz, and 300 MHz respectively */
4335 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4336 dev_priv->rps.max_freq_softlimit << 24 |
4337 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004338
Daniel Vetter7526ed72014-09-29 15:07:19 +02004339 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4340 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4341 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4342 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004343
Daniel Vetter7526ed72014-09-29 15:07:19 +02004344 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004345
4346 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004347 I915_WRITE(GEN6_RP_CONTROL,
4348 GEN6_RP_MEDIA_TURBO |
4349 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4350 GEN6_RP_MEDIA_IS_GFX |
4351 GEN6_RP_ENABLE |
4352 GEN6_RP_UP_BUSY_AVG |
4353 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004354
Daniel Vetter7526ed72014-09-29 15:07:19 +02004355 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004356
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004357 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4358 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004359
Mika Kuoppala59bad942015-01-16 11:34:40 +02004360 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004361}
4362
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004363static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004364{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004365 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004366 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004367 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004368 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004369 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004370 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004371
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004372 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004373
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004374 /* Here begins a magic sequence of register writes to enable
4375 * auto-downclocking.
4376 *
4377 * Perhaps there might be some value in exposing these to
4378 * userspace...
4379 */
4380 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004381
4382 /* Clear the DBG now so we don't confuse earlier errors */
4383 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4384 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4385 I915_WRITE(GTFIFODBG, gtfifodbg);
4386 }
4387
Mika Kuoppala59bad942015-01-16 11:34:40 +02004388 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004389
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004390 /* Initialize rps frequencies */
4391 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004392
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004393 /* disable the counters and set deterministic thresholds */
4394 I915_WRITE(GEN6_RC_CONTROL, 0);
4395
4396 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4397 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4398 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4399 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4400 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4401
Chris Wilsonb4519512012-05-11 14:29:30 +01004402 for_each_ring(ring, dev_priv, i)
4403 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004404
4405 I915_WRITE(GEN6_RC_SLEEP, 0);
4406 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004407 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004408 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4409 else
4410 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004411 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004412 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4413
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004414 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004415 rc6_mode = intel_enable_rc6(dev_priv->dev);
4416 if (rc6_mode & INTEL_RC6_ENABLE)
4417 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4418
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004419 /* We don't use those on Haswell */
4420 if (!IS_HASWELL(dev)) {
4421 if (rc6_mode & INTEL_RC6p_ENABLE)
4422 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004423
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004424 if (rc6_mode & INTEL_RC6pp_ENABLE)
4425 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4426 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004427
Ben Widawskydc39fff2013-10-18 12:32:07 -07004428 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004429
4430 I915_WRITE(GEN6_RC_CONTROL,
4431 rc6_mask |
4432 GEN6_RC_CTL_EI_MODE(1) |
4433 GEN6_RC_CTL_HW_ENABLE);
4434
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004435 /* Power down if completely idle for over 50ms */
4436 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004437 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004438
Ben Widawsky42c05262012-09-26 10:34:00 -07004439 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004440 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004441 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004442
4443 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4444 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4445 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004446 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004447 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004448 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004449 }
4450
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004451 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004452 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004453
Ben Widawsky31643d52012-09-26 10:34:01 -07004454 rc6vids = 0;
4455 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4456 if (IS_GEN6(dev) && ret) {
4457 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4458 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4459 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4460 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4461 rc6vids &= 0xffff00;
4462 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4463 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4464 if (ret)
4465 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4466 }
4467
Mika Kuoppala59bad942015-01-16 11:34:40 +02004468 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004469}
4470
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004471static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004472{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004473 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004474 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004475 unsigned int gpu_freq;
4476 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004477 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004478 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004479
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004480 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004481
Ben Widawskyeda79642013-10-07 17:15:48 -03004482 policy = cpufreq_cpu_get(0);
4483 if (policy) {
4484 max_ia_freq = policy->cpuinfo.max_freq;
4485 cpufreq_cpu_put(policy);
4486 } else {
4487 /*
4488 * Default to measured freq if none found, PCU will ensure we
4489 * don't go over
4490 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004491 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004492 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004493
4494 /* Convert from kHz to MHz */
4495 max_ia_freq /= 1000;
4496
Ben Widawsky153b4b952013-10-22 22:05:09 -07004497 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004498 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4499 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004500
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004501 /*
4502 * For each potential GPU frequency, load a ring frequency we'd like
4503 * to use for memory access. We do this by specifying the IA frequency
4504 * the PCU should use as a reference to determine the ring frequency.
4505 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004506 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004507 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004508 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004509 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004510
Ben Widawsky46c764d2013-11-02 21:07:49 -07004511 if (INTEL_INFO(dev)->gen >= 8) {
4512 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4513 ring_freq = max(min_ring_freq, gpu_freq);
4514 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004515 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004516 ring_freq = max(min_ring_freq, ring_freq);
4517 /* leave ia_freq as the default, chosen by cpufreq */
4518 } else {
4519 /* On older processors, there is no separate ring
4520 * clock domain, so in order to boost the bandwidth
4521 * of the ring, we need to upclock the CPU (ia_freq).
4522 *
4523 * For GPU frequencies less than 750MHz,
4524 * just use the lowest ring freq.
4525 */
4526 if (gpu_freq < min_freq)
4527 ia_freq = 800;
4528 else
4529 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4530 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4531 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004532
Ben Widawsky42c05262012-09-26 10:34:00 -07004533 sandybridge_pcode_write(dev_priv,
4534 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004535 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4536 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4537 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004538 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004539}
4540
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004541void gen6_update_ring_freq(struct drm_device *dev)
4542{
4543 struct drm_i915_private *dev_priv = dev->dev_private;
4544
4545 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4546 return;
4547
4548 mutex_lock(&dev_priv->rps.hw_lock);
4549 __gen6_update_ring_freq(dev);
4550 mutex_unlock(&dev_priv->rps.hw_lock);
4551}
4552
Ville Syrjälä03af2042014-06-28 02:03:53 +03004553static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304554{
Deepak S095acd52015-01-17 11:05:59 +05304555 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304556 u32 val, rp0;
4557
Deepak S095acd52015-01-17 11:05:59 +05304558 if (dev->pdev->revision >= 0x20) {
4559 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304560
Deepak S095acd52015-01-17 11:05:59 +05304561 switch (INTEL_INFO(dev)->eu_total) {
4562 case 8:
4563 /* (2 * 4) config */
4564 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4565 break;
4566 case 12:
4567 /* (2 * 6) config */
4568 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4569 break;
4570 case 16:
4571 /* (2 * 8) config */
4572 default:
4573 /* Setting (2 * 8) Min RP0 for any other combination */
4574 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4575 break;
4576 }
4577 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4578 } else {
4579 /* For pre-production hardware */
4580 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4581 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4582 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4583 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304584 return rp0;
4585}
4586
4587static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4588{
4589 u32 val, rpe;
4590
4591 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4592 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4593
4594 return rpe;
4595}
4596
Deepak S7707df42014-07-12 18:46:14 +05304597static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4598{
Deepak S095acd52015-01-17 11:05:59 +05304599 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304600 u32 val, rp1;
4601
Deepak S095acd52015-01-17 11:05:59 +05304602 if (dev->pdev->revision >= 0x20) {
4603 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4604 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4605 } else {
4606 /* For pre-production hardware */
4607 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4608 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4609 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4610 }
Deepak S7707df42014-07-12 18:46:14 +05304611 return rp1;
4612}
4613
Ville Syrjälä03af2042014-06-28 02:03:53 +03004614static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304615{
Deepak S095acd52015-01-17 11:05:59 +05304616 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304617 u32 val, rpn;
4618
Deepak S095acd52015-01-17 11:05:59 +05304619 if (dev->pdev->revision >= 0x20) {
4620 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4621 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4622 FB_GFX_FREQ_FUSE_MASK);
4623 } else { /* For pre-production hardware */
4624 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4625 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4626 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4627 }
4628
Deepak S2b6b3a02014-05-27 15:59:30 +05304629 return rpn;
4630}
4631
Deepak Sf8f2b002014-07-10 13:16:21 +05304632static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4633{
4634 u32 val, rp1;
4635
4636 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4637
4638 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4639
4640 return rp1;
4641}
4642
Ville Syrjälä03af2042014-06-28 02:03:53 +03004643static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004644{
4645 u32 val, rp0;
4646
Jani Nikula64936252013-05-22 15:36:20 +03004647 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004648
4649 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4650 /* Clamp to max */
4651 rp0 = min_t(u32, rp0, 0xea);
4652
4653 return rp0;
4654}
4655
4656static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4657{
4658 u32 val, rpe;
4659
Jani Nikula64936252013-05-22 15:36:20 +03004660 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004661 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004662 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004663 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4664
4665 return rpe;
4666}
4667
Ville Syrjälä03af2042014-06-28 02:03:53 +03004668static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004669{
Jani Nikula64936252013-05-22 15:36:20 +03004670 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004671}
4672
Imre Deakae484342014-03-31 15:10:44 +03004673/* Check that the pctx buffer wasn't move under us. */
4674static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4675{
4676 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4677
4678 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4679 dev_priv->vlv_pctx->stolen->start);
4680}
4681
Deepak S38807742014-05-23 21:00:15 +05304682
4683/* Check that the pcbr address is not empty. */
4684static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4685{
4686 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4687
4688 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4689}
4690
4691static void cherryview_setup_pctx(struct drm_device *dev)
4692{
4693 struct drm_i915_private *dev_priv = dev->dev_private;
4694 unsigned long pctx_paddr, paddr;
4695 struct i915_gtt *gtt = &dev_priv->gtt;
4696 u32 pcbr;
4697 int pctx_size = 32*1024;
4698
4699 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4700
4701 pcbr = I915_READ(VLV_PCBR);
4702 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004703 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304704 paddr = (dev_priv->mm.stolen_base +
4705 (gtt->stolen_size - pctx_size));
4706
4707 pctx_paddr = (paddr & (~4095));
4708 I915_WRITE(VLV_PCBR, pctx_paddr);
4709 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004710
4711 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304712}
4713
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004714static void valleyview_setup_pctx(struct drm_device *dev)
4715{
4716 struct drm_i915_private *dev_priv = dev->dev_private;
4717 struct drm_i915_gem_object *pctx;
4718 unsigned long pctx_paddr;
4719 u32 pcbr;
4720 int pctx_size = 24*1024;
4721
Imre Deak17b0c1f2014-02-11 21:39:06 +02004722 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4723
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004724 pcbr = I915_READ(VLV_PCBR);
4725 if (pcbr) {
4726 /* BIOS set it up already, grab the pre-alloc'd space */
4727 int pcbr_offset;
4728
4729 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4730 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4731 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004732 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004733 pctx_size);
4734 goto out;
4735 }
4736
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004737 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4738
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004739 /*
4740 * From the Gunit register HAS:
4741 * The Gfx driver is expected to program this register and ensure
4742 * proper allocation within Gfx stolen memory. For example, this
4743 * register should be programmed such than the PCBR range does not
4744 * overlap with other ranges, such as the frame buffer, protected
4745 * memory, or any other relevant ranges.
4746 */
4747 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4748 if (!pctx) {
4749 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4750 return;
4751 }
4752
4753 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4754 I915_WRITE(VLV_PCBR, pctx_paddr);
4755
4756out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004757 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004758 dev_priv->vlv_pctx = pctx;
4759}
4760
Imre Deakae484342014-03-31 15:10:44 +03004761static void valleyview_cleanup_pctx(struct drm_device *dev)
4762{
4763 struct drm_i915_private *dev_priv = dev->dev_private;
4764
4765 if (WARN_ON(!dev_priv->vlv_pctx))
4766 return;
4767
4768 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4769 dev_priv->vlv_pctx = NULL;
4770}
4771
Imre Deak4e805192014-04-14 20:24:41 +03004772static void valleyview_init_gt_powersave(struct drm_device *dev)
4773{
4774 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004775 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004776
4777 valleyview_setup_pctx(dev);
4778
4779 mutex_lock(&dev_priv->rps.hw_lock);
4780
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004781 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4782 switch ((val >> 6) & 3) {
4783 case 0:
4784 case 1:
4785 dev_priv->mem_freq = 800;
4786 break;
4787 case 2:
4788 dev_priv->mem_freq = 1066;
4789 break;
4790 case 3:
4791 dev_priv->mem_freq = 1333;
4792 break;
4793 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004794 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004795
Imre Deak4e805192014-04-14 20:24:41 +03004796 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4797 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4798 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004799 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004800 dev_priv->rps.max_freq);
4801
4802 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4803 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004804 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004805 dev_priv->rps.efficient_freq);
4806
Deepak Sf8f2b002014-07-10 13:16:21 +05304807 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4808 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004809 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304810 dev_priv->rps.rp1_freq);
4811
Imre Deak4e805192014-04-14 20:24:41 +03004812 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4813 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004814 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004815 dev_priv->rps.min_freq);
4816
4817 /* Preserve min/max settings in case of re-init */
4818 if (dev_priv->rps.max_freq_softlimit == 0)
4819 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4820
4821 if (dev_priv->rps.min_freq_softlimit == 0)
4822 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4823
4824 mutex_unlock(&dev_priv->rps.hw_lock);
4825}
4826
Deepak S38807742014-05-23 21:00:15 +05304827static void cherryview_init_gt_powersave(struct drm_device *dev)
4828{
Deepak S2b6b3a02014-05-27 15:59:30 +05304829 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004830 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304831
Deepak S38807742014-05-23 21:00:15 +05304832 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304833
4834 mutex_lock(&dev_priv->rps.hw_lock);
4835
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004836 mutex_lock(&dev_priv->dpio_lock);
4837 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4838 mutex_unlock(&dev_priv->dpio_lock);
4839
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004840 switch ((val >> 2) & 0x7) {
4841 case 0:
4842 case 1:
4843 dev_priv->rps.cz_freq = 200;
4844 dev_priv->mem_freq = 1600;
4845 break;
4846 case 2:
4847 dev_priv->rps.cz_freq = 267;
4848 dev_priv->mem_freq = 1600;
4849 break;
4850 case 3:
4851 dev_priv->rps.cz_freq = 333;
4852 dev_priv->mem_freq = 2000;
4853 break;
4854 case 4:
4855 dev_priv->rps.cz_freq = 320;
4856 dev_priv->mem_freq = 1600;
4857 break;
4858 case 5:
4859 dev_priv->rps.cz_freq = 400;
4860 dev_priv->mem_freq = 1600;
4861 break;
4862 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004863 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004864
Deepak S2b6b3a02014-05-27 15:59:30 +05304865 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4866 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4867 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004868 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304869 dev_priv->rps.max_freq);
4870
4871 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4872 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004873 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304874 dev_priv->rps.efficient_freq);
4875
Deepak S7707df42014-07-12 18:46:14 +05304876 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4877 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004878 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304879 dev_priv->rps.rp1_freq);
4880
Deepak S2b6b3a02014-05-27 15:59:30 +05304881 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4882 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004883 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304884 dev_priv->rps.min_freq);
4885
Ville Syrjälä1c147622014-08-18 14:42:43 +03004886 WARN_ONCE((dev_priv->rps.max_freq |
4887 dev_priv->rps.efficient_freq |
4888 dev_priv->rps.rp1_freq |
4889 dev_priv->rps.min_freq) & 1,
4890 "Odd GPU freq values\n");
4891
Deepak S2b6b3a02014-05-27 15:59:30 +05304892 /* Preserve min/max settings in case of re-init */
4893 if (dev_priv->rps.max_freq_softlimit == 0)
4894 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4895
4896 if (dev_priv->rps.min_freq_softlimit == 0)
4897 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4898
4899 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304900}
4901
Imre Deak4e805192014-04-14 20:24:41 +03004902static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4903{
4904 valleyview_cleanup_pctx(dev);
4905}
4906
Deepak S38807742014-05-23 21:00:15 +05304907static void cherryview_enable_rps(struct drm_device *dev)
4908{
4909 struct drm_i915_private *dev_priv = dev->dev_private;
4910 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304911 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304912 int i;
4913
4914 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4915
4916 gtfifodbg = I915_READ(GTFIFODBG);
4917 if (gtfifodbg) {
4918 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4919 gtfifodbg);
4920 I915_WRITE(GTFIFODBG, gtfifodbg);
4921 }
4922
4923 cherryview_check_pctx(dev_priv);
4924
4925 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4926 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004927 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304928
Ville Syrjälä160614a2015-01-19 13:50:47 +02004929 /* Disable RC states. */
4930 I915_WRITE(GEN6_RC_CONTROL, 0);
4931
Deepak S38807742014-05-23 21:00:15 +05304932 /* 2a: Program RC6 thresholds.*/
4933 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4934 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4935 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4936
4937 for_each_ring(ring, dev_priv, i)
4938 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4939 I915_WRITE(GEN6_RC_SLEEP, 0);
4940
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004941 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4942 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304943
4944 /* allows RC6 residency counter to work */
4945 I915_WRITE(VLV_COUNTER_CONTROL,
4946 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4947 VLV_MEDIA_RC6_COUNT_EN |
4948 VLV_RENDER_RC6_COUNT_EN));
4949
4950 /* For now we assume BIOS is allocating and populating the PCBR */
4951 pcbr = I915_READ(VLV_PCBR);
4952
Deepak S38807742014-05-23 21:00:15 +05304953 /* 3: Enable RC6 */
4954 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4955 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004956 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304957
4958 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4959
Deepak S2b6b3a02014-05-27 15:59:30 +05304960 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004961 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304962 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4963 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4964 I915_WRITE(GEN6_RP_UP_EI, 66000);
4965 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4966
4967 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4968
4969 /* 5: Enable RPS */
4970 I915_WRITE(GEN6_RP_CONTROL,
4971 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004972 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304973 GEN6_RP_ENABLE |
4974 GEN6_RP_UP_BUSY_AVG |
4975 GEN6_RP_DOWN_IDLE_AVG);
4976
4977 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4978
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004979 /* RPS code assumes GPLL is used */
4980 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4981
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004982 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304983 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4984
4985 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4986 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004987 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304988 dev_priv->rps.cur_freq);
4989
4990 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004991 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304992 dev_priv->rps.efficient_freq);
4993
4994 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4995
Mika Kuoppala59bad942015-01-16 11:34:40 +02004996 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304997}
4998
Jesse Barnes0a073b82013-04-17 15:54:58 -07004999static void valleyview_enable_rps(struct drm_device *dev)
5000{
5001 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005002 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005003 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005004 int i;
5005
5006 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5007
Imre Deakae484342014-03-31 15:10:44 +03005008 valleyview_check_pctx(dev_priv);
5009
Jesse Barnes0a073b82013-04-17 15:54:58 -07005010 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005011 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5012 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005013 I915_WRITE(GTFIFODBG, gtfifodbg);
5014 }
5015
Deepak Sc8d9a592013-11-23 14:55:42 +05305016 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005017 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005018
Ville Syrjälä160614a2015-01-19 13:50:47 +02005019 /* Disable RC states. */
5020 I915_WRITE(GEN6_RC_CONTROL, 0);
5021
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005022 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005023 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5024 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5025 I915_WRITE(GEN6_RP_UP_EI, 66000);
5026 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5027
5028 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5029
5030 I915_WRITE(GEN6_RP_CONTROL,
5031 GEN6_RP_MEDIA_TURBO |
5032 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5033 GEN6_RP_MEDIA_IS_GFX |
5034 GEN6_RP_ENABLE |
5035 GEN6_RP_UP_BUSY_AVG |
5036 GEN6_RP_DOWN_IDLE_CONT);
5037
5038 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5039 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5040 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5041
5042 for_each_ring(ring, dev_priv, i)
5043 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5044
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005045 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005046
5047 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005048 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005049 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5050 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005051 VLV_MEDIA_RC6_COUNT_EN |
5052 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005053
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005054 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005055 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005056
5057 intel_print_rc6_info(dev, rc6_mode);
5058
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005059 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005060
Jani Nikula64936252013-05-22 15:36:20 +03005061 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005062
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005063 /* RPS code assumes GPLL is used */
5064 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5065
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005066 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07005067 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5068
Ben Widawskyb39fb292014-03-19 18:31:11 -07005069 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005070 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005071 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005072 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005073
Ville Syrjälä73008b92013-06-25 19:21:01 +03005074 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005075 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005076 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005077
Ben Widawskyb39fb292014-03-19 18:31:11 -07005078 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005079
Mika Kuoppala59bad942015-01-16 11:34:40 +02005080 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005081}
5082
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005083static unsigned long intel_pxfreq(u32 vidfreq)
5084{
5085 unsigned long freq;
5086 int div = (vidfreq & 0x3f0000) >> 16;
5087 int post = (vidfreq & 0x3000) >> 12;
5088 int pre = (vidfreq & 0x7);
5089
5090 if (!pre)
5091 return 0;
5092
5093 freq = ((div * 133333) / ((1<<post) * pre));
5094
5095 return freq;
5096}
5097
Daniel Vettereb48eb02012-04-26 23:28:12 +02005098static const struct cparams {
5099 u16 i;
5100 u16 t;
5101 u16 m;
5102 u16 c;
5103} cparams[] = {
5104 { 1, 1333, 301, 28664 },
5105 { 1, 1066, 294, 24460 },
5106 { 1, 800, 294, 25192 },
5107 { 0, 1333, 276, 27605 },
5108 { 0, 1066, 276, 27605 },
5109 { 0, 800, 231, 23784 },
5110};
5111
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005112static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005113{
5114 u64 total_count, diff, ret;
5115 u32 count1, count2, count3, m = 0, c = 0;
5116 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5117 int i;
5118
Daniel Vetter02d71952012-08-09 16:44:54 +02005119 assert_spin_locked(&mchdev_lock);
5120
Daniel Vetter20e4d402012-08-08 23:35:39 +02005121 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005122
5123 /* Prevent division-by-zero if we are asking too fast.
5124 * Also, we don't get interesting results if we are polling
5125 * faster than once in 10ms, so just return the saved value
5126 * in such cases.
5127 */
5128 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005129 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005130
5131 count1 = I915_READ(DMIEC);
5132 count2 = I915_READ(DDREC);
5133 count3 = I915_READ(CSIEC);
5134
5135 total_count = count1 + count2 + count3;
5136
5137 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005138 if (total_count < dev_priv->ips.last_count1) {
5139 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005140 diff += total_count;
5141 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005142 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005143 }
5144
5145 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005146 if (cparams[i].i == dev_priv->ips.c_m &&
5147 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005148 m = cparams[i].m;
5149 c = cparams[i].c;
5150 break;
5151 }
5152 }
5153
5154 diff = div_u64(diff, diff1);
5155 ret = ((m * diff) + c);
5156 ret = div_u64(ret, 10);
5157
Daniel Vetter20e4d402012-08-08 23:35:39 +02005158 dev_priv->ips.last_count1 = total_count;
5159 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005160
Daniel Vetter20e4d402012-08-08 23:35:39 +02005161 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005162
5163 return ret;
5164}
5165
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005166unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5167{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005168 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005169 unsigned long val;
5170
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005171 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005172 return 0;
5173
5174 spin_lock_irq(&mchdev_lock);
5175
5176 val = __i915_chipset_val(dev_priv);
5177
5178 spin_unlock_irq(&mchdev_lock);
5179
5180 return val;
5181}
5182
Daniel Vettereb48eb02012-04-26 23:28:12 +02005183unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5184{
5185 unsigned long m, x, b;
5186 u32 tsfs;
5187
5188 tsfs = I915_READ(TSFS);
5189
5190 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5191 x = I915_READ8(TR1);
5192
5193 b = tsfs & TSFS_INTR_MASK;
5194
5195 return ((m * x) / 127) - b;
5196}
5197
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005198static int _pxvid_to_vd(u8 pxvid)
5199{
5200 if (pxvid == 0)
5201 return 0;
5202
5203 if (pxvid >= 8 && pxvid < 31)
5204 pxvid = 31;
5205
5206 return (pxvid + 2) * 125;
5207}
5208
5209static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005210{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005211 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005212 const int vd = _pxvid_to_vd(pxvid);
5213 const int vm = vd - 1125;
5214
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005215 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005216 return vm > 0 ? vm : 0;
5217
5218 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005219}
5220
Daniel Vetter02d71952012-08-09 16:44:54 +02005221static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005222{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005223 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005224 u32 count;
5225
Daniel Vetter02d71952012-08-09 16:44:54 +02005226 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005227
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005228 now = ktime_get_raw_ns();
5229 diffms = now - dev_priv->ips.last_time2;
5230 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005231
5232 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005233 if (!diffms)
5234 return;
5235
5236 count = I915_READ(GFXEC);
5237
Daniel Vetter20e4d402012-08-08 23:35:39 +02005238 if (count < dev_priv->ips.last_count2) {
5239 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005240 diff += count;
5241 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005242 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005243 }
5244
Daniel Vetter20e4d402012-08-08 23:35:39 +02005245 dev_priv->ips.last_count2 = count;
5246 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005247
5248 /* More magic constants... */
5249 diff = diff * 1181;
5250 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005251 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005252}
5253
Daniel Vetter02d71952012-08-09 16:44:54 +02005254void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5255{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005256 struct drm_device *dev = dev_priv->dev;
5257
5258 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005259 return;
5260
Daniel Vetter92703882012-08-09 16:46:01 +02005261 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005262
5263 __i915_update_gfx_val(dev_priv);
5264
Daniel Vetter92703882012-08-09 16:46:01 +02005265 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005266}
5267
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005268static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005269{
5270 unsigned long t, corr, state1, corr2, state2;
5271 u32 pxvid, ext_v;
5272
Daniel Vetter02d71952012-08-09 16:44:54 +02005273 assert_spin_locked(&mchdev_lock);
5274
Ben Widawskyb39fb292014-03-19 18:31:11 -07005275 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005276 pxvid = (pxvid >> 24) & 0x7f;
5277 ext_v = pvid_to_extvid(dev_priv, pxvid);
5278
5279 state1 = ext_v;
5280
5281 t = i915_mch_val(dev_priv);
5282
5283 /* Revel in the empirically derived constants */
5284
5285 /* Correction factor in 1/100000 units */
5286 if (t > 80)
5287 corr = ((t * 2349) + 135940);
5288 else if (t >= 50)
5289 corr = ((t * 964) + 29317);
5290 else /* < 50 */
5291 corr = ((t * 301) + 1004);
5292
5293 corr = corr * ((150142 * state1) / 10000 - 78642);
5294 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005295 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005296
5297 state2 = (corr2 * state1) / 10000;
5298 state2 /= 100; /* convert to mW */
5299
Daniel Vetter02d71952012-08-09 16:44:54 +02005300 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005301
Daniel Vetter20e4d402012-08-08 23:35:39 +02005302 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005303}
5304
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005305unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5306{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005307 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005308 unsigned long val;
5309
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005310 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005311 return 0;
5312
5313 spin_lock_irq(&mchdev_lock);
5314
5315 val = __i915_gfx_val(dev_priv);
5316
5317 spin_unlock_irq(&mchdev_lock);
5318
5319 return val;
5320}
5321
Daniel Vettereb48eb02012-04-26 23:28:12 +02005322/**
5323 * i915_read_mch_val - return value for IPS use
5324 *
5325 * Calculate and return a value for the IPS driver to use when deciding whether
5326 * we have thermal and power headroom to increase CPU or GPU power budget.
5327 */
5328unsigned long i915_read_mch_val(void)
5329{
5330 struct drm_i915_private *dev_priv;
5331 unsigned long chipset_val, graphics_val, ret = 0;
5332
Daniel Vetter92703882012-08-09 16:46:01 +02005333 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005334 if (!i915_mch_dev)
5335 goto out_unlock;
5336 dev_priv = i915_mch_dev;
5337
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005338 chipset_val = __i915_chipset_val(dev_priv);
5339 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005340
5341 ret = chipset_val + graphics_val;
5342
5343out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005344 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005345
5346 return ret;
5347}
5348EXPORT_SYMBOL_GPL(i915_read_mch_val);
5349
5350/**
5351 * i915_gpu_raise - raise GPU frequency limit
5352 *
5353 * Raise the limit; IPS indicates we have thermal headroom.
5354 */
5355bool i915_gpu_raise(void)
5356{
5357 struct drm_i915_private *dev_priv;
5358 bool ret = true;
5359
Daniel Vetter92703882012-08-09 16:46:01 +02005360 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005361 if (!i915_mch_dev) {
5362 ret = false;
5363 goto out_unlock;
5364 }
5365 dev_priv = i915_mch_dev;
5366
Daniel Vetter20e4d402012-08-08 23:35:39 +02005367 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5368 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005369
5370out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005371 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005372
5373 return ret;
5374}
5375EXPORT_SYMBOL_GPL(i915_gpu_raise);
5376
5377/**
5378 * i915_gpu_lower - lower GPU frequency limit
5379 *
5380 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5381 * frequency maximum.
5382 */
5383bool i915_gpu_lower(void)
5384{
5385 struct drm_i915_private *dev_priv;
5386 bool ret = true;
5387
Daniel Vetter92703882012-08-09 16:46:01 +02005388 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005389 if (!i915_mch_dev) {
5390 ret = false;
5391 goto out_unlock;
5392 }
5393 dev_priv = i915_mch_dev;
5394
Daniel Vetter20e4d402012-08-08 23:35:39 +02005395 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5396 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005397
5398out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005399 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005400
5401 return ret;
5402}
5403EXPORT_SYMBOL_GPL(i915_gpu_lower);
5404
5405/**
5406 * i915_gpu_busy - indicate GPU business to IPS
5407 *
5408 * Tell the IPS driver whether or not the GPU is busy.
5409 */
5410bool i915_gpu_busy(void)
5411{
5412 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005413 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005414 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005415 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005416
Daniel Vetter92703882012-08-09 16:46:01 +02005417 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005418 if (!i915_mch_dev)
5419 goto out_unlock;
5420 dev_priv = i915_mch_dev;
5421
Chris Wilsonf047e392012-07-21 12:31:41 +01005422 for_each_ring(ring, dev_priv, i)
5423 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005424
5425out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005426 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005427
5428 return ret;
5429}
5430EXPORT_SYMBOL_GPL(i915_gpu_busy);
5431
5432/**
5433 * i915_gpu_turbo_disable - disable graphics turbo
5434 *
5435 * Disable graphics turbo by resetting the max frequency and setting the
5436 * current frequency to the default.
5437 */
5438bool i915_gpu_turbo_disable(void)
5439{
5440 struct drm_i915_private *dev_priv;
5441 bool ret = true;
5442
Daniel Vetter92703882012-08-09 16:46:01 +02005443 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005444 if (!i915_mch_dev) {
5445 ret = false;
5446 goto out_unlock;
5447 }
5448 dev_priv = i915_mch_dev;
5449
Daniel Vetter20e4d402012-08-08 23:35:39 +02005450 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005451
Daniel Vetter20e4d402012-08-08 23:35:39 +02005452 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005453 ret = false;
5454
5455out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005456 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005457
5458 return ret;
5459}
5460EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5461
5462/**
5463 * Tells the intel_ips driver that the i915 driver is now loaded, if
5464 * IPS got loaded first.
5465 *
5466 * This awkward dance is so that neither module has to depend on the
5467 * other in order for IPS to do the appropriate communication of
5468 * GPU turbo limits to i915.
5469 */
5470static void
5471ips_ping_for_i915_load(void)
5472{
5473 void (*link)(void);
5474
5475 link = symbol_get(ips_link_to_i915_driver);
5476 if (link) {
5477 link();
5478 symbol_put(ips_link_to_i915_driver);
5479 }
5480}
5481
5482void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5483{
Daniel Vetter02d71952012-08-09 16:44:54 +02005484 /* We only register the i915 ips part with intel-ips once everything is
5485 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005486 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005487 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005488 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005489
5490 ips_ping_for_i915_load();
5491}
5492
5493void intel_gpu_ips_teardown(void)
5494{
Daniel Vetter92703882012-08-09 16:46:01 +02005495 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005496 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005497 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005498}
Deepak S76c3552f2014-01-30 23:08:16 +05305499
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005500static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005501{
5502 struct drm_i915_private *dev_priv = dev->dev_private;
5503 u32 lcfuse;
5504 u8 pxw[16];
5505 int i;
5506
5507 /* Disable to program */
5508 I915_WRITE(ECR, 0);
5509 POSTING_READ(ECR);
5510
5511 /* Program energy weights for various events */
5512 I915_WRITE(SDEW, 0x15040d00);
5513 I915_WRITE(CSIEW0, 0x007f0000);
5514 I915_WRITE(CSIEW1, 0x1e220004);
5515 I915_WRITE(CSIEW2, 0x04000004);
5516
5517 for (i = 0; i < 5; i++)
5518 I915_WRITE(PEW + (i * 4), 0);
5519 for (i = 0; i < 3; i++)
5520 I915_WRITE(DEW + (i * 4), 0);
5521
5522 /* Program P-state weights to account for frequency power adjustment */
5523 for (i = 0; i < 16; i++) {
5524 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5525 unsigned long freq = intel_pxfreq(pxvidfreq);
5526 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5527 PXVFREQ_PX_SHIFT;
5528 unsigned long val;
5529
5530 val = vid * vid;
5531 val *= (freq / 1000);
5532 val *= 255;
5533 val /= (127*127*900);
5534 if (val > 0xff)
5535 DRM_ERROR("bad pxval: %ld\n", val);
5536 pxw[i] = val;
5537 }
5538 /* Render standby states get 0 weight */
5539 pxw[14] = 0;
5540 pxw[15] = 0;
5541
5542 for (i = 0; i < 4; i++) {
5543 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5544 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5545 I915_WRITE(PXW + (i * 4), val);
5546 }
5547
5548 /* Adjust magic regs to magic values (more experimental results) */
5549 I915_WRITE(OGW0, 0);
5550 I915_WRITE(OGW1, 0);
5551 I915_WRITE(EG0, 0x00007f00);
5552 I915_WRITE(EG1, 0x0000000e);
5553 I915_WRITE(EG2, 0x000e0000);
5554 I915_WRITE(EG3, 0x68000300);
5555 I915_WRITE(EG4, 0x42000000);
5556 I915_WRITE(EG5, 0x00140031);
5557 I915_WRITE(EG6, 0);
5558 I915_WRITE(EG7, 0);
5559
5560 for (i = 0; i < 8; i++)
5561 I915_WRITE(PXWL + (i * 4), 0);
5562
5563 /* Enable PMON + select events */
5564 I915_WRITE(ECR, 0x80000019);
5565
5566 lcfuse = I915_READ(LCFUSE02);
5567
Daniel Vetter20e4d402012-08-08 23:35:39 +02005568 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005569}
5570
Imre Deakae484342014-03-31 15:10:44 +03005571void intel_init_gt_powersave(struct drm_device *dev)
5572{
Imre Deake6069ca2014-04-18 16:01:02 +03005573 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5574
Deepak S38807742014-05-23 21:00:15 +05305575 if (IS_CHERRYVIEW(dev))
5576 cherryview_init_gt_powersave(dev);
5577 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005578 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005579}
5580
5581void intel_cleanup_gt_powersave(struct drm_device *dev)
5582{
Deepak S38807742014-05-23 21:00:15 +05305583 if (IS_CHERRYVIEW(dev))
5584 return;
5585 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005586 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005587}
5588
Imre Deakdbea3ce2014-12-15 18:59:28 +02005589static void gen6_suspend_rps(struct drm_device *dev)
5590{
5591 struct drm_i915_private *dev_priv = dev->dev_private;
5592
5593 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5594
5595 /*
5596 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5597 * is added for it.
5598 */
5599 if (INTEL_INFO(dev)->gen < 9)
5600 gen6_disable_rps_interrupts(dev);
5601}
5602
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005603/**
5604 * intel_suspend_gt_powersave - suspend PM work and helper threads
5605 * @dev: drm device
5606 *
5607 * We don't want to disable RC6 or other features here, we just want
5608 * to make sure any work we've queued has finished and won't bother
5609 * us while we're suspended.
5610 */
5611void intel_suspend_gt_powersave(struct drm_device *dev)
5612{
5613 struct drm_i915_private *dev_priv = dev->dev_private;
5614
Imre Deakd4d70aa2014-11-19 15:30:04 +02005615 if (INTEL_INFO(dev)->gen < 6)
5616 return;
5617
Imre Deakdbea3ce2014-12-15 18:59:28 +02005618 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305619
5620 /* Force GPU to min freq during suspend */
5621 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005622}
5623
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005624void intel_disable_gt_powersave(struct drm_device *dev)
5625{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005626 struct drm_i915_private *dev_priv = dev->dev_private;
5627
Daniel Vetter930ebb42012-06-29 23:32:16 +02005628 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005629 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305630 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005631 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005632
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005633 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005634 if (INTEL_INFO(dev)->gen >= 9)
5635 gen9_disable_rps(dev);
5636 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305637 cherryview_disable_rps(dev);
5638 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005639 valleyview_disable_rps(dev);
5640 else
5641 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005642
Chris Wilsonc0951f02013-10-10 21:58:50 +01005643 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005644 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005645 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005646}
5647
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005648static void intel_gen6_powersave_work(struct work_struct *work)
5649{
5650 struct drm_i915_private *dev_priv =
5651 container_of(work, struct drm_i915_private,
5652 rps.delayed_resume_work.work);
5653 struct drm_device *dev = dev_priv->dev;
5654
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005655 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005656
Imre Deak3cc134e2014-11-19 15:30:03 +02005657 /*
5658 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5659 * added for it.
5660 */
5661 if (INTEL_INFO(dev)->gen < 9)
5662 gen6_reset_rps_interrupts(dev);
5663
Deepak S38807742014-05-23 21:00:15 +05305664 if (IS_CHERRYVIEW(dev)) {
5665 cherryview_enable_rps(dev);
5666 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005667 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005668 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005669 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005670 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005671 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005672 } else if (IS_BROADWELL(dev)) {
5673 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005674 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005675 } else {
5676 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005677 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005678 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005679 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005680
5681 if (INTEL_INFO(dev)->gen < 9)
5682 gen6_enable_rps_interrupts(dev);
5683
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005684 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005685
5686 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005687}
5688
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005689void intel_enable_gt_powersave(struct drm_device *dev)
5690{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005691 struct drm_i915_private *dev_priv = dev->dev_private;
5692
Yu Zhangf61018b2015-02-10 19:05:52 +08005693 /* Powersaving is controlled by the host when inside a VM */
5694 if (intel_vgpu_active(dev))
5695 return;
5696
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005697 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005698 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005699 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005700 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005701 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305702 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005703 /*
5704 * PCU communication is slow and this doesn't need to be
5705 * done at any specific time, so do this out of our fast path
5706 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005707 *
5708 * We depend on the HW RC6 power context save/restore
5709 * mechanism when entering D3 through runtime PM suspend. So
5710 * disable RPM until RPS/RC6 is properly setup. We can only
5711 * get here via the driver load/system resume/runtime resume
5712 * paths, so the _noresume version is enough (and in case of
5713 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005714 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005715 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5716 round_jiffies_up_relative(HZ)))
5717 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005718 }
5719}
5720
Imre Deakc6df39b2014-04-14 20:24:29 +03005721void intel_reset_gt_powersave(struct drm_device *dev)
5722{
5723 struct drm_i915_private *dev_priv = dev->dev_private;
5724
Imre Deakdbea3ce2014-12-15 18:59:28 +02005725 if (INTEL_INFO(dev)->gen < 6)
5726 return;
5727
5728 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005729 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005730}
5731
Daniel Vetter3107bd42012-10-31 22:52:31 +01005732static void ibx_init_clock_gating(struct drm_device *dev)
5733{
5734 struct drm_i915_private *dev_priv = dev->dev_private;
5735
5736 /*
5737 * On Ibex Peak and Cougar Point, we need to disable clock
5738 * gating for the panel power sequencer or it will fail to
5739 * start up when no ports are active.
5740 */
5741 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5742}
5743
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005744static void g4x_disable_trickle_feed(struct drm_device *dev)
5745{
5746 struct drm_i915_private *dev_priv = dev->dev_private;
5747 int pipe;
5748
Damien Lespiau055e3932014-08-18 13:49:10 +01005749 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005750 I915_WRITE(DSPCNTR(pipe),
5751 I915_READ(DSPCNTR(pipe)) |
5752 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005753 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005754 }
5755}
5756
Ville Syrjälä017636c2013-12-05 15:51:37 +02005757static void ilk_init_lp_watermarks(struct drm_device *dev)
5758{
5759 struct drm_i915_private *dev_priv = dev->dev_private;
5760
5761 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5762 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5763 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5764
5765 /*
5766 * Don't touch WM1S_LP_EN here.
5767 * Doing so could cause underruns.
5768 */
5769}
5770
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005771static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005772{
5773 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005774 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005775
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005776 /*
5777 * Required for FBC
5778 * WaFbcDisableDpfcClockGating:ilk
5779 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005780 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5781 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5782 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005783
5784 I915_WRITE(PCH_3DCGDIS0,
5785 MARIUNIT_CLOCK_GATE_DISABLE |
5786 SVSMUNIT_CLOCK_GATE_DISABLE);
5787 I915_WRITE(PCH_3DCGDIS1,
5788 VFMUNIT_CLOCK_GATE_DISABLE);
5789
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005790 /*
5791 * According to the spec the following bits should be set in
5792 * order to enable memory self-refresh
5793 * The bit 22/21 of 0x42004
5794 * The bit 5 of 0x42020
5795 * The bit 15 of 0x45000
5796 */
5797 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5798 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5799 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005800 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005801 I915_WRITE(DISP_ARB_CTL,
5802 (I915_READ(DISP_ARB_CTL) |
5803 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005804
5805 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005806
5807 /*
5808 * Based on the document from hardware guys the following bits
5809 * should be set unconditionally in order to enable FBC.
5810 * The bit 22 of 0x42000
5811 * The bit 22 of 0x42004
5812 * The bit 7,8,9 of 0x42020.
5813 */
5814 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005815 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005816 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5817 I915_READ(ILK_DISPLAY_CHICKEN1) |
5818 ILK_FBCQ_DIS);
5819 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5820 I915_READ(ILK_DISPLAY_CHICKEN2) |
5821 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005822 }
5823
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005824 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5825
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005826 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5827 I915_READ(ILK_DISPLAY_CHICKEN2) |
5828 ILK_ELPIN_409_SELECT);
5829 I915_WRITE(_3D_CHICKEN2,
5830 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5831 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005832
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005833 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005834 I915_WRITE(CACHE_MODE_0,
5835 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005836
Akash Goel4e046322014-04-04 17:14:38 +05305837 /* WaDisable_RenderCache_OperationalFlush:ilk */
5838 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5839
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005840 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005841
Daniel Vetter3107bd42012-10-31 22:52:31 +01005842 ibx_init_clock_gating(dev);
5843}
5844
5845static void cpt_init_clock_gating(struct drm_device *dev)
5846{
5847 struct drm_i915_private *dev_priv = dev->dev_private;
5848 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005849 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005850
5851 /*
5852 * On Ibex Peak and Cougar Point, we need to disable clock
5853 * gating for the panel power sequencer or it will fail to
5854 * start up when no ports are active.
5855 */
Jesse Barnescd664072013-10-02 10:34:19 -07005856 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5857 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5858 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005859 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5860 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005861 /* The below fixes the weird display corruption, a few pixels shifted
5862 * downward, on (only) LVDS of some HP laptops with IVY.
5863 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005864 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005865 val = I915_READ(TRANS_CHICKEN2(pipe));
5866 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5867 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005868 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005869 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005870 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5871 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5872 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005873 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5874 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005875 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005876 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005877 I915_WRITE(TRANS_CHICKEN1(pipe),
5878 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5879 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005880}
5881
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005882static void gen6_check_mch_setup(struct drm_device *dev)
5883{
5884 struct drm_i915_private *dev_priv = dev->dev_private;
5885 uint32_t tmp;
5886
5887 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005888 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5889 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5890 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005891}
5892
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005893static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005894{
5895 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005896 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005897
Damien Lespiau231e54f2012-10-19 17:55:41 +01005898 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005899
5900 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5901 I915_READ(ILK_DISPLAY_CHICKEN2) |
5902 ILK_ELPIN_409_SELECT);
5903
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005904 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005905 I915_WRITE(_3D_CHICKEN,
5906 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5907
Akash Goel4e046322014-04-04 17:14:38 +05305908 /* WaDisable_RenderCache_OperationalFlush:snb */
5909 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5910
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005911 /*
5912 * BSpec recoomends 8x4 when MSAA is used,
5913 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005914 *
5915 * Note that PS/WM thread counts depend on the WIZ hashing
5916 * disable bit, which we don't touch here, but it's good
5917 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005918 */
5919 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005920 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005921
Ville Syrjälä017636c2013-12-05 15:51:37 +02005922 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005923
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005924 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005925 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005926
5927 I915_WRITE(GEN6_UCGCTL1,
5928 I915_READ(GEN6_UCGCTL1) |
5929 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5930 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5931
5932 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5933 * gating disable must be set. Failure to set it results in
5934 * flickering pixels due to Z write ordering failures after
5935 * some amount of runtime in the Mesa "fire" demo, and Unigine
5936 * Sanctuary and Tropics, and apparently anything else with
5937 * alpha test or pixel discard.
5938 *
5939 * According to the spec, bit 11 (RCCUNIT) must also be set,
5940 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005941 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005942 * WaDisableRCCUnitClockGating:snb
5943 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005944 */
5945 I915_WRITE(GEN6_UCGCTL2,
5946 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5947 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5948
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005949 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005950 I915_WRITE(_3D_CHICKEN3,
5951 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005952
5953 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005954 * Bspec says:
5955 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5956 * 3DSTATE_SF number of SF output attributes is more than 16."
5957 */
5958 I915_WRITE(_3D_CHICKEN3,
5959 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5960
5961 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005962 * According to the spec the following bits should be
5963 * set in order to enable memory self-refresh and fbc:
5964 * The bit21 and bit22 of 0x42000
5965 * The bit21 and bit22 of 0x42004
5966 * The bit5 and bit7 of 0x42020
5967 * The bit14 of 0x70180
5968 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005969 *
5970 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005971 */
5972 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5973 I915_READ(ILK_DISPLAY_CHICKEN1) |
5974 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5975 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5976 I915_READ(ILK_DISPLAY_CHICKEN2) |
5977 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005978 I915_WRITE(ILK_DSPCLK_GATE_D,
5979 I915_READ(ILK_DSPCLK_GATE_D) |
5980 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5981 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005982
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005983 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005984
Daniel Vetter3107bd42012-10-31 22:52:31 +01005985 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005986
5987 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005988}
5989
5990static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5991{
5992 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5993
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005994 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005995 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005996 *
5997 * This actually overrides the dispatch
5998 * mode for all thread types.
5999 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006000 reg &= ~GEN7_FF_SCHED_MASK;
6001 reg |= GEN7_FF_TS_SCHED_HW;
6002 reg |= GEN7_FF_VS_SCHED_HW;
6003 reg |= GEN7_FF_DS_SCHED_HW;
6004
6005 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6006}
6007
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006008static void lpt_init_clock_gating(struct drm_device *dev)
6009{
6010 struct drm_i915_private *dev_priv = dev->dev_private;
6011
6012 /*
6013 * TODO: this bit should only be enabled when really needed, then
6014 * disabled when not needed anymore in order to save power.
6015 */
6016 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6017 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6018 I915_READ(SOUTH_DSPCLK_GATE_D) |
6019 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006020
6021 /* WADPOClockGatingDisable:hsw */
6022 I915_WRITE(_TRANSA_CHICKEN1,
6023 I915_READ(_TRANSA_CHICKEN1) |
6024 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006025}
6026
Imre Deak7d708ee2013-04-17 14:04:50 +03006027static void lpt_suspend_hw(struct drm_device *dev)
6028{
6029 struct drm_i915_private *dev_priv = dev->dev_private;
6030
6031 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6032 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6033
6034 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6035 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6036 }
6037}
6038
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006039static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006040{
6041 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006042 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006043
6044 I915_WRITE(WM3_LP_ILK, 0);
6045 I915_WRITE(WM2_LP_ILK, 0);
6046 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006047
Ben Widawskyab57fff2013-12-12 15:28:04 -08006048 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006049 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006050
Ben Widawskyab57fff2013-12-12 15:28:04 -08006051 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006052 I915_WRITE(CHICKEN_PAR1_1,
6053 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6054
Ben Widawskyab57fff2013-12-12 15:28:04 -08006055 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006056 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006057 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006058 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006059 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006060 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006061
Ben Widawskyab57fff2013-12-12 15:28:04 -08006062 /* WaVSRefCountFullforceMissDisable:bdw */
6063 /* WaDSRefCountFullforceMissDisable:bdw */
6064 I915_WRITE(GEN7_FF_THREAD_MODE,
6065 I915_READ(GEN7_FF_THREAD_MODE) &
6066 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006067
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006068 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6069 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006070
6071 /* WaDisableSDEUnitClockGating:bdw */
6072 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6073 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006074
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006075 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006076}
6077
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006078static void haswell_init_clock_gating(struct drm_device *dev)
6079{
6080 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006081
Ville Syrjälä017636c2013-12-05 15:51:37 +02006082 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006083
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006084 /* L3 caching of data atomics doesn't work -- disable it. */
6085 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6086 I915_WRITE(HSW_ROW_CHICKEN3,
6087 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6088
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006089 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006090 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6091 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6092 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6093
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006094 /* WaVSRefCountFullforceMissDisable:hsw */
6095 I915_WRITE(GEN7_FF_THREAD_MODE,
6096 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006097
Akash Goel4e046322014-04-04 17:14:38 +05306098 /* WaDisable_RenderCache_OperationalFlush:hsw */
6099 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6100
Chia-I Wufe27c602014-01-28 13:29:33 +08006101 /* enable HiZ Raw Stall Optimization */
6102 I915_WRITE(CACHE_MODE_0_GEN7,
6103 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6104
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006105 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006106 I915_WRITE(CACHE_MODE_1,
6107 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006108
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006109 /*
6110 * BSpec recommends 8x4 when MSAA is used,
6111 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006112 *
6113 * Note that PS/WM thread counts depend on the WIZ hashing
6114 * disable bit, which we don't touch here, but it's good
6115 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006116 */
6117 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006118 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006119
Kenneth Graunke94411592014-12-31 16:23:00 -08006120 /* WaSampleCChickenBitEnable:hsw */
6121 I915_WRITE(HALF_SLICE_CHICKEN3,
6122 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6123
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006124 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006125 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6126
Paulo Zanoni90a88642013-05-03 17:23:45 -03006127 /* WaRsPkgCStateDisplayPMReq:hsw */
6128 I915_WRITE(CHICKEN_PAR1_1,
6129 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006130
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006131 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006132}
6133
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006134static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006135{
6136 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006137 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006138
Ville Syrjälä017636c2013-12-05 15:51:37 +02006139 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006140
Damien Lespiau231e54f2012-10-19 17:55:41 +01006141 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006142
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006143 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006144 I915_WRITE(_3D_CHICKEN3,
6145 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6146
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006147 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006148 I915_WRITE(IVB_CHICKEN3,
6149 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6150 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6151
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006152 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006153 if (IS_IVB_GT1(dev))
6154 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6155 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006156
Akash Goel4e046322014-04-04 17:14:38 +05306157 /* WaDisable_RenderCache_OperationalFlush:ivb */
6158 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6159
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006160 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006161 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6162 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6163
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006164 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006165 I915_WRITE(GEN7_L3CNTLREG1,
6166 GEN7_WA_FOR_GEN7_L3_CONTROL);
6167 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006168 GEN7_WA_L3_CHICKEN_MODE);
6169 if (IS_IVB_GT1(dev))
6170 I915_WRITE(GEN7_ROW_CHICKEN2,
6171 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006172 else {
6173 /* must write both registers */
6174 I915_WRITE(GEN7_ROW_CHICKEN2,
6175 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006176 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6177 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006178 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006179
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006180 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006181 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6182 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6183
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006184 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006185 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006186 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006187 */
6188 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006189 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006190
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006191 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006192 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6193 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6194 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6195
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006196 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006197
6198 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006199
Chris Wilson22721342014-03-04 09:41:43 +00006200 if (0) { /* causes HiZ corruption on ivb:gt1 */
6201 /* enable HiZ Raw Stall Optimization */
6202 I915_WRITE(CACHE_MODE_0_GEN7,
6203 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6204 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006205
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006206 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006207 I915_WRITE(CACHE_MODE_1,
6208 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006209
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006210 /*
6211 * BSpec recommends 8x4 when MSAA is used,
6212 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006213 *
6214 * Note that PS/WM thread counts depend on the WIZ hashing
6215 * disable bit, which we don't touch here, but it's good
6216 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006217 */
6218 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006219 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006220
Ben Widawsky20848222012-05-04 18:58:59 -07006221 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6222 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6223 snpcr |= GEN6_MBC_SNPCR_MED;
6224 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006225
Ben Widawskyab5c6082013-04-05 13:12:41 -07006226 if (!HAS_PCH_NOP(dev))
6227 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006228
6229 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006230}
6231
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006232static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6233{
6234 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6235
6236 /*
6237 * Disable trickle feed and enable pnd deadline calculation
6238 */
6239 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6240 I915_WRITE(CBR1_VLV, 0);
6241}
6242
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006243static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006244{
6245 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006246
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006247 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006248
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006249 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006250 I915_WRITE(_3D_CHICKEN3,
6251 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6252
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006253 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006254 I915_WRITE(IVB_CHICKEN3,
6255 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6256 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6257
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006258 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006259 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006260 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006261 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6262 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006263
Akash Goel4e046322014-04-04 17:14:38 +05306264 /* WaDisable_RenderCache_OperationalFlush:vlv */
6265 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6266
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006267 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006268 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6269 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6270
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006271 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006272 I915_WRITE(GEN7_ROW_CHICKEN2,
6273 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6274
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006275 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006276 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6277 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6278 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6279
Ville Syrjälä46680e02014-01-22 21:33:01 +02006280 gen7_setup_fixed_func_scheduler(dev_priv);
6281
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006282 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006283 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006284 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006285 */
6286 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006287 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006288
Akash Goelc98f5062014-03-24 23:00:07 +05306289 /* WaDisableL3Bank2xClockGate:vlv
6290 * Disabling L3 clock gating- MMIO 940c[25] = 1
6291 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6292 I915_WRITE(GEN7_UCGCTL4,
6293 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006294
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006295 /*
6296 * BSpec says this must be set, even though
6297 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6298 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006299 I915_WRITE(CACHE_MODE_1,
6300 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006301
6302 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006303 * BSpec recommends 8x4 when MSAA is used,
6304 * however in practice 16x4 seems fastest.
6305 *
6306 * Note that PS/WM thread counts depend on the WIZ hashing
6307 * disable bit, which we don't touch here, but it's good
6308 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6309 */
6310 I915_WRITE(GEN7_GT_MODE,
6311 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6312
6313 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006314 * WaIncreaseL3CreditsForVLVB0:vlv
6315 * This is the hardware default actually.
6316 */
6317 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6318
6319 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006320 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006321 * Disable clock gating on th GCFG unit to prevent a delay
6322 * in the reporting of vblank events.
6323 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006324 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006325}
6326
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006327static void cherryview_init_clock_gating(struct drm_device *dev)
6328{
6329 struct drm_i915_private *dev_priv = dev->dev_private;
6330
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006331 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006332
Ville Syrjälä232ce332014-04-09 13:28:35 +03006333 /* WaVSRefCountFullforceMissDisable:chv */
6334 /* WaDSRefCountFullforceMissDisable:chv */
6335 I915_WRITE(GEN7_FF_THREAD_MODE,
6336 I915_READ(GEN7_FF_THREAD_MODE) &
6337 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006338
6339 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6340 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6341 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006342
6343 /* WaDisableCSUnitClockGating:chv */
6344 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6345 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006346
6347 /* WaDisableSDEUnitClockGating:chv */
6348 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6349 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006350}
6351
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006352static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006353{
6354 struct drm_i915_private *dev_priv = dev->dev_private;
6355 uint32_t dspclk_gate;
6356
6357 I915_WRITE(RENCLK_GATE_D1, 0);
6358 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6359 GS_UNIT_CLOCK_GATE_DISABLE |
6360 CL_UNIT_CLOCK_GATE_DISABLE);
6361 I915_WRITE(RAMCLK_GATE_D, 0);
6362 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6363 OVRUNIT_CLOCK_GATE_DISABLE |
6364 OVCUNIT_CLOCK_GATE_DISABLE;
6365 if (IS_GM45(dev))
6366 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6367 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006368
6369 /* WaDisableRenderCachePipelinedFlush */
6370 I915_WRITE(CACHE_MODE_0,
6371 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006372
Akash Goel4e046322014-04-04 17:14:38 +05306373 /* WaDisable_RenderCache_OperationalFlush:g4x */
6374 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6375
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006376 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006377}
6378
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006379static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006380{
6381 struct drm_i915_private *dev_priv = dev->dev_private;
6382
6383 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6384 I915_WRITE(RENCLK_GATE_D2, 0);
6385 I915_WRITE(DSPCLK_GATE_D, 0);
6386 I915_WRITE(RAMCLK_GATE_D, 0);
6387 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006388 I915_WRITE(MI_ARB_STATE,
6389 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306390
6391 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6392 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006393}
6394
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006395static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006396{
6397 struct drm_i915_private *dev_priv = dev->dev_private;
6398
6399 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6400 I965_RCC_CLOCK_GATE_DISABLE |
6401 I965_RCPB_CLOCK_GATE_DISABLE |
6402 I965_ISC_CLOCK_GATE_DISABLE |
6403 I965_FBC_CLOCK_GATE_DISABLE);
6404 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006405 I915_WRITE(MI_ARB_STATE,
6406 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306407
6408 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6409 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006410}
6411
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006412static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006413{
6414 struct drm_i915_private *dev_priv = dev->dev_private;
6415 u32 dstate = I915_READ(D_STATE);
6416
6417 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6418 DSTATE_DOT_CLOCK_GATING;
6419 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006420
6421 if (IS_PINEVIEW(dev))
6422 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006423
6424 /* IIR "flip pending" means done if this bit is set */
6425 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006426
6427 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006428 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006429
6430 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6431 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006432
6433 I915_WRITE(MI_ARB_STATE,
6434 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006435}
6436
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006437static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006438{
6439 struct drm_i915_private *dev_priv = dev->dev_private;
6440
6441 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006442
6443 /* interrupts should cause a wake up from C3 */
6444 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6445 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006446
6447 I915_WRITE(MEM_MODE,
6448 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006449}
6450
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006451static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006452{
6453 struct drm_i915_private *dev_priv = dev->dev_private;
6454
6455 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006456
6457 I915_WRITE(MEM_MODE,
6458 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6459 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006460}
6461
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006462void intel_init_clock_gating(struct drm_device *dev)
6463{
6464 struct drm_i915_private *dev_priv = dev->dev_private;
6465
Damien Lespiauc57e3552015-02-09 19:33:05 +00006466 if (dev_priv->display.init_clock_gating)
6467 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006468}
6469
Imre Deak7d708ee2013-04-17 14:04:50 +03006470void intel_suspend_hw(struct drm_device *dev)
6471{
6472 if (HAS_PCH_LPT(dev))
6473 lpt_suspend_hw(dev);
6474}
6475
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006476/* Set up chip specific power management-related functions */
6477void intel_init_pm(struct drm_device *dev)
6478{
6479 struct drm_i915_private *dev_priv = dev->dev_private;
6480
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006481 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006482
Daniel Vetterc921aba2012-04-26 23:28:17 +02006483 /* For cxsr */
6484 if (IS_PINEVIEW(dev))
6485 i915_pineview_get_mem_freq(dev);
6486 else if (IS_GEN5(dev))
6487 i915_ironlake_get_mem_freq(dev);
6488
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006489 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006490 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006491 skl_setup_wm_latency(dev);
6492
Damien Lespiau45db2192015-02-09 19:33:09 +00006493 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006494 dev_priv->display.update_wm = skl_update_wm;
6495 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306496 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006497 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006498
Ville Syrjäläbd602542014-01-07 16:14:10 +02006499 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6500 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6501 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6502 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6503 dev_priv->display.update_wm = ilk_update_wm;
6504 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6505 } else {
6506 DRM_DEBUG_KMS("Failed to read display plane latency. "
6507 "Disable CxSR\n");
6508 }
6509
6510 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006511 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006512 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006513 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006514 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006515 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006516 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006517 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006518 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006519 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006520 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläae801522015-03-05 21:19:49 +02006521 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306522 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006523 dev_priv->display.init_clock_gating =
6524 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006525 } else if (IS_VALLEYVIEW(dev)) {
6526 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306527 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006528 dev_priv->display.init_clock_gating =
6529 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006530 } else if (IS_PINEVIEW(dev)) {
6531 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6532 dev_priv->is_ddr3,
6533 dev_priv->fsb_freq,
6534 dev_priv->mem_freq)) {
6535 DRM_INFO("failed to find known CxSR latency "
6536 "(found ddr%s fsb freq %d, mem freq %d), "
6537 "disabling CxSR\n",
6538 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6539 dev_priv->fsb_freq, dev_priv->mem_freq);
6540 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006541 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006542 dev_priv->display.update_wm = NULL;
6543 } else
6544 dev_priv->display.update_wm = pineview_update_wm;
6545 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6546 } else if (IS_G4X(dev)) {
6547 dev_priv->display.update_wm = g4x_update_wm;
6548 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6549 } else if (IS_GEN4(dev)) {
6550 dev_priv->display.update_wm = i965_update_wm;
6551 if (IS_CRESTLINE(dev))
6552 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6553 else if (IS_BROADWATER(dev))
6554 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6555 } else if (IS_GEN3(dev)) {
6556 dev_priv->display.update_wm = i9xx_update_wm;
6557 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6558 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006559 } else if (IS_GEN2(dev)) {
6560 if (INTEL_INFO(dev)->num_pipes == 1) {
6561 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006562 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006563 } else {
6564 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006565 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006566 }
6567
6568 if (IS_I85X(dev) || IS_I865G(dev))
6569 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6570 else
6571 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6572 } else {
6573 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006574 }
6575}
6576
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006577int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006578{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006579 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006580
6581 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6582 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6583 return -EAGAIN;
6584 }
6585
6586 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006587 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006588 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6589
6590 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6591 500)) {
6592 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6593 return -ETIMEDOUT;
6594 }
6595
6596 *val = I915_READ(GEN6_PCODE_DATA);
6597 I915_WRITE(GEN6_PCODE_DATA, 0);
6598
6599 return 0;
6600}
6601
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006602int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006603{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006604 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006605
6606 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6607 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6608 return -EAGAIN;
6609 }
6610
6611 I915_WRITE(GEN6_PCODE_DATA, val);
6612 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6613
6614 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6615 500)) {
6616 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6617 return -ETIMEDOUT;
6618 }
6619
6620 I915_WRITE(GEN6_PCODE_DATA, 0);
6621
6622 return 0;
6623}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006624
Ville Syrjälädd06f882014-11-10 22:55:12 +02006625static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006626{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006627 switch (czclk_freq) {
6628 case 200:
6629 return 10;
6630 case 267:
6631 return 12;
6632 case 320:
6633 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006634 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006635 case 400:
6636 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006637 default:
6638 return -1;
6639 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006640}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006641
Ville Syrjälädd06f882014-11-10 22:55:12 +02006642static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6643{
6644 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6645
6646 div = vlv_gpu_freq_div(czclk_freq);
6647 if (div < 0)
6648 return div;
6649
6650 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006651}
6652
Fengguang Wub55dd642014-07-12 11:21:39 +02006653static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006654{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006655 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006656
Ville Syrjälädd06f882014-11-10 22:55:12 +02006657 mul = vlv_gpu_freq_div(czclk_freq);
6658 if (mul < 0)
6659 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006660
Ville Syrjälädd06f882014-11-10 22:55:12 +02006661 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006662}
6663
Fengguang Wub55dd642014-07-12 11:21:39 +02006664static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306665{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006666 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306667
Ville Syrjälädd06f882014-11-10 22:55:12 +02006668 div = vlv_gpu_freq_div(czclk_freq) / 2;
6669 if (div < 0)
6670 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306671
Ville Syrjälädd06f882014-11-10 22:55:12 +02006672 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306673}
6674
Fengguang Wub55dd642014-07-12 11:21:39 +02006675static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306676{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006677 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306678
Ville Syrjälädd06f882014-11-10 22:55:12 +02006679 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6680 if (mul < 0)
6681 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306682
Ville Syrjälä1c147622014-08-18 14:42:43 +03006683 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006684 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306685}
6686
Ville Syrjälä616bc822015-01-23 21:04:25 +02006687int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6688{
6689 if (IS_CHERRYVIEW(dev_priv->dev))
6690 return chv_gpu_freq(dev_priv, val);
6691 else if (IS_VALLEYVIEW(dev_priv->dev))
6692 return byt_gpu_freq(dev_priv, val);
6693 else
6694 return val * GT_FREQUENCY_MULTIPLIER;
6695}
6696
Ville Syrjälä616bc822015-01-23 21:04:25 +02006697int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6698{
Deepak S22b1b2f2014-07-12 14:54:33 +05306699 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006700 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306701 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006702 return byt_freq_opcode(dev_priv, val);
6703 else
6704 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306705}
6706
Daniel Vetterf742a552013-12-06 10:17:53 +01006707void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006708{
6709 struct drm_i915_private *dev_priv = dev->dev_private;
6710
Daniel Vetterf742a552013-12-06 10:17:53 +01006711 mutex_init(&dev_priv->rps.hw_lock);
6712
Chris Wilson907b28c2013-07-19 20:36:52 +01006713 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6714 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006715
Paulo Zanoni33688d92014-03-07 20:08:19 -03006716 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006717}