blob: 64bc77ef6b2ed4c6db8e3a4d32693a3b76852326 [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);
Nick Hoath6381b552015-07-14 14:41:15 +010062
63 /* WaDisableKillLogic:bxt,skl */
64 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
65 ECOCHK_DIS_TLB);
Damien Lespiau77719d22015-02-09 19:33:13 +000066}
Damien Lespiau91e41d12014-03-26 17:42:50 +000067
Damien Lespiau45db2192015-02-09 19:33:09 +000068static void skl_init_clock_gating(struct drm_device *dev)
Damien Lespiauda2078c2013-02-13 15:27:27 +000069{
Damien Lespiauacd5c342014-03-26 16:55:46 +000070 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau3ca5da42014-03-26 18:18:01 +000071
Damien Lespiau77719d22015-02-09 19:33:13 +000072 gen9_init_clock_gating(dev);
73
Damien Lespiau669506e2015-02-26 18:20:38 +000074 if (INTEL_REVID(dev) <= SKL_REVID_B0) {
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000075 /*
76 * WaDisableSDEUnitClockGating:skl
Damien Lespiau9253c2e2015-02-09 19:33:10 +000077 * WaSetGAPSunitClckGateDisable:skl
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000078 */
79 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Damien Lespiau9253c2e2015-02-09 19:33:10 +000080 GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000081 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiauf9fc42f2015-02-26 18:20:39 +000082
83 /* WaDisableVFUnitClockGating:skl */
84 I915_WRITE(GEN6_UCGCTL2, I915_READ(GEN6_UCGCTL2) |
85 GEN6_VFUNIT_CLOCK_GATE_DISABLE);
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000086 }
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000087
Damien Lespiau2caa3b22015-02-09 19:33:20 +000088 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
Damien Lespiau81e231a2015-02-09 19:33:19 +000089 /* WaDisableHDCInvalidation:skl */
90 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
91 BDW_DISABLE_HDC_INVALIDATION);
92
Damien Lespiau2caa3b22015-02-09 19:33:20 +000093 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
94 I915_WRITE(FF_SLICE_CS_CHICKEN2,
Damien Lespiauf1d3d342015-05-06 14:36:27 +010095 _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
Damien Lespiau2caa3b22015-02-09 19:33:20 +000096 }
Damien Lespiau81e231a2015-02-09 19:33:19 +000097
Arun Siluverya4106a72015-07-14 15:01:29 +010098 /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
99 * involving this register should also be added to WA batch as required.
100 */
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +0000101 if (INTEL_REVID(dev) <= SKL_REVID_E0)
102 /* WaDisableLSQCROPERFforOCL:skl */
103 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
104 GEN8_LQSC_RO_PERF_DIS);
Arun Siluvery245d9662015-08-03 20:24:56 +0100105
106 /* WaEnableGapsTsvCreditFix:skl */
107 if (IS_SKYLAKE(dev) && (INTEL_REVID(dev) >= SKL_REVID_C0)) {
108 I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
109 GEN9_GAPS_TSV_CREDIT_DISABLE));
110 }
Damien Lespiauda2078c2013-02-13 15:27:27 +0000111}
112
Imre Deaka82abe42015-03-27 14:00:04 +0200113static void bxt_init_clock_gating(struct drm_device *dev)
114{
Imre Deak32608ca2015-03-11 11:10:27 +0200115 struct drm_i915_private *dev_priv = dev->dev_private;
116
Imre Deaka82abe42015-03-27 14:00:04 +0200117 gen9_init_clock_gating(dev);
Imre Deak32608ca2015-03-11 11:10:27 +0200118
Nick Hoatha7546152015-06-29 14:07:32 +0100119 /* WaDisableSDEUnitClockGating:bxt */
120 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
121 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
122
Imre Deak32608ca2015-03-11 11:10:27 +0200123 /*
124 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +0200125 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200126 */
Imre Deak32608ca2015-03-11 11:10:27 +0200127 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200128 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deak32608ca2015-03-11 11:10:27 +0200129
Nick Hoatha7546152015-06-29 14:07:32 +0100130 if (INTEL_REVID(dev) == BXT_REVID_A0) {
131 /*
132 * Hardware specification requires this bit to be
133 * set to 1 for A0
134 */
135 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
136 }
Imre Deaka82abe42015-03-27 14:00:04 +0200137}
138
Daniel Vetterc921aba2012-04-26 23:28:17 +0200139static void i915_pineview_get_mem_freq(struct drm_device *dev)
140{
Jani Nikula50227e12014-03-31 14:27:21 +0300141 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200142 u32 tmp;
143
144 tmp = I915_READ(CLKCFG);
145
146 switch (tmp & CLKCFG_FSB_MASK) {
147 case CLKCFG_FSB_533:
148 dev_priv->fsb_freq = 533; /* 133*4 */
149 break;
150 case CLKCFG_FSB_800:
151 dev_priv->fsb_freq = 800; /* 200*4 */
152 break;
153 case CLKCFG_FSB_667:
154 dev_priv->fsb_freq = 667; /* 167*4 */
155 break;
156 case CLKCFG_FSB_400:
157 dev_priv->fsb_freq = 400; /* 100*4 */
158 break;
159 }
160
161 switch (tmp & CLKCFG_MEM_MASK) {
162 case CLKCFG_MEM_533:
163 dev_priv->mem_freq = 533;
164 break;
165 case CLKCFG_MEM_667:
166 dev_priv->mem_freq = 667;
167 break;
168 case CLKCFG_MEM_800:
169 dev_priv->mem_freq = 800;
170 break;
171 }
172
173 /* detect pineview DDR3 setting */
174 tmp = I915_READ(CSHRDDR3CTL);
175 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
176}
177
178static void i915_ironlake_get_mem_freq(struct drm_device *dev)
179{
Jani Nikula50227e12014-03-31 14:27:21 +0300180 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200181 u16 ddrpll, csipll;
182
183 ddrpll = I915_READ16(DDRMPLL1);
184 csipll = I915_READ16(CSIPLL0);
185
186 switch (ddrpll & 0xff) {
187 case 0xc:
188 dev_priv->mem_freq = 800;
189 break;
190 case 0x10:
191 dev_priv->mem_freq = 1066;
192 break;
193 case 0x14:
194 dev_priv->mem_freq = 1333;
195 break;
196 case 0x18:
197 dev_priv->mem_freq = 1600;
198 break;
199 default:
200 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
201 ddrpll & 0xff);
202 dev_priv->mem_freq = 0;
203 break;
204 }
205
Daniel Vetter20e4d402012-08-08 23:35:39 +0200206 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200207
208 switch (csipll & 0x3ff) {
209 case 0x00c:
210 dev_priv->fsb_freq = 3200;
211 break;
212 case 0x00e:
213 dev_priv->fsb_freq = 3733;
214 break;
215 case 0x010:
216 dev_priv->fsb_freq = 4266;
217 break;
218 case 0x012:
219 dev_priv->fsb_freq = 4800;
220 break;
221 case 0x014:
222 dev_priv->fsb_freq = 5333;
223 break;
224 case 0x016:
225 dev_priv->fsb_freq = 5866;
226 break;
227 case 0x018:
228 dev_priv->fsb_freq = 6400;
229 break;
230 default:
231 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
232 csipll & 0x3ff);
233 dev_priv->fsb_freq = 0;
234 break;
235 }
236
237 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200238 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200239 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200240 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200241 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200242 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200243 }
244}
245
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300246static const struct cxsr_latency cxsr_latency_table[] = {
247 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
248 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
249 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
250 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
251 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
252
253 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
254 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
255 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
256 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
257 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
258
259 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
260 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
261 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
262 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
263 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
264
265 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
266 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
267 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
268 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
269 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
270
271 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
272 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
273 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
274 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
275 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
276
277 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
278 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
279 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
280 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
281 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
282};
283
Daniel Vetter63c62272012-04-21 23:17:55 +0200284static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300285 int is_ddr3,
286 int fsb,
287 int mem)
288{
289 const struct cxsr_latency *latency;
290 int i;
291
292 if (fsb == 0 || mem == 0)
293 return NULL;
294
295 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
296 latency = &cxsr_latency_table[i];
297 if (is_desktop == latency->is_desktop &&
298 is_ddr3 == latency->is_ddr3 &&
299 fsb == latency->fsb_freq && mem == latency->mem_freq)
300 return latency;
301 }
302
303 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
304
305 return NULL;
306}
307
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200308static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
309{
310 u32 val;
311
312 mutex_lock(&dev_priv->rps.hw_lock);
313
314 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
315 if (enable)
316 val &= ~FORCE_DDR_HIGH_FREQ;
317 else
318 val |= FORCE_DDR_HIGH_FREQ;
319 val &= ~FORCE_DDR_LOW_FREQ;
320 val |= FORCE_DDR_FREQ_REQ_ACK;
321 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
322
323 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
324 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
325 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
326
327 mutex_unlock(&dev_priv->rps.hw_lock);
328}
329
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200330static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
331{
332 u32 val;
333
334 mutex_lock(&dev_priv->rps.hw_lock);
335
336 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
337 if (enable)
338 val |= DSP_MAXFIFO_PM5_ENABLE;
339 else
340 val &= ~DSP_MAXFIFO_PM5_ENABLE;
341 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
342
343 mutex_unlock(&dev_priv->rps.hw_lock);
344}
345
Ville Syrjäläf4998962015-03-10 17:02:21 +0200346#define FW_WM(value, plane) \
347 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
348
Imre Deak5209b1f2014-07-01 12:36:17 +0300349void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300350{
Imre Deak5209b1f2014-07-01 12:36:17 +0300351 struct drm_device *dev = dev_priv->dev;
352 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300353
Imre Deak5209b1f2014-07-01 12:36:17 +0300354 if (IS_VALLEYVIEW(dev)) {
355 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300356 POSTING_READ(FW_BLC_SELF_VLV);
Ville Syrjälä852eb002015-06-24 22:00:07 +0300357 dev_priv->wm.vlv.cxsr = enable;
Imre Deak5209b1f2014-07-01 12:36:17 +0300358 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
359 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300360 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300361 } else if (IS_PINEVIEW(dev)) {
362 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
363 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
364 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300365 POSTING_READ(DSPFW3);
Imre Deak5209b1f2014-07-01 12:36:17 +0300366 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
367 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
368 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
369 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300370 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300371 } else if (IS_I915GM(dev)) {
372 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
373 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
374 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300376 } else {
377 return;
378 }
379
380 DRM_DEBUG_KMS("memory self-refresh is %s\n",
381 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300382}
383
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200384
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300385/*
386 * Latency for FIFO fetches is dependent on several factors:
387 * - memory configuration (speed, channels)
388 * - chipset
389 * - current MCH state
390 * It can be fairly high in some situations, so here we assume a fairly
391 * pessimal value. It's a tradeoff between extra memory fetches (if we
392 * set this value too high, the FIFO will fetch frequently to stay full)
393 * and power consumption (set it too low to save power and we might see
394 * FIFO underruns and display "flicker").
395 *
396 * A value of 5us seems to be a good balance; safe for very low end
397 * platforms but not overly aggressive on lower latency configs.
398 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100399static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300400
Ville Syrjäläb5004722015-03-05 21:19:47 +0200401#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
402 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
403
404static int vlv_get_fifo_size(struct drm_device *dev,
405 enum pipe pipe, int plane)
406{
407 struct drm_i915_private *dev_priv = dev->dev_private;
408 int sprite0_start, sprite1_start, size;
409
410 switch (pipe) {
411 uint32_t dsparb, dsparb2, dsparb3;
412 case PIPE_A:
413 dsparb = I915_READ(DSPARB);
414 dsparb2 = I915_READ(DSPARB2);
415 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
416 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
417 break;
418 case PIPE_B:
419 dsparb = I915_READ(DSPARB);
420 dsparb2 = I915_READ(DSPARB2);
421 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
422 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
423 break;
424 case PIPE_C:
425 dsparb2 = I915_READ(DSPARB2);
426 dsparb3 = I915_READ(DSPARB3);
427 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
428 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
429 break;
430 default:
431 return 0;
432 }
433
434 switch (plane) {
435 case 0:
436 size = sprite0_start;
437 break;
438 case 1:
439 size = sprite1_start - sprite0_start;
440 break;
441 case 2:
442 size = 512 - 1 - sprite1_start;
443 break;
444 default:
445 return 0;
446 }
447
448 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
449 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
450 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
451 size);
452
453 return size;
454}
455
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300456static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300457{
458 struct drm_i915_private *dev_priv = dev->dev_private;
459 uint32_t dsparb = I915_READ(DSPARB);
460 int size;
461
462 size = dsparb & 0x7f;
463 if (plane)
464 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
465
466 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
467 plane ? "B" : "A", size);
468
469 return size;
470}
471
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200472static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300473{
474 struct drm_i915_private *dev_priv = dev->dev_private;
475 uint32_t dsparb = I915_READ(DSPARB);
476 int size;
477
478 size = dsparb & 0x1ff;
479 if (plane)
480 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
481 size >>= 1; /* Convert to cachelines */
482
483 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
484 plane ? "B" : "A", size);
485
486 return size;
487}
488
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300489static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300490{
491 struct drm_i915_private *dev_priv = dev->dev_private;
492 uint32_t dsparb = I915_READ(DSPARB);
493 int size;
494
495 size = dsparb & 0x7f;
496 size >>= 2; /* Convert to cachelines */
497
498 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
499 plane ? "B" : "A",
500 size);
501
502 return size;
503}
504
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300505/* Pineview has different values for various configs */
506static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300507 .fifo_size = PINEVIEW_DISPLAY_FIFO,
508 .max_wm = PINEVIEW_MAX_WM,
509 .default_wm = PINEVIEW_DFT_WM,
510 .guard_size = PINEVIEW_GUARD_WM,
511 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300512};
513static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300514 .fifo_size = PINEVIEW_DISPLAY_FIFO,
515 .max_wm = PINEVIEW_MAX_WM,
516 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
517 .guard_size = PINEVIEW_GUARD_WM,
518 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300519};
520static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300521 .fifo_size = PINEVIEW_CURSOR_FIFO,
522 .max_wm = PINEVIEW_CURSOR_MAX_WM,
523 .default_wm = PINEVIEW_CURSOR_DFT_WM,
524 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
525 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526};
527static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300528 .fifo_size = PINEVIEW_CURSOR_FIFO,
529 .max_wm = PINEVIEW_CURSOR_MAX_WM,
530 .default_wm = PINEVIEW_CURSOR_DFT_WM,
531 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
532 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533};
534static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300535 .fifo_size = G4X_FIFO_SIZE,
536 .max_wm = G4X_MAX_WM,
537 .default_wm = G4X_MAX_WM,
538 .guard_size = 2,
539 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540};
541static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300542 .fifo_size = I965_CURSOR_FIFO,
543 .max_wm = I965_CURSOR_MAX_WM,
544 .default_wm = I965_CURSOR_DFT_WM,
545 .guard_size = 2,
546 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300547};
548static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300549 .fifo_size = VALLEYVIEW_FIFO_SIZE,
550 .max_wm = VALLEYVIEW_MAX_WM,
551 .default_wm = VALLEYVIEW_MAX_WM,
552 .guard_size = 2,
553 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300554};
555static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300556 .fifo_size = I965_CURSOR_FIFO,
557 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
558 .default_wm = I965_CURSOR_DFT_WM,
559 .guard_size = 2,
560 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561};
562static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300563 .fifo_size = I965_CURSOR_FIFO,
564 .max_wm = I965_CURSOR_MAX_WM,
565 .default_wm = I965_CURSOR_DFT_WM,
566 .guard_size = 2,
567 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568};
569static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300570 .fifo_size = I945_FIFO_SIZE,
571 .max_wm = I915_MAX_WM,
572 .default_wm = 1,
573 .guard_size = 2,
574 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300575};
576static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300577 .fifo_size = I915_FIFO_SIZE,
578 .max_wm = I915_MAX_WM,
579 .default_wm = 1,
580 .guard_size = 2,
581 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300582};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300583static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300584 .fifo_size = I855GM_FIFO_SIZE,
585 .max_wm = I915_MAX_WM,
586 .default_wm = 1,
587 .guard_size = 2,
588 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300589};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300590static const struct intel_watermark_params i830_bc_wm_info = {
591 .fifo_size = I855GM_FIFO_SIZE,
592 .max_wm = I915_MAX_WM/2,
593 .default_wm = 1,
594 .guard_size = 2,
595 .cacheline_size = I830_FIFO_LINE_SIZE,
596};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200597static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300598 .fifo_size = I830_FIFO_SIZE,
599 .max_wm = I915_MAX_WM,
600 .default_wm = 1,
601 .guard_size = 2,
602 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300603};
604
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605/**
606 * intel_calculate_wm - calculate watermark level
607 * @clock_in_khz: pixel clock
608 * @wm: chip FIFO params
609 * @pixel_size: display pixel size
610 * @latency_ns: memory latency for the platform
611 *
612 * Calculate the watermark level (the level at which the display plane will
613 * start fetching from memory again). Each chip has a different display
614 * FIFO size and allocation, so the caller needs to figure that out and pass
615 * in the correct intel_watermark_params structure.
616 *
617 * As the pixel clock runs, the FIFO will be drained at a rate that depends
618 * on the pixel size. When it reaches the watermark level, it'll start
619 * fetching FIFO line sized based chunks from memory until the FIFO fills
620 * past the watermark point. If the FIFO drains completely, a FIFO underrun
621 * will occur, and a display engine hang could result.
622 */
623static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
624 const struct intel_watermark_params *wm,
625 int fifo_size,
626 int pixel_size,
627 unsigned long latency_ns)
628{
629 long entries_required, wm_size;
630
631 /*
632 * Note: we need to make sure we don't overflow for various clock &
633 * latency values.
634 * clocks go from a few thousand to several hundred thousand.
635 * latency is usually a few thousand
636 */
637 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
638 1000;
639 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
640
641 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
642
643 wm_size = fifo_size - (entries_required + wm->guard_size);
644
645 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
646
647 /* Don't promote wm_size to unsigned... */
648 if (wm_size > (long)wm->max_wm)
649 wm_size = wm->max_wm;
650 if (wm_size <= 0)
651 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300652
653 /*
654 * Bspec seems to indicate that the value shouldn't be lower than
655 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
656 * Lets go for 8 which is the burst size since certain platforms
657 * already use a hardcoded 8 (which is what the spec says should be
658 * done).
659 */
660 if (wm_size <= 8)
661 wm_size = 8;
662
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300663 return wm_size;
664}
665
666static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
667{
668 struct drm_crtc *crtc, *enabled = NULL;
669
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100670 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000671 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300672 if (enabled)
673 return NULL;
674 enabled = crtc;
675 }
676 }
677
678 return enabled;
679}
680
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300681static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300682{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300683 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300684 struct drm_i915_private *dev_priv = dev->dev_private;
685 struct drm_crtc *crtc;
686 const struct cxsr_latency *latency;
687 u32 reg;
688 unsigned long wm;
689
690 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
691 dev_priv->fsb_freq, dev_priv->mem_freq);
692 if (!latency) {
693 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300694 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300695 return;
696 }
697
698 crtc = single_enabled_crtc(dev);
699 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100700 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800701 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100702 int clock;
703
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200704 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100705 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300706
707 /* Display SR */
708 wm = intel_calculate_wm(clock, &pineview_display_wm,
709 pineview_display_wm.fifo_size,
710 pixel_size, latency->display_sr);
711 reg = I915_READ(DSPFW1);
712 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200713 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300714 I915_WRITE(DSPFW1, reg);
715 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
716
717 /* cursor SR */
718 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
719 pineview_display_wm.fifo_size,
720 pixel_size, latency->cursor_sr);
721 reg = I915_READ(DSPFW3);
722 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200723 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724 I915_WRITE(DSPFW3, reg);
725
726 /* Display HPLL off SR */
727 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
728 pineview_display_hplloff_wm.fifo_size,
729 pixel_size, latency->display_hpll_disable);
730 reg = I915_READ(DSPFW3);
731 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200732 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300733 I915_WRITE(DSPFW3, reg);
734
735 /* cursor HPLL off SR */
736 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
737 pineview_display_hplloff_wm.fifo_size,
738 pixel_size, latency->cursor_hpll_disable);
739 reg = I915_READ(DSPFW3);
740 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200741 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300742 I915_WRITE(DSPFW3, reg);
743 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
744
Imre Deak5209b1f2014-07-01 12:36:17 +0300745 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300747 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300748 }
749}
750
751static bool g4x_compute_wm0(struct drm_device *dev,
752 int plane,
753 const struct intel_watermark_params *display,
754 int display_latency_ns,
755 const struct intel_watermark_params *cursor,
756 int cursor_latency_ns,
757 int *plane_wm,
758 int *cursor_wm)
759{
760 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300761 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300762 int htotal, hdisplay, clock, pixel_size;
763 int line_time_us, line_count;
764 int entries, tlb_miss;
765
766 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000767 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300768 *cursor_wm = cursor->guard_size;
769 *plane_wm = display->guard_size;
770 return false;
771 }
772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200773 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100774 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800775 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200776 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800777 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778
779 /* Use the small buffer method to calculate plane watermark */
780 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
781 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
782 if (tlb_miss > 0)
783 entries += tlb_miss;
784 entries = DIV_ROUND_UP(entries, display->cacheline_size);
785 *plane_wm = entries + display->guard_size;
786 if (*plane_wm > (int)display->max_wm)
787 *plane_wm = display->max_wm;
788
789 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200790 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300791 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800792 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300793 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
794 if (tlb_miss > 0)
795 entries += tlb_miss;
796 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
797 *cursor_wm = entries + cursor->guard_size;
798 if (*cursor_wm > (int)cursor->max_wm)
799 *cursor_wm = (int)cursor->max_wm;
800
801 return true;
802}
803
804/*
805 * Check the wm result.
806 *
807 * If any calculated watermark values is larger than the maximum value that
808 * can be programmed into the associated watermark register, that watermark
809 * must be disabled.
810 */
811static bool g4x_check_srwm(struct drm_device *dev,
812 int display_wm, int cursor_wm,
813 const struct intel_watermark_params *display,
814 const struct intel_watermark_params *cursor)
815{
816 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
817 display_wm, cursor_wm);
818
819 if (display_wm > display->max_wm) {
820 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
821 display_wm, display->max_wm);
822 return false;
823 }
824
825 if (cursor_wm > cursor->max_wm) {
826 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
827 cursor_wm, cursor->max_wm);
828 return false;
829 }
830
831 if (!(display_wm || cursor_wm)) {
832 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
833 return false;
834 }
835
836 return true;
837}
838
839static bool g4x_compute_srwm(struct drm_device *dev,
840 int plane,
841 int latency_ns,
842 const struct intel_watermark_params *display,
843 const struct intel_watermark_params *cursor,
844 int *display_wm, int *cursor_wm)
845{
846 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300847 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300848 int hdisplay, htotal, pixel_size, clock;
849 unsigned long line_time_us;
850 int line_count, line_size;
851 int small, large;
852 int entries;
853
854 if (!latency_ns) {
855 *display_wm = *cursor_wm = 0;
856 return false;
857 }
858
859 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200860 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100861 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800862 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200863 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800864 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300865
Ville Syrjälä922044c2014-02-14 14:18:57 +0200866 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300867 line_count = (latency_ns / line_time_us + 1000) / 1000;
868 line_size = hdisplay * pixel_size;
869
870 /* Use the minimum of the small and large buffer method for primary */
871 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
872 large = line_count * line_size;
873
874 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
875 *display_wm = entries + display->guard_size;
876
877 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800878 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300879 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
880 *cursor_wm = entries + cursor->guard_size;
881
882 return g4x_check_srwm(dev,
883 *display_wm, *cursor_wm,
884 display, cursor);
885}
886
Ville Syrjälä15665972015-03-10 16:16:28 +0200887#define FW_WM_VLV(value, plane) \
888 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
889
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200890static void vlv_write_wm_values(struct intel_crtc *crtc,
891 const struct vlv_wm_values *wm)
892{
893 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
894 enum pipe pipe = crtc->pipe;
895
896 I915_WRITE(VLV_DDL(pipe),
897 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
898 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
899 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
900 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
901
Ville Syrjäläae801522015-03-05 21:19:49 +0200902 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200903 FW_WM(wm->sr.plane, SR) |
904 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
905 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
906 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200907 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200908 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
909 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
910 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200911 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200912 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200913
914 if (IS_CHERRYVIEW(dev_priv)) {
915 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200916 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
917 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200918 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200919 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
920 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200921 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200922 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
923 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200924 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200925 FW_WM(wm->sr.plane >> 9, SR_HI) |
926 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
927 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
928 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
929 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
930 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
931 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
932 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
933 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
934 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200935 } else {
936 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200937 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
938 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200939 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200940 FW_WM(wm->sr.plane >> 9, SR_HI) |
941 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
942 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
943 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
944 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
945 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
946 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200947 }
948
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300949 /* zero (unused) WM1 watermarks */
950 I915_WRITE(DSPFW4, 0);
951 I915_WRITE(DSPFW5, 0);
952 I915_WRITE(DSPFW6, 0);
953 I915_WRITE(DSPHOWM1, 0);
954
Ville Syrjäläae801522015-03-05 21:19:49 +0200955 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200956}
957
Ville Syrjälä15665972015-03-10 16:16:28 +0200958#undef FW_WM_VLV
959
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300960enum vlv_wm_level {
961 VLV_WM_LEVEL_PM2,
962 VLV_WM_LEVEL_PM5,
963 VLV_WM_LEVEL_DDR_DVFS,
964 CHV_WM_NUM_LEVELS,
965 VLV_WM_NUM_LEVELS = 1,
966};
967
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300968/* latency must be in 0.1us units. */
969static unsigned int vlv_wm_method2(unsigned int pixel_rate,
970 unsigned int pipe_htotal,
971 unsigned int horiz_pixels,
972 unsigned int bytes_per_pixel,
973 unsigned int latency)
974{
975 unsigned int ret;
976
977 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
978 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
979 ret = DIV_ROUND_UP(ret, 64);
980
981 return ret;
982}
983
984static void vlv_setup_wm_latency(struct drm_device *dev)
985{
986 struct drm_i915_private *dev_priv = dev->dev_private;
987
988 /* all latencies in usec */
989 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
990
991 if (IS_CHERRYVIEW(dev_priv)) {
992 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
993 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
994 }
995}
996
997static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
998 struct intel_crtc *crtc,
999 const struct intel_plane_state *state,
1000 int level)
1001{
1002 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1003 int clock, htotal, pixel_size, width, wm;
1004
1005 if (dev_priv->wm.pri_latency[level] == 0)
1006 return USHRT_MAX;
1007
1008 if (!state->visible)
1009 return 0;
1010
1011 pixel_size = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1012 clock = crtc->config->base.adjusted_mode.crtc_clock;
1013 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
1014 width = crtc->config->pipe_src_w;
1015 if (WARN_ON(htotal == 0))
1016 htotal = 1;
1017
1018 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1019 /*
1020 * FIXME the formula gives values that are
1021 * too big for the cursor FIFO, and hence we
1022 * would never be able to use cursors. For
1023 * now just hardcode the watermark.
1024 */
1025 wm = 63;
1026 } else {
1027 wm = vlv_wm_method2(clock, htotal, width, pixel_size,
1028 dev_priv->wm.pri_latency[level] * 10);
1029 }
1030
1031 return min_t(int, wm, USHRT_MAX);
1032}
1033
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001034static void vlv_compute_fifo(struct intel_crtc *crtc)
1035{
1036 struct drm_device *dev = crtc->base.dev;
1037 struct vlv_wm_state *wm_state = &crtc->wm_state;
1038 struct intel_plane *plane;
1039 unsigned int total_rate = 0;
1040 const int fifo_size = 512 - 1;
1041 int fifo_extra, fifo_left = fifo_size;
1042
1043 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1044 struct intel_plane_state *state =
1045 to_intel_plane_state(plane->base.state);
1046
1047 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1048 continue;
1049
1050 if (state->visible) {
1051 wm_state->num_active_planes++;
1052 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1053 }
1054 }
1055
1056 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1057 struct intel_plane_state *state =
1058 to_intel_plane_state(plane->base.state);
1059 unsigned int rate;
1060
1061 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1062 plane->wm.fifo_size = 63;
1063 continue;
1064 }
1065
1066 if (!state->visible) {
1067 plane->wm.fifo_size = 0;
1068 continue;
1069 }
1070
1071 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1072 plane->wm.fifo_size = fifo_size * rate / total_rate;
1073 fifo_left -= plane->wm.fifo_size;
1074 }
1075
1076 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1077
1078 /* spread the remainder evenly */
1079 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1080 int plane_extra;
1081
1082 if (fifo_left == 0)
1083 break;
1084
1085 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1086 continue;
1087
1088 /* give it all to the first plane if none are active */
1089 if (plane->wm.fifo_size == 0 &&
1090 wm_state->num_active_planes)
1091 continue;
1092
1093 plane_extra = min(fifo_extra, fifo_left);
1094 plane->wm.fifo_size += plane_extra;
1095 fifo_left -= plane_extra;
1096 }
1097
1098 WARN_ON(fifo_left != 0);
1099}
1100
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001101static void vlv_invert_wms(struct intel_crtc *crtc)
1102{
1103 struct vlv_wm_state *wm_state = &crtc->wm_state;
1104 int level;
1105
1106 for (level = 0; level < wm_state->num_levels; level++) {
1107 struct drm_device *dev = crtc->base.dev;
1108 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1109 struct intel_plane *plane;
1110
1111 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1112 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1113
1114 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1115 switch (plane->base.type) {
1116 int sprite;
1117 case DRM_PLANE_TYPE_CURSOR:
1118 wm_state->wm[level].cursor = plane->wm.fifo_size -
1119 wm_state->wm[level].cursor;
1120 break;
1121 case DRM_PLANE_TYPE_PRIMARY:
1122 wm_state->wm[level].primary = plane->wm.fifo_size -
1123 wm_state->wm[level].primary;
1124 break;
1125 case DRM_PLANE_TYPE_OVERLAY:
1126 sprite = plane->plane;
1127 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1128 wm_state->wm[level].sprite[sprite];
1129 break;
1130 }
1131 }
1132 }
1133}
1134
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001135static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001136{
1137 struct drm_device *dev = crtc->base.dev;
1138 struct vlv_wm_state *wm_state = &crtc->wm_state;
1139 struct intel_plane *plane;
1140 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1141 int level;
1142
1143 memset(wm_state, 0, sizeof(*wm_state));
1144
Ville Syrjälä852eb002015-06-24 22:00:07 +03001145 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001146 if (IS_CHERRYVIEW(dev))
1147 wm_state->num_levels = CHV_WM_NUM_LEVELS;
1148 else
1149 wm_state->num_levels = VLV_WM_NUM_LEVELS;
1150
1151 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001152
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001153 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001154
1155 if (wm_state->num_active_planes != 1)
1156 wm_state->cxsr = false;
1157
1158 if (wm_state->cxsr) {
1159 for (level = 0; level < wm_state->num_levels; level++) {
1160 wm_state->sr[level].plane = sr_fifo_size;
1161 wm_state->sr[level].cursor = 63;
1162 }
1163 }
1164
1165 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1166 struct intel_plane_state *state =
1167 to_intel_plane_state(plane->base.state);
1168
1169 if (!state->visible)
1170 continue;
1171
1172 /* normal watermarks */
1173 for (level = 0; level < wm_state->num_levels; level++) {
1174 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1175 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1176
1177 /* hack */
1178 if (WARN_ON(level == 0 && wm > max_wm))
1179 wm = max_wm;
1180
1181 if (wm > plane->wm.fifo_size)
1182 break;
1183
1184 switch (plane->base.type) {
1185 int sprite;
1186 case DRM_PLANE_TYPE_CURSOR:
1187 wm_state->wm[level].cursor = wm;
1188 break;
1189 case DRM_PLANE_TYPE_PRIMARY:
1190 wm_state->wm[level].primary = wm;
1191 break;
1192 case DRM_PLANE_TYPE_OVERLAY:
1193 sprite = plane->plane;
1194 wm_state->wm[level].sprite[sprite] = wm;
1195 break;
1196 }
1197 }
1198
1199 wm_state->num_levels = level;
1200
1201 if (!wm_state->cxsr)
1202 continue;
1203
1204 /* maxfifo watermarks */
1205 switch (plane->base.type) {
1206 int sprite, level;
1207 case DRM_PLANE_TYPE_CURSOR:
1208 for (level = 0; level < wm_state->num_levels; level++)
1209 wm_state->sr[level].cursor =
1210 wm_state->sr[level].cursor;
1211 break;
1212 case DRM_PLANE_TYPE_PRIMARY:
1213 for (level = 0; level < wm_state->num_levels; level++)
1214 wm_state->sr[level].plane =
1215 min(wm_state->sr[level].plane,
1216 wm_state->wm[level].primary);
1217 break;
1218 case DRM_PLANE_TYPE_OVERLAY:
1219 sprite = plane->plane;
1220 for (level = 0; level < wm_state->num_levels; level++)
1221 wm_state->sr[level].plane =
1222 min(wm_state->sr[level].plane,
1223 wm_state->wm[level].sprite[sprite]);
1224 break;
1225 }
1226 }
1227
1228 /* clear any (partially) filled invalid levels */
1229 for (level = wm_state->num_levels; level < CHV_WM_NUM_LEVELS; level++) {
1230 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1231 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1232 }
1233
1234 vlv_invert_wms(crtc);
1235}
1236
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001237#define VLV_FIFO(plane, value) \
1238 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1239
1240static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1241{
1242 struct drm_device *dev = crtc->base.dev;
1243 struct drm_i915_private *dev_priv = to_i915(dev);
1244 struct intel_plane *plane;
1245 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1246
1247 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1248 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1249 WARN_ON(plane->wm.fifo_size != 63);
1250 continue;
1251 }
1252
1253 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1254 sprite0_start = plane->wm.fifo_size;
1255 else if (plane->plane == 0)
1256 sprite1_start = sprite0_start + plane->wm.fifo_size;
1257 else
1258 fifo_size = sprite1_start + plane->wm.fifo_size;
1259 }
1260
1261 WARN_ON(fifo_size != 512 - 1);
1262
1263 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1264 pipe_name(crtc->pipe), sprite0_start,
1265 sprite1_start, fifo_size);
1266
1267 switch (crtc->pipe) {
1268 uint32_t dsparb, dsparb2, dsparb3;
1269 case PIPE_A:
1270 dsparb = I915_READ(DSPARB);
1271 dsparb2 = I915_READ(DSPARB2);
1272
1273 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1274 VLV_FIFO(SPRITEB, 0xff));
1275 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1276 VLV_FIFO(SPRITEB, sprite1_start));
1277
1278 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1279 VLV_FIFO(SPRITEB_HI, 0x1));
1280 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1281 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1282
1283 I915_WRITE(DSPARB, dsparb);
1284 I915_WRITE(DSPARB2, dsparb2);
1285 break;
1286 case PIPE_B:
1287 dsparb = I915_READ(DSPARB);
1288 dsparb2 = I915_READ(DSPARB2);
1289
1290 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1291 VLV_FIFO(SPRITED, 0xff));
1292 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1293 VLV_FIFO(SPRITED, sprite1_start));
1294
1295 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1296 VLV_FIFO(SPRITED_HI, 0xff));
1297 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1298 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1299
1300 I915_WRITE(DSPARB, dsparb);
1301 I915_WRITE(DSPARB2, dsparb2);
1302 break;
1303 case PIPE_C:
1304 dsparb3 = I915_READ(DSPARB3);
1305 dsparb2 = I915_READ(DSPARB2);
1306
1307 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1308 VLV_FIFO(SPRITEF, 0xff));
1309 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1310 VLV_FIFO(SPRITEF, sprite1_start));
1311
1312 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1313 VLV_FIFO(SPRITEF_HI, 0xff));
1314 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1315 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1316
1317 I915_WRITE(DSPARB3, dsparb3);
1318 I915_WRITE(DSPARB2, dsparb2);
1319 break;
1320 default:
1321 break;
1322 }
1323}
1324
1325#undef VLV_FIFO
1326
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001327static void vlv_merge_wm(struct drm_device *dev,
1328 struct vlv_wm_values *wm)
1329{
1330 struct intel_crtc *crtc;
1331 int num_active_crtcs = 0;
1332
1333 if (IS_CHERRYVIEW(dev))
1334 wm->level = VLV_WM_LEVEL_DDR_DVFS;
1335 else
1336 wm->level = VLV_WM_LEVEL_PM2;
1337 wm->cxsr = true;
1338
1339 for_each_intel_crtc(dev, crtc) {
1340 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1341
1342 if (!crtc->active)
1343 continue;
1344
1345 if (!wm_state->cxsr)
1346 wm->cxsr = false;
1347
1348 num_active_crtcs++;
1349 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1350 }
1351
1352 if (num_active_crtcs != 1)
1353 wm->cxsr = false;
1354
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001355 if (num_active_crtcs > 1)
1356 wm->level = VLV_WM_LEVEL_PM2;
1357
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001358 for_each_intel_crtc(dev, crtc) {
1359 struct vlv_wm_state *wm_state = &crtc->wm_state;
1360 enum pipe pipe = crtc->pipe;
1361
1362 if (!crtc->active)
1363 continue;
1364
1365 wm->pipe[pipe] = wm_state->wm[wm->level];
1366 if (wm->cxsr)
1367 wm->sr = wm_state->sr[wm->level];
1368
1369 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1370 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1371 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1372 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1373 }
1374}
1375
1376static void vlv_update_wm(struct drm_crtc *crtc)
1377{
1378 struct drm_device *dev = crtc->dev;
1379 struct drm_i915_private *dev_priv = dev->dev_private;
1380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1381 enum pipe pipe = intel_crtc->pipe;
1382 struct vlv_wm_values wm = {};
1383
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001384 vlv_compute_wm(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001385 vlv_merge_wm(dev, &wm);
1386
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001387 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1388 /* FIXME should be part of crtc atomic commit */
1389 vlv_pipe_set_fifo_size(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001390 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001391 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001392
1393 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1394 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1395 chv_set_memory_dvfs(dev_priv, false);
1396
1397 if (wm.level < VLV_WM_LEVEL_PM5 &&
1398 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1399 chv_set_memory_pm5(dev_priv, false);
1400
Ville Syrjälä852eb002015-06-24 22:00:07 +03001401 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001402 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001403
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001404 /* FIXME should be part of crtc atomic commit */
1405 vlv_pipe_set_fifo_size(intel_crtc);
1406
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001407 vlv_write_wm_values(intel_crtc, &wm);
1408
1409 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1410 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1411 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1412 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1413 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1414
Ville Syrjälä852eb002015-06-24 22:00:07 +03001415 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001416 intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001417
1418 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1419 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1420 chv_set_memory_pm5(dev_priv, true);
1421
1422 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1423 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1424 chv_set_memory_dvfs(dev_priv, true);
1425
1426 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001427}
1428
Ville Syrjäläae801522015-03-05 21:19:49 +02001429#define single_plane_enabled(mask) is_power_of_2(mask)
1430
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001431static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001432{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001433 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001434 static const int sr_latency_ns = 12000;
1435 struct drm_i915_private *dev_priv = dev->dev_private;
1436 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1437 int plane_sr, cursor_sr;
1438 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001439 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001440
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001441 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001442 &g4x_wm_info, pessimal_latency_ns,
1443 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001444 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001445 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001446
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001447 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001448 &g4x_wm_info, pessimal_latency_ns,
1449 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001450 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001451 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001452
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001453 if (single_plane_enabled(enabled) &&
1454 g4x_compute_srwm(dev, ffs(enabled) - 1,
1455 sr_latency_ns,
1456 &g4x_wm_info,
1457 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001458 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001459 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001460 } else {
Imre Deak98584252014-06-13 14:54:20 +03001461 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001462 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001463 plane_sr = cursor_sr = 0;
1464 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001465
Ville Syrjäläa5043452014-06-28 02:04:18 +03001466 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1467 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001468 planea_wm, cursora_wm,
1469 planeb_wm, cursorb_wm,
1470 plane_sr, cursor_sr);
1471
1472 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001473 FW_WM(plane_sr, SR) |
1474 FW_WM(cursorb_wm, CURSORB) |
1475 FW_WM(planeb_wm, PLANEB) |
1476 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001477 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001478 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001479 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001480 /* HPLL off in SR has some issues on G4x... disable it */
1481 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001482 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001483 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001484
1485 if (cxsr_enabled)
1486 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001487}
1488
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001489static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001490{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001491 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001492 struct drm_i915_private *dev_priv = dev->dev_private;
1493 struct drm_crtc *crtc;
1494 int srwm = 1;
1495 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001496 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001497
1498 /* Calc sr entries for one plane configs */
1499 crtc = single_enabled_crtc(dev);
1500 if (crtc) {
1501 /* self-refresh has much higher latency */
1502 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001503 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001504 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001505 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001506 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001507 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001508 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001509 unsigned long line_time_us;
1510 int entries;
1511
Ville Syrjälä922044c2014-02-14 14:18:57 +02001512 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001513
1514 /* Use ns/us then divide to preserve precision */
1515 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1516 pixel_size * hdisplay;
1517 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1518 srwm = I965_FIFO_SIZE - entries;
1519 if (srwm < 0)
1520 srwm = 1;
1521 srwm &= 0x1ff;
1522 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1523 entries, srwm);
1524
1525 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001526 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001527 entries = DIV_ROUND_UP(entries,
1528 i965_cursor_wm_info.cacheline_size);
1529 cursor_sr = i965_cursor_wm_info.fifo_size -
1530 (entries + i965_cursor_wm_info.guard_size);
1531
1532 if (cursor_sr > i965_cursor_wm_info.max_wm)
1533 cursor_sr = i965_cursor_wm_info.max_wm;
1534
1535 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1536 "cursor %d\n", srwm, cursor_sr);
1537
Imre Deak98584252014-06-13 14:54:20 +03001538 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001539 } else {
Imre Deak98584252014-06-13 14:54:20 +03001540 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001541 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001542 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001543 }
1544
1545 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1546 srwm);
1547
1548 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001549 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1550 FW_WM(8, CURSORB) |
1551 FW_WM(8, PLANEB) |
1552 FW_WM(8, PLANEA));
1553 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1554 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001555 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001556 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001557
1558 if (cxsr_enabled)
1559 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001560}
1561
Ville Syrjäläf4998962015-03-10 17:02:21 +02001562#undef FW_WM
1563
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001564static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001565{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001566 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001567 struct drm_i915_private *dev_priv = dev->dev_private;
1568 const struct intel_watermark_params *wm_info;
1569 uint32_t fwater_lo;
1570 uint32_t fwater_hi;
1571 int cwm, srwm = 1;
1572 int fifo_size;
1573 int planea_wm, planeb_wm;
1574 struct drm_crtc *crtc, *enabled = NULL;
1575
1576 if (IS_I945GM(dev))
1577 wm_info = &i945_wm_info;
1578 else if (!IS_GEN2(dev))
1579 wm_info = &i915_wm_info;
1580 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001581 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001582
1583 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1584 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001585 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001586 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001587 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001588 if (IS_GEN2(dev))
1589 cpp = 4;
1590
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001591 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001592 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001593 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001594 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001595 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001596 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001597 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001598 if (planea_wm > (long)wm_info->max_wm)
1599 planea_wm = wm_info->max_wm;
1600 }
1601
1602 if (IS_GEN2(dev))
1603 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001604
1605 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1606 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001607 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001608 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001609 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001610 if (IS_GEN2(dev))
1611 cpp = 4;
1612
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001613 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001614 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001615 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001616 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001617 if (enabled == NULL)
1618 enabled = crtc;
1619 else
1620 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001621 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001623 if (planeb_wm > (long)wm_info->max_wm)
1624 planeb_wm = wm_info->max_wm;
1625 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001626
1627 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1628
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001629 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001630 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001631
Matt Roper59bea882015-02-27 10:12:01 -08001632 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001633
1634 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001635 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001636 enabled = NULL;
1637 }
1638
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001639 /*
1640 * Overlay gets an aggressive default since video jitter is bad.
1641 */
1642 cwm = 2;
1643
1644 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001645 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001646
1647 /* Calc sr entries for one plane configs */
1648 if (HAS_FW_BLC(dev) && enabled) {
1649 /* self-refresh has much higher latency */
1650 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001651 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001652 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001653 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001654 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001655 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001656 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001657 unsigned long line_time_us;
1658 int entries;
1659
Ville Syrjälä922044c2014-02-14 14:18:57 +02001660 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001661
1662 /* Use ns/us then divide to preserve precision */
1663 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1664 pixel_size * hdisplay;
1665 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1666 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1667 srwm = wm_info->fifo_size - entries;
1668 if (srwm < 0)
1669 srwm = 1;
1670
1671 if (IS_I945G(dev) || IS_I945GM(dev))
1672 I915_WRITE(FW_BLC_SELF,
1673 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1674 else if (IS_I915GM(dev))
1675 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1676 }
1677
1678 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1679 planea_wm, planeb_wm, cwm, srwm);
1680
1681 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1682 fwater_hi = (cwm & 0x1f);
1683
1684 /* Set request length to 8 cachelines per fetch */
1685 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1686 fwater_hi = fwater_hi | (1 << 8);
1687
1688 I915_WRITE(FW_BLC, fwater_lo);
1689 I915_WRITE(FW_BLC2, fwater_hi);
1690
Imre Deak5209b1f2014-07-01 12:36:17 +03001691 if (enabled)
1692 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001693}
1694
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001695static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001696{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001697 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001698 struct drm_i915_private *dev_priv = dev->dev_private;
1699 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001700 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001701 uint32_t fwater_lo;
1702 int planea_wm;
1703
1704 crtc = single_enabled_crtc(dev);
1705 if (crtc == NULL)
1706 return;
1707
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001708 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001709 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001710 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001711 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001712 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001713 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1714 fwater_lo |= (3<<8) | planea_wm;
1715
1716 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1717
1718 I915_WRITE(FW_BLC, fwater_lo);
1719}
1720
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001721uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001722{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001723 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001724
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001725 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001726
1727 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1728 * adjust the pixel_rate here. */
1729
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001730 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001731 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001732 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001733
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001734 pipe_w = pipe_config->pipe_src_w;
1735 pipe_h = pipe_config->pipe_src_h;
1736
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001737 pfit_w = (pfit_size >> 16) & 0xFFFF;
1738 pfit_h = pfit_size & 0xFFFF;
1739 if (pipe_w < pfit_w)
1740 pipe_w = pfit_w;
1741 if (pipe_h < pfit_h)
1742 pipe_h = pfit_h;
1743
1744 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1745 pfit_w * pfit_h);
1746 }
1747
1748 return pixel_rate;
1749}
1750
Ville Syrjälä37126462013-08-01 16:18:55 +03001751/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001752static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001753 uint32_t latency)
1754{
1755 uint64_t ret;
1756
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001757 if (WARN(latency == 0, "Latency value missing\n"))
1758 return UINT_MAX;
1759
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001760 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1761 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1762
1763 return ret;
1764}
1765
Ville Syrjälä37126462013-08-01 16:18:55 +03001766/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001767static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001768 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1769 uint32_t latency)
1770{
1771 uint32_t ret;
1772
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001773 if (WARN(latency == 0, "Latency value missing\n"))
1774 return UINT_MAX;
1775
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001776 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1777 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1778 ret = DIV_ROUND_UP(ret, 64) + 2;
1779 return ret;
1780}
1781
Ville Syrjälä23297042013-07-05 11:57:17 +03001782static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001783 uint8_t bytes_per_pixel)
1784{
1785 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1786}
1787
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001788struct skl_pipe_wm_parameters {
1789 bool active;
1790 uint32_t pipe_htotal;
1791 uint32_t pixel_rate; /* in KHz */
1792 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1793 struct intel_plane_wm_parameters cursor;
1794};
1795
Imre Deak820c1982013-12-17 14:46:36 +02001796struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001797 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001798 uint32_t pipe_htotal;
1799 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001800 struct intel_plane_wm_parameters pri;
1801 struct intel_plane_wm_parameters spr;
1802 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001803};
1804
Imre Deak820c1982013-12-17 14:46:36 +02001805struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001806 uint16_t pri;
1807 uint16_t spr;
1808 uint16_t cur;
1809 uint16_t fbc;
1810};
1811
Ville Syrjälä240264f2013-08-07 13:29:12 +03001812/* used in computing the new watermarks state */
1813struct intel_wm_config {
1814 unsigned int num_pipes_active;
1815 bool sprites_enabled;
1816 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001817};
1818
Ville Syrjälä37126462013-08-01 16:18:55 +03001819/*
1820 * For both WM_PIPE and WM_LP.
1821 * mem_value must be in 0.1us units.
1822 */
Imre Deak820c1982013-12-17 14:46:36 +02001823static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001824 uint32_t mem_value,
1825 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001826{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001827 uint32_t method1, method2;
1828
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001829 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001830 return 0;
1831
Ville Syrjälä23297042013-07-05 11:57:17 +03001832 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001833 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001834 mem_value);
1835
1836 if (!is_lp)
1837 return method1;
1838
Ville Syrjälä23297042013-07-05 11:57:17 +03001839 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001840 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001841 params->pri.horiz_pixels,
1842 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001843 mem_value);
1844
1845 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001846}
1847
Ville Syrjälä37126462013-08-01 16:18:55 +03001848/*
1849 * For both WM_PIPE and WM_LP.
1850 * mem_value must be in 0.1us units.
1851 */
Imre Deak820c1982013-12-17 14:46:36 +02001852static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001853 uint32_t mem_value)
1854{
1855 uint32_t method1, method2;
1856
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001857 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001858 return 0;
1859
Ville Syrjälä23297042013-07-05 11:57:17 +03001860 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001861 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001862 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001863 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001864 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001865 params->spr.horiz_pixels,
1866 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001867 mem_value);
1868 return min(method1, method2);
1869}
1870
Ville Syrjälä37126462013-08-01 16:18:55 +03001871/*
1872 * For both WM_PIPE and WM_LP.
1873 * mem_value must be in 0.1us units.
1874 */
Imre Deak820c1982013-12-17 14:46:36 +02001875static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001876 uint32_t mem_value)
1877{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001878 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001879 return 0;
1880
Ville Syrjälä23297042013-07-05 11:57:17 +03001881 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001882 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001883 params->cur.horiz_pixels,
1884 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001885 mem_value);
1886}
1887
Paulo Zanonicca32e92013-05-31 11:45:06 -03001888/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001889static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001890 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001891{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001892 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001893 return 0;
1894
Ville Syrjälä23297042013-07-05 11:57:17 +03001895 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001896 params->pri.horiz_pixels,
1897 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001898}
1899
Ville Syrjälä158ae642013-08-07 13:28:19 +03001900static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1901{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001902 if (INTEL_INFO(dev)->gen >= 8)
1903 return 3072;
1904 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001905 return 768;
1906 else
1907 return 512;
1908}
1909
Ville Syrjälä4e975082014-03-07 18:32:11 +02001910static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1911 int level, bool is_sprite)
1912{
1913 if (INTEL_INFO(dev)->gen >= 8)
1914 /* BDW primary/sprite plane watermarks */
1915 return level == 0 ? 255 : 2047;
1916 else if (INTEL_INFO(dev)->gen >= 7)
1917 /* IVB/HSW primary/sprite plane watermarks */
1918 return level == 0 ? 127 : 1023;
1919 else if (!is_sprite)
1920 /* ILK/SNB primary plane watermarks */
1921 return level == 0 ? 127 : 511;
1922 else
1923 /* ILK/SNB sprite plane watermarks */
1924 return level == 0 ? 63 : 255;
1925}
1926
1927static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1928 int level)
1929{
1930 if (INTEL_INFO(dev)->gen >= 7)
1931 return level == 0 ? 63 : 255;
1932 else
1933 return level == 0 ? 31 : 63;
1934}
1935
1936static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1937{
1938 if (INTEL_INFO(dev)->gen >= 8)
1939 return 31;
1940 else
1941 return 15;
1942}
1943
Ville Syrjälä158ae642013-08-07 13:28:19 +03001944/* Calculate the maximum primary/sprite plane watermark */
1945static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1946 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001947 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001948 enum intel_ddb_partitioning ddb_partitioning,
1949 bool is_sprite)
1950{
1951 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001952
1953 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001954 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001955 return 0;
1956
1957 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001958 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001959 fifo_size /= INTEL_INFO(dev)->num_pipes;
1960
1961 /*
1962 * For some reason the non self refresh
1963 * FIFO size is only half of the self
1964 * refresh FIFO size on ILK/SNB.
1965 */
1966 if (INTEL_INFO(dev)->gen <= 6)
1967 fifo_size /= 2;
1968 }
1969
Ville Syrjälä240264f2013-08-07 13:29:12 +03001970 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001971 /* level 0 is always calculated with 1:1 split */
1972 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1973 if (is_sprite)
1974 fifo_size *= 5;
1975 fifo_size /= 6;
1976 } else {
1977 fifo_size /= 2;
1978 }
1979 }
1980
1981 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001982 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001983}
1984
1985/* Calculate the maximum cursor plane watermark */
1986static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001987 int level,
1988 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001989{
1990 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001991 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001992 return 64;
1993
1994 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001995 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001996}
1997
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001998static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001999 int level,
2000 const struct intel_wm_config *config,
2001 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002002 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002003{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002004 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2005 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2006 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02002007 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002008}
2009
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002010static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
2011 int level,
2012 struct ilk_wm_maximums *max)
2013{
2014 max->pri = ilk_plane_wm_reg_max(dev, level, false);
2015 max->spr = ilk_plane_wm_reg_max(dev, level, true);
2016 max->cur = ilk_cursor_wm_reg_max(dev, level);
2017 max->fbc = ilk_fbc_wm_reg_max(dev);
2018}
2019
Ville Syrjäläd9395652013-10-09 19:18:10 +03002020static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002021 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002022 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002023{
2024 bool ret;
2025
2026 /* already determined to be invalid? */
2027 if (!result->enable)
2028 return false;
2029
2030 result->enable = result->pri_val <= max->pri &&
2031 result->spr_val <= max->spr &&
2032 result->cur_val <= max->cur;
2033
2034 ret = result->enable;
2035
2036 /*
2037 * HACK until we can pre-compute everything,
2038 * and thus fail gracefully if LP0 watermarks
2039 * are exceeded...
2040 */
2041 if (level == 0 && !result->enable) {
2042 if (result->pri_val > max->pri)
2043 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2044 level, result->pri_val, max->pri);
2045 if (result->spr_val > max->spr)
2046 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2047 level, result->spr_val, max->spr);
2048 if (result->cur_val > max->cur)
2049 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2050 level, result->cur_val, max->cur);
2051
2052 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2053 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2054 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2055 result->enable = true;
2056 }
2057
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002058 return ret;
2059}
2060
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002061static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002062 int level,
Imre Deak820c1982013-12-17 14:46:36 +02002063 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002064 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002065{
2066 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2067 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2068 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2069
2070 /* WM1+ latency values stored in 0.5us units */
2071 if (level > 0) {
2072 pri_latency *= 5;
2073 spr_latency *= 5;
2074 cur_latency *= 5;
2075 }
2076
2077 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
2078 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
2079 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
2080 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
2081 result->enable = true;
2082}
2083
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002084static uint32_t
2085hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002086{
2087 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002089 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002090 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002091
Matt Roper3ef00282015-03-09 10:19:24 -07002092 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002093 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002094
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002095 /* The WM are computed with base on how long it takes to fill a single
2096 * row at the given clock rate, multiplied by 8.
2097 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08002098 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
2099 mode->crtc_clock);
2100 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Ville Syrjälä05024da2015-06-03 15:45:08 +03002101 dev_priv->cdclk_freq);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002102
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002103 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2104 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002105}
2106
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002107static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002108{
2109 struct drm_i915_private *dev_priv = dev->dev_private;
2110
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002111 if (IS_GEN9(dev)) {
2112 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002113 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00002114 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002115
2116 /* read the first set of memory latencies[0:3] */
2117 val = 0; /* data0 to be programmed to 0 for first set */
2118 mutex_lock(&dev_priv->rps.hw_lock);
2119 ret = sandybridge_pcode_read(dev_priv,
2120 GEN9_PCODE_READ_MEM_LATENCY,
2121 &val);
2122 mutex_unlock(&dev_priv->rps.hw_lock);
2123
2124 if (ret) {
2125 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2126 return;
2127 }
2128
2129 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2130 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2131 GEN9_MEM_LATENCY_LEVEL_MASK;
2132 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2133 GEN9_MEM_LATENCY_LEVEL_MASK;
2134 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2135 GEN9_MEM_LATENCY_LEVEL_MASK;
2136
2137 /* read the second set of memory latencies[4:7] */
2138 val = 1; /* data0 to be programmed to 1 for second set */
2139 mutex_lock(&dev_priv->rps.hw_lock);
2140 ret = sandybridge_pcode_read(dev_priv,
2141 GEN9_PCODE_READ_MEM_LATENCY,
2142 &val);
2143 mutex_unlock(&dev_priv->rps.hw_lock);
2144 if (ret) {
2145 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2146 return;
2147 }
2148
2149 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2150 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2151 GEN9_MEM_LATENCY_LEVEL_MASK;
2152 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2153 GEN9_MEM_LATENCY_LEVEL_MASK;
2154 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2155 GEN9_MEM_LATENCY_LEVEL_MASK;
2156
Vandana Kannan367294b2014-11-04 17:06:46 +00002157 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002158 * WaWmMemoryReadLatency:skl
2159 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002160 * punit doesn't take into account the read latency so we need
2161 * to add 2us to the various latency levels we retrieve from
2162 * the punit.
2163 * - W0 is a bit special in that it's the only level that
2164 * can't be disabled if we want to have display working, so
2165 * we always add 2us there.
2166 * - For levels >=1, punit returns 0us latency when they are
2167 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00002168 *
2169 * Additionally, if a level n (n > 1) has a 0us latency, all
2170 * levels m (m >= n) need to be disabled. We make sure to
2171 * sanitize the values out of the punit to satisfy this
2172 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00002173 */
2174 wm[0] += 2;
2175 for (level = 1; level <= max_level; level++)
2176 if (wm[level] != 0)
2177 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002178 else {
2179 for (i = level + 1; i <= max_level; i++)
2180 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00002181
Vandana Kannan4f947382014-11-04 17:06:47 +00002182 break;
2183 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002184 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002185 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2186
2187 wm[0] = (sskpd >> 56) & 0xFF;
2188 if (wm[0] == 0)
2189 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002190 wm[1] = (sskpd >> 4) & 0xFF;
2191 wm[2] = (sskpd >> 12) & 0xFF;
2192 wm[3] = (sskpd >> 20) & 0x1FF;
2193 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002194 } else if (INTEL_INFO(dev)->gen >= 6) {
2195 uint32_t sskpd = I915_READ(MCH_SSKPD);
2196
2197 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2198 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2199 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2200 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002201 } else if (INTEL_INFO(dev)->gen >= 5) {
2202 uint32_t mltr = I915_READ(MLTR_ILK);
2203
2204 /* ILK primary LP0 latency is 700 ns */
2205 wm[0] = 7;
2206 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2207 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002208 }
2209}
2210
Ville Syrjälä53615a52013-08-01 16:18:50 +03002211static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2212{
2213 /* ILK sprite LP0 latency is 1300 ns */
2214 if (INTEL_INFO(dev)->gen == 5)
2215 wm[0] = 13;
2216}
2217
2218static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2219{
2220 /* ILK cursor LP0 latency is 1300 ns */
2221 if (INTEL_INFO(dev)->gen == 5)
2222 wm[0] = 13;
2223
2224 /* WaDoubleCursorLP3Latency:ivb */
2225 if (IS_IVYBRIDGE(dev))
2226 wm[3] *= 2;
2227}
2228
Damien Lespiau546c81f2014-05-13 15:30:26 +01002229int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002230{
2231 /* how many WM levels are we expecting */
Damien Lespiaub6e742f2015-05-09 02:05:55 +01002232 if (INTEL_INFO(dev)->gen >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002233 return 7;
2234 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002235 return 4;
2236 else if (INTEL_INFO(dev)->gen >= 6)
2237 return 3;
2238 else
2239 return 2;
2240}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002241
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002242static void intel_print_wm_latency(struct drm_device *dev,
2243 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002244 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002245{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002246 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002247
2248 for (level = 0; level <= max_level; level++) {
2249 unsigned int latency = wm[level];
2250
2251 if (latency == 0) {
2252 DRM_ERROR("%s WM%d latency not provided\n",
2253 name, level);
2254 continue;
2255 }
2256
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002257 /*
2258 * - latencies are in us on gen9.
2259 * - before then, WM1+ latency values are in 0.5us units
2260 */
2261 if (IS_GEN9(dev))
2262 latency *= 10;
2263 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002264 latency *= 5;
2265
2266 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2267 name, level, wm[level],
2268 latency / 10, latency % 10);
2269 }
2270}
2271
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002272static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2273 uint16_t wm[5], uint16_t min)
2274{
2275 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2276
2277 if (wm[0] >= min)
2278 return false;
2279
2280 wm[0] = max(wm[0], min);
2281 for (level = 1; level <= max_level; level++)
2282 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2283
2284 return true;
2285}
2286
2287static void snb_wm_latency_quirk(struct drm_device *dev)
2288{
2289 struct drm_i915_private *dev_priv = dev->dev_private;
2290 bool changed;
2291
2292 /*
2293 * The BIOS provided WM memory latency values are often
2294 * inadequate for high resolution displays. Adjust them.
2295 */
2296 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2297 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2298 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2299
2300 if (!changed)
2301 return;
2302
2303 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2304 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2305 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2306 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2307}
2308
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002309static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002310{
2311 struct drm_i915_private *dev_priv = dev->dev_private;
2312
2313 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2314
2315 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2316 sizeof(dev_priv->wm.pri_latency));
2317 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2318 sizeof(dev_priv->wm.pri_latency));
2319
2320 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2321 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002322
2323 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2324 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2325 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002326
2327 if (IS_GEN6(dev))
2328 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002329}
2330
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002331static void skl_setup_wm_latency(struct drm_device *dev)
2332{
2333 struct drm_i915_private *dev_priv = dev->dev_private;
2334
2335 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2336 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2337}
2338
Imre Deak820c1982013-12-17 14:46:36 +02002339static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002340 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002341{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002342 struct drm_device *dev = crtc->dev;
2343 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2344 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002345 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002346
Matt Roper3ef00282015-03-09 10:19:24 -07002347 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002348 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002349
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002350 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002351 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03002352 p->pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
Matt Roperc9f038a2015-03-09 11:06:02 -07002353
Thomas Gummerer54da6912015-05-14 09:16:39 +02002354 if (crtc->primary->state->fb)
Matt Roperc9f038a2015-03-09 11:06:02 -07002355 p->pri.bytes_per_pixel =
2356 crtc->primary->state->fb->bits_per_pixel / 8;
Thomas Gummerer54da6912015-05-14 09:16:39 +02002357 else
2358 p->pri.bytes_per_pixel = 4;
Matt Roperc9f038a2015-03-09 11:06:02 -07002359
Thomas Gummerer54da6912015-05-14 09:16:39 +02002360 p->cur.bytes_per_pixel = 4;
2361 /*
2362 * TODO: for now, assume primary and cursor planes are always enabled.
2363 * Setting them to false makes the screen flicker.
2364 */
2365 p->pri.enabled = true;
2366 p->cur.enabled = true;
2367
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002368 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08002369 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002370
Daniel Vetter4ea50e92015-07-09 23:44:24 +02002371 drm_for_each_legacy_plane(plane, dev) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002372 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002373
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002374 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002375 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002376 break;
2377 }
2378 }
2379}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002380
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002381static void ilk_compute_wm_config(struct drm_device *dev,
2382 struct intel_wm_config *config)
2383{
2384 struct intel_crtc *intel_crtc;
2385
2386 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002387 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002388 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2389
2390 if (!wm->pipe_enabled)
2391 continue;
2392
2393 config->sprites_enabled |= wm->sprites_enabled;
2394 config->sprites_scaled |= wm->sprites_scaled;
2395 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002396 }
2397}
2398
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002399/* Compute new watermarks for the pipe */
2400static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002401 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002402 struct intel_pipe_wm *pipe_wm)
2403{
2404 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002405 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002406 int level, max_level = ilk_wm_max_level(dev);
2407 /* LP0 watermark maximums depend on this pipe alone */
2408 struct intel_wm_config config = {
2409 .num_pipes_active = 1,
2410 .sprites_enabled = params->spr.enabled,
2411 .sprites_scaled = params->spr.scaled,
2412 };
Imre Deak820c1982013-12-17 14:46:36 +02002413 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002414
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002415 pipe_wm->pipe_enabled = params->active;
2416 pipe_wm->sprites_enabled = params->spr.enabled;
2417 pipe_wm->sprites_scaled = params->spr.scaled;
2418
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002419 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2420 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2421 max_level = 1;
2422
2423 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2424 if (params->spr.scaled)
2425 max_level = 0;
2426
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002427 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002428
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002429 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002430 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002431
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002432 /* LP0 watermarks always use 1/2 DDB partitioning */
2433 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2434
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002435 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002436 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2437 return false;
2438
2439 ilk_compute_wm_reg_maximums(dev, 1, &max);
2440
2441 for (level = 1; level <= max_level; level++) {
2442 struct intel_wm_level wm = {};
2443
2444 ilk_compute_wm_level(dev_priv, level, params, &wm);
2445
2446 /*
2447 * Disable any watermark level that exceeds the
2448 * register maximums since such watermarks are
2449 * always invalid.
2450 */
2451 if (!ilk_validate_wm_level(level, &max, &wm))
2452 break;
2453
2454 pipe_wm->wm[level] = wm;
2455 }
2456
2457 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002458}
2459
2460/*
2461 * Merge the watermarks from all active pipes for a specific level.
2462 */
2463static void ilk_merge_wm_level(struct drm_device *dev,
2464 int level,
2465 struct intel_wm_level *ret_wm)
2466{
2467 const struct intel_crtc *intel_crtc;
2468
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002469 ret_wm->enable = true;
2470
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002471 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002472 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2473 const struct intel_wm_level *wm = &active->wm[level];
2474
2475 if (!active->pipe_enabled)
2476 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002477
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002478 /*
2479 * The watermark values may have been used in the past,
2480 * so we must maintain them in the registers for some
2481 * time even if the level is now disabled.
2482 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002483 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002484 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002485
2486 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2487 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2488 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2489 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2490 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002491}
2492
2493/*
2494 * Merge all low power watermarks for all active pipes.
2495 */
2496static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002497 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002498 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002499 struct intel_pipe_wm *merged)
2500{
Paulo Zanoni7733b492015-07-07 15:26:04 -03002501 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002502 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002503 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002504
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002505 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2506 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2507 config->num_pipes_active > 1)
2508 return;
2509
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002510 /* ILK: FBC WM must be disabled always */
2511 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002512
2513 /* merge each WM1+ level */
2514 for (level = 1; level <= max_level; level++) {
2515 struct intel_wm_level *wm = &merged->wm[level];
2516
2517 ilk_merge_wm_level(dev, level, wm);
2518
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002519 if (level > last_enabled_level)
2520 wm->enable = false;
2521 else if (!ilk_validate_wm_level(level, max, wm))
2522 /* make sure all following levels get disabled */
2523 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002524
2525 /*
2526 * The spec says it is preferred to disable
2527 * FBC WMs instead of disabling a WM level.
2528 */
2529 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002530 if (wm->enable)
2531 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002532 wm->fbc_val = 0;
2533 }
2534 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002535
2536 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2537 /*
2538 * FIXME this is racy. FBC might get enabled later.
2539 * What we should check here is whether FBC can be
2540 * enabled sometime later.
2541 */
Paulo Zanoni7733b492015-07-07 15:26:04 -03002542 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
2543 intel_fbc_enabled(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002544 for (level = 2; level <= max_level; level++) {
2545 struct intel_wm_level *wm = &merged->wm[level];
2546
2547 wm->enable = false;
2548 }
2549 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002550}
2551
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002552static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2553{
2554 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2555 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2556}
2557
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002558/* The value we need to program into the WM_LPx latency field */
2559static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2560{
2561 struct drm_i915_private *dev_priv = dev->dev_private;
2562
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002563 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002564 return 2 * level;
2565 else
2566 return dev_priv->wm.pri_latency[level];
2567}
2568
Imre Deak820c1982013-12-17 14:46:36 +02002569static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002570 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002571 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002572 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002573{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002574 struct intel_crtc *intel_crtc;
2575 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002576
Ville Syrjälä0362c782013-10-09 19:17:57 +03002577 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002578 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002579
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002580 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002581 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002582 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002583
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002584 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002585
Ville Syrjälä0362c782013-10-09 19:17:57 +03002586 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002587
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002588 /*
2589 * Maintain the watermark values even if the level is
2590 * disabled. Doing otherwise could cause underruns.
2591 */
2592 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002593 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002594 (r->pri_val << WM1_LP_SR_SHIFT) |
2595 r->cur_val;
2596
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002597 if (r->enable)
2598 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2599
Ville Syrjälä416f4722013-11-02 21:07:46 -07002600 if (INTEL_INFO(dev)->gen >= 8)
2601 results->wm_lp[wm_lp - 1] |=
2602 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2603 else
2604 results->wm_lp[wm_lp - 1] |=
2605 r->fbc_val << WM1_LP_FBC_SHIFT;
2606
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002607 /*
2608 * Always set WM1S_LP_EN when spr_val != 0, even if the
2609 * level is disabled. Doing otherwise could cause underruns.
2610 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002611 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2612 WARN_ON(wm_lp != 1);
2613 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2614 } else
2615 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002616 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002617
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002618 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002619 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002620 enum pipe pipe = intel_crtc->pipe;
2621 const struct intel_wm_level *r =
2622 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002623
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002624 if (WARN_ON(!r->enable))
2625 continue;
2626
2627 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2628
2629 results->wm_pipe[pipe] =
2630 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2631 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2632 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002633 }
2634}
2635
Paulo Zanoni861f3382013-05-31 10:19:21 -03002636/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2637 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002638static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002639 struct intel_pipe_wm *r1,
2640 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002641{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002642 int level, max_level = ilk_wm_max_level(dev);
2643 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002644
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002645 for (level = 1; level <= max_level; level++) {
2646 if (r1->wm[level].enable)
2647 level1 = level;
2648 if (r2->wm[level].enable)
2649 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002650 }
2651
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002652 if (level1 == level2) {
2653 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002654 return r2;
2655 else
2656 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002657 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002658 return r1;
2659 } else {
2660 return r2;
2661 }
2662}
2663
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002664/* dirty bits used to track which watermarks need changes */
2665#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2666#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2667#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2668#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2669#define WM_DIRTY_FBC (1 << 24)
2670#define WM_DIRTY_DDB (1 << 25)
2671
Damien Lespiau055e3932014-08-18 13:49:10 +01002672static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002673 const struct ilk_wm_values *old,
2674 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002675{
2676 unsigned int dirty = 0;
2677 enum pipe pipe;
2678 int wm_lp;
2679
Damien Lespiau055e3932014-08-18 13:49:10 +01002680 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002681 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2682 dirty |= WM_DIRTY_LINETIME(pipe);
2683 /* Must disable LP1+ watermarks too */
2684 dirty |= WM_DIRTY_LP_ALL;
2685 }
2686
2687 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2688 dirty |= WM_DIRTY_PIPE(pipe);
2689 /* Must disable LP1+ watermarks too */
2690 dirty |= WM_DIRTY_LP_ALL;
2691 }
2692 }
2693
2694 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2695 dirty |= WM_DIRTY_FBC;
2696 /* Must disable LP1+ watermarks too */
2697 dirty |= WM_DIRTY_LP_ALL;
2698 }
2699
2700 if (old->partitioning != new->partitioning) {
2701 dirty |= WM_DIRTY_DDB;
2702 /* Must disable LP1+ watermarks too */
2703 dirty |= WM_DIRTY_LP_ALL;
2704 }
2705
2706 /* LP1+ watermarks already deemed dirty, no need to continue */
2707 if (dirty & WM_DIRTY_LP_ALL)
2708 return dirty;
2709
2710 /* Find the lowest numbered LP1+ watermark in need of an update... */
2711 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2712 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2713 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2714 break;
2715 }
2716
2717 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2718 for (; wm_lp <= 3; wm_lp++)
2719 dirty |= WM_DIRTY_LP(wm_lp);
2720
2721 return dirty;
2722}
2723
Ville Syrjälä8553c182013-12-05 15:51:39 +02002724static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2725 unsigned int dirty)
2726{
Imre Deak820c1982013-12-17 14:46:36 +02002727 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002728 bool changed = false;
2729
2730 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2731 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2732 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2733 changed = true;
2734 }
2735 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2736 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2737 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2738 changed = true;
2739 }
2740 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2741 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2742 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2743 changed = true;
2744 }
2745
2746 /*
2747 * Don't touch WM1S_LP_EN here.
2748 * Doing so could cause underruns.
2749 */
2750
2751 return changed;
2752}
2753
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002754/*
2755 * The spec says we shouldn't write when we don't need, because every write
2756 * causes WMs to be re-evaluated, expending some power.
2757 */
Imre Deak820c1982013-12-17 14:46:36 +02002758static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2759 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002760{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002761 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002762 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002763 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002764 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002765
Damien Lespiau055e3932014-08-18 13:49:10 +01002766 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002767 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002768 return;
2769
Ville Syrjälä8553c182013-12-05 15:51:39 +02002770 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002771
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002772 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002773 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002774 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002775 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002776 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002777 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2778
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002779 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002780 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002781 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002782 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002783 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002784 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2785
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002786 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002787 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002788 val = I915_READ(WM_MISC);
2789 if (results->partitioning == INTEL_DDB_PART_1_2)
2790 val &= ~WM_MISC_DATA_PARTITION_5_6;
2791 else
2792 val |= WM_MISC_DATA_PARTITION_5_6;
2793 I915_WRITE(WM_MISC, val);
2794 } else {
2795 val = I915_READ(DISP_ARB_CTL2);
2796 if (results->partitioning == INTEL_DDB_PART_1_2)
2797 val &= ~DISP_DATA_PARTITION_5_6;
2798 else
2799 val |= DISP_DATA_PARTITION_5_6;
2800 I915_WRITE(DISP_ARB_CTL2, val);
2801 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002802 }
2803
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002804 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002805 val = I915_READ(DISP_ARB_CTL);
2806 if (results->enable_fbc_wm)
2807 val &= ~DISP_FBC_WM_DIS;
2808 else
2809 val |= DISP_FBC_WM_DIS;
2810 I915_WRITE(DISP_ARB_CTL, val);
2811 }
2812
Imre Deak954911e2013-12-17 14:46:34 +02002813 if (dirty & WM_DIRTY_LP(1) &&
2814 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2815 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2816
2817 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002818 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2819 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2820 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2821 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2822 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002823
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002824 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002825 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002826 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002827 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002828 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002829 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002830
2831 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002832}
2833
Ville Syrjälä8553c182013-12-05 15:51:39 +02002834static bool ilk_disable_lp_wm(struct drm_device *dev)
2835{
2836 struct drm_i915_private *dev_priv = dev->dev_private;
2837
2838 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2839}
2840
Damien Lespiaub9cec072014-11-04 17:06:43 +00002841/*
2842 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2843 * different active planes.
2844 */
2845
2846#define SKL_DDB_SIZE 896 /* in blocks */
Damien Lespiau43d735a2015-03-17 11:39:34 +02002847#define BXT_DDB_SIZE 512
Damien Lespiaub9cec072014-11-04 17:06:43 +00002848
2849static void
2850skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2851 struct drm_crtc *for_crtc,
2852 const struct intel_wm_config *config,
2853 const struct skl_pipe_wm_parameters *params,
2854 struct skl_ddb_entry *alloc /* out */)
2855{
2856 struct drm_crtc *crtc;
2857 unsigned int pipe_size, ddb_size;
2858 int nth_active_pipe;
2859
2860 if (!params->active) {
2861 alloc->start = 0;
2862 alloc->end = 0;
2863 return;
2864 }
2865
Damien Lespiau43d735a2015-03-17 11:39:34 +02002866 if (IS_BROXTON(dev))
2867 ddb_size = BXT_DDB_SIZE;
2868 else
2869 ddb_size = SKL_DDB_SIZE;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002870
2871 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2872
2873 nth_active_pipe = 0;
2874 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002875 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002876 continue;
2877
2878 if (crtc == for_crtc)
2879 break;
2880
2881 nth_active_pipe++;
2882 }
2883
2884 pipe_size = ddb_size / config->num_pipes_active;
2885 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002886 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002887}
2888
2889static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2890{
2891 if (config->num_pipes_active == 1)
2892 return 32;
2893
2894 return 8;
2895}
2896
Damien Lespiaua269c582014-11-04 17:06:49 +00002897static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2898{
2899 entry->start = reg & 0x3ff;
2900 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002901 if (entry->end)
2902 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002903}
2904
Damien Lespiau08db6652014-11-04 17:06:52 +00002905void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2906 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002907{
Damien Lespiaua269c582014-11-04 17:06:49 +00002908 enum pipe pipe;
2909 int plane;
2910 u32 val;
2911
2912 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002913 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002914 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2915 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2916 val);
2917 }
2918
2919 val = I915_READ(CUR_BUF_CFG(pipe));
2920 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2921 }
2922}
2923
Damien Lespiaub9cec072014-11-04 17:06:43 +00002924static unsigned int
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002925skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p, int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002926{
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002927
2928 /* for planar format */
2929 if (p->y_bytes_per_pixel) {
2930 if (y) /* y-plane data rate */
2931 return p->horiz_pixels * p->vert_pixels * p->y_bytes_per_pixel;
2932 else /* uv-plane data rate */
2933 return (p->horiz_pixels/2) * (p->vert_pixels/2) * p->bytes_per_pixel;
2934 }
2935
2936 /* for packed formats */
Damien Lespiaub9cec072014-11-04 17:06:43 +00002937 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2938}
2939
2940/*
2941 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2942 * a 8192x4096@32bpp framebuffer:
2943 * 3 * 4096 * 8192 * 4 < 2^32
2944 */
2945static unsigned int
2946skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2947 const struct skl_pipe_wm_parameters *params)
2948{
2949 unsigned int total_data_rate = 0;
2950 int plane;
2951
2952 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2953 const struct intel_plane_wm_parameters *p;
2954
2955 p = &params->plane[plane];
2956 if (!p->enabled)
2957 continue;
2958
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002959 total_data_rate += skl_plane_relative_data_rate(p, 0); /* packed/uv */
2960 if (p->y_bytes_per_pixel) {
2961 total_data_rate += skl_plane_relative_data_rate(p, 1); /* y-plane */
2962 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00002963 }
2964
2965 return total_data_rate;
2966}
2967
2968static void
2969skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2970 const struct intel_wm_config *config,
2971 const struct skl_pipe_wm_parameters *params,
2972 struct skl_ddb_allocation *ddb /* out */)
2973{
2974 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002975 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2977 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002978 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002979 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002980 uint16_t minimum[I915_MAX_PLANES];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002981 uint16_t y_minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002982 unsigned int total_data_rate;
2983 int plane;
2984
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002985 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2986 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002987 if (alloc_size == 0) {
2988 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2989 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2990 return;
2991 }
2992
2993 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002994 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2995 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002996
2997 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002998 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002999
Damien Lespiau80958152015-02-09 13:35:10 +00003000 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00003001 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00003002 const struct intel_plane_wm_parameters *p;
3003
3004 p = &params->plane[plane];
3005 if (!p->enabled)
3006 continue;
3007
3008 minimum[plane] = 8;
3009 alloc_size -= minimum[plane];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003010 y_minimum[plane] = p->y_bytes_per_pixel ? 8 : 0;
3011 alloc_size -= y_minimum[plane];
Damien Lespiau80958152015-02-09 13:35:10 +00003012 }
3013
Damien Lespiaub9cec072014-11-04 17:06:43 +00003014 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003015 * 2. Distribute the remaining space in proportion to the amount of
3016 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003017 *
3018 * FIXME: we may not allocate every single block here.
3019 */
3020 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
3021
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003022 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003023 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
3024 const struct intel_plane_wm_parameters *p;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003025 unsigned int data_rate, y_data_rate;
3026 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003027
3028 p = &params->plane[plane];
3029 if (!p->enabled)
3030 continue;
3031
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003032 data_rate = skl_plane_relative_data_rate(p, 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003033
3034 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003035 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003036 * promote the expression to 64 bits to avoid overflowing, the
3037 * result is < available as data_rate / total_data_rate < 1
3038 */
Damien Lespiau80958152015-02-09 13:35:10 +00003039 plane_blocks = minimum[plane];
3040 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3041 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003042
3043 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00003044 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003045
3046 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003047
3048 /*
3049 * allocation for y_plane part of planar format:
3050 */
3051 if (p->y_bytes_per_pixel) {
3052 y_data_rate = skl_plane_relative_data_rate(p, 1);
3053 y_plane_blocks = y_minimum[plane];
3054 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3055 total_data_rate);
3056
3057 ddb->y_plane[pipe][plane].start = start;
3058 ddb->y_plane[pipe][plane].end = start + y_plane_blocks;
3059
3060 start += y_plane_blocks;
3061 }
3062
Damien Lespiaub9cec072014-11-04 17:06:43 +00003063 }
3064
3065}
3066
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02003067static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003068{
3069 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02003070 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003071}
3072
3073/*
3074 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3075 * for the read latency) and bytes_per_pixel should always be <= 8, so that
3076 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3077 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3078*/
3079static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3080 uint32_t latency)
3081{
3082 uint32_t wm_intermediate_val, ret;
3083
3084 if (latency == 0)
3085 return UINT_MAX;
3086
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003087 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003088 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3089
3090 return ret;
3091}
3092
3093static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3094 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003095 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003096{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003097 uint32_t ret;
3098 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3099 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003100
3101 if (latency == 0)
3102 return UINT_MAX;
3103
3104 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003105
3106 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3107 tiling == I915_FORMAT_MOD_Yf_TILED) {
3108 plane_bytes_per_line *= 4;
3109 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3110 plane_blocks_per_line /= 4;
3111 } else {
3112 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3113 }
3114
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003115 wm_intermediate_val = latency * pixel_rate;
3116 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003117 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003118
3119 return ret;
3120}
3121
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003122static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3123 const struct intel_crtc *intel_crtc)
3124{
3125 struct drm_device *dev = intel_crtc->base.dev;
3126 struct drm_i915_private *dev_priv = dev->dev_private;
3127 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3128 enum pipe pipe = intel_crtc->pipe;
3129
3130 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
3131 sizeof(new_ddb->plane[pipe])))
3132 return true;
3133
3134 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
3135 sizeof(new_ddb->cursor[pipe])))
3136 return true;
3137
3138 return false;
3139}
3140
3141static void skl_compute_wm_global_parameters(struct drm_device *dev,
3142 struct intel_wm_config *config)
3143{
3144 struct drm_crtc *crtc;
3145 struct drm_plane *plane;
3146
3147 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07003148 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003149
3150 /* FIXME: I don't think we need those two global parameters on SKL */
3151 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3152 struct intel_plane *intel_plane = to_intel_plane(plane);
3153
3154 config->sprites_enabled |= intel_plane->wm.enabled;
3155 config->sprites_scaled |= intel_plane->wm.scaled;
3156 }
3157}
3158
3159static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
3160 struct skl_pipe_wm_parameters *p)
3161{
3162 struct drm_device *dev = crtc->dev;
3163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3164 enum pipe pipe = intel_crtc->pipe;
3165 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003166 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003167 int i = 1; /* Index for sprite planes start */
3168
Matt Roper3ef00282015-03-09 10:19:24 -07003169 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003170 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003171 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
3172 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003173
Matt Roperc9f038a2015-03-09 11:06:02 -07003174 fb = crtc->primary->state->fb;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003175 /* For planar: Bpp is for uv plane, y_Bpp is for y plane */
Matt Roperc9f038a2015-03-09 11:06:02 -07003176 if (fb) {
3177 p->plane[0].enabled = true;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003178 p->plane[0].bytes_per_pixel = fb->pixel_format == DRM_FORMAT_NV12 ?
Kumar, Mahesh395ab752015-09-03 16:17:08 +05303179 drm_format_plane_cpp(fb->pixel_format, 1) :
3180 drm_format_plane_cpp(fb->pixel_format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003181 p->plane[0].y_bytes_per_pixel = fb->pixel_format == DRM_FORMAT_NV12 ?
3182 drm_format_plane_cpp(fb->pixel_format, 0) : 0;
Matt Roperc9f038a2015-03-09 11:06:02 -07003183 p->plane[0].tiling = fb->modifier[0];
3184 } else {
3185 p->plane[0].enabled = false;
3186 p->plane[0].bytes_per_pixel = 0;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003187 p->plane[0].y_bytes_per_pixel = 0;
Matt Roperc9f038a2015-03-09 11:06:02 -07003188 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
3189 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003190 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
3191 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003192 p->plane[0].rotation = crtc->primary->state->rotation;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003193
Matt Roperc9f038a2015-03-09 11:06:02 -07003194 fb = crtc->cursor->state->fb;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003195 p->cursor.y_bytes_per_pixel = 0;
Matt Roperc9f038a2015-03-09 11:06:02 -07003196 if (fb) {
3197 p->cursor.enabled = true;
3198 p->cursor.bytes_per_pixel = fb->bits_per_pixel / 8;
3199 p->cursor.horiz_pixels = crtc->cursor->state->crtc_w;
3200 p->cursor.vert_pixels = crtc->cursor->state->crtc_h;
3201 } else {
3202 p->cursor.enabled = false;
3203 p->cursor.bytes_per_pixel = 0;
3204 p->cursor.horiz_pixels = 64;
3205 p->cursor.vert_pixels = 64;
3206 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003207 }
3208
3209 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
3210 struct intel_plane *intel_plane = to_intel_plane(plane);
3211
Sonika Jindala712f8e2014-12-09 10:59:15 +05303212 if (intel_plane->pipe == pipe &&
3213 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003214 p->plane[i++] = intel_plane->wm;
3215 }
3216}
3217
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003218static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3219 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003220 struct intel_plane_wm_parameters *p_params,
3221 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003222 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003223 uint16_t *out_blocks, /* out */
3224 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003225{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003226 uint32_t latency = dev_priv->wm.skl_latency[level];
3227 uint32_t method1, method2;
3228 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3229 uint32_t res_blocks, res_lines;
3230 uint32_t selected_result;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003231 uint8_t bytes_per_pixel;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003232
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003233 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003234 return false;
3235
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003236 bytes_per_pixel = p_params->y_bytes_per_pixel ?
3237 p_params->y_bytes_per_pixel :
3238 p_params->bytes_per_pixel;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003239 method1 = skl_wm_method1(p->pixel_rate,
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003240 bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003241 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003242 method2 = skl_wm_method2(p->pixel_rate,
3243 p->pipe_htotal,
3244 p_params->horiz_pixels,
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003245 bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003246 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003247 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003248
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003249 plane_bytes_per_line = p_params->horiz_pixels * bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003250 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003251
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003252 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
3253 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003254 uint32_t min_scanlines = 4;
3255 uint32_t y_tile_minimum;
3256 if (intel_rotation_90_or_270(p_params->rotation)) {
3257 switch (p_params->bytes_per_pixel) {
3258 case 1:
3259 min_scanlines = 16;
3260 break;
3261 case 2:
3262 min_scanlines = 8;
3263 break;
3264 case 8:
3265 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08003266 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003267 }
3268 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003269 selected_result = max(method2, y_tile_minimum);
3270 } else {
3271 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3272 selected_result = min(method1, method2);
3273 else
3274 selected_result = method1;
3275 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003276
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003277 res_blocks = selected_result + 1;
3278 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003279
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003280 if (level >= 1 && level <= 7) {
3281 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
3282 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
3283 res_lines += 4;
3284 else
3285 res_blocks++;
3286 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003287
3288 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00003289 return false;
3290
3291 *out_blocks = res_blocks;
3292 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003293
3294 return true;
3295}
3296
3297static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3298 struct skl_ddb_allocation *ddb,
3299 struct skl_pipe_wm_parameters *p,
3300 enum pipe pipe,
3301 int level,
3302 int num_planes,
3303 struct skl_wm_level *result)
3304{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003305 uint16_t ddb_blocks;
3306 int i;
3307
3308 for (i = 0; i < num_planes; i++) {
3309 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3310
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003311 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
3312 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003313 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003314 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003315 &result->plane_res_b[i],
3316 &result->plane_res_l[i]);
3317 }
3318
3319 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003320 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
3321 ddb_blocks, level,
3322 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003323 &result->cursor_res_l);
3324}
3325
Damien Lespiau407b50f2014-11-04 17:06:57 +00003326static uint32_t
3327skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
3328{
Matt Roper3ef00282015-03-09 10:19:24 -07003329 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003330 return 0;
3331
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003332 if (WARN_ON(p->pixel_rate == 0))
3333 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003334
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003335 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
Damien Lespiau407b50f2014-11-04 17:06:57 +00003336}
3337
3338static void skl_compute_transition_wm(struct drm_crtc *crtc,
3339 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00003340 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003341{
Damien Lespiau9414f562014-11-04 17:06:58 +00003342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3343 int i;
3344
Damien Lespiau407b50f2014-11-04 17:06:57 +00003345 if (!params->active)
3346 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003347
3348 /* Until we know more, just disable transition WMs */
3349 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3350 trans_wm->plane_en[i] = false;
3351 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003352}
3353
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003354static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3355 struct skl_ddb_allocation *ddb,
3356 struct skl_pipe_wm_parameters *params,
3357 struct skl_pipe_wm *pipe_wm)
3358{
3359 struct drm_device *dev = crtc->dev;
3360 const struct drm_i915_private *dev_priv = dev->dev_private;
3361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3362 int level, max_level = ilk_wm_max_level(dev);
3363
3364 for (level = 0; level <= max_level; level++) {
3365 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3366 level, intel_num_planes(intel_crtc),
3367 &pipe_wm->wm[level]);
3368 }
3369 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3370
Damien Lespiau9414f562014-11-04 17:06:58 +00003371 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003372}
3373
3374static void skl_compute_wm_results(struct drm_device *dev,
3375 struct skl_pipe_wm_parameters *p,
3376 struct skl_pipe_wm *p_wm,
3377 struct skl_wm_values *r,
3378 struct intel_crtc *intel_crtc)
3379{
3380 int level, max_level = ilk_wm_max_level(dev);
3381 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003382 uint32_t temp;
3383 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003384
3385 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003386 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3387 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003388
3389 temp |= p_wm->wm[level].plane_res_l[i] <<
3390 PLANE_WM_LINES_SHIFT;
3391 temp |= p_wm->wm[level].plane_res_b[i];
3392 if (p_wm->wm[level].plane_en[i])
3393 temp |= PLANE_WM_EN;
3394
3395 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003396 }
3397
3398 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003399
3400 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3401 temp |= p_wm->wm[level].cursor_res_b;
3402
3403 if (p_wm->wm[level].cursor_en)
3404 temp |= PLANE_WM_EN;
3405
3406 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003407
3408 }
3409
Damien Lespiau9414f562014-11-04 17:06:58 +00003410 /* transition WMs */
3411 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3412 temp = 0;
3413 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3414 temp |= p_wm->trans_wm.plane_res_b[i];
3415 if (p_wm->trans_wm.plane_en[i])
3416 temp |= PLANE_WM_EN;
3417
3418 r->plane_trans[pipe][i] = temp;
3419 }
3420
3421 temp = 0;
3422 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3423 temp |= p_wm->trans_wm.cursor_res_b;
3424 if (p_wm->trans_wm.cursor_en)
3425 temp |= PLANE_WM_EN;
3426
3427 r->cursor_trans[pipe] = temp;
3428
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003429 r->wm_linetime[pipe] = p_wm->linetime;
3430}
3431
Damien Lespiau16160e32014-11-04 17:06:53 +00003432static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3433 const struct skl_ddb_entry *entry)
3434{
3435 if (entry->end)
3436 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3437 else
3438 I915_WRITE(reg, 0);
3439}
3440
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003441static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3442 const struct skl_wm_values *new)
3443{
3444 struct drm_device *dev = dev_priv->dev;
3445 struct intel_crtc *crtc;
3446
3447 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3448 int i, level, max_level = ilk_wm_max_level(dev);
3449 enum pipe pipe = crtc->pipe;
3450
Damien Lespiau5d374d92014-11-04 17:07:00 +00003451 if (!new->dirty[pipe])
3452 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003453
Damien Lespiau5d374d92014-11-04 17:07:00 +00003454 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3455
3456 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003457 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003458 I915_WRITE(PLANE_WM(pipe, i, level),
3459 new->plane[pipe][i][level]);
3460 I915_WRITE(CUR_WM(pipe, level),
3461 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003462 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003463 for (i = 0; i < intel_num_planes(crtc); i++)
3464 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3465 new->plane_trans[pipe][i]);
3466 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3467
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003468 for (i = 0; i < intel_num_planes(crtc); i++) {
Damien Lespiau5d374d92014-11-04 17:07:00 +00003469 skl_ddb_entry_write(dev_priv,
3470 PLANE_BUF_CFG(pipe, i),
3471 &new->ddb.plane[pipe][i]);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003472 skl_ddb_entry_write(dev_priv,
3473 PLANE_NV12_BUF_CFG(pipe, i),
3474 &new->ddb.y_plane[pipe][i]);
3475 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003476
3477 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3478 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003479 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003480}
3481
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003482/*
3483 * When setting up a new DDB allocation arrangement, we need to correctly
3484 * sequence the times at which the new allocations for the pipes are taken into
3485 * account or we'll have pipes fetching from space previously allocated to
3486 * another pipe.
3487 *
3488 * Roughly the sequence looks like:
3489 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3490 * overlapping with a previous light-up pipe (another way to put it is:
3491 * pipes with their new allocation strickly included into their old ones).
3492 * 2. re-allocate the other pipes that get their allocation reduced
3493 * 3. allocate the pipes having their allocation increased
3494 *
3495 * Steps 1. and 2. are here to take care of the following case:
3496 * - Initially DDB looks like this:
3497 * | B | C |
3498 * - enable pipe A.
3499 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3500 * allocation
3501 * | A | B | C |
3502 *
3503 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3504 */
3505
Damien Lespiaud21b7952014-11-04 17:07:03 +00003506static void
3507skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003508{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003509 int plane;
3510
Damien Lespiaud21b7952014-11-04 17:07:03 +00003511 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3512
Damien Lespiaudd740782015-02-28 14:54:08 +00003513 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003514 I915_WRITE(PLANE_SURF(pipe, plane),
3515 I915_READ(PLANE_SURF(pipe, plane)));
3516 }
3517 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3518}
3519
3520static bool
3521skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3522 const struct skl_ddb_allocation *new,
3523 enum pipe pipe)
3524{
3525 uint16_t old_size, new_size;
3526
3527 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3528 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3529
3530 return old_size != new_size &&
3531 new->pipe[pipe].start >= old->pipe[pipe].start &&
3532 new->pipe[pipe].end <= old->pipe[pipe].end;
3533}
3534
3535static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3536 struct skl_wm_values *new_values)
3537{
3538 struct drm_device *dev = dev_priv->dev;
3539 struct skl_ddb_allocation *cur_ddb, *new_ddb;
Ville Syrjäläc929cb42015-04-02 18:28:07 +03003540 bool reallocated[I915_MAX_PIPES] = {};
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003541 struct intel_crtc *crtc;
3542 enum pipe pipe;
3543
3544 new_ddb = &new_values->ddb;
3545 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3546
3547 /*
3548 * First pass: flush the pipes with the new allocation contained into
3549 * the old space.
3550 *
3551 * We'll wait for the vblank on those pipes to ensure we can safely
3552 * re-allocate the freed space without this pipe fetching from it.
3553 */
3554 for_each_intel_crtc(dev, crtc) {
3555 if (!crtc->active)
3556 continue;
3557
3558 pipe = crtc->pipe;
3559
3560 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3561 continue;
3562
Damien Lespiaud21b7952014-11-04 17:07:03 +00003563 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003564 intel_wait_for_vblank(dev, pipe);
3565
3566 reallocated[pipe] = true;
3567 }
3568
3569
3570 /*
3571 * Second pass: flush the pipes that are having their allocation
3572 * reduced, but overlapping with a previous allocation.
3573 *
3574 * Here as well we need to wait for the vblank to make sure the freed
3575 * space is not used anymore.
3576 */
3577 for_each_intel_crtc(dev, crtc) {
3578 if (!crtc->active)
3579 continue;
3580
3581 pipe = crtc->pipe;
3582
3583 if (reallocated[pipe])
3584 continue;
3585
3586 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3587 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003588 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003589 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303590 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003591 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003592 }
3593
3594 /*
3595 * Third pass: flush the pipes that got more space allocated.
3596 *
3597 * We don't need to actively wait for the update here, next vblank
3598 * will just get more DDB space with the correct WM values.
3599 */
3600 for_each_intel_crtc(dev, crtc) {
3601 if (!crtc->active)
3602 continue;
3603
3604 pipe = crtc->pipe;
3605
3606 /*
3607 * At this point, only the pipes more space than before are
3608 * left to re-allocate.
3609 */
3610 if (reallocated[pipe])
3611 continue;
3612
Damien Lespiaud21b7952014-11-04 17:07:03 +00003613 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003614 }
3615}
3616
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003617static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3618 struct skl_pipe_wm_parameters *params,
3619 struct intel_wm_config *config,
3620 struct skl_ddb_allocation *ddb, /* out */
3621 struct skl_pipe_wm *pipe_wm /* out */)
3622{
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3624
3625 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003626 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003627 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3628
3629 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3630 return false;
3631
3632 intel_crtc->wm.skl_active = *pipe_wm;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003633
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003634 return true;
3635}
3636
3637static void skl_update_other_pipe_wm(struct drm_device *dev,
3638 struct drm_crtc *crtc,
3639 struct intel_wm_config *config,
3640 struct skl_wm_values *r)
3641{
3642 struct intel_crtc *intel_crtc;
3643 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3644
3645 /*
3646 * If the WM update hasn't changed the allocation for this_crtc (the
3647 * crtc we are currently computing the new WM values for), other
3648 * enabled crtcs will keep the same allocation and we don't need to
3649 * recompute anything for them.
3650 */
3651 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3652 return;
3653
3654 /*
3655 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3656 * other active pipes need new DDB allocation and WM values.
3657 */
3658 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3659 base.head) {
3660 struct skl_pipe_wm_parameters params = {};
3661 struct skl_pipe_wm pipe_wm = {};
3662 bool wm_changed;
3663
3664 if (this_crtc->pipe == intel_crtc->pipe)
3665 continue;
3666
3667 if (!intel_crtc->active)
3668 continue;
3669
3670 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3671 &params, config,
3672 &r->ddb, &pipe_wm);
3673
3674 /*
3675 * If we end up re-computing the other pipe WM values, it's
3676 * because it was really needed, so we expect the WM values to
3677 * be different.
3678 */
3679 WARN_ON(!wm_changed);
3680
3681 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3682 r->dirty[intel_crtc->pipe] = true;
3683 }
3684}
3685
3686static void skl_update_wm(struct drm_crtc *crtc)
3687{
3688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3689 struct drm_device *dev = crtc->dev;
3690 struct drm_i915_private *dev_priv = dev->dev_private;
3691 struct skl_pipe_wm_parameters params = {};
3692 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3693 struct skl_pipe_wm pipe_wm = {};
3694 struct intel_wm_config config = {};
3695
3696 memset(results, 0, sizeof(*results));
3697
3698 skl_compute_wm_global_parameters(dev, &config);
3699
3700 if (!skl_update_pipe_wm(crtc, &params, &config,
3701 &results->ddb, &pipe_wm))
3702 return;
3703
3704 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3705 results->dirty[intel_crtc->pipe] = true;
3706
3707 skl_update_other_pipe_wm(dev, crtc, &config, results);
3708 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003709 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003710
3711 /* store the new configuration */
3712 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003713}
3714
3715static void
3716skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3717 uint32_t sprite_width, uint32_t sprite_height,
3718 int pixel_size, bool enabled, bool scaled)
3719{
3720 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003721 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003722
3723 intel_plane->wm.enabled = enabled;
3724 intel_plane->wm.scaled = scaled;
3725 intel_plane->wm.horiz_pixels = sprite_width;
3726 intel_plane->wm.vert_pixels = sprite_height;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003727 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003728
3729 /* For planar: Bpp is for UV plane, y_Bpp is for Y plane */
3730 intel_plane->wm.bytes_per_pixel =
3731 (fb && fb->pixel_format == DRM_FORMAT_NV12) ?
3732 drm_format_plane_cpp(plane->state->fb->pixel_format, 1) : pixel_size;
3733 intel_plane->wm.y_bytes_per_pixel =
3734 (fb && fb->pixel_format == DRM_FORMAT_NV12) ?
3735 drm_format_plane_cpp(plane->state->fb->pixel_format, 0) : 0;
3736
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003737 /*
3738 * Framebuffer can be NULL on plane disable, but it does not
3739 * matter for watermarks if we assume no tiling in that case.
3740 */
3741 if (fb)
3742 intel_plane->wm.tiling = fb->modifier[0];
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003743 intel_plane->wm.rotation = plane->state->rotation;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003744
3745 skl_update_wm(crtc);
3746}
3747
Imre Deak820c1982013-12-17 14:46:36 +02003748static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003749{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003751 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003752 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003753 struct ilk_wm_maximums max;
3754 struct ilk_pipe_wm_parameters params = {};
3755 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003756 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003757 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003758 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003759 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003760
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003761 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003762
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003763 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3764
3765 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3766 return;
3767
3768 intel_crtc->wm.active = pipe_wm;
3769
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003770 ilk_compute_wm_config(dev, &config);
3771
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003772 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003773 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003774
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003775 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003776 if (INTEL_INFO(dev)->gen >= 7 &&
3777 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003778 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003779 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003780
Imre Deak820c1982013-12-17 14:46:36 +02003781 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003782 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003783 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003784 }
3785
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003786 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003787 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003788
Imre Deak820c1982013-12-17 14:46:36 +02003789 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003790
Imre Deak820c1982013-12-17 14:46:36 +02003791 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003792}
3793
Damien Lespiaued57cb82014-07-15 09:21:24 +02003794static void
3795ilk_update_sprite_wm(struct drm_plane *plane,
3796 struct drm_crtc *crtc,
3797 uint32_t sprite_width, uint32_t sprite_height,
3798 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003799{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003800 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003801 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003802
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003803 intel_plane->wm.enabled = enabled;
3804 intel_plane->wm.scaled = scaled;
3805 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003806 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003807 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003808
Ville Syrjälä8553c182013-12-05 15:51:39 +02003809 /*
3810 * IVB workaround: must disable low power watermarks for at least
3811 * one frame before enabling scaling. LP watermarks can be re-enabled
3812 * when scaling is disabled.
3813 *
3814 * WaCxSRDisabledForSpriteScaling:ivb
3815 */
3816 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3817 intel_wait_for_vblank(dev, intel_plane->pipe);
3818
Imre Deak820c1982013-12-17 14:46:36 +02003819 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003820}
3821
Pradeep Bhat30789992014-11-04 17:06:45 +00003822static void skl_pipe_wm_active_state(uint32_t val,
3823 struct skl_pipe_wm *active,
3824 bool is_transwm,
3825 bool is_cursor,
3826 int i,
3827 int level)
3828{
3829 bool is_enabled = (val & PLANE_WM_EN) != 0;
3830
3831 if (!is_transwm) {
3832 if (!is_cursor) {
3833 active->wm[level].plane_en[i] = is_enabled;
3834 active->wm[level].plane_res_b[i] =
3835 val & PLANE_WM_BLOCKS_MASK;
3836 active->wm[level].plane_res_l[i] =
3837 (val >> PLANE_WM_LINES_SHIFT) &
3838 PLANE_WM_LINES_MASK;
3839 } else {
3840 active->wm[level].cursor_en = is_enabled;
3841 active->wm[level].cursor_res_b =
3842 val & PLANE_WM_BLOCKS_MASK;
3843 active->wm[level].cursor_res_l =
3844 (val >> PLANE_WM_LINES_SHIFT) &
3845 PLANE_WM_LINES_MASK;
3846 }
3847 } else {
3848 if (!is_cursor) {
3849 active->trans_wm.plane_en[i] = is_enabled;
3850 active->trans_wm.plane_res_b[i] =
3851 val & PLANE_WM_BLOCKS_MASK;
3852 active->trans_wm.plane_res_l[i] =
3853 (val >> PLANE_WM_LINES_SHIFT) &
3854 PLANE_WM_LINES_MASK;
3855 } else {
3856 active->trans_wm.cursor_en = is_enabled;
3857 active->trans_wm.cursor_res_b =
3858 val & PLANE_WM_BLOCKS_MASK;
3859 active->trans_wm.cursor_res_l =
3860 (val >> PLANE_WM_LINES_SHIFT) &
3861 PLANE_WM_LINES_MASK;
3862 }
3863 }
3864}
3865
3866static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3867{
3868 struct drm_device *dev = crtc->dev;
3869 struct drm_i915_private *dev_priv = dev->dev_private;
3870 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3872 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3873 enum pipe pipe = intel_crtc->pipe;
3874 int level, i, max_level;
3875 uint32_t temp;
3876
3877 max_level = ilk_wm_max_level(dev);
3878
3879 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3880
3881 for (level = 0; level <= max_level; level++) {
3882 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3883 hw->plane[pipe][i][level] =
3884 I915_READ(PLANE_WM(pipe, i, level));
3885 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3886 }
3887
3888 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3889 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3890 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3891
Matt Roper3ef00282015-03-09 10:19:24 -07003892 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003893 return;
3894
3895 hw->dirty[pipe] = true;
3896
3897 active->linetime = hw->wm_linetime[pipe];
3898
3899 for (level = 0; level <= max_level; level++) {
3900 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3901 temp = hw->plane[pipe][i][level];
3902 skl_pipe_wm_active_state(temp, active, false,
3903 false, i, level);
3904 }
3905 temp = hw->cursor[pipe][level];
3906 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3907 }
3908
3909 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3910 temp = hw->plane_trans[pipe][i];
3911 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3912 }
3913
3914 temp = hw->cursor_trans[pipe];
3915 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3916}
3917
3918void skl_wm_get_hw_state(struct drm_device *dev)
3919{
Damien Lespiaua269c582014-11-04 17:06:49 +00003920 struct drm_i915_private *dev_priv = dev->dev_private;
3921 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003922 struct drm_crtc *crtc;
3923
Damien Lespiaua269c582014-11-04 17:06:49 +00003924 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003925 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3926 skl_pipe_wm_get_hw_state(crtc);
3927}
3928
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003929static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3930{
3931 struct drm_device *dev = crtc->dev;
3932 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003933 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3935 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3936 enum pipe pipe = intel_crtc->pipe;
3937 static const unsigned int wm0_pipe_reg[] = {
3938 [PIPE_A] = WM0_PIPEA_ILK,
3939 [PIPE_B] = WM0_PIPEB_ILK,
3940 [PIPE_C] = WM0_PIPEC_IVB,
3941 };
3942
3943 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003944 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003945 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003946
Matt Roper3ef00282015-03-09 10:19:24 -07003947 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003948
3949 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003950 u32 tmp = hw->wm_pipe[pipe];
3951
3952 /*
3953 * For active pipes LP0 watermark is marked as
3954 * enabled, and LP1+ watermaks as disabled since
3955 * we can't really reverse compute them in case
3956 * multiple pipes are active.
3957 */
3958 active->wm[0].enable = true;
3959 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3960 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3961 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3962 active->linetime = hw->wm_linetime[pipe];
3963 } else {
3964 int level, max_level = ilk_wm_max_level(dev);
3965
3966 /*
3967 * For inactive pipes, all watermark levels
3968 * should be marked as enabled but zeroed,
3969 * which is what we'd compute them to.
3970 */
3971 for (level = 0; level <= max_level; level++)
3972 active->wm[level].enable = true;
3973 }
3974}
3975
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03003976#define _FW_WM(value, plane) \
3977 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3978#define _FW_WM_VLV(value, plane) \
3979 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3980
3981static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3982 struct vlv_wm_values *wm)
3983{
3984 enum pipe pipe;
3985 uint32_t tmp;
3986
3987 for_each_pipe(dev_priv, pipe) {
3988 tmp = I915_READ(VLV_DDL(pipe));
3989
3990 wm->ddl[pipe].primary =
3991 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3992 wm->ddl[pipe].cursor =
3993 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3994 wm->ddl[pipe].sprite[0] =
3995 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3996 wm->ddl[pipe].sprite[1] =
3997 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3998 }
3999
4000 tmp = I915_READ(DSPFW1);
4001 wm->sr.plane = _FW_WM(tmp, SR);
4002 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
4003 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
4004 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
4005
4006 tmp = I915_READ(DSPFW2);
4007 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
4008 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
4009 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
4010
4011 tmp = I915_READ(DSPFW3);
4012 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4013
4014 if (IS_CHERRYVIEW(dev_priv)) {
4015 tmp = I915_READ(DSPFW7_CHV);
4016 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4017 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4018
4019 tmp = I915_READ(DSPFW8_CHV);
4020 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
4021 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
4022
4023 tmp = I915_READ(DSPFW9_CHV);
4024 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
4025 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
4026
4027 tmp = I915_READ(DSPHOWM);
4028 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4029 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4030 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4031 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4032 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4033 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4034 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4035 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4036 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4037 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4038 } else {
4039 tmp = I915_READ(DSPFW7);
4040 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4041 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4042
4043 tmp = I915_READ(DSPHOWM);
4044 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4045 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4046 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4047 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4048 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4049 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4050 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4051 }
4052}
4053
4054#undef _FW_WM
4055#undef _FW_WM_VLV
4056
4057void vlv_wm_get_hw_state(struct drm_device *dev)
4058{
4059 struct drm_i915_private *dev_priv = to_i915(dev);
4060 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4061 struct intel_plane *plane;
4062 enum pipe pipe;
4063 u32 val;
4064
4065 vlv_read_wm_values(dev_priv, wm);
4066
4067 for_each_intel_plane(dev, plane) {
4068 switch (plane->base.type) {
4069 int sprite;
4070 case DRM_PLANE_TYPE_CURSOR:
4071 plane->wm.fifo_size = 63;
4072 break;
4073 case DRM_PLANE_TYPE_PRIMARY:
4074 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4075 break;
4076 case DRM_PLANE_TYPE_OVERLAY:
4077 sprite = plane->plane;
4078 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4079 break;
4080 }
4081 }
4082
4083 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4084 wm->level = VLV_WM_LEVEL_PM2;
4085
4086 if (IS_CHERRYVIEW(dev_priv)) {
4087 mutex_lock(&dev_priv->rps.hw_lock);
4088
4089 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4090 if (val & DSP_MAXFIFO_PM5_ENABLE)
4091 wm->level = VLV_WM_LEVEL_PM5;
4092
4093 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4094 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4095 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4096
4097 mutex_unlock(&dev_priv->rps.hw_lock);
4098 }
4099
4100 for_each_pipe(dev_priv, pipe)
4101 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4102 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4103 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4104
4105 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4106 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4107}
4108
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004109void ilk_wm_get_hw_state(struct drm_device *dev)
4110{
4111 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004112 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004113 struct drm_crtc *crtc;
4114
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004115 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004116 ilk_pipe_wm_get_hw_state(crtc);
4117
4118 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4119 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4120 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4121
4122 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004123 if (INTEL_INFO(dev)->gen >= 7) {
4124 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4125 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4126 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004127
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004128 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004129 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4130 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4131 else if (IS_IVYBRIDGE(dev))
4132 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4133 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004134
4135 hw->enable_fbc_wm =
4136 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4137}
4138
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004139/**
4140 * intel_update_watermarks - update FIFO watermark values based on current modes
4141 *
4142 * Calculate watermark values for the various WM regs based on current mode
4143 * and plane configuration.
4144 *
4145 * There are several cases to deal with here:
4146 * - normal (i.e. non-self-refresh)
4147 * - self-refresh (SR) mode
4148 * - lines are large relative to FIFO size (buffer can hold up to 2)
4149 * - lines are small relative to FIFO size (buffer can hold more than 2
4150 * lines), so need to account for TLB latency
4151 *
4152 * The normal calculation is:
4153 * watermark = dotclock * bytes per pixel * latency
4154 * where latency is platform & configuration dependent (we assume pessimal
4155 * values here).
4156 *
4157 * The SR calculation is:
4158 * watermark = (trunc(latency/line time)+1) * surface width *
4159 * bytes per pixel
4160 * where
4161 * line time = htotal / dotclock
4162 * surface width = hdisplay for normal plane and 64 for cursor
4163 * and latency is assumed to be high, as above.
4164 *
4165 * The final value programmed to the register should always be rounded up,
4166 * and include an extra 2 entries to account for clock crossings.
4167 *
4168 * We don't use the sprite, so we can ignore that. And on Crestline we have
4169 * to set the non-SR watermarks to 8.
4170 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004171void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004172{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004173 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004174
4175 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004176 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004177}
4178
Ville Syrjäläadf3d352013-08-06 22:24:11 +03004179void intel_update_sprite_watermarks(struct drm_plane *plane,
4180 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02004181 uint32_t sprite_width,
4182 uint32_t sprite_height,
4183 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03004184 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004185{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03004186 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004187
4188 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02004189 dev_priv->display.update_sprite_wm(plane, crtc,
4190 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03004191 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004192}
4193
Daniel Vetter92703882012-08-09 16:46:01 +02004194/**
4195 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004196 */
4197DEFINE_SPINLOCK(mchdev_lock);
4198
4199/* Global for IPS driver to get at the current i915 device. Protected by
4200 * mchdev_lock. */
4201static struct drm_i915_private *i915_mch_dev;
4202
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004203bool ironlake_set_drps(struct drm_device *dev, u8 val)
4204{
4205 struct drm_i915_private *dev_priv = dev->dev_private;
4206 u16 rgvswctl;
4207
Daniel Vetter92703882012-08-09 16:46:01 +02004208 assert_spin_locked(&mchdev_lock);
4209
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004210 rgvswctl = I915_READ16(MEMSWCTL);
4211 if (rgvswctl & MEMCTL_CMD_STS) {
4212 DRM_DEBUG("gpu busy, RCS change rejected\n");
4213 return false; /* still busy with another command */
4214 }
4215
4216 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4217 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4218 I915_WRITE16(MEMSWCTL, rgvswctl);
4219 POSTING_READ16(MEMSWCTL);
4220
4221 rgvswctl |= MEMCTL_CMD_STS;
4222 I915_WRITE16(MEMSWCTL, rgvswctl);
4223
4224 return true;
4225}
4226
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004227static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004228{
4229 struct drm_i915_private *dev_priv = dev->dev_private;
4230 u32 rgvmodectl = I915_READ(MEMMODECTL);
4231 u8 fmax, fmin, fstart, vstart;
4232
Daniel Vetter92703882012-08-09 16:46:01 +02004233 spin_lock_irq(&mchdev_lock);
4234
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004235 /* Enable temp reporting */
4236 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4237 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4238
4239 /* 100ms RC evaluation intervals */
4240 I915_WRITE(RCUPEI, 100000);
4241 I915_WRITE(RCDNEI, 100000);
4242
4243 /* Set max/min thresholds to 90ms and 80ms respectively */
4244 I915_WRITE(RCBMAXAVG, 90000);
4245 I915_WRITE(RCBMINAVG, 80000);
4246
4247 I915_WRITE(MEMIHYST, 1);
4248
4249 /* Set up min, max, and cur for interrupt handling */
4250 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4251 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4252 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4253 MEMMODE_FSTART_SHIFT;
4254
4255 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
4256 PXVFREQ_PX_SHIFT;
4257
Daniel Vetter20e4d402012-08-08 23:35:39 +02004258 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4259 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004260
Daniel Vetter20e4d402012-08-08 23:35:39 +02004261 dev_priv->ips.max_delay = fstart;
4262 dev_priv->ips.min_delay = fmin;
4263 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004264
4265 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4266 fmax, fmin, fstart);
4267
4268 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4269
4270 /*
4271 * Interrupts will be enabled in ironlake_irq_postinstall
4272 */
4273
4274 I915_WRITE(VIDSTART, vstart);
4275 POSTING_READ(VIDSTART);
4276
4277 rgvmodectl |= MEMMODE_SWMODE_EN;
4278 I915_WRITE(MEMMODECTL, rgvmodectl);
4279
Daniel Vetter92703882012-08-09 16:46:01 +02004280 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004281 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004282 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004283
4284 ironlake_set_drps(dev, fstart);
4285
Daniel Vetter20e4d402012-08-08 23:35:39 +02004286 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004287 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004288 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4289 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004290 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004291
4292 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004293}
4294
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004295static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004296{
4297 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02004298 u16 rgvswctl;
4299
4300 spin_lock_irq(&mchdev_lock);
4301
4302 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004303
4304 /* Ack interrupts, disable EFC interrupt */
4305 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4306 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4307 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4308 I915_WRITE(DEIIR, DE_PCU_EVENT);
4309 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4310
4311 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004312 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004313 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004314 rgvswctl |= MEMCTL_CMD_STS;
4315 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004316 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004317
Daniel Vetter92703882012-08-09 16:46:01 +02004318 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004319}
4320
Daniel Vetteracbe9472012-07-26 11:50:05 +02004321/* There's a funny hw issue where the hw returns all 0 when reading from
4322 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4323 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4324 * all limits and the gpu stuck at whatever frequency it is at atm).
4325 */
Akash Goel74ef1172015-03-06 11:07:19 +05304326static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004327{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004328 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004329
Daniel Vetter20b46e52012-07-26 11:16:14 +02004330 /* Only set the down limit when we've reached the lowest level to avoid
4331 * getting more interrupts, otherwise leave this clear. This prevents a
4332 * race in the hw when coming out of rc6: There's a tiny window where
4333 * the hw runs at the minimal clock before selecting the desired
4334 * frequency, if the down threshold expires in that window we will not
4335 * receive a down interrupt. */
Akash Goel74ef1172015-03-06 11:07:19 +05304336 if (IS_GEN9(dev_priv->dev)) {
4337 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4338 if (val <= dev_priv->rps.min_freq_softlimit)
4339 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4340 } else {
4341 limits = dev_priv->rps.max_freq_softlimit << 24;
4342 if (val <= dev_priv->rps.min_freq_softlimit)
4343 limits |= dev_priv->rps.min_freq_softlimit << 16;
4344 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004345
4346 return limits;
4347}
4348
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004349static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4350{
4351 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304352 u32 threshold_up = 0, threshold_down = 0; /* in % */
4353 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004354
4355 new_power = dev_priv->rps.power;
4356 switch (dev_priv->rps.power) {
4357 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004358 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004359 new_power = BETWEEN;
4360 break;
4361
4362 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004363 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004364 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004365 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004366 new_power = HIGH_POWER;
4367 break;
4368
4369 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004370 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 +01004371 new_power = BETWEEN;
4372 break;
4373 }
4374 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004375 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004376 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004377 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004378 new_power = HIGH_POWER;
4379 if (new_power == dev_priv->rps.power)
4380 return;
4381
4382 /* Note the units here are not exactly 1us, but 1280ns. */
4383 switch (new_power) {
4384 case LOW_POWER:
4385 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304386 ei_up = 16000;
4387 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004388
4389 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304390 ei_down = 32000;
4391 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004392 break;
4393
4394 case BETWEEN:
4395 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304396 ei_up = 13000;
4397 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004398
4399 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304400 ei_down = 32000;
4401 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004402 break;
4403
4404 case HIGH_POWER:
4405 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304406 ei_up = 10000;
4407 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004408
4409 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304410 ei_down = 32000;
4411 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004412 break;
4413 }
4414
Akash Goel8a586432015-03-06 11:07:18 +05304415 I915_WRITE(GEN6_RP_UP_EI,
4416 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4417 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4418 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4419
4420 I915_WRITE(GEN6_RP_DOWN_EI,
4421 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4422 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4423 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4424
4425 I915_WRITE(GEN6_RP_CONTROL,
4426 GEN6_RP_MEDIA_TURBO |
4427 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4428 GEN6_RP_MEDIA_IS_GFX |
4429 GEN6_RP_ENABLE |
4430 GEN6_RP_UP_BUSY_AVG |
4431 GEN6_RP_DOWN_IDLE_AVG);
4432
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004433 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004434 dev_priv->rps.up_threshold = threshold_up;
4435 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004436 dev_priv->rps.last_adj = 0;
4437}
4438
Chris Wilson2876ce72014-03-28 08:03:34 +00004439static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4440{
4441 u32 mask = 0;
4442
4443 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004444 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004445 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004446 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004447
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004448 mask &= dev_priv->pm_rps_events;
4449
Imre Deak59d02a12014-12-19 19:33:26 +02004450 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004451}
4452
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004453/* gen6_set_rps is called to update the frequency request, but should also be
4454 * called when the range (min_delay and max_delay) is modified so that we can
4455 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004456static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004457{
4458 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004459
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004460 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004461 WARN_ON(val > dev_priv->rps.max_freq);
4462 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004463
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004464 /* min/max delay may still have been modified so be sure to
4465 * write the limits value.
4466 */
4467 if (val != dev_priv->rps.cur_freq) {
4468 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004469
Akash Goel57041952015-03-06 11:07:17 +05304470 if (IS_GEN9(dev))
4471 I915_WRITE(GEN6_RPNSWREQ,
4472 GEN9_FREQUENCY(val));
4473 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004474 I915_WRITE(GEN6_RPNSWREQ,
4475 HSW_FREQUENCY(val));
4476 else
4477 I915_WRITE(GEN6_RPNSWREQ,
4478 GEN6_FREQUENCY(val) |
4479 GEN6_OFFSET(0) |
4480 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004481 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004482
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004483 /* Make sure we continue to get interrupts
4484 * until we hit the minimum or maximum frequencies.
4485 */
Akash Goel74ef1172015-03-06 11:07:19 +05304486 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004487 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004488
Ben Widawskyd5570a72012-09-07 19:43:41 -07004489 POSTING_READ(GEN6_RPNSWREQ);
4490
Ben Widawskyb39fb292014-03-19 18:31:11 -07004491 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02004492 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004493}
4494
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004495static void valleyview_set_rps(struct drm_device *dev, u8 val)
4496{
4497 struct drm_i915_private *dev_priv = dev->dev_private;
4498
4499 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004500 WARN_ON(val > dev_priv->rps.max_freq);
4501 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004502
4503 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4504 "Odd GPU freq value\n"))
4505 val &= ~1;
4506
Deepak Scd25dd52015-07-10 18:31:40 +05304507 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4508
Chris Wilson8fb55192015-04-07 16:20:28 +01004509 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004510 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01004511 if (!IS_CHERRYVIEW(dev_priv))
4512 gen6_set_rps_thresholds(dev_priv, val);
4513 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004514
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004515 dev_priv->rps.cur_freq = val;
4516 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4517}
4518
Deepak Sa7f6e232015-05-09 18:04:44 +05304519/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05304520 *
4521 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05304522 * 1. Forcewake Media well.
4523 * 2. Request idle freq.
4524 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05304525*/
4526static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4527{
Chris Wilsonaed242f2015-03-18 09:48:21 +00004528 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304529
Chris Wilsonaed242f2015-03-18 09:48:21 +00004530 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304531 return;
4532
Deepak Sa7f6e232015-05-09 18:04:44 +05304533 /* Wake up the media well, as that takes a lot less
4534 * power than the Render well. */
4535 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4536 valleyview_set_rps(dev_priv->dev, val);
4537 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05304538}
4539
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004540void gen6_rps_busy(struct drm_i915_private *dev_priv)
4541{
4542 mutex_lock(&dev_priv->rps.hw_lock);
4543 if (dev_priv->rps.enabled) {
4544 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4545 gen6_rps_reset_ei(dev_priv);
4546 I915_WRITE(GEN6_PMINTRMSK,
4547 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4548 }
4549 mutex_unlock(&dev_priv->rps.hw_lock);
4550}
4551
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004552void gen6_rps_idle(struct drm_i915_private *dev_priv)
4553{
Damien Lespiau691bb712013-12-12 14:36:36 +00004554 struct drm_device *dev = dev_priv->dev;
4555
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004556 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004557 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004558 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304559 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004560 else
Chris Wilsonaed242f2015-03-18 09:48:21 +00004561 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004562 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004563 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004564 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004565 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004566
Chris Wilson8d3afd72015-05-21 21:01:47 +01004567 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004568 while (!list_empty(&dev_priv->rps.clients))
4569 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004570 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004571}
4572
Chris Wilson1854d5c2015-04-07 16:20:32 +01004573void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01004574 struct intel_rps_client *rps,
4575 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004576{
Chris Wilson8d3afd72015-05-21 21:01:47 +01004577 /* This is intentionally racy! We peek at the state here, then
4578 * validate inside the RPS worker.
4579 */
4580 if (!(dev_priv->mm.busy &&
4581 dev_priv->rps.enabled &&
4582 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4583 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004584
Chris Wilsone61b9952015-04-27 13:41:24 +01004585 /* Force a RPS boost (and don't count it against the client) if
4586 * the GPU is severely congested.
4587 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004588 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01004589 rps = NULL;
4590
Chris Wilson8d3afd72015-05-21 21:01:47 +01004591 spin_lock(&dev_priv->rps.client_lock);
4592 if (rps == NULL || list_empty(&rps->link)) {
4593 spin_lock_irq(&dev_priv->irq_lock);
4594 if (dev_priv->rps.interrupts_enabled) {
4595 dev_priv->rps.client_boost = true;
4596 queue_work(dev_priv->wq, &dev_priv->rps.work);
4597 }
4598 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004599
Chris Wilson2e1b8732015-04-27 13:41:22 +01004600 if (rps != NULL) {
4601 list_add(&rps->link, &dev_priv->rps.clients);
4602 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01004603 } else
4604 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01004605 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004606 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004607}
4608
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004609void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004610{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004611 if (IS_VALLEYVIEW(dev))
4612 valleyview_set_rps(dev, val);
4613 else
4614 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004615}
4616
Zhe Wang20e49362014-11-04 17:07:05 +00004617static void gen9_disable_rps(struct drm_device *dev)
4618{
4619 struct drm_i915_private *dev_priv = dev->dev_private;
4620
4621 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004622 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004623}
4624
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004625static void gen6_disable_rps(struct drm_device *dev)
4626{
4627 struct drm_i915_private *dev_priv = dev->dev_private;
4628
4629 I915_WRITE(GEN6_RC_CONTROL, 0);
4630 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004631}
4632
Deepak S38807742014-05-23 21:00:15 +05304633static void cherryview_disable_rps(struct drm_device *dev)
4634{
4635 struct drm_i915_private *dev_priv = dev->dev_private;
4636
4637 I915_WRITE(GEN6_RC_CONTROL, 0);
4638}
4639
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004640static void valleyview_disable_rps(struct drm_device *dev)
4641{
4642 struct drm_i915_private *dev_priv = dev->dev_private;
4643
Deepak S98a2e5f2014-08-18 10:35:27 -07004644 /* we're doing forcewake before Disabling RC6,
4645 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004646 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004647
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004648 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004649
Mika Kuoppala59bad942015-01-16 11:34:40 +02004650 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004651}
4652
Ben Widawskydc39fff2013-10-18 12:32:07 -07004653static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4654{
Imre Deak91ca6892014-04-14 20:24:25 +03004655 if (IS_VALLEYVIEW(dev)) {
4656 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4657 mode = GEN6_RC_CTL_RC6_ENABLE;
4658 else
4659 mode = 0;
4660 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004661 if (HAS_RC6p(dev))
4662 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4663 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4664 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4665 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4666
4667 else
4668 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4669 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004670}
4671
Imre Deake6069ca2014-04-18 16:01:02 +03004672static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004673{
Daniel Vettere7d66d82015-06-15 23:23:54 +02004674 /* No RC6 before Ironlake and code is gone for ilk. */
4675 if (INTEL_INFO(dev)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03004676 return 0;
4677
Daniel Vetter456470e2012-08-08 23:35:40 +02004678 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004679 if (enable_rc6 >= 0) {
4680 int mask;
4681
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004682 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004683 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4684 INTEL_RC6pp_ENABLE;
4685 else
4686 mask = INTEL_RC6_ENABLE;
4687
4688 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004689 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4690 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004691
4692 return enable_rc6 & mask;
4693 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004694
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004695 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004696 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004697
4698 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004699}
4700
Imre Deake6069ca2014-04-18 16:01:02 +03004701int intel_enable_rc6(const struct drm_device *dev)
4702{
4703 return i915.enable_rc6;
4704}
4705
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004706static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004707{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004708 struct drm_i915_private *dev_priv = dev->dev_private;
4709 uint32_t rp_state_cap;
4710 u32 ddcc_status = 0;
4711 int ret;
4712
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004713 /* All of these values are in units of 50MHz */
4714 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004715 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Bob Paauwe35040562015-06-25 14:54:07 -07004716 if (IS_BROXTON(dev)) {
4717 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4718 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4719 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4720 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4721 } else {
4722 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4723 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4724 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4725 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4726 }
4727
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004728 /* hw_max = RP0 until we check for overclocking */
4729 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4730
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004731 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Akash Goelc5e06882015-06-29 14:50:19 +05304732 if (IS_HASWELL(dev) || IS_BROADWELL(dev) || IS_SKYLAKE(dev)) {
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004733 ret = sandybridge_pcode_read(dev_priv,
4734 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4735 &ddcc_status);
4736 if (0 == ret)
4737 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004738 clamp_t(u8,
4739 ((ddcc_status >> 8) & 0xff),
4740 dev_priv->rps.min_freq,
4741 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004742 }
4743
Akash Goelc5e06882015-06-29 14:50:19 +05304744 if (IS_SKYLAKE(dev)) {
4745 /* Store the frequency values in 16.66 MHZ units, which is
4746 the natural hardware unit for SKL */
4747 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4748 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4749 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4750 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4751 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4752 }
4753
Chris Wilsonaed242f2015-03-18 09:48:21 +00004754 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4755
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004756 /* Preserve min/max settings in case of re-init */
4757 if (dev_priv->rps.max_freq_softlimit == 0)
4758 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4759
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004760 if (dev_priv->rps.min_freq_softlimit == 0) {
4761 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4762 dev_priv->rps.min_freq_softlimit =
Ville Syrjälä813b5e62015-03-25 19:27:16 +02004763 max_t(int, dev_priv->rps.efficient_freq,
4764 intel_freq_opcode(dev_priv, 450));
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004765 else
4766 dev_priv->rps.min_freq_softlimit =
4767 dev_priv->rps.min_freq;
4768 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004769}
4770
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004771/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004772static void gen9_enable_rps(struct drm_device *dev)
4773{
4774 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004775
4776 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4777
Damien Lespiauba1c5542015-01-16 18:07:26 +00004778 gen6_init_rps_frequencies(dev);
4779
Akash Goel0beb0592015-03-06 11:07:20 +05304780 /* Program defaults and thresholds for RPS*/
4781 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4782 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004783
Akash Goel0beb0592015-03-06 11:07:20 +05304784 /* 1 second timeout*/
4785 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4786 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4787
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004788 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004789
Akash Goel0beb0592015-03-06 11:07:20 +05304790 /* Leaning on the below call to gen6_set_rps to program/setup the
4791 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4792 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4793 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4794 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004795
4796 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4797}
4798
4799static void gen9_enable_rc6(struct drm_device *dev)
4800{
4801 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004802 struct intel_engine_cs *ring;
4803 uint32_t rc6_mask = 0;
4804 int unused;
4805
4806 /* 1a: Software RC state - RC0 */
4807 I915_WRITE(GEN6_RC_STATE, 0);
4808
4809 /* 1b: Get forcewake during program sequence. Although the driver
4810 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004811 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004812
4813 /* 2a: Disable RC states. */
4814 I915_WRITE(GEN6_RC_CONTROL, 0);
4815
4816 /* 2b: Program RC6 thresholds.*/
4817 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4818 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4819 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4820 for_each_ring(ring, dev_priv, unused)
4821 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4822 I915_WRITE(GEN6_RC_SLEEP, 0);
4823 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4824
Zhe Wang38c23522015-01-20 12:23:04 +00004825 /* 2c: Program Coarse Power Gating Policies. */
4826 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4827 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4828
Zhe Wang20e49362014-11-04 17:07:05 +00004829 /* 3a: Enable RC6 */
4830 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4831 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4832 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4833 "on" : "off");
4834 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4835 GEN6_RC_CTL_EI_MODE(1) |
4836 rc6_mask);
4837
Sagar Kamblecb07bae2015-04-12 11:28:14 +05304838 /*
4839 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
4840 * WaDisableRenderPowerGating:skl,bxt - Render PG need to be disabled with RC6.
4841 */
Sagar Kamblea4104c52015-04-10 14:11:29 +05304842 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
Sagar Kamblecb07bae2015-04-12 11:28:14 +05304843 GEN9_MEDIA_PG_ENABLE : 0);
Sagar Kamblea4104c52015-04-10 14:11:29 +05304844
Zhe Wang38c23522015-01-20 12:23:04 +00004845
Mika Kuoppala59bad942015-01-16 11:34:40 +02004846 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004847
4848}
4849
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004850static void gen8_enable_rps(struct drm_device *dev)
4851{
4852 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004853 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004854 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004855 int unused;
4856
4857 /* 1a: Software RC state - RC0 */
4858 I915_WRITE(GEN6_RC_STATE, 0);
4859
4860 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4861 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004862 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004863
4864 /* 2a: Disable RC states. */
4865 I915_WRITE(GEN6_RC_CONTROL, 0);
4866
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004867 /* Initialize rps frequencies */
4868 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004869
4870 /* 2b: Program RC6 thresholds.*/
4871 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4872 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4873 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4874 for_each_ring(ring, dev_priv, unused)
4875 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4876 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004877 if (IS_BROADWELL(dev))
4878 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4879 else
4880 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004881
4882 /* 3: Enable RC6 */
4883 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4884 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004885 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004886 if (IS_BROADWELL(dev))
4887 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4888 GEN7_RC_CTL_TO_MODE |
4889 rc6_mask);
4890 else
4891 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4892 GEN6_RC_CTL_EI_MODE(1) |
4893 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004894
4895 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004896 I915_WRITE(GEN6_RPNSWREQ,
4897 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4898 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4899 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004900 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4901 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004902
Daniel Vetter7526ed72014-09-29 15:07:19 +02004903 /* Docs recommend 900MHz, and 300 MHz respectively */
4904 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4905 dev_priv->rps.max_freq_softlimit << 24 |
4906 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004907
Daniel Vetter7526ed72014-09-29 15:07:19 +02004908 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4909 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4910 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4911 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004912
Daniel Vetter7526ed72014-09-29 15:07:19 +02004913 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004914
4915 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004916 I915_WRITE(GEN6_RP_CONTROL,
4917 GEN6_RP_MEDIA_TURBO |
4918 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4919 GEN6_RP_MEDIA_IS_GFX |
4920 GEN6_RP_ENABLE |
4921 GEN6_RP_UP_BUSY_AVG |
4922 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004923
Daniel Vetter7526ed72014-09-29 15:07:19 +02004924 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004925
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004926 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004927 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004928
Mika Kuoppala59bad942015-01-16 11:34:40 +02004929 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004930}
4931
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004932static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004933{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004934 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004935 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004936 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004937 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004938 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004939 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004940
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004941 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004942
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004943 /* Here begins a magic sequence of register writes to enable
4944 * auto-downclocking.
4945 *
4946 * Perhaps there might be some value in exposing these to
4947 * userspace...
4948 */
4949 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004950
4951 /* Clear the DBG now so we don't confuse earlier errors */
4952 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4953 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4954 I915_WRITE(GTFIFODBG, gtfifodbg);
4955 }
4956
Mika Kuoppala59bad942015-01-16 11:34:40 +02004957 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004958
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004959 /* Initialize rps frequencies */
4960 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004961
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004962 /* disable the counters and set deterministic thresholds */
4963 I915_WRITE(GEN6_RC_CONTROL, 0);
4964
4965 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4966 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4967 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4968 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4969 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4970
Chris Wilsonb4519512012-05-11 14:29:30 +01004971 for_each_ring(ring, dev_priv, i)
4972 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004973
4974 I915_WRITE(GEN6_RC_SLEEP, 0);
4975 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004976 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004977 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4978 else
4979 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004980 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004981 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4982
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004983 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004984 rc6_mode = intel_enable_rc6(dev_priv->dev);
4985 if (rc6_mode & INTEL_RC6_ENABLE)
4986 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4987
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004988 /* We don't use those on Haswell */
4989 if (!IS_HASWELL(dev)) {
4990 if (rc6_mode & INTEL_RC6p_ENABLE)
4991 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004992
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004993 if (rc6_mode & INTEL_RC6pp_ENABLE)
4994 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4995 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004996
Ben Widawskydc39fff2013-10-18 12:32:07 -07004997 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004998
4999 I915_WRITE(GEN6_RC_CONTROL,
5000 rc6_mask |
5001 GEN6_RC_CTL_EI_MODE(1) |
5002 GEN6_RC_CTL_HW_ENABLE);
5003
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005004 /* Power down if completely idle for over 50ms */
5005 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005006 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005007
Ben Widawsky42c05262012-09-26 10:34:00 -07005008 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07005009 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07005010 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07005011
5012 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5013 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5014 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07005015 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07005016 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07005017 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005018 }
5019
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005020 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005021 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005022
Ben Widawsky31643d52012-09-26 10:34:01 -07005023 rc6vids = 0;
5024 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5025 if (IS_GEN6(dev) && ret) {
5026 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5027 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5028 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5029 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5030 rc6vids &= 0xffff00;
5031 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5032 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5033 if (ret)
5034 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5035 }
5036
Mika Kuoppala59bad942015-01-16 11:34:40 +02005037 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005038}
5039
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005040static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005041{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005042 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005043 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005044 unsigned int gpu_freq;
5045 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305046 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005047 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005048 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005049
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005050 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005051
Ben Widawskyeda79642013-10-07 17:15:48 -03005052 policy = cpufreq_cpu_get(0);
5053 if (policy) {
5054 max_ia_freq = policy->cpuinfo.max_freq;
5055 cpufreq_cpu_put(policy);
5056 } else {
5057 /*
5058 * Default to measured freq if none found, PCU will ensure we
5059 * don't go over
5060 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005061 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005062 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005063
5064 /* Convert from kHz to MHz */
5065 max_ia_freq /= 1000;
5066
Ben Widawsky153b4b952013-10-22 22:05:09 -07005067 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005068 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5069 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005070
Akash Goel4c8c7742015-06-29 14:50:20 +05305071 if (IS_SKYLAKE(dev)) {
5072 /* Convert GT frequency to 50 HZ units */
5073 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5074 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5075 } else {
5076 min_gpu_freq = dev_priv->rps.min_freq;
5077 max_gpu_freq = dev_priv->rps.max_freq;
5078 }
5079
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005080 /*
5081 * For each potential GPU frequency, load a ring frequency we'd like
5082 * to use for memory access. We do this by specifying the IA frequency
5083 * the PCU should use as a reference to determine the ring frequency.
5084 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305085 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5086 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005087 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005088
Akash Goel4c8c7742015-06-29 14:50:20 +05305089 if (IS_SKYLAKE(dev)) {
5090 /*
5091 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5092 * No floor required for ring frequency on SKL.
5093 */
5094 ring_freq = gpu_freq;
5095 } else if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005096 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5097 ring_freq = max(min_ring_freq, gpu_freq);
5098 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005099 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005100 ring_freq = max(min_ring_freq, ring_freq);
5101 /* leave ia_freq as the default, chosen by cpufreq */
5102 } else {
5103 /* On older processors, there is no separate ring
5104 * clock domain, so in order to boost the bandwidth
5105 * of the ring, we need to upclock the CPU (ia_freq).
5106 *
5107 * For GPU frequencies less than 750MHz,
5108 * just use the lowest ring freq.
5109 */
5110 if (gpu_freq < min_freq)
5111 ia_freq = 800;
5112 else
5113 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5114 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5115 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005116
Ben Widawsky42c05262012-09-26 10:34:00 -07005117 sandybridge_pcode_write(dev_priv,
5118 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005119 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5120 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5121 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005122 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005123}
5124
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005125void gen6_update_ring_freq(struct drm_device *dev)
5126{
5127 struct drm_i915_private *dev_priv = dev->dev_private;
5128
Akash Goel97d33082015-06-29 14:50:23 +05305129 if (!HAS_CORE_RING_FREQ(dev))
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005130 return;
5131
5132 mutex_lock(&dev_priv->rps.hw_lock);
5133 __gen6_update_ring_freq(dev);
5134 mutex_unlock(&dev_priv->rps.hw_lock);
5135}
5136
Ville Syrjälä03af2042014-06-28 02:03:53 +03005137static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305138{
Deepak S095acd52015-01-17 11:05:59 +05305139 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05305140 u32 val, rp0;
5141
Deepak S095acd52015-01-17 11:05:59 +05305142 if (dev->pdev->revision >= 0x20) {
5143 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305144
Deepak S095acd52015-01-17 11:05:59 +05305145 switch (INTEL_INFO(dev)->eu_total) {
5146 case 8:
5147 /* (2 * 4) config */
5148 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5149 break;
5150 case 12:
5151 /* (2 * 6) config */
5152 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5153 break;
5154 case 16:
5155 /* (2 * 8) config */
5156 default:
5157 /* Setting (2 * 8) Min RP0 for any other combination */
5158 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5159 break;
5160 }
5161 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5162 } else {
5163 /* For pre-production hardware */
5164 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
5165 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
5166 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
5167 }
Deepak S2b6b3a02014-05-27 15:59:30 +05305168 return rp0;
5169}
5170
5171static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5172{
5173 u32 val, rpe;
5174
5175 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5176 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5177
5178 return rpe;
5179}
5180
Deepak S7707df42014-07-12 18:46:14 +05305181static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5182{
Deepak S095acd52015-01-17 11:05:59 +05305183 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05305184 u32 val, rp1;
5185
Deepak S095acd52015-01-17 11:05:59 +05305186 if (dev->pdev->revision >= 0x20) {
5187 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5188 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5189 } else {
5190 /* For pre-production hardware */
5191 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5192 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
5193 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
5194 }
Deepak S7707df42014-07-12 18:46:14 +05305195 return rp1;
5196}
5197
Deepak Sf8f2b002014-07-10 13:16:21 +05305198static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5199{
5200 u32 val, rp1;
5201
5202 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5203
5204 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5205
5206 return rp1;
5207}
5208
Ville Syrjälä03af2042014-06-28 02:03:53 +03005209static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005210{
5211 u32 val, rp0;
5212
Jani Nikula64936252013-05-22 15:36:20 +03005213 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005214
5215 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5216 /* Clamp to max */
5217 rp0 = min_t(u32, rp0, 0xea);
5218
5219 return rp0;
5220}
5221
5222static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5223{
5224 u32 val, rpe;
5225
Jani Nikula64936252013-05-22 15:36:20 +03005226 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005227 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005228 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005229 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5230
5231 return rpe;
5232}
5233
Ville Syrjälä03af2042014-06-28 02:03:53 +03005234static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005235{
Jani Nikula64936252013-05-22 15:36:20 +03005236 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005237}
5238
Imre Deakae484342014-03-31 15:10:44 +03005239/* Check that the pctx buffer wasn't move under us. */
5240static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5241{
5242 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5243
5244 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5245 dev_priv->vlv_pctx->stolen->start);
5246}
5247
Deepak S38807742014-05-23 21:00:15 +05305248
5249/* Check that the pcbr address is not empty. */
5250static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5251{
5252 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5253
5254 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5255}
5256
5257static void cherryview_setup_pctx(struct drm_device *dev)
5258{
5259 struct drm_i915_private *dev_priv = dev->dev_private;
5260 unsigned long pctx_paddr, paddr;
5261 struct i915_gtt *gtt = &dev_priv->gtt;
5262 u32 pcbr;
5263 int pctx_size = 32*1024;
5264
5265 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5266
5267 pcbr = I915_READ(VLV_PCBR);
5268 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005269 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305270 paddr = (dev_priv->mm.stolen_base +
5271 (gtt->stolen_size - pctx_size));
5272
5273 pctx_paddr = (paddr & (~4095));
5274 I915_WRITE(VLV_PCBR, pctx_paddr);
5275 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005276
5277 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305278}
5279
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005280static void valleyview_setup_pctx(struct drm_device *dev)
5281{
5282 struct drm_i915_private *dev_priv = dev->dev_private;
5283 struct drm_i915_gem_object *pctx;
5284 unsigned long pctx_paddr;
5285 u32 pcbr;
5286 int pctx_size = 24*1024;
5287
Imre Deak17b0c1f2014-02-11 21:39:06 +02005288 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5289
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005290 pcbr = I915_READ(VLV_PCBR);
5291 if (pcbr) {
5292 /* BIOS set it up already, grab the pre-alloc'd space */
5293 int pcbr_offset;
5294
5295 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5296 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5297 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005298 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005299 pctx_size);
5300 goto out;
5301 }
5302
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005303 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5304
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005305 /*
5306 * From the Gunit register HAS:
5307 * The Gfx driver is expected to program this register and ensure
5308 * proper allocation within Gfx stolen memory. For example, this
5309 * register should be programmed such than the PCBR range does not
5310 * overlap with other ranges, such as the frame buffer, protected
5311 * memory, or any other relevant ranges.
5312 */
5313 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5314 if (!pctx) {
5315 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5316 return;
5317 }
5318
5319 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5320 I915_WRITE(VLV_PCBR, pctx_paddr);
5321
5322out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005323 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005324 dev_priv->vlv_pctx = pctx;
5325}
5326
Imre Deakae484342014-03-31 15:10:44 +03005327static void valleyview_cleanup_pctx(struct drm_device *dev)
5328{
5329 struct drm_i915_private *dev_priv = dev->dev_private;
5330
5331 if (WARN_ON(!dev_priv->vlv_pctx))
5332 return;
5333
5334 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5335 dev_priv->vlv_pctx = NULL;
5336}
5337
Imre Deak4e805192014-04-14 20:24:41 +03005338static void valleyview_init_gt_powersave(struct drm_device *dev)
5339{
5340 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005341 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005342
5343 valleyview_setup_pctx(dev);
5344
5345 mutex_lock(&dev_priv->rps.hw_lock);
5346
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005347 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5348 switch ((val >> 6) & 3) {
5349 case 0:
5350 case 1:
5351 dev_priv->mem_freq = 800;
5352 break;
5353 case 2:
5354 dev_priv->mem_freq = 1066;
5355 break;
5356 case 3:
5357 dev_priv->mem_freq = 1333;
5358 break;
5359 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005360 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005361
Imre Deak4e805192014-04-14 20:24:41 +03005362 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5363 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5364 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005365 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005366 dev_priv->rps.max_freq);
5367
5368 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5369 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005370 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005371 dev_priv->rps.efficient_freq);
5372
Deepak Sf8f2b002014-07-10 13:16:21 +05305373 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5374 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005375 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305376 dev_priv->rps.rp1_freq);
5377
Imre Deak4e805192014-04-14 20:24:41 +03005378 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5379 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005380 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005381 dev_priv->rps.min_freq);
5382
Chris Wilsonaed242f2015-03-18 09:48:21 +00005383 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5384
Imre Deak4e805192014-04-14 20:24:41 +03005385 /* Preserve min/max settings in case of re-init */
5386 if (dev_priv->rps.max_freq_softlimit == 0)
5387 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5388
5389 if (dev_priv->rps.min_freq_softlimit == 0)
5390 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5391
5392 mutex_unlock(&dev_priv->rps.hw_lock);
5393}
5394
Deepak S38807742014-05-23 21:00:15 +05305395static void cherryview_init_gt_powersave(struct drm_device *dev)
5396{
Deepak S2b6b3a02014-05-27 15:59:30 +05305397 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005398 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305399
Deepak S38807742014-05-23 21:00:15 +05305400 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05305401
5402 mutex_lock(&dev_priv->rps.hw_lock);
5403
Ville Syrjäläa5805162015-05-26 20:42:30 +03005404 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005405 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005406 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005407
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005408 switch ((val >> 2) & 0x7) {
5409 case 0:
5410 case 1:
5411 dev_priv->rps.cz_freq = 200;
5412 dev_priv->mem_freq = 1600;
5413 break;
5414 case 2:
5415 dev_priv->rps.cz_freq = 267;
5416 dev_priv->mem_freq = 1600;
5417 break;
5418 case 3:
5419 dev_priv->rps.cz_freq = 333;
5420 dev_priv->mem_freq = 2000;
5421 break;
5422 case 4:
5423 dev_priv->rps.cz_freq = 320;
5424 dev_priv->mem_freq = 1600;
5425 break;
5426 case 5:
5427 dev_priv->rps.cz_freq = 400;
5428 dev_priv->mem_freq = 1600;
5429 break;
5430 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005431 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005432
Deepak S2b6b3a02014-05-27 15:59:30 +05305433 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5434 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5435 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005436 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305437 dev_priv->rps.max_freq);
5438
5439 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5440 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005441 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305442 dev_priv->rps.efficient_freq);
5443
Deepak S7707df42014-07-12 18:46:14 +05305444 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5445 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005446 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305447 dev_priv->rps.rp1_freq);
5448
Deepak S5b7c91b2015-05-09 18:15:46 +05305449 /* PUnit validated range is only [RPe, RP0] */
5450 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305451 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005452 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305453 dev_priv->rps.min_freq);
5454
Ville Syrjälä1c147622014-08-18 14:42:43 +03005455 WARN_ONCE((dev_priv->rps.max_freq |
5456 dev_priv->rps.efficient_freq |
5457 dev_priv->rps.rp1_freq |
5458 dev_priv->rps.min_freq) & 1,
5459 "Odd GPU freq values\n");
5460
Chris Wilsonaed242f2015-03-18 09:48:21 +00005461 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5462
Deepak S2b6b3a02014-05-27 15:59:30 +05305463 /* Preserve min/max settings in case of re-init */
5464 if (dev_priv->rps.max_freq_softlimit == 0)
5465 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5466
5467 if (dev_priv->rps.min_freq_softlimit == 0)
5468 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5469
5470 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305471}
5472
Imre Deak4e805192014-04-14 20:24:41 +03005473static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5474{
5475 valleyview_cleanup_pctx(dev);
5476}
5477
Deepak S38807742014-05-23 21:00:15 +05305478static void cherryview_enable_rps(struct drm_device *dev)
5479{
5480 struct drm_i915_private *dev_priv = dev->dev_private;
5481 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05305482 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305483 int i;
5484
5485 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5486
5487 gtfifodbg = I915_READ(GTFIFODBG);
5488 if (gtfifodbg) {
5489 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5490 gtfifodbg);
5491 I915_WRITE(GTFIFODBG, gtfifodbg);
5492 }
5493
5494 cherryview_check_pctx(dev_priv);
5495
5496 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5497 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005498 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305499
Ville Syrjälä160614a2015-01-19 13:50:47 +02005500 /* Disable RC states. */
5501 I915_WRITE(GEN6_RC_CONTROL, 0);
5502
Deepak S38807742014-05-23 21:00:15 +05305503 /* 2a: Program RC6 thresholds.*/
5504 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5505 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5506 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5507
5508 for_each_ring(ring, dev_priv, i)
5509 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5510 I915_WRITE(GEN6_RC_SLEEP, 0);
5511
Deepak Sf4f71c72015-03-28 15:23:35 +05305512 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5513 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05305514
5515 /* allows RC6 residency counter to work */
5516 I915_WRITE(VLV_COUNTER_CONTROL,
5517 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5518 VLV_MEDIA_RC6_COUNT_EN |
5519 VLV_RENDER_RC6_COUNT_EN));
5520
5521 /* For now we assume BIOS is allocating and populating the PCBR */
5522 pcbr = I915_READ(VLV_PCBR);
5523
Deepak S38807742014-05-23 21:00:15 +05305524 /* 3: Enable RC6 */
5525 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5526 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005527 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305528
5529 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5530
Deepak S2b6b3a02014-05-27 15:59:30 +05305531 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005532 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305533 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5534 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5535 I915_WRITE(GEN6_RP_UP_EI, 66000);
5536 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5537
5538 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5539
5540 /* 5: Enable RPS */
5541 I915_WRITE(GEN6_RP_CONTROL,
5542 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005543 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305544 GEN6_RP_ENABLE |
5545 GEN6_RP_UP_BUSY_AVG |
5546 GEN6_RP_DOWN_IDLE_AVG);
5547
Deepak S3ef62342015-04-29 08:36:24 +05305548 /* Setting Fixed Bias */
5549 val = VLV_OVERRIDE_EN |
5550 VLV_SOC_TDP_EN |
5551 CHV_BIAS_CPU_50_SOC_50;
5552 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5553
Deepak S2b6b3a02014-05-27 15:59:30 +05305554 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5555
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005556 /* RPS code assumes GPLL is used */
5557 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5558
Jani Nikula742f4912015-09-03 11:16:09 +03005559 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05305560 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5561
5562 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5563 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005564 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305565 dev_priv->rps.cur_freq);
5566
5567 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005568 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305569 dev_priv->rps.efficient_freq);
5570
5571 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5572
Mika Kuoppala59bad942015-01-16 11:34:40 +02005573 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305574}
5575
Jesse Barnes0a073b82013-04-17 15:54:58 -07005576static void valleyview_enable_rps(struct drm_device *dev)
5577{
5578 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005579 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005580 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005581 int i;
5582
5583 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5584
Imre Deakae484342014-03-31 15:10:44 +03005585 valleyview_check_pctx(dev_priv);
5586
Jesse Barnes0a073b82013-04-17 15:54:58 -07005587 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005588 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5589 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005590 I915_WRITE(GTFIFODBG, gtfifodbg);
5591 }
5592
Deepak Sc8d9a592013-11-23 14:55:42 +05305593 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005594 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005595
Ville Syrjälä160614a2015-01-19 13:50:47 +02005596 /* Disable RC states. */
5597 I915_WRITE(GEN6_RC_CONTROL, 0);
5598
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005599 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005600 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5601 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5602 I915_WRITE(GEN6_RP_UP_EI, 66000);
5603 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5604
5605 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5606
5607 I915_WRITE(GEN6_RP_CONTROL,
5608 GEN6_RP_MEDIA_TURBO |
5609 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5610 GEN6_RP_MEDIA_IS_GFX |
5611 GEN6_RP_ENABLE |
5612 GEN6_RP_UP_BUSY_AVG |
5613 GEN6_RP_DOWN_IDLE_CONT);
5614
5615 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5616 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5617 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5618
5619 for_each_ring(ring, dev_priv, i)
5620 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5621
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005622 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005623
5624 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005625 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005626 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5627 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005628 VLV_MEDIA_RC6_COUNT_EN |
5629 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005630
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005631 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005632 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005633
5634 intel_print_rc6_info(dev, rc6_mode);
5635
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005636 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005637
Deepak S3ef62342015-04-29 08:36:24 +05305638 /* Setting Fixed Bias */
5639 val = VLV_OVERRIDE_EN |
5640 VLV_SOC_TDP_EN |
5641 VLV_BIAS_CPU_125_SOC_875;
5642 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5643
Jani Nikula64936252013-05-22 15:36:20 +03005644 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005645
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005646 /* RPS code assumes GPLL is used */
5647 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5648
Jani Nikula742f4912015-09-03 11:16:09 +03005649 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07005650 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5651
Ben Widawskyb39fb292014-03-19 18:31:11 -07005652 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005653 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005654 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005655 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005656
Ville Syrjälä73008b92013-06-25 19:21:01 +03005657 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005658 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005659 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005660
Ben Widawskyb39fb292014-03-19 18:31:11 -07005661 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005662
Mika Kuoppala59bad942015-01-16 11:34:40 +02005663 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005664}
5665
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005666static unsigned long intel_pxfreq(u32 vidfreq)
5667{
5668 unsigned long freq;
5669 int div = (vidfreq & 0x3f0000) >> 16;
5670 int post = (vidfreq & 0x3000) >> 12;
5671 int pre = (vidfreq & 0x7);
5672
5673 if (!pre)
5674 return 0;
5675
5676 freq = ((div * 133333) / ((1<<post) * pre));
5677
5678 return freq;
5679}
5680
Daniel Vettereb48eb02012-04-26 23:28:12 +02005681static const struct cparams {
5682 u16 i;
5683 u16 t;
5684 u16 m;
5685 u16 c;
5686} cparams[] = {
5687 { 1, 1333, 301, 28664 },
5688 { 1, 1066, 294, 24460 },
5689 { 1, 800, 294, 25192 },
5690 { 0, 1333, 276, 27605 },
5691 { 0, 1066, 276, 27605 },
5692 { 0, 800, 231, 23784 },
5693};
5694
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005695static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005696{
5697 u64 total_count, diff, ret;
5698 u32 count1, count2, count3, m = 0, c = 0;
5699 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5700 int i;
5701
Daniel Vetter02d71952012-08-09 16:44:54 +02005702 assert_spin_locked(&mchdev_lock);
5703
Daniel Vetter20e4d402012-08-08 23:35:39 +02005704 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005705
5706 /* Prevent division-by-zero if we are asking too fast.
5707 * Also, we don't get interesting results if we are polling
5708 * faster than once in 10ms, so just return the saved value
5709 * in such cases.
5710 */
5711 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005712 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005713
5714 count1 = I915_READ(DMIEC);
5715 count2 = I915_READ(DDREC);
5716 count3 = I915_READ(CSIEC);
5717
5718 total_count = count1 + count2 + count3;
5719
5720 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005721 if (total_count < dev_priv->ips.last_count1) {
5722 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005723 diff += total_count;
5724 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005725 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005726 }
5727
5728 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005729 if (cparams[i].i == dev_priv->ips.c_m &&
5730 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005731 m = cparams[i].m;
5732 c = cparams[i].c;
5733 break;
5734 }
5735 }
5736
5737 diff = div_u64(diff, diff1);
5738 ret = ((m * diff) + c);
5739 ret = div_u64(ret, 10);
5740
Daniel Vetter20e4d402012-08-08 23:35:39 +02005741 dev_priv->ips.last_count1 = total_count;
5742 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005743
Daniel Vetter20e4d402012-08-08 23:35:39 +02005744 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005745
5746 return ret;
5747}
5748
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005749unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5750{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005751 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005752 unsigned long val;
5753
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005754 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005755 return 0;
5756
5757 spin_lock_irq(&mchdev_lock);
5758
5759 val = __i915_chipset_val(dev_priv);
5760
5761 spin_unlock_irq(&mchdev_lock);
5762
5763 return val;
5764}
5765
Daniel Vettereb48eb02012-04-26 23:28:12 +02005766unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5767{
5768 unsigned long m, x, b;
5769 u32 tsfs;
5770
5771 tsfs = I915_READ(TSFS);
5772
5773 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5774 x = I915_READ8(TR1);
5775
5776 b = tsfs & TSFS_INTR_MASK;
5777
5778 return ((m * x) / 127) - b;
5779}
5780
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005781static int _pxvid_to_vd(u8 pxvid)
5782{
5783 if (pxvid == 0)
5784 return 0;
5785
5786 if (pxvid >= 8 && pxvid < 31)
5787 pxvid = 31;
5788
5789 return (pxvid + 2) * 125;
5790}
5791
5792static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005793{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005794 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005795 const int vd = _pxvid_to_vd(pxvid);
5796 const int vm = vd - 1125;
5797
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005798 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005799 return vm > 0 ? vm : 0;
5800
5801 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005802}
5803
Daniel Vetter02d71952012-08-09 16:44:54 +02005804static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005805{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005806 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005807 u32 count;
5808
Daniel Vetter02d71952012-08-09 16:44:54 +02005809 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005810
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005811 now = ktime_get_raw_ns();
5812 diffms = now - dev_priv->ips.last_time2;
5813 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005814
5815 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005816 if (!diffms)
5817 return;
5818
5819 count = I915_READ(GFXEC);
5820
Daniel Vetter20e4d402012-08-08 23:35:39 +02005821 if (count < dev_priv->ips.last_count2) {
5822 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005823 diff += count;
5824 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005825 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005826 }
5827
Daniel Vetter20e4d402012-08-08 23:35:39 +02005828 dev_priv->ips.last_count2 = count;
5829 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005830
5831 /* More magic constants... */
5832 diff = diff * 1181;
5833 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005834 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005835}
5836
Daniel Vetter02d71952012-08-09 16:44:54 +02005837void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5838{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005839 struct drm_device *dev = dev_priv->dev;
5840
5841 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005842 return;
5843
Daniel Vetter92703882012-08-09 16:46:01 +02005844 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005845
5846 __i915_update_gfx_val(dev_priv);
5847
Daniel Vetter92703882012-08-09 16:46:01 +02005848 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005849}
5850
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005851static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005852{
5853 unsigned long t, corr, state1, corr2, state2;
5854 u32 pxvid, ext_v;
5855
Daniel Vetter02d71952012-08-09 16:44:54 +02005856 assert_spin_locked(&mchdev_lock);
5857
Ben Widawskyb39fb292014-03-19 18:31:11 -07005858 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005859 pxvid = (pxvid >> 24) & 0x7f;
5860 ext_v = pvid_to_extvid(dev_priv, pxvid);
5861
5862 state1 = ext_v;
5863
5864 t = i915_mch_val(dev_priv);
5865
5866 /* Revel in the empirically derived constants */
5867
5868 /* Correction factor in 1/100000 units */
5869 if (t > 80)
5870 corr = ((t * 2349) + 135940);
5871 else if (t >= 50)
5872 corr = ((t * 964) + 29317);
5873 else /* < 50 */
5874 corr = ((t * 301) + 1004);
5875
5876 corr = corr * ((150142 * state1) / 10000 - 78642);
5877 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005878 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005879
5880 state2 = (corr2 * state1) / 10000;
5881 state2 /= 100; /* convert to mW */
5882
Daniel Vetter02d71952012-08-09 16:44:54 +02005883 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005884
Daniel Vetter20e4d402012-08-08 23:35:39 +02005885 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005886}
5887
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005888unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5889{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005890 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005891 unsigned long val;
5892
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005893 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005894 return 0;
5895
5896 spin_lock_irq(&mchdev_lock);
5897
5898 val = __i915_gfx_val(dev_priv);
5899
5900 spin_unlock_irq(&mchdev_lock);
5901
5902 return val;
5903}
5904
Daniel Vettereb48eb02012-04-26 23:28:12 +02005905/**
5906 * i915_read_mch_val - return value for IPS use
5907 *
5908 * Calculate and return a value for the IPS driver to use when deciding whether
5909 * we have thermal and power headroom to increase CPU or GPU power budget.
5910 */
5911unsigned long i915_read_mch_val(void)
5912{
5913 struct drm_i915_private *dev_priv;
5914 unsigned long chipset_val, graphics_val, ret = 0;
5915
Daniel Vetter92703882012-08-09 16:46:01 +02005916 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005917 if (!i915_mch_dev)
5918 goto out_unlock;
5919 dev_priv = i915_mch_dev;
5920
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005921 chipset_val = __i915_chipset_val(dev_priv);
5922 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005923
5924 ret = chipset_val + graphics_val;
5925
5926out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005927 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005928
5929 return ret;
5930}
5931EXPORT_SYMBOL_GPL(i915_read_mch_val);
5932
5933/**
5934 * i915_gpu_raise - raise GPU frequency limit
5935 *
5936 * Raise the limit; IPS indicates we have thermal headroom.
5937 */
5938bool i915_gpu_raise(void)
5939{
5940 struct drm_i915_private *dev_priv;
5941 bool ret = true;
5942
Daniel Vetter92703882012-08-09 16:46:01 +02005943 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005944 if (!i915_mch_dev) {
5945 ret = false;
5946 goto out_unlock;
5947 }
5948 dev_priv = i915_mch_dev;
5949
Daniel Vetter20e4d402012-08-08 23:35:39 +02005950 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5951 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005952
5953out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005954 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005955
5956 return ret;
5957}
5958EXPORT_SYMBOL_GPL(i915_gpu_raise);
5959
5960/**
5961 * i915_gpu_lower - lower GPU frequency limit
5962 *
5963 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5964 * frequency maximum.
5965 */
5966bool i915_gpu_lower(void)
5967{
5968 struct drm_i915_private *dev_priv;
5969 bool ret = true;
5970
Daniel Vetter92703882012-08-09 16:46:01 +02005971 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005972 if (!i915_mch_dev) {
5973 ret = false;
5974 goto out_unlock;
5975 }
5976 dev_priv = i915_mch_dev;
5977
Daniel Vetter20e4d402012-08-08 23:35:39 +02005978 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5979 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005980
5981out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005982 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005983
5984 return ret;
5985}
5986EXPORT_SYMBOL_GPL(i915_gpu_lower);
5987
5988/**
5989 * i915_gpu_busy - indicate GPU business to IPS
5990 *
5991 * Tell the IPS driver whether or not the GPU is busy.
5992 */
5993bool i915_gpu_busy(void)
5994{
5995 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005996 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005997 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005998 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005999
Daniel Vetter92703882012-08-09 16:46:01 +02006000 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006001 if (!i915_mch_dev)
6002 goto out_unlock;
6003 dev_priv = i915_mch_dev;
6004
Chris Wilsonf047e392012-07-21 12:31:41 +01006005 for_each_ring(ring, dev_priv, i)
6006 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006007
6008out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006009 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006010
6011 return ret;
6012}
6013EXPORT_SYMBOL_GPL(i915_gpu_busy);
6014
6015/**
6016 * i915_gpu_turbo_disable - disable graphics turbo
6017 *
6018 * Disable graphics turbo by resetting the max frequency and setting the
6019 * current frequency to the default.
6020 */
6021bool i915_gpu_turbo_disable(void)
6022{
6023 struct drm_i915_private *dev_priv;
6024 bool ret = true;
6025
Daniel Vetter92703882012-08-09 16:46:01 +02006026 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006027 if (!i915_mch_dev) {
6028 ret = false;
6029 goto out_unlock;
6030 }
6031 dev_priv = i915_mch_dev;
6032
Daniel Vetter20e4d402012-08-08 23:35:39 +02006033 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006034
Daniel Vetter20e4d402012-08-08 23:35:39 +02006035 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006036 ret = false;
6037
6038out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006039 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006040
6041 return ret;
6042}
6043EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6044
6045/**
6046 * Tells the intel_ips driver that the i915 driver is now loaded, if
6047 * IPS got loaded first.
6048 *
6049 * This awkward dance is so that neither module has to depend on the
6050 * other in order for IPS to do the appropriate communication of
6051 * GPU turbo limits to i915.
6052 */
6053static void
6054ips_ping_for_i915_load(void)
6055{
6056 void (*link)(void);
6057
6058 link = symbol_get(ips_link_to_i915_driver);
6059 if (link) {
6060 link();
6061 symbol_put(ips_link_to_i915_driver);
6062 }
6063}
6064
6065void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6066{
Daniel Vetter02d71952012-08-09 16:44:54 +02006067 /* We only register the i915 ips part with intel-ips once everything is
6068 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006069 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006070 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006071 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006072
6073 ips_ping_for_i915_load();
6074}
6075
6076void intel_gpu_ips_teardown(void)
6077{
Daniel Vetter92703882012-08-09 16:46:01 +02006078 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006079 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006080 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006081}
Deepak S76c3552f2014-01-30 23:08:16 +05306082
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006083static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006084{
6085 struct drm_i915_private *dev_priv = dev->dev_private;
6086 u32 lcfuse;
6087 u8 pxw[16];
6088 int i;
6089
6090 /* Disable to program */
6091 I915_WRITE(ECR, 0);
6092 POSTING_READ(ECR);
6093
6094 /* Program energy weights for various events */
6095 I915_WRITE(SDEW, 0x15040d00);
6096 I915_WRITE(CSIEW0, 0x007f0000);
6097 I915_WRITE(CSIEW1, 0x1e220004);
6098 I915_WRITE(CSIEW2, 0x04000004);
6099
6100 for (i = 0; i < 5; i++)
6101 I915_WRITE(PEW + (i * 4), 0);
6102 for (i = 0; i < 3; i++)
6103 I915_WRITE(DEW + (i * 4), 0);
6104
6105 /* Program P-state weights to account for frequency power adjustment */
6106 for (i = 0; i < 16; i++) {
6107 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
6108 unsigned long freq = intel_pxfreq(pxvidfreq);
6109 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6110 PXVFREQ_PX_SHIFT;
6111 unsigned long val;
6112
6113 val = vid * vid;
6114 val *= (freq / 1000);
6115 val *= 255;
6116 val /= (127*127*900);
6117 if (val > 0xff)
6118 DRM_ERROR("bad pxval: %ld\n", val);
6119 pxw[i] = val;
6120 }
6121 /* Render standby states get 0 weight */
6122 pxw[14] = 0;
6123 pxw[15] = 0;
6124
6125 for (i = 0; i < 4; i++) {
6126 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6127 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6128 I915_WRITE(PXW + (i * 4), val);
6129 }
6130
6131 /* Adjust magic regs to magic values (more experimental results) */
6132 I915_WRITE(OGW0, 0);
6133 I915_WRITE(OGW1, 0);
6134 I915_WRITE(EG0, 0x00007f00);
6135 I915_WRITE(EG1, 0x0000000e);
6136 I915_WRITE(EG2, 0x000e0000);
6137 I915_WRITE(EG3, 0x68000300);
6138 I915_WRITE(EG4, 0x42000000);
6139 I915_WRITE(EG5, 0x00140031);
6140 I915_WRITE(EG6, 0);
6141 I915_WRITE(EG7, 0);
6142
6143 for (i = 0; i < 8; i++)
6144 I915_WRITE(PXWL + (i * 4), 0);
6145
6146 /* Enable PMON + select events */
6147 I915_WRITE(ECR, 0x80000019);
6148
6149 lcfuse = I915_READ(LCFUSE02);
6150
Daniel Vetter20e4d402012-08-08 23:35:39 +02006151 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006152}
6153
Imre Deakae484342014-03-31 15:10:44 +03006154void intel_init_gt_powersave(struct drm_device *dev)
6155{
Imre Deake6069ca2014-04-18 16:01:02 +03006156 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
6157
Deepak S38807742014-05-23 21:00:15 +05306158 if (IS_CHERRYVIEW(dev))
6159 cherryview_init_gt_powersave(dev);
6160 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006161 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03006162}
6163
6164void intel_cleanup_gt_powersave(struct drm_device *dev)
6165{
Deepak S38807742014-05-23 21:00:15 +05306166 if (IS_CHERRYVIEW(dev))
6167 return;
6168 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006169 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03006170}
6171
Imre Deakdbea3ce2014-12-15 18:59:28 +02006172static void gen6_suspend_rps(struct drm_device *dev)
6173{
6174 struct drm_i915_private *dev_priv = dev->dev_private;
6175
6176 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6177
Akash Goel4c2a8892015-03-06 11:07:24 +05306178 gen6_disable_rps_interrupts(dev);
Imre Deakdbea3ce2014-12-15 18:59:28 +02006179}
6180
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006181/**
6182 * intel_suspend_gt_powersave - suspend PM work and helper threads
6183 * @dev: drm device
6184 *
6185 * We don't want to disable RC6 or other features here, we just want
6186 * to make sure any work we've queued has finished and won't bother
6187 * us while we're suspended.
6188 */
6189void intel_suspend_gt_powersave(struct drm_device *dev)
6190{
6191 struct drm_i915_private *dev_priv = dev->dev_private;
6192
Imre Deakd4d70aa2014-11-19 15:30:04 +02006193 if (INTEL_INFO(dev)->gen < 6)
6194 return;
6195
Imre Deakdbea3ce2014-12-15 18:59:28 +02006196 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05306197
6198 /* Force GPU to min freq during suspend */
6199 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006200}
6201
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006202void intel_disable_gt_powersave(struct drm_device *dev)
6203{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006204 struct drm_i915_private *dev_priv = dev->dev_private;
6205
Daniel Vetter930ebb42012-06-29 23:32:16 +02006206 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006207 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05306208 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02006209 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03006210
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006211 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00006212 if (INTEL_INFO(dev)->gen >= 9)
6213 gen9_disable_rps(dev);
6214 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05306215 cherryview_disable_rps(dev);
6216 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006217 valleyview_disable_rps(dev);
6218 else
6219 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02006220
Chris Wilsonc0951f02013-10-10 21:58:50 +01006221 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006222 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006223 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006224}
6225
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006226static void intel_gen6_powersave_work(struct work_struct *work)
6227{
6228 struct drm_i915_private *dev_priv =
6229 container_of(work, struct drm_i915_private,
6230 rps.delayed_resume_work.work);
6231 struct drm_device *dev = dev_priv->dev;
6232
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006233 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006234
Akash Goel4c2a8892015-03-06 11:07:24 +05306235 gen6_reset_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02006236
Deepak S38807742014-05-23 21:00:15 +05306237 if (IS_CHERRYVIEW(dev)) {
6238 cherryview_enable_rps(dev);
6239 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07006240 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006241 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006242 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006243 gen9_enable_rps(dev);
Akash Goelcc017fb42015-06-29 14:50:21 +05306244 if (IS_SKYLAKE(dev))
6245 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006246 } else if (IS_BROADWELL(dev)) {
6247 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006248 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006249 } else {
6250 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006251 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006252 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006253
6254 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6255 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6256
6257 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6258 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6259
Chris Wilsonc0951f02013-10-10 21:58:50 +01006260 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02006261
Akash Goel4c2a8892015-03-06 11:07:24 +05306262 gen6_enable_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02006263
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006264 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03006265
6266 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006267}
6268
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006269void intel_enable_gt_powersave(struct drm_device *dev)
6270{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006271 struct drm_i915_private *dev_priv = dev->dev_private;
6272
Yu Zhangf61018b2015-02-10 19:05:52 +08006273 /* Powersaving is controlled by the host when inside a VM */
6274 if (intel_vgpu_active(dev))
6275 return;
6276
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006277 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03006278 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006279 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006280 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03006281 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05306282 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006283 /*
6284 * PCU communication is slow and this doesn't need to be
6285 * done at any specific time, so do this out of our fast path
6286 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03006287 *
6288 * We depend on the HW RC6 power context save/restore
6289 * mechanism when entering D3 through runtime PM suspend. So
6290 * disable RPM until RPS/RC6 is properly setup. We can only
6291 * get here via the driver load/system resume/runtime resume
6292 * paths, so the _noresume version is enough (and in case of
6293 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006294 */
Imre Deakc6df39b2014-04-14 20:24:29 +03006295 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6296 round_jiffies_up_relative(HZ)))
6297 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006298 }
6299}
6300
Imre Deakc6df39b2014-04-14 20:24:29 +03006301void intel_reset_gt_powersave(struct drm_device *dev)
6302{
6303 struct drm_i915_private *dev_priv = dev->dev_private;
6304
Imre Deakdbea3ce2014-12-15 18:59:28 +02006305 if (INTEL_INFO(dev)->gen < 6)
6306 return;
6307
6308 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03006309 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03006310}
6311
Daniel Vetter3107bd42012-10-31 22:52:31 +01006312static void ibx_init_clock_gating(struct drm_device *dev)
6313{
6314 struct drm_i915_private *dev_priv = dev->dev_private;
6315
6316 /*
6317 * On Ibex Peak and Cougar Point, we need to disable clock
6318 * gating for the panel power sequencer or it will fail to
6319 * start up when no ports are active.
6320 */
6321 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6322}
6323
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006324static void g4x_disable_trickle_feed(struct drm_device *dev)
6325{
6326 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006327 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006328
Damien Lespiau055e3932014-08-18 13:49:10 +01006329 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006330 I915_WRITE(DSPCNTR(pipe),
6331 I915_READ(DSPCNTR(pipe)) |
6332 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006333
6334 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6335 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006336 }
6337}
6338
Ville Syrjälä017636c2013-12-05 15:51:37 +02006339static void ilk_init_lp_watermarks(struct drm_device *dev)
6340{
6341 struct drm_i915_private *dev_priv = dev->dev_private;
6342
6343 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6344 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6345 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6346
6347 /*
6348 * Don't touch WM1S_LP_EN here.
6349 * Doing so could cause underruns.
6350 */
6351}
6352
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006353static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006354{
6355 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006356 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006357
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006358 /*
6359 * Required for FBC
6360 * WaFbcDisableDpfcClockGating:ilk
6361 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006362 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6363 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6364 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006365
6366 I915_WRITE(PCH_3DCGDIS0,
6367 MARIUNIT_CLOCK_GATE_DISABLE |
6368 SVSMUNIT_CLOCK_GATE_DISABLE);
6369 I915_WRITE(PCH_3DCGDIS1,
6370 VFMUNIT_CLOCK_GATE_DISABLE);
6371
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006372 /*
6373 * According to the spec the following bits should be set in
6374 * order to enable memory self-refresh
6375 * The bit 22/21 of 0x42004
6376 * The bit 5 of 0x42020
6377 * The bit 15 of 0x45000
6378 */
6379 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6380 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6381 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006382 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006383 I915_WRITE(DISP_ARB_CTL,
6384 (I915_READ(DISP_ARB_CTL) |
6385 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006386
6387 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006388
6389 /*
6390 * Based on the document from hardware guys the following bits
6391 * should be set unconditionally in order to enable FBC.
6392 * The bit 22 of 0x42000
6393 * The bit 22 of 0x42004
6394 * The bit 7,8,9 of 0x42020.
6395 */
6396 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006397 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006398 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6399 I915_READ(ILK_DISPLAY_CHICKEN1) |
6400 ILK_FBCQ_DIS);
6401 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6402 I915_READ(ILK_DISPLAY_CHICKEN2) |
6403 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006404 }
6405
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006406 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6407
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006408 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6409 I915_READ(ILK_DISPLAY_CHICKEN2) |
6410 ILK_ELPIN_409_SELECT);
6411 I915_WRITE(_3D_CHICKEN2,
6412 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6413 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006414
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006415 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006416 I915_WRITE(CACHE_MODE_0,
6417 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006418
Akash Goel4e046322014-04-04 17:14:38 +05306419 /* WaDisable_RenderCache_OperationalFlush:ilk */
6420 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6421
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006422 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006423
Daniel Vetter3107bd42012-10-31 22:52:31 +01006424 ibx_init_clock_gating(dev);
6425}
6426
6427static void cpt_init_clock_gating(struct drm_device *dev)
6428{
6429 struct drm_i915_private *dev_priv = dev->dev_private;
6430 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006431 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006432
6433 /*
6434 * On Ibex Peak and Cougar Point, we need to disable clock
6435 * gating for the panel power sequencer or it will fail to
6436 * start up when no ports are active.
6437 */
Jesse Barnescd664072013-10-02 10:34:19 -07006438 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6439 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6440 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006441 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6442 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006443 /* The below fixes the weird display corruption, a few pixels shifted
6444 * downward, on (only) LVDS of some HP laptops with IVY.
6445 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006446 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006447 val = I915_READ(TRANS_CHICKEN2(pipe));
6448 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6449 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006450 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006451 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006452 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6453 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6454 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006455 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6456 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006457 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006458 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006459 I915_WRITE(TRANS_CHICKEN1(pipe),
6460 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6461 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006462}
6463
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006464static void gen6_check_mch_setup(struct drm_device *dev)
6465{
6466 struct drm_i915_private *dev_priv = dev->dev_private;
6467 uint32_t tmp;
6468
6469 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006470 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6471 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6472 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006473}
6474
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006475static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006476{
6477 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006478 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006479
Damien Lespiau231e54f2012-10-19 17:55:41 +01006480 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006481
6482 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6483 I915_READ(ILK_DISPLAY_CHICKEN2) |
6484 ILK_ELPIN_409_SELECT);
6485
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006486 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006487 I915_WRITE(_3D_CHICKEN,
6488 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6489
Akash Goel4e046322014-04-04 17:14:38 +05306490 /* WaDisable_RenderCache_OperationalFlush:snb */
6491 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6492
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006493 /*
6494 * BSpec recoomends 8x4 when MSAA is used,
6495 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006496 *
6497 * Note that PS/WM thread counts depend on the WIZ hashing
6498 * disable bit, which we don't touch here, but it's good
6499 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006500 */
6501 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006502 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006503
Ville Syrjälä017636c2013-12-05 15:51:37 +02006504 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006505
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006506 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006507 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006508
6509 I915_WRITE(GEN6_UCGCTL1,
6510 I915_READ(GEN6_UCGCTL1) |
6511 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6512 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6513
6514 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6515 * gating disable must be set. Failure to set it results in
6516 * flickering pixels due to Z write ordering failures after
6517 * some amount of runtime in the Mesa "fire" demo, and Unigine
6518 * Sanctuary and Tropics, and apparently anything else with
6519 * alpha test or pixel discard.
6520 *
6521 * According to the spec, bit 11 (RCCUNIT) must also be set,
6522 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006523 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006524 * WaDisableRCCUnitClockGating:snb
6525 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006526 */
6527 I915_WRITE(GEN6_UCGCTL2,
6528 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6529 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6530
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006531 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006532 I915_WRITE(_3D_CHICKEN3,
6533 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006534
6535 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006536 * Bspec says:
6537 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6538 * 3DSTATE_SF number of SF output attributes is more than 16."
6539 */
6540 I915_WRITE(_3D_CHICKEN3,
6541 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6542
6543 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006544 * According to the spec the following bits should be
6545 * set in order to enable memory self-refresh and fbc:
6546 * The bit21 and bit22 of 0x42000
6547 * The bit21 and bit22 of 0x42004
6548 * The bit5 and bit7 of 0x42020
6549 * The bit14 of 0x70180
6550 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006551 *
6552 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006553 */
6554 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6555 I915_READ(ILK_DISPLAY_CHICKEN1) |
6556 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6557 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6558 I915_READ(ILK_DISPLAY_CHICKEN2) |
6559 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006560 I915_WRITE(ILK_DSPCLK_GATE_D,
6561 I915_READ(ILK_DSPCLK_GATE_D) |
6562 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6563 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006564
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006565 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006566
Daniel Vetter3107bd42012-10-31 22:52:31 +01006567 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006568
6569 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006570}
6571
6572static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6573{
6574 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6575
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006576 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006577 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006578 *
6579 * This actually overrides the dispatch
6580 * mode for all thread types.
6581 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006582 reg &= ~GEN7_FF_SCHED_MASK;
6583 reg |= GEN7_FF_TS_SCHED_HW;
6584 reg |= GEN7_FF_VS_SCHED_HW;
6585 reg |= GEN7_FF_DS_SCHED_HW;
6586
6587 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6588}
6589
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006590static void lpt_init_clock_gating(struct drm_device *dev)
6591{
6592 struct drm_i915_private *dev_priv = dev->dev_private;
6593
6594 /*
6595 * TODO: this bit should only be enabled when really needed, then
6596 * disabled when not needed anymore in order to save power.
6597 */
Ville Syrjäläc2699522015-08-27 23:55:59 +03006598 if (HAS_PCH_LPT_LP(dev))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006599 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6600 I915_READ(SOUTH_DSPCLK_GATE_D) |
6601 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006602
6603 /* WADPOClockGatingDisable:hsw */
6604 I915_WRITE(_TRANSA_CHICKEN1,
6605 I915_READ(_TRANSA_CHICKEN1) |
6606 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006607}
6608
Imre Deak7d708ee2013-04-17 14:04:50 +03006609static void lpt_suspend_hw(struct drm_device *dev)
6610{
6611 struct drm_i915_private *dev_priv = dev->dev_private;
6612
Ville Syrjäläc2699522015-08-27 23:55:59 +03006613 if (HAS_PCH_LPT_LP(dev)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03006614 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6615
6616 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6617 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6618 }
6619}
6620
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006621static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006622{
6623 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006624 enum pipe pipe;
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006625 uint32_t misccpctl;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006626
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03006627 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006628
Ben Widawskyab57fff2013-12-12 15:28:04 -08006629 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006630 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006631
Ben Widawskyab57fff2013-12-12 15:28:04 -08006632 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006633 I915_WRITE(CHICKEN_PAR1_1,
6634 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6635
Ben Widawskyab57fff2013-12-12 15:28:04 -08006636 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006637 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006638 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006639 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006640 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006641 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006642
Ben Widawskyab57fff2013-12-12 15:28:04 -08006643 /* WaVSRefCountFullforceMissDisable:bdw */
6644 /* WaDSRefCountFullforceMissDisable:bdw */
6645 I915_WRITE(GEN7_FF_THREAD_MODE,
6646 I915_READ(GEN7_FF_THREAD_MODE) &
6647 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006648
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006649 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6650 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006651
6652 /* WaDisableSDEUnitClockGating:bdw */
6653 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6654 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006655
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006656 /*
6657 * WaProgramL3SqcReg1Default:bdw
6658 * WaTempDisableDOPClkGating:bdw
6659 */
6660 misccpctl = I915_READ(GEN7_MISCCPCTL);
6661 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6662 I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
6663 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6664
Ville Syrjälä6d50b062015-05-19 20:32:57 +03006665 /*
6666 * WaGttCachingOffByDefault:bdw
6667 * GTT cache may not work with big pages, so if those
6668 * are ever enabled GTT cache may need to be disabled.
6669 */
6670 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6671
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006672 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006673}
6674
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006675static void haswell_init_clock_gating(struct drm_device *dev)
6676{
6677 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006678
Ville Syrjälä017636c2013-12-05 15:51:37 +02006679 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006680
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006681 /* L3 caching of data atomics doesn't work -- disable it. */
6682 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6683 I915_WRITE(HSW_ROW_CHICKEN3,
6684 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6685
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006686 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006687 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6688 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6689 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6690
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006691 /* WaVSRefCountFullforceMissDisable:hsw */
6692 I915_WRITE(GEN7_FF_THREAD_MODE,
6693 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006694
Akash Goel4e046322014-04-04 17:14:38 +05306695 /* WaDisable_RenderCache_OperationalFlush:hsw */
6696 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6697
Chia-I Wufe27c602014-01-28 13:29:33 +08006698 /* enable HiZ Raw Stall Optimization */
6699 I915_WRITE(CACHE_MODE_0_GEN7,
6700 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6701
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006702 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006703 I915_WRITE(CACHE_MODE_1,
6704 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006705
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006706 /*
6707 * BSpec recommends 8x4 when MSAA is used,
6708 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006709 *
6710 * Note that PS/WM thread counts depend on the WIZ hashing
6711 * disable bit, which we don't touch here, but it's good
6712 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006713 */
6714 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006715 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006716
Kenneth Graunke94411592014-12-31 16:23:00 -08006717 /* WaSampleCChickenBitEnable:hsw */
6718 I915_WRITE(HALF_SLICE_CHICKEN3,
6719 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6720
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006721 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006722 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6723
Paulo Zanoni90a88642013-05-03 17:23:45 -03006724 /* WaRsPkgCStateDisplayPMReq:hsw */
6725 I915_WRITE(CHICKEN_PAR1_1,
6726 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006727
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006728 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006729}
6730
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006731static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006732{
6733 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006734 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006735
Ville Syrjälä017636c2013-12-05 15:51:37 +02006736 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006737
Damien Lespiau231e54f2012-10-19 17:55:41 +01006738 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006739
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006740 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006741 I915_WRITE(_3D_CHICKEN3,
6742 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6743
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006744 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006745 I915_WRITE(IVB_CHICKEN3,
6746 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6747 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6748
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006749 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006750 if (IS_IVB_GT1(dev))
6751 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6752 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006753
Akash Goel4e046322014-04-04 17:14:38 +05306754 /* WaDisable_RenderCache_OperationalFlush:ivb */
6755 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6756
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006757 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006758 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6759 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6760
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006761 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006762 I915_WRITE(GEN7_L3CNTLREG1,
6763 GEN7_WA_FOR_GEN7_L3_CONTROL);
6764 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006765 GEN7_WA_L3_CHICKEN_MODE);
6766 if (IS_IVB_GT1(dev))
6767 I915_WRITE(GEN7_ROW_CHICKEN2,
6768 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006769 else {
6770 /* must write both registers */
6771 I915_WRITE(GEN7_ROW_CHICKEN2,
6772 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006773 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6774 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006775 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006776
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006777 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006778 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6779 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6780
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006781 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006782 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006783 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006784 */
6785 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006786 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006787
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006788 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006789 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6790 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6791 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6792
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006793 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006794
6795 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006796
Chris Wilson22721342014-03-04 09:41:43 +00006797 if (0) { /* causes HiZ corruption on ivb:gt1 */
6798 /* enable HiZ Raw Stall Optimization */
6799 I915_WRITE(CACHE_MODE_0_GEN7,
6800 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6801 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006802
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006803 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006804 I915_WRITE(CACHE_MODE_1,
6805 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006806
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006807 /*
6808 * BSpec recommends 8x4 when MSAA is used,
6809 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006810 *
6811 * Note that PS/WM thread counts depend on the WIZ hashing
6812 * disable bit, which we don't touch here, but it's good
6813 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006814 */
6815 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006816 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006817
Ben Widawsky20848222012-05-04 18:58:59 -07006818 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6819 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6820 snpcr |= GEN6_MBC_SNPCR_MED;
6821 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006822
Ben Widawskyab5c6082013-04-05 13:12:41 -07006823 if (!HAS_PCH_NOP(dev))
6824 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006825
6826 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006827}
6828
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006829static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6830{
6831 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6832
6833 /*
6834 * Disable trickle feed and enable pnd deadline calculation
6835 */
6836 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6837 I915_WRITE(CBR1_VLV, 0);
6838}
6839
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006840static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006841{
6842 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006843
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006844 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006845
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006846 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006847 I915_WRITE(_3D_CHICKEN3,
6848 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6849
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006850 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006851 I915_WRITE(IVB_CHICKEN3,
6852 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6853 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6854
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006855 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006856 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006857 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006858 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6859 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006860
Akash Goel4e046322014-04-04 17:14:38 +05306861 /* WaDisable_RenderCache_OperationalFlush:vlv */
6862 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6863
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006864 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006865 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6866 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6867
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006868 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006869 I915_WRITE(GEN7_ROW_CHICKEN2,
6870 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6871
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006872 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006873 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6874 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6875 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6876
Ville Syrjälä46680e02014-01-22 21:33:01 +02006877 gen7_setup_fixed_func_scheduler(dev_priv);
6878
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006879 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006880 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006881 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006882 */
6883 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006884 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006885
Akash Goelc98f5062014-03-24 23:00:07 +05306886 /* WaDisableL3Bank2xClockGate:vlv
6887 * Disabling L3 clock gating- MMIO 940c[25] = 1
6888 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6889 I915_WRITE(GEN7_UCGCTL4,
6890 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006891
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006892 /*
6893 * BSpec says this must be set, even though
6894 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6895 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006896 I915_WRITE(CACHE_MODE_1,
6897 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006898
6899 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006900 * BSpec recommends 8x4 when MSAA is used,
6901 * however in practice 16x4 seems fastest.
6902 *
6903 * Note that PS/WM thread counts depend on the WIZ hashing
6904 * disable bit, which we don't touch here, but it's good
6905 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6906 */
6907 I915_WRITE(GEN7_GT_MODE,
6908 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6909
6910 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006911 * WaIncreaseL3CreditsForVLVB0:vlv
6912 * This is the hardware default actually.
6913 */
6914 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6915
6916 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006917 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006918 * Disable clock gating on th GCFG unit to prevent a delay
6919 * in the reporting of vblank events.
6920 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006921 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006922}
6923
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006924static void cherryview_init_clock_gating(struct drm_device *dev)
6925{
6926 struct drm_i915_private *dev_priv = dev->dev_private;
6927
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006928 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006929
Ville Syrjälä232ce332014-04-09 13:28:35 +03006930 /* WaVSRefCountFullforceMissDisable:chv */
6931 /* WaDSRefCountFullforceMissDisable:chv */
6932 I915_WRITE(GEN7_FF_THREAD_MODE,
6933 I915_READ(GEN7_FF_THREAD_MODE) &
6934 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006935
6936 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6937 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6938 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006939
6940 /* WaDisableCSUnitClockGating:chv */
6941 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6942 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006943
6944 /* WaDisableSDEUnitClockGating:chv */
6945 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6946 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03006947
6948 /*
6949 * GTT cache may not work with big pages, so if those
6950 * are ever enabled GTT cache may need to be disabled.
6951 */
6952 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006953}
6954
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006955static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006956{
6957 struct drm_i915_private *dev_priv = dev->dev_private;
6958 uint32_t dspclk_gate;
6959
6960 I915_WRITE(RENCLK_GATE_D1, 0);
6961 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6962 GS_UNIT_CLOCK_GATE_DISABLE |
6963 CL_UNIT_CLOCK_GATE_DISABLE);
6964 I915_WRITE(RAMCLK_GATE_D, 0);
6965 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6966 OVRUNIT_CLOCK_GATE_DISABLE |
6967 OVCUNIT_CLOCK_GATE_DISABLE;
6968 if (IS_GM45(dev))
6969 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6970 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006971
6972 /* WaDisableRenderCachePipelinedFlush */
6973 I915_WRITE(CACHE_MODE_0,
6974 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006975
Akash Goel4e046322014-04-04 17:14:38 +05306976 /* WaDisable_RenderCache_OperationalFlush:g4x */
6977 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6978
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006979 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006980}
6981
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006982static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006983{
6984 struct drm_i915_private *dev_priv = dev->dev_private;
6985
6986 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6987 I915_WRITE(RENCLK_GATE_D2, 0);
6988 I915_WRITE(DSPCLK_GATE_D, 0);
6989 I915_WRITE(RAMCLK_GATE_D, 0);
6990 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006991 I915_WRITE(MI_ARB_STATE,
6992 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306993
6994 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6995 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006996}
6997
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006998static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006999{
7000 struct drm_i915_private *dev_priv = dev->dev_private;
7001
7002 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7003 I965_RCC_CLOCK_GATE_DISABLE |
7004 I965_RCPB_CLOCK_GATE_DISABLE |
7005 I965_ISC_CLOCK_GATE_DISABLE |
7006 I965_FBC_CLOCK_GATE_DISABLE);
7007 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007008 I915_WRITE(MI_ARB_STATE,
7009 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307010
7011 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7012 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007013}
7014
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007015static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007016{
7017 struct drm_i915_private *dev_priv = dev->dev_private;
7018 u32 dstate = I915_READ(D_STATE);
7019
7020 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7021 DSTATE_DOT_CLOCK_GATING;
7022 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007023
7024 if (IS_PINEVIEW(dev))
7025 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007026
7027 /* IIR "flip pending" means done if this bit is set */
7028 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007029
7030 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007031 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007032
7033 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7034 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007035
7036 I915_WRITE(MI_ARB_STATE,
7037 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007038}
7039
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007040static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007041{
7042 struct drm_i915_private *dev_priv = dev->dev_private;
7043
7044 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007045
7046 /* interrupts should cause a wake up from C3 */
7047 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7048 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007049
7050 I915_WRITE(MEM_MODE,
7051 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007052}
7053
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007054static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007055{
7056 struct drm_i915_private *dev_priv = dev->dev_private;
7057
7058 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007059
7060 I915_WRITE(MEM_MODE,
7061 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7062 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007063}
7064
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007065void intel_init_clock_gating(struct drm_device *dev)
7066{
7067 struct drm_i915_private *dev_priv = dev->dev_private;
7068
Damien Lespiauc57e3552015-02-09 19:33:05 +00007069 if (dev_priv->display.init_clock_gating)
7070 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007071}
7072
Imre Deak7d708ee2013-04-17 14:04:50 +03007073void intel_suspend_hw(struct drm_device *dev)
7074{
7075 if (HAS_PCH_LPT(dev))
7076 lpt_suspend_hw(dev);
7077}
7078
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007079/* Set up chip specific power management-related functions */
7080void intel_init_pm(struct drm_device *dev)
7081{
7082 struct drm_i915_private *dev_priv = dev->dev_private;
7083
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007084 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007085
Daniel Vetterc921aba2012-04-26 23:28:17 +02007086 /* For cxsr */
7087 if (IS_PINEVIEW(dev))
7088 i915_pineview_get_mem_freq(dev);
7089 else if (IS_GEN5(dev))
7090 i915_ironlake_get_mem_freq(dev);
7091
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007092 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007093 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007094 skl_setup_wm_latency(dev);
7095
Imre Deaka82abe42015-03-27 14:00:04 +02007096 if (IS_BROXTON(dev))
7097 dev_priv->display.init_clock_gating =
7098 bxt_init_clock_gating;
7099 else if (IS_SKYLAKE(dev))
7100 dev_priv->display.init_clock_gating =
7101 skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007102 dev_priv->display.update_wm = skl_update_wm;
7103 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05307104 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007105 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007106
Ville Syrjäläbd602542014-01-07 16:14:10 +02007107 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7108 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7109 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7110 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7111 dev_priv->display.update_wm = ilk_update_wm;
7112 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
7113 } else {
7114 DRM_DEBUG_KMS("Failed to read display plane latency. "
7115 "Disable CxSR\n");
7116 }
7117
7118 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007119 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007120 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007121 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007122 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007123 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007124 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007125 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007126 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03007127 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007128 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007129 vlv_setup_wm_latency(dev);
7130
7131 dev_priv->display.update_wm = vlv_update_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007132 dev_priv->display.init_clock_gating =
7133 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007134 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007135 vlv_setup_wm_latency(dev);
7136
7137 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007138 dev_priv->display.init_clock_gating =
7139 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007140 } else if (IS_PINEVIEW(dev)) {
7141 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7142 dev_priv->is_ddr3,
7143 dev_priv->fsb_freq,
7144 dev_priv->mem_freq)) {
7145 DRM_INFO("failed to find known CxSR latency "
7146 "(found ddr%s fsb freq %d, mem freq %d), "
7147 "disabling CxSR\n",
7148 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7149 dev_priv->fsb_freq, dev_priv->mem_freq);
7150 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007151 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007152 dev_priv->display.update_wm = NULL;
7153 } else
7154 dev_priv->display.update_wm = pineview_update_wm;
7155 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7156 } else if (IS_G4X(dev)) {
7157 dev_priv->display.update_wm = g4x_update_wm;
7158 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7159 } else if (IS_GEN4(dev)) {
7160 dev_priv->display.update_wm = i965_update_wm;
7161 if (IS_CRESTLINE(dev))
7162 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7163 else if (IS_BROADWATER(dev))
7164 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7165 } else if (IS_GEN3(dev)) {
7166 dev_priv->display.update_wm = i9xx_update_wm;
7167 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7168 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007169 } else if (IS_GEN2(dev)) {
7170 if (INTEL_INFO(dev)->num_pipes == 1) {
7171 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007172 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007173 } else {
7174 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007175 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007176 }
7177
7178 if (IS_I85X(dev) || IS_I865G(dev))
7179 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7180 else
7181 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7182 } else {
7183 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007184 }
7185}
7186
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007187int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007188{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007189 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007190
7191 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7192 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7193 return -EAGAIN;
7194 }
7195
7196 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00007197 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007198 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7199
7200 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7201 500)) {
7202 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7203 return -ETIMEDOUT;
7204 }
7205
7206 *val = I915_READ(GEN6_PCODE_DATA);
7207 I915_WRITE(GEN6_PCODE_DATA, 0);
7208
7209 return 0;
7210}
7211
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007212int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007213{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007214 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007215
7216 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7217 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7218 return -EAGAIN;
7219 }
7220
7221 I915_WRITE(GEN6_PCODE_DATA, val);
7222 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7223
7224 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7225 500)) {
7226 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7227 return -ETIMEDOUT;
7228 }
7229
7230 I915_WRITE(GEN6_PCODE_DATA, 0);
7231
7232 return 0;
7233}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007234
Ville Syrjälädd06f882014-11-10 22:55:12 +02007235static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007236{
Ville Syrjälädd06f882014-11-10 22:55:12 +02007237 switch (czclk_freq) {
7238 case 200:
7239 return 10;
7240 case 267:
7241 return 12;
7242 case 320:
7243 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02007244 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02007245 case 400:
7246 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007247 default:
7248 return -1;
7249 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02007250}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007251
Ville Syrjälädd06f882014-11-10 22:55:12 +02007252static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7253{
7254 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
7255
7256 div = vlv_gpu_freq_div(czclk_freq);
7257 if (div < 0)
7258 return div;
7259
7260 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007261}
7262
Fengguang Wub55dd642014-07-12 11:21:39 +02007263static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007264{
Ville Syrjälädd06f882014-11-10 22:55:12 +02007265 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007266
Ville Syrjälädd06f882014-11-10 22:55:12 +02007267 mul = vlv_gpu_freq_div(czclk_freq);
7268 if (mul < 0)
7269 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007270
Ville Syrjälädd06f882014-11-10 22:55:12 +02007271 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007272}
7273
Fengguang Wub55dd642014-07-12 11:21:39 +02007274static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307275{
Ville Syrjälädd06f882014-11-10 22:55:12 +02007276 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05307277
Ville Syrjälädd06f882014-11-10 22:55:12 +02007278 div = vlv_gpu_freq_div(czclk_freq) / 2;
7279 if (div < 0)
7280 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05307281
Ville Syrjälädd06f882014-11-10 22:55:12 +02007282 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307283}
7284
Fengguang Wub55dd642014-07-12 11:21:39 +02007285static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307286{
Ville Syrjälädd06f882014-11-10 22:55:12 +02007287 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05307288
Ville Syrjälädd06f882014-11-10 22:55:12 +02007289 mul = vlv_gpu_freq_div(czclk_freq) / 2;
7290 if (mul < 0)
7291 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05307292
Ville Syrjälä1c147622014-08-18 14:42:43 +03007293 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02007294 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307295}
7296
Ville Syrjälä616bc822015-01-23 21:04:25 +02007297int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7298{
Akash Goel80b6dda2015-03-06 11:07:15 +05307299 if (IS_GEN9(dev_priv->dev))
7300 return (val * GT_FREQUENCY_MULTIPLIER) / GEN9_FREQ_SCALER;
7301 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007302 return chv_gpu_freq(dev_priv, val);
7303 else if (IS_VALLEYVIEW(dev_priv->dev))
7304 return byt_gpu_freq(dev_priv, val);
7305 else
7306 return val * GT_FREQUENCY_MULTIPLIER;
7307}
7308
Ville Syrjälä616bc822015-01-23 21:04:25 +02007309int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7310{
Akash Goel80b6dda2015-03-06 11:07:15 +05307311 if (IS_GEN9(dev_priv->dev))
7312 return (val * GEN9_FREQ_SCALER) / GT_FREQUENCY_MULTIPLIER;
7313 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007314 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05307315 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007316 return byt_freq_opcode(dev_priv, val);
7317 else
7318 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05307319}
7320
Chris Wilson6ad790c2015-04-07 16:20:31 +01007321struct request_boost {
7322 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02007323 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007324};
7325
7326static void __intel_rps_boost_work(struct work_struct *work)
7327{
7328 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01007329 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007330
Chris Wilsone61b9952015-04-27 13:41:24 +01007331 if (!i915_gem_request_completed(req, true))
7332 gen6_rps_boost(to_i915(req->ring->dev), NULL,
7333 req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007334
Chris Wilsone61b9952015-04-27 13:41:24 +01007335 i915_gem_request_unreference__unlocked(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007336 kfree(boost);
7337}
7338
7339void intel_queue_rps_boost_for_request(struct drm_device *dev,
Daniel Vettereed29a52015-05-21 14:21:25 +02007340 struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007341{
7342 struct request_boost *boost;
7343
Daniel Vettereed29a52015-05-21 14:21:25 +02007344 if (req == NULL || INTEL_INFO(dev)->gen < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007345 return;
7346
Chris Wilsone61b9952015-04-27 13:41:24 +01007347 if (i915_gem_request_completed(req, true))
7348 return;
7349
Chris Wilson6ad790c2015-04-07 16:20:31 +01007350 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7351 if (boost == NULL)
7352 return;
7353
Daniel Vettereed29a52015-05-21 14:21:25 +02007354 i915_gem_request_reference(req);
7355 boost->req = req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007356
7357 INIT_WORK(&boost->work, __intel_rps_boost_work);
7358 queue_work(to_i915(dev)->wq, &boost->work);
7359}
7360
Daniel Vetterf742a552013-12-06 10:17:53 +01007361void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007362{
7363 struct drm_i915_private *dev_priv = dev->dev_private;
7364
Daniel Vetterf742a552013-12-06 10:17:53 +01007365 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01007366 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01007367
Chris Wilson907b28c2013-07-19 20:36:52 +01007368 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7369 intel_gen6_powersave_work);
Chris Wilson1854d5c2015-04-07 16:20:32 +01007370 INIT_LIST_HEAD(&dev_priv->rps.clients);
Chris Wilson2e1b8732015-04-27 13:41:22 +01007371 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7372 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007373
Paulo Zanoni33688d92014-03-07 20:08:19 -03007374 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01007375}