blob: 47246380675959456086a7c80fa762c96b9f181f [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawskydc39fff2013-10-18 12:32:07 -070034/**
35 * RC6 is a special power stage which allows the GPU to enter an very
36 * low-voltage mode when idle, using down to 0V while at this stage. This
37 * stage is entered automatically when the GPU is idle when RC6 support is
38 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
39 *
40 * There are different RC6 modes available in Intel GPU, which differentiate
41 * among each other with the latency required to enter and leave RC6 and
42 * voltage consumed by the GPU in different states.
43 *
44 * The combination of the following flags define which states GPU is allowed
45 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
46 * RC6pp is deepest RC6. Their support by hardware varies according to the
47 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
48 * which brings the most power savings; deeper states save more power, but
49 * require higher latency to switch to and wake up.
50 */
51#define INTEL_RC6_ENABLE (1<<0)
52#define INTEL_RC6p_ENABLE (1<<1)
53#define INTEL_RC6pp_ENABLE (1<<2)
54
Damien Lespiauda2078c2013-02-13 15:27:27 +000055static void gen9_init_clock_gating(struct drm_device *dev)
56{
Damien Lespiauacd5c342014-03-26 16:55:46 +000057 struct drm_i915_private *dev_priv = dev->dev_private;
58
Damien Lespiau77719d22015-02-09 19:33:13 +000059 /* WaEnableLbsSlaRetryTimerDecrement:skl */
60 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
61 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
62}
Damien Lespiau91e41d12014-03-26 17:42:50 +000063
Damien Lespiau45db2192015-02-09 19:33:09 +000064static void skl_init_clock_gating(struct drm_device *dev)
Damien Lespiauda2078c2013-02-13 15:27:27 +000065{
Damien Lespiauacd5c342014-03-26 16:55:46 +000066 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau3ca5da42014-03-26 18:18:01 +000067
Damien Lespiau77719d22015-02-09 19:33:13 +000068 gen9_init_clock_gating(dev);
69
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000070 if (INTEL_REVID(dev) == SKL_REVID_A0) {
71 /*
72 * WaDisableSDEUnitClockGating:skl
Damien Lespiau9253c2e2015-02-09 19:33:10 +000073 * WaSetGAPSunitClckGateDisable:skl
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000074 */
75 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Damien Lespiau9253c2e2015-02-09 19:33:10 +000076 GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000077 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
78 }
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000079
Damien Lespiau2caa3b22015-02-09 19:33:20 +000080 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
Damien Lespiau81e231a2015-02-09 19:33:19 +000081 /* WaDisableHDCInvalidation:skl */
82 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
83 BDW_DISABLE_HDC_INVALIDATION);
84
Damien Lespiau2caa3b22015-02-09 19:33:20 +000085 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
86 I915_WRITE(FF_SLICE_CS_CHICKEN2,
87 I915_READ(FF_SLICE_CS_CHICKEN2) |
88 GEN9_TSG_BARRIER_ACK_DISABLE);
89 }
Damien Lespiau81e231a2015-02-09 19:33:19 +000090
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000091 if (INTEL_REVID(dev) <= SKL_REVID_E0)
92 /* WaDisableLSQCROPERFforOCL:skl */
93 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
94 GEN8_LQSC_RO_PERF_DIS);
Damien Lespiauda2078c2013-02-13 15:27:27 +000095}
96
Imre Deaka82abe42015-03-27 14:00:04 +020097static void bxt_init_clock_gating(struct drm_device *dev)
98{
Imre Deak32608ca2015-03-11 11:10:27 +020099 struct drm_i915_private *dev_priv = dev->dev_private;
100
Imre Deaka82abe42015-03-27 14:00:04 +0200101 gen9_init_clock_gating(dev);
Imre Deak32608ca2015-03-11 11:10:27 +0200102
103 /*
104 * FIXME:
105 * GEN8_SDEUNIT_CLOCK_GATE_DISABLE applies on A0 only.
Ben Widawsky868434c2015-03-11 10:49:32 +0200106 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +0200107 */
108 /* WaDisableSDEUnitClockGating:bxt */
109 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +0200110 GEN8_SDEUNIT_CLOCK_GATE_DISABLE |
111 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deak32608ca2015-03-11 11:10:27 +0200112
Robert Beckette3a29052015-03-11 10:28:25 +0200113 /* FIXME: apply on A0 only */
114 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
Imre Deaka82abe42015-03-27 14:00:04 +0200115}
116
Daniel Vetterc921aba2012-04-26 23:28:17 +0200117static void i915_pineview_get_mem_freq(struct drm_device *dev)
118{
Jani Nikula50227e12014-03-31 14:27:21 +0300119 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200120 u32 tmp;
121
122 tmp = I915_READ(CLKCFG);
123
124 switch (tmp & CLKCFG_FSB_MASK) {
125 case CLKCFG_FSB_533:
126 dev_priv->fsb_freq = 533; /* 133*4 */
127 break;
128 case CLKCFG_FSB_800:
129 dev_priv->fsb_freq = 800; /* 200*4 */
130 break;
131 case CLKCFG_FSB_667:
132 dev_priv->fsb_freq = 667; /* 167*4 */
133 break;
134 case CLKCFG_FSB_400:
135 dev_priv->fsb_freq = 400; /* 100*4 */
136 break;
137 }
138
139 switch (tmp & CLKCFG_MEM_MASK) {
140 case CLKCFG_MEM_533:
141 dev_priv->mem_freq = 533;
142 break;
143 case CLKCFG_MEM_667:
144 dev_priv->mem_freq = 667;
145 break;
146 case CLKCFG_MEM_800:
147 dev_priv->mem_freq = 800;
148 break;
149 }
150
151 /* detect pineview DDR3 setting */
152 tmp = I915_READ(CSHRDDR3CTL);
153 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
154}
155
156static void i915_ironlake_get_mem_freq(struct drm_device *dev)
157{
Jani Nikula50227e12014-03-31 14:27:21 +0300158 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200159 u16 ddrpll, csipll;
160
161 ddrpll = I915_READ16(DDRMPLL1);
162 csipll = I915_READ16(CSIPLL0);
163
164 switch (ddrpll & 0xff) {
165 case 0xc:
166 dev_priv->mem_freq = 800;
167 break;
168 case 0x10:
169 dev_priv->mem_freq = 1066;
170 break;
171 case 0x14:
172 dev_priv->mem_freq = 1333;
173 break;
174 case 0x18:
175 dev_priv->mem_freq = 1600;
176 break;
177 default:
178 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
179 ddrpll & 0xff);
180 dev_priv->mem_freq = 0;
181 break;
182 }
183
Daniel Vetter20e4d402012-08-08 23:35:39 +0200184 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200185
186 switch (csipll & 0x3ff) {
187 case 0x00c:
188 dev_priv->fsb_freq = 3200;
189 break;
190 case 0x00e:
191 dev_priv->fsb_freq = 3733;
192 break;
193 case 0x010:
194 dev_priv->fsb_freq = 4266;
195 break;
196 case 0x012:
197 dev_priv->fsb_freq = 4800;
198 break;
199 case 0x014:
200 dev_priv->fsb_freq = 5333;
201 break;
202 case 0x016:
203 dev_priv->fsb_freq = 5866;
204 break;
205 case 0x018:
206 dev_priv->fsb_freq = 6400;
207 break;
208 default:
209 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
210 csipll & 0x3ff);
211 dev_priv->fsb_freq = 0;
212 break;
213 }
214
215 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200216 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200217 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200218 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200219 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200220 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200221 }
222}
223
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300224static const struct cxsr_latency cxsr_latency_table[] = {
225 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
226 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
227 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
228 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
229 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
230
231 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
232 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
233 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
234 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
235 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
236
237 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
238 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
239 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
240 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
241 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
242
243 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
244 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
245 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
246 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
247 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
248
249 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
250 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
251 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
252 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
253 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
254
255 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
256 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
257 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
258 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
259 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
260};
261
Daniel Vetter63c62272012-04-21 23:17:55 +0200262static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300263 int is_ddr3,
264 int fsb,
265 int mem)
266{
267 const struct cxsr_latency *latency;
268 int i;
269
270 if (fsb == 0 || mem == 0)
271 return NULL;
272
273 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
274 latency = &cxsr_latency_table[i];
275 if (is_desktop == latency->is_desktop &&
276 is_ddr3 == latency->is_ddr3 &&
277 fsb == latency->fsb_freq && mem == latency->mem_freq)
278 return latency;
279 }
280
281 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
282
283 return NULL;
284}
285
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200286static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
287{
288 u32 val;
289
290 mutex_lock(&dev_priv->rps.hw_lock);
291
292 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
293 if (enable)
294 val &= ~FORCE_DDR_HIGH_FREQ;
295 else
296 val |= FORCE_DDR_HIGH_FREQ;
297 val &= ~FORCE_DDR_LOW_FREQ;
298 val |= FORCE_DDR_FREQ_REQ_ACK;
299 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
300
301 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
302 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
303 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
304
305 mutex_unlock(&dev_priv->rps.hw_lock);
306}
307
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200308static void chv_set_memory_pm5(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_DSPFREQ);
315 if (enable)
316 val |= DSP_MAXFIFO_PM5_ENABLE;
317 else
318 val &= ~DSP_MAXFIFO_PM5_ENABLE;
319 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
320
321 mutex_unlock(&dev_priv->rps.hw_lock);
322}
323
Ville Syrjäläf4998962015-03-10 17:02:21 +0200324#define FW_WM(value, plane) \
325 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
326
Imre Deak5209b1f2014-07-01 12:36:17 +0300327void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300328{
Imre Deak5209b1f2014-07-01 12:36:17 +0300329 struct drm_device *dev = dev_priv->dev;
330 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300331
Imre Deak5209b1f2014-07-01 12:36:17 +0300332 if (IS_VALLEYVIEW(dev)) {
333 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200334 if (IS_CHERRYVIEW(dev))
335 chv_set_memory_pm5(dev_priv, enable);
Imre Deak5209b1f2014-07-01 12:36:17 +0300336 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
337 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
338 } else if (IS_PINEVIEW(dev)) {
339 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
340 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
341 I915_WRITE(DSPFW3, val);
342 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
343 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
344 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
345 I915_WRITE(FW_BLC_SELF, val);
346 } else if (IS_I915GM(dev)) {
347 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
348 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
349 I915_WRITE(INSTPM, val);
350 } else {
351 return;
352 }
353
354 DRM_DEBUG_KMS("memory self-refresh is %s\n",
355 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300356}
357
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200358
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300359/*
360 * Latency for FIFO fetches is dependent on several factors:
361 * - memory configuration (speed, channels)
362 * - chipset
363 * - current MCH state
364 * It can be fairly high in some situations, so here we assume a fairly
365 * pessimal value. It's a tradeoff between extra memory fetches (if we
366 * set this value too high, the FIFO will fetch frequently to stay full)
367 * and power consumption (set it too low to save power and we might see
368 * FIFO underruns and display "flicker").
369 *
370 * A value of 5us seems to be a good balance; safe for very low end
371 * platforms but not overly aggressive on lower latency configs.
372 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100373static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300374
Ville Syrjäläb5004722015-03-05 21:19:47 +0200375#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
376 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
377
378static int vlv_get_fifo_size(struct drm_device *dev,
379 enum pipe pipe, int plane)
380{
381 struct drm_i915_private *dev_priv = dev->dev_private;
382 int sprite0_start, sprite1_start, size;
383
384 switch (pipe) {
385 uint32_t dsparb, dsparb2, dsparb3;
386 case PIPE_A:
387 dsparb = I915_READ(DSPARB);
388 dsparb2 = I915_READ(DSPARB2);
389 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
390 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
391 break;
392 case PIPE_B:
393 dsparb = I915_READ(DSPARB);
394 dsparb2 = I915_READ(DSPARB2);
395 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
396 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
397 break;
398 case PIPE_C:
399 dsparb2 = I915_READ(DSPARB2);
400 dsparb3 = I915_READ(DSPARB3);
401 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
402 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
403 break;
404 default:
405 return 0;
406 }
407
408 switch (plane) {
409 case 0:
410 size = sprite0_start;
411 break;
412 case 1:
413 size = sprite1_start - sprite0_start;
414 break;
415 case 2:
416 size = 512 - 1 - sprite1_start;
417 break;
418 default:
419 return 0;
420 }
421
422 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
423 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
424 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
425 size);
426
427 return size;
428}
429
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300430static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300431{
432 struct drm_i915_private *dev_priv = dev->dev_private;
433 uint32_t dsparb = I915_READ(DSPARB);
434 int size;
435
436 size = dsparb & 0x7f;
437 if (plane)
438 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
439
440 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
441 plane ? "B" : "A", size);
442
443 return size;
444}
445
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200446static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300447{
448 struct drm_i915_private *dev_priv = dev->dev_private;
449 uint32_t dsparb = I915_READ(DSPARB);
450 int size;
451
452 size = dsparb & 0x1ff;
453 if (plane)
454 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
455 size >>= 1; /* Convert to cachelines */
456
457 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
458 plane ? "B" : "A", size);
459
460 return size;
461}
462
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300463static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300464{
465 struct drm_i915_private *dev_priv = dev->dev_private;
466 uint32_t dsparb = I915_READ(DSPARB);
467 int size;
468
469 size = dsparb & 0x7f;
470 size >>= 2; /* Convert to cachelines */
471
472 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
473 plane ? "B" : "A",
474 size);
475
476 return size;
477}
478
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300479/* Pineview has different values for various configs */
480static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300481 .fifo_size = PINEVIEW_DISPLAY_FIFO,
482 .max_wm = PINEVIEW_MAX_WM,
483 .default_wm = PINEVIEW_DFT_WM,
484 .guard_size = PINEVIEW_GUARD_WM,
485 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300486};
487static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300488 .fifo_size = PINEVIEW_DISPLAY_FIFO,
489 .max_wm = PINEVIEW_MAX_WM,
490 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
491 .guard_size = PINEVIEW_GUARD_WM,
492 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300493};
494static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300495 .fifo_size = PINEVIEW_CURSOR_FIFO,
496 .max_wm = PINEVIEW_CURSOR_MAX_WM,
497 .default_wm = PINEVIEW_CURSOR_DFT_WM,
498 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
499 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300500};
501static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300502 .fifo_size = PINEVIEW_CURSOR_FIFO,
503 .max_wm = PINEVIEW_CURSOR_MAX_WM,
504 .default_wm = PINEVIEW_CURSOR_DFT_WM,
505 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
506 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300507};
508static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300509 .fifo_size = G4X_FIFO_SIZE,
510 .max_wm = G4X_MAX_WM,
511 .default_wm = G4X_MAX_WM,
512 .guard_size = 2,
513 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300514};
515static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300516 .fifo_size = I965_CURSOR_FIFO,
517 .max_wm = I965_CURSOR_MAX_WM,
518 .default_wm = I965_CURSOR_DFT_WM,
519 .guard_size = 2,
520 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300521};
522static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300523 .fifo_size = VALLEYVIEW_FIFO_SIZE,
524 .max_wm = VALLEYVIEW_MAX_WM,
525 .default_wm = VALLEYVIEW_MAX_WM,
526 .guard_size = 2,
527 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300528};
529static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300530 .fifo_size = I965_CURSOR_FIFO,
531 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
532 .default_wm = I965_CURSOR_DFT_WM,
533 .guard_size = 2,
534 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300535};
536static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300537 .fifo_size = I965_CURSOR_FIFO,
538 .max_wm = I965_CURSOR_MAX_WM,
539 .default_wm = I965_CURSOR_DFT_WM,
540 .guard_size = 2,
541 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300542};
543static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300544 .fifo_size = I945_FIFO_SIZE,
545 .max_wm = I915_MAX_WM,
546 .default_wm = 1,
547 .guard_size = 2,
548 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300549};
550static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300551 .fifo_size = I915_FIFO_SIZE,
552 .max_wm = I915_MAX_WM,
553 .default_wm = 1,
554 .guard_size = 2,
555 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300556};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300557static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300558 .fifo_size = I855GM_FIFO_SIZE,
559 .max_wm = I915_MAX_WM,
560 .default_wm = 1,
561 .guard_size = 2,
562 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300563};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300564static const struct intel_watermark_params i830_bc_wm_info = {
565 .fifo_size = I855GM_FIFO_SIZE,
566 .max_wm = I915_MAX_WM/2,
567 .default_wm = 1,
568 .guard_size = 2,
569 .cacheline_size = I830_FIFO_LINE_SIZE,
570};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200571static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300572 .fifo_size = I830_FIFO_SIZE,
573 .max_wm = I915_MAX_WM,
574 .default_wm = 1,
575 .guard_size = 2,
576 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300577};
578
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579/**
580 * intel_calculate_wm - calculate watermark level
581 * @clock_in_khz: pixel clock
582 * @wm: chip FIFO params
583 * @pixel_size: display pixel size
584 * @latency_ns: memory latency for the platform
585 *
586 * Calculate the watermark level (the level at which the display plane will
587 * start fetching from memory again). Each chip has a different display
588 * FIFO size and allocation, so the caller needs to figure that out and pass
589 * in the correct intel_watermark_params structure.
590 *
591 * As the pixel clock runs, the FIFO will be drained at a rate that depends
592 * on the pixel size. When it reaches the watermark level, it'll start
593 * fetching FIFO line sized based chunks from memory until the FIFO fills
594 * past the watermark point. If the FIFO drains completely, a FIFO underrun
595 * will occur, and a display engine hang could result.
596 */
597static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
598 const struct intel_watermark_params *wm,
599 int fifo_size,
600 int pixel_size,
601 unsigned long latency_ns)
602{
603 long entries_required, wm_size;
604
605 /*
606 * Note: we need to make sure we don't overflow for various clock &
607 * latency values.
608 * clocks go from a few thousand to several hundred thousand.
609 * latency is usually a few thousand
610 */
611 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
612 1000;
613 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
614
615 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
616
617 wm_size = fifo_size - (entries_required + wm->guard_size);
618
619 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
620
621 /* Don't promote wm_size to unsigned... */
622 if (wm_size > (long)wm->max_wm)
623 wm_size = wm->max_wm;
624 if (wm_size <= 0)
625 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300626
627 /*
628 * Bspec seems to indicate that the value shouldn't be lower than
629 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
630 * Lets go for 8 which is the burst size since certain platforms
631 * already use a hardcoded 8 (which is what the spec says should be
632 * done).
633 */
634 if (wm_size <= 8)
635 wm_size = 8;
636
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300637 return wm_size;
638}
639
640static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
641{
642 struct drm_crtc *crtc, *enabled = NULL;
643
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100644 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000645 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300646 if (enabled)
647 return NULL;
648 enabled = crtc;
649 }
650 }
651
652 return enabled;
653}
654
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300655static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300656{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300657 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300658 struct drm_i915_private *dev_priv = dev->dev_private;
659 struct drm_crtc *crtc;
660 const struct cxsr_latency *latency;
661 u32 reg;
662 unsigned long wm;
663
664 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
665 dev_priv->fsb_freq, dev_priv->mem_freq);
666 if (!latency) {
667 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300668 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300669 return;
670 }
671
672 crtc = single_enabled_crtc(dev);
673 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100674 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800675 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100676 int clock;
677
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200678 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100679 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300680
681 /* Display SR */
682 wm = intel_calculate_wm(clock, &pineview_display_wm,
683 pineview_display_wm.fifo_size,
684 pixel_size, latency->display_sr);
685 reg = I915_READ(DSPFW1);
686 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200687 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300688 I915_WRITE(DSPFW1, reg);
689 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
690
691 /* cursor SR */
692 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
693 pineview_display_wm.fifo_size,
694 pixel_size, latency->cursor_sr);
695 reg = I915_READ(DSPFW3);
696 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200697 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300698 I915_WRITE(DSPFW3, reg);
699
700 /* Display HPLL off SR */
701 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
702 pineview_display_hplloff_wm.fifo_size,
703 pixel_size, latency->display_hpll_disable);
704 reg = I915_READ(DSPFW3);
705 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200706 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300707 I915_WRITE(DSPFW3, reg);
708
709 /* cursor HPLL off SR */
710 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
711 pineview_display_hplloff_wm.fifo_size,
712 pixel_size, latency->cursor_hpll_disable);
713 reg = I915_READ(DSPFW3);
714 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200715 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300716 I915_WRITE(DSPFW3, reg);
717 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
718
Imre Deak5209b1f2014-07-01 12:36:17 +0300719 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300720 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300721 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 }
723}
724
725static bool g4x_compute_wm0(struct drm_device *dev,
726 int plane,
727 const struct intel_watermark_params *display,
728 int display_latency_ns,
729 const struct intel_watermark_params *cursor,
730 int cursor_latency_ns,
731 int *plane_wm,
732 int *cursor_wm)
733{
734 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300735 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300736 int htotal, hdisplay, clock, pixel_size;
737 int line_time_us, line_count;
738 int entries, tlb_miss;
739
740 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000741 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300742 *cursor_wm = cursor->guard_size;
743 *plane_wm = display->guard_size;
744 return false;
745 }
746
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200747 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100748 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800749 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200750 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800751 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300752
753 /* Use the small buffer method to calculate plane watermark */
754 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
755 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
756 if (tlb_miss > 0)
757 entries += tlb_miss;
758 entries = DIV_ROUND_UP(entries, display->cacheline_size);
759 *plane_wm = entries + display->guard_size;
760 if (*plane_wm > (int)display->max_wm)
761 *plane_wm = display->max_wm;
762
763 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200764 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300765 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800766 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300767 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
768 if (tlb_miss > 0)
769 entries += tlb_miss;
770 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
771 *cursor_wm = entries + cursor->guard_size;
772 if (*cursor_wm > (int)cursor->max_wm)
773 *cursor_wm = (int)cursor->max_wm;
774
775 return true;
776}
777
778/*
779 * Check the wm result.
780 *
781 * If any calculated watermark values is larger than the maximum value that
782 * can be programmed into the associated watermark register, that watermark
783 * must be disabled.
784 */
785static bool g4x_check_srwm(struct drm_device *dev,
786 int display_wm, int cursor_wm,
787 const struct intel_watermark_params *display,
788 const struct intel_watermark_params *cursor)
789{
790 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
791 display_wm, cursor_wm);
792
793 if (display_wm > display->max_wm) {
794 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
795 display_wm, display->max_wm);
796 return false;
797 }
798
799 if (cursor_wm > cursor->max_wm) {
800 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
801 cursor_wm, cursor->max_wm);
802 return false;
803 }
804
805 if (!(display_wm || cursor_wm)) {
806 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
807 return false;
808 }
809
810 return true;
811}
812
813static bool g4x_compute_srwm(struct drm_device *dev,
814 int plane,
815 int latency_ns,
816 const struct intel_watermark_params *display,
817 const struct intel_watermark_params *cursor,
818 int *display_wm, int *cursor_wm)
819{
820 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300821 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300822 int hdisplay, htotal, pixel_size, clock;
823 unsigned long line_time_us;
824 int line_count, line_size;
825 int small, large;
826 int entries;
827
828 if (!latency_ns) {
829 *display_wm = *cursor_wm = 0;
830 return false;
831 }
832
833 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200834 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100835 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800836 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200837 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800838 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839
Ville Syrjälä922044c2014-02-14 14:18:57 +0200840 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300841 line_count = (latency_ns / line_time_us + 1000) / 1000;
842 line_size = hdisplay * pixel_size;
843
844 /* Use the minimum of the small and large buffer method for primary */
845 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
846 large = line_count * line_size;
847
848 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
849 *display_wm = entries + display->guard_size;
850
851 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800852 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300853 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
854 *cursor_wm = entries + cursor->guard_size;
855
856 return g4x_check_srwm(dev,
857 *display_wm, *cursor_wm,
858 display, cursor);
859}
860
Ville Syrjälä15665972015-03-10 16:16:28 +0200861#define FW_WM_VLV(value, plane) \
862 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
863
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200864static void vlv_write_wm_values(struct intel_crtc *crtc,
865 const struct vlv_wm_values *wm)
866{
867 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
868 enum pipe pipe = crtc->pipe;
869
870 I915_WRITE(VLV_DDL(pipe),
871 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
872 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
873 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
874 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
875
Ville Syrjäläae801522015-03-05 21:19:49 +0200876 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200877 FW_WM(wm->sr.plane, SR) |
878 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
879 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
880 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200881 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200882 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
883 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
884 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200885 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200886 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200887
888 if (IS_CHERRYVIEW(dev_priv)) {
889 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200890 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
891 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200892 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200893 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
894 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200895 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200896 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
897 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200898 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200899 FW_WM(wm->sr.plane >> 9, SR_HI) |
900 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
901 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
902 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
903 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
904 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
905 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
906 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
907 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
908 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200909 } else {
910 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200911 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
912 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200913 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200914 FW_WM(wm->sr.plane >> 9, SR_HI) |
915 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
916 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
917 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
918 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
919 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
920 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200921 }
922
923 POSTING_READ(DSPFW1);
924
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200925 dev_priv->wm.vlv = *wm;
926}
927
Ville Syrjälä15665972015-03-10 16:16:28 +0200928#undef FW_WM_VLV
929
Ville Syrjälä341c5262015-03-05 21:19:44 +0200930static uint8_t vlv_compute_drain_latency(struct drm_crtc *crtc,
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200931 struct drm_plane *plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300932{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700933 struct drm_device *dev = crtc->dev;
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200934 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
935 int entries, prec_mult, drain_latency, pixel_size;
936 int clock = intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä341c5262015-03-05 21:19:44 +0200937 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300938
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200939 /*
940 * FIXME the plane might have an fb
941 * but be invisible (eg. due to clipping)
942 */
943 if (!intel_crtc->active || !plane->state->fb)
944 return 0;
945
Gajanan Bhat0948c262014-08-07 01:58:24 +0530946 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200947 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300948
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200949 pixel_size = drm_format_plane_cpp(plane->state->fb->pixel_format, 0);
950
Gajanan Bhat0948c262014-08-07 01:58:24 +0530951 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200952 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300953
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530954 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200955
Ville Syrjälä341c5262015-03-05 21:19:44 +0200956 prec_mult = high_precision;
957 drain_latency = 64 * prec_mult * 4 / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300958
Ville Syrjälä341c5262015-03-05 21:19:44 +0200959 if (drain_latency > DRAIN_LATENCY_MASK) {
960 prec_mult /= 2;
961 drain_latency = 64 * prec_mult * 4 / entries;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200962 }
963
Ville Syrjälä341c5262015-03-05 21:19:44 +0200964 if (drain_latency > DRAIN_LATENCY_MASK)
965 drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300966
Ville Syrjälä341c5262015-03-05 21:19:44 +0200967 return drain_latency | (prec_mult == high_precision ?
968 DDL_PRECISION_HIGH : DDL_PRECISION_LOW);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300969}
970
Ville Syrjäläae801522015-03-05 21:19:49 +0200971static int vlv_compute_wm(struct intel_crtc *crtc,
972 struct intel_plane *plane,
973 int fifo_size)
974{
975 int clock, entries, pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300976
Ville Syrjäläae801522015-03-05 21:19:49 +0200977 /*
978 * FIXME the plane might have an fb
979 * but be invisible (eg. due to clipping)
980 */
981 if (!crtc->active || !plane->base.state->fb)
982 return 0;
983
984 pixel_size = drm_format_plane_cpp(plane->base.state->fb->pixel_format, 0);
985 clock = crtc->config->base.adjusted_mode.crtc_clock;
986
987 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
988
989 /*
990 * Set up the watermark such that we don't start issuing memory
991 * requests until we are within PND's max deadline value (256us).
992 * Idea being to be idle as long as possible while still taking
993 * advatange of PND's deadline scheduling. The limit of 8
994 * cachelines (used when the FIFO will anyway drain in less time
995 * than 256us) should match what we would be done if trickle
996 * feed were enabled.
997 */
998 return fifo_size - clamp(DIV_ROUND_UP(256 * entries, 64), 0, fifo_size - 8);
999}
1000
1001static bool vlv_compute_sr_wm(struct drm_device *dev,
1002 struct vlv_wm_values *wm)
1003{
1004 struct drm_i915_private *dev_priv = to_i915(dev);
1005 struct drm_crtc *crtc;
1006 enum pipe pipe = INVALID_PIPE;
1007 int num_planes = 0;
1008 int fifo_size = 0;
1009 struct intel_plane *plane;
1010
1011 wm->sr.cursor = wm->sr.plane = 0;
1012
1013 crtc = single_enabled_crtc(dev);
1014 /* maxfifo not supported on pipe C */
1015 if (crtc && to_intel_crtc(crtc)->pipe != PIPE_C) {
1016 pipe = to_intel_crtc(crtc)->pipe;
1017 num_planes = !!wm->pipe[pipe].primary +
1018 !!wm->pipe[pipe].sprite[0] +
1019 !!wm->pipe[pipe].sprite[1];
1020 fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
1021 }
1022
1023 if (fifo_size == 0 || num_planes > 1)
1024 return false;
1025
1026 wm->sr.cursor = vlv_compute_wm(to_intel_crtc(crtc),
1027 to_intel_plane(crtc->cursor), 0x3f);
1028
1029 list_for_each_entry(plane, &dev->mode_config.plane_list, base.head) {
1030 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1031 continue;
1032
1033 if (plane->pipe != pipe)
1034 continue;
1035
1036 wm->sr.plane = vlv_compute_wm(to_intel_crtc(crtc),
1037 plane, fifo_size);
1038 if (wm->sr.plane != 0)
1039 break;
1040 }
1041
1042 return true;
1043}
1044
1045static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001046{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -07001047 struct drm_device *dev = crtc->dev;
1048 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +05301049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301050 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläae801522015-03-05 21:19:49 +02001051 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001052 struct vlv_wm_values wm = dev_priv->wm.vlv;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001053
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001054 wm.ddl[pipe].primary = vlv_compute_drain_latency(crtc, crtc->primary);
Ville Syrjäläae801522015-03-05 21:19:49 +02001055 wm.pipe[pipe].primary = vlv_compute_wm(intel_crtc,
1056 to_intel_plane(crtc->primary),
1057 vlv_get_fifo_size(dev, pipe, 0));
1058
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001059 wm.ddl[pipe].cursor = vlv_compute_drain_latency(crtc, crtc->cursor);
Ville Syrjäläae801522015-03-05 21:19:49 +02001060 wm.pipe[pipe].cursor = vlv_compute_wm(intel_crtc,
1061 to_intel_plane(crtc->cursor),
1062 0x3f);
1063
1064 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1065
1066 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1067 return;
1068
1069 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1070 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1071 wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1072 wm.sr.plane, wm.sr.cursor);
1073
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +02001074 /*
1075 * FIXME DDR DVFS introduces massive memory latencies which
1076 * are not known to system agent so any deadline specified
1077 * by the display may not be respected. To support DDR DVFS
1078 * the watermark code needs to be rewritten to essentially
1079 * bypass deadline mechanism and rely solely on the
1080 * watermarks. For now disable DDR DVFS.
1081 */
1082 if (IS_CHERRYVIEW(dev_priv))
1083 chv_set_memory_dvfs(dev_priv, false);
1084
Ville Syrjäläae801522015-03-05 21:19:49 +02001085 if (!cxsr_enabled)
1086 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301087
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001088 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001089
1090 if (cxsr_enabled)
1091 intel_set_memory_cxsr(dev_priv, true);
1092}
1093
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301094static void valleyview_update_sprite_wm(struct drm_plane *plane,
1095 struct drm_crtc *crtc,
1096 uint32_t sprite_width,
1097 uint32_t sprite_height,
1098 int pixel_size,
1099 bool enabled, bool scaled)
1100{
1101 struct drm_device *dev = crtc->dev;
1102 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1104 enum pipe pipe = intel_crtc->pipe;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301105 int sprite = to_intel_plane(plane)->plane;
Ville Syrjäläae801522015-03-05 21:19:49 +02001106 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001107 struct vlv_wm_values wm = dev_priv->wm.vlv;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301108
Ville Syrjäläae801522015-03-05 21:19:49 +02001109 if (enabled) {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001110 wm.ddl[pipe].sprite[sprite] =
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001111 vlv_compute_drain_latency(crtc, plane);
Ville Syrjäläae801522015-03-05 21:19:49 +02001112
1113 wm.pipe[pipe].sprite[sprite] =
1114 vlv_compute_wm(intel_crtc,
1115 to_intel_plane(plane),
1116 vlv_get_fifo_size(dev, pipe, sprite+1));
1117 } else {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001118 wm.ddl[pipe].sprite[sprite] = 0;
Ville Syrjäläae801522015-03-05 21:19:49 +02001119 wm.pipe[pipe].sprite[sprite] = 0;
1120 }
1121
1122 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1123
1124 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1125 return;
1126
1127 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: sprite %c=%d, "
1128 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1129 sprite_name(pipe, sprite),
1130 wm.pipe[pipe].sprite[sprite],
1131 wm.sr.plane, wm.sr.cursor);
1132
1133 if (!cxsr_enabled)
1134 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301135
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001136 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjäläae801522015-03-05 21:19:49 +02001137
1138 if (cxsr_enabled)
1139 intel_set_memory_cxsr(dev_priv, true);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301140}
1141
Ville Syrjäläae801522015-03-05 21:19:49 +02001142#define single_plane_enabled(mask) is_power_of_2(mask)
1143
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001144static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001145{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001146 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001147 static const int sr_latency_ns = 12000;
1148 struct drm_i915_private *dev_priv = dev->dev_private;
1149 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1150 int plane_sr, cursor_sr;
1151 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001152 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001153
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001154 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001155 &g4x_wm_info, pessimal_latency_ns,
1156 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001157 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001158 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001159
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001160 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001161 &g4x_wm_info, pessimal_latency_ns,
1162 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001163 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001164 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001165
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001166 if (single_plane_enabled(enabled) &&
1167 g4x_compute_srwm(dev, ffs(enabled) - 1,
1168 sr_latency_ns,
1169 &g4x_wm_info,
1170 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001171 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001172 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001173 } else {
Imre Deak98584252014-06-13 14:54:20 +03001174 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001175 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001176 plane_sr = cursor_sr = 0;
1177 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001178
Ville Syrjäläa5043452014-06-28 02:04:18 +03001179 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1180 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001181 planea_wm, cursora_wm,
1182 planeb_wm, cursorb_wm,
1183 plane_sr, cursor_sr);
1184
1185 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001186 FW_WM(plane_sr, SR) |
1187 FW_WM(cursorb_wm, CURSORB) |
1188 FW_WM(planeb_wm, PLANEB) |
1189 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001190 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001191 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001192 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001193 /* HPLL off in SR has some issues on G4x... disable it */
1194 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001195 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001196 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001197
1198 if (cxsr_enabled)
1199 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001200}
1201
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001202static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001203{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001204 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001205 struct drm_i915_private *dev_priv = dev->dev_private;
1206 struct drm_crtc *crtc;
1207 int srwm = 1;
1208 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001209 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001210
1211 /* Calc sr entries for one plane configs */
1212 crtc = single_enabled_crtc(dev);
1213 if (crtc) {
1214 /* self-refresh has much higher latency */
1215 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001216 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001217 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001218 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001219 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001220 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001221 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001222 unsigned long line_time_us;
1223 int entries;
1224
Ville Syrjälä922044c2014-02-14 14:18:57 +02001225 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001226
1227 /* Use ns/us then divide to preserve precision */
1228 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1229 pixel_size * hdisplay;
1230 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1231 srwm = I965_FIFO_SIZE - entries;
1232 if (srwm < 0)
1233 srwm = 1;
1234 srwm &= 0x1ff;
1235 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1236 entries, srwm);
1237
1238 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001239 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001240 entries = DIV_ROUND_UP(entries,
1241 i965_cursor_wm_info.cacheline_size);
1242 cursor_sr = i965_cursor_wm_info.fifo_size -
1243 (entries + i965_cursor_wm_info.guard_size);
1244
1245 if (cursor_sr > i965_cursor_wm_info.max_wm)
1246 cursor_sr = i965_cursor_wm_info.max_wm;
1247
1248 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1249 "cursor %d\n", srwm, cursor_sr);
1250
Imre Deak98584252014-06-13 14:54:20 +03001251 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001252 } else {
Imre Deak98584252014-06-13 14:54:20 +03001253 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001254 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001255 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001256 }
1257
1258 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1259 srwm);
1260
1261 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001262 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1263 FW_WM(8, CURSORB) |
1264 FW_WM(8, PLANEB) |
1265 FW_WM(8, PLANEA));
1266 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1267 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001268 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001269 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001270
1271 if (cxsr_enabled)
1272 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001273}
1274
Ville Syrjäläf4998962015-03-10 17:02:21 +02001275#undef FW_WM
1276
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001277static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001278{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001279 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001280 struct drm_i915_private *dev_priv = dev->dev_private;
1281 const struct intel_watermark_params *wm_info;
1282 uint32_t fwater_lo;
1283 uint32_t fwater_hi;
1284 int cwm, srwm = 1;
1285 int fifo_size;
1286 int planea_wm, planeb_wm;
1287 struct drm_crtc *crtc, *enabled = NULL;
1288
1289 if (IS_I945GM(dev))
1290 wm_info = &i945_wm_info;
1291 else if (!IS_GEN2(dev))
1292 wm_info = &i915_wm_info;
1293 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001294 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001295
1296 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1297 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001298 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001299 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001300 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001301 if (IS_GEN2(dev))
1302 cpp = 4;
1303
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001304 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001305 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001306 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001307 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001308 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001309 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001310 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001311 if (planea_wm > (long)wm_info->max_wm)
1312 planea_wm = wm_info->max_wm;
1313 }
1314
1315 if (IS_GEN2(dev))
1316 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001317
1318 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1319 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001320 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001321 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001322 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001323 if (IS_GEN2(dev))
1324 cpp = 4;
1325
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001326 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001327 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001328 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001329 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001330 if (enabled == NULL)
1331 enabled = crtc;
1332 else
1333 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001334 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001335 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001336 if (planeb_wm > (long)wm_info->max_wm)
1337 planeb_wm = wm_info->max_wm;
1338 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001339
1340 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1341
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001342 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001343 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001344
Matt Roper59bea882015-02-27 10:12:01 -08001345 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001346
1347 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001348 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001349 enabled = NULL;
1350 }
1351
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001352 /*
1353 * Overlay gets an aggressive default since video jitter is bad.
1354 */
1355 cwm = 2;
1356
1357 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001358 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001359
1360 /* Calc sr entries for one plane configs */
1361 if (HAS_FW_BLC(dev) && enabled) {
1362 /* self-refresh has much higher latency */
1363 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001364 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001365 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001366 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001367 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001368 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001369 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001370 unsigned long line_time_us;
1371 int entries;
1372
Ville Syrjälä922044c2014-02-14 14:18:57 +02001373 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374
1375 /* Use ns/us then divide to preserve precision */
1376 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1377 pixel_size * hdisplay;
1378 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1379 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1380 srwm = wm_info->fifo_size - entries;
1381 if (srwm < 0)
1382 srwm = 1;
1383
1384 if (IS_I945G(dev) || IS_I945GM(dev))
1385 I915_WRITE(FW_BLC_SELF,
1386 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1387 else if (IS_I915GM(dev))
1388 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1389 }
1390
1391 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1392 planea_wm, planeb_wm, cwm, srwm);
1393
1394 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1395 fwater_hi = (cwm & 0x1f);
1396
1397 /* Set request length to 8 cachelines per fetch */
1398 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1399 fwater_hi = fwater_hi | (1 << 8);
1400
1401 I915_WRITE(FW_BLC, fwater_lo);
1402 I915_WRITE(FW_BLC2, fwater_hi);
1403
Imre Deak5209b1f2014-07-01 12:36:17 +03001404 if (enabled)
1405 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001406}
1407
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001408static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001409{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001410 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001411 struct drm_i915_private *dev_priv = dev->dev_private;
1412 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001413 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001414 uint32_t fwater_lo;
1415 int planea_wm;
1416
1417 crtc = single_enabled_crtc(dev);
1418 if (crtc == NULL)
1419 return;
1420
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001421 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001422 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001423 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001424 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001425 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001426 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1427 fwater_lo |= (3<<8) | planea_wm;
1428
1429 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1430
1431 I915_WRITE(FW_BLC, fwater_lo);
1432}
1433
Ville Syrjälä36587292013-07-05 11:57:16 +03001434static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1435 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001436{
1437 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001438 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001439
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001440 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001441
1442 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1443 * adjust the pixel_rate here. */
1444
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001445 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001446 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001447 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001448
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001449 pipe_w = intel_crtc->config->pipe_src_w;
1450 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001451 pfit_w = (pfit_size >> 16) & 0xFFFF;
1452 pfit_h = pfit_size & 0xFFFF;
1453 if (pipe_w < pfit_w)
1454 pipe_w = pfit_w;
1455 if (pipe_h < pfit_h)
1456 pipe_h = pfit_h;
1457
1458 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1459 pfit_w * pfit_h);
1460 }
1461
1462 return pixel_rate;
1463}
1464
Ville Syrjälä37126462013-08-01 16:18:55 +03001465/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001466static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001467 uint32_t latency)
1468{
1469 uint64_t ret;
1470
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001471 if (WARN(latency == 0, "Latency value missing\n"))
1472 return UINT_MAX;
1473
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001474 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1475 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1476
1477 return ret;
1478}
1479
Ville Syrjälä37126462013-08-01 16:18:55 +03001480/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001481static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001482 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1483 uint32_t latency)
1484{
1485 uint32_t ret;
1486
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001487 if (WARN(latency == 0, "Latency value missing\n"))
1488 return UINT_MAX;
1489
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001490 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1491 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1492 ret = DIV_ROUND_UP(ret, 64) + 2;
1493 return ret;
1494}
1495
Ville Syrjälä23297042013-07-05 11:57:17 +03001496static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001497 uint8_t bytes_per_pixel)
1498{
1499 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1500}
1501
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001502struct skl_pipe_wm_parameters {
1503 bool active;
1504 uint32_t pipe_htotal;
1505 uint32_t pixel_rate; /* in KHz */
1506 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1507 struct intel_plane_wm_parameters cursor;
1508};
1509
Imre Deak820c1982013-12-17 14:46:36 +02001510struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001511 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001512 uint32_t pipe_htotal;
1513 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001514 struct intel_plane_wm_parameters pri;
1515 struct intel_plane_wm_parameters spr;
1516 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001517};
1518
Imre Deak820c1982013-12-17 14:46:36 +02001519struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001520 uint16_t pri;
1521 uint16_t spr;
1522 uint16_t cur;
1523 uint16_t fbc;
1524};
1525
Ville Syrjälä240264f2013-08-07 13:29:12 +03001526/* used in computing the new watermarks state */
1527struct intel_wm_config {
1528 unsigned int num_pipes_active;
1529 bool sprites_enabled;
1530 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001531};
1532
Ville Syrjälä37126462013-08-01 16:18:55 +03001533/*
1534 * For both WM_PIPE and WM_LP.
1535 * mem_value must be in 0.1us units.
1536 */
Imre Deak820c1982013-12-17 14:46:36 +02001537static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001538 uint32_t mem_value,
1539 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001540{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001541 uint32_t method1, method2;
1542
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001543 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001544 return 0;
1545
Ville Syrjälä23297042013-07-05 11:57:17 +03001546 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001547 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001548 mem_value);
1549
1550 if (!is_lp)
1551 return method1;
1552
Ville Syrjälä23297042013-07-05 11:57:17 +03001553 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001554 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001555 params->pri.horiz_pixels,
1556 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001557 mem_value);
1558
1559 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001560}
1561
Ville Syrjälä37126462013-08-01 16:18:55 +03001562/*
1563 * For both WM_PIPE and WM_LP.
1564 * mem_value must be in 0.1us units.
1565 */
Imre Deak820c1982013-12-17 14:46:36 +02001566static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001567 uint32_t mem_value)
1568{
1569 uint32_t method1, method2;
1570
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001571 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001572 return 0;
1573
Ville Syrjälä23297042013-07-05 11:57:17 +03001574 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001575 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001576 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001577 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001578 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001579 params->spr.horiz_pixels,
1580 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001581 mem_value);
1582 return min(method1, method2);
1583}
1584
Ville Syrjälä37126462013-08-01 16:18:55 +03001585/*
1586 * For both WM_PIPE and WM_LP.
1587 * mem_value must be in 0.1us units.
1588 */
Imre Deak820c1982013-12-17 14:46:36 +02001589static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001590 uint32_t mem_value)
1591{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001592 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001593 return 0;
1594
Ville Syrjälä23297042013-07-05 11:57:17 +03001595 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001596 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001597 params->cur.horiz_pixels,
1598 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001599 mem_value);
1600}
1601
Paulo Zanonicca32e92013-05-31 11:45:06 -03001602/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001603static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001604 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001605{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001606 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001607 return 0;
1608
Ville Syrjälä23297042013-07-05 11:57:17 +03001609 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001610 params->pri.horiz_pixels,
1611 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001612}
1613
Ville Syrjälä158ae642013-08-07 13:28:19 +03001614static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1615{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001616 if (INTEL_INFO(dev)->gen >= 8)
1617 return 3072;
1618 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001619 return 768;
1620 else
1621 return 512;
1622}
1623
Ville Syrjälä4e975082014-03-07 18:32:11 +02001624static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1625 int level, bool is_sprite)
1626{
1627 if (INTEL_INFO(dev)->gen >= 8)
1628 /* BDW primary/sprite plane watermarks */
1629 return level == 0 ? 255 : 2047;
1630 else if (INTEL_INFO(dev)->gen >= 7)
1631 /* IVB/HSW primary/sprite plane watermarks */
1632 return level == 0 ? 127 : 1023;
1633 else if (!is_sprite)
1634 /* ILK/SNB primary plane watermarks */
1635 return level == 0 ? 127 : 511;
1636 else
1637 /* ILK/SNB sprite plane watermarks */
1638 return level == 0 ? 63 : 255;
1639}
1640
1641static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1642 int level)
1643{
1644 if (INTEL_INFO(dev)->gen >= 7)
1645 return level == 0 ? 63 : 255;
1646 else
1647 return level == 0 ? 31 : 63;
1648}
1649
1650static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1651{
1652 if (INTEL_INFO(dev)->gen >= 8)
1653 return 31;
1654 else
1655 return 15;
1656}
1657
Ville Syrjälä158ae642013-08-07 13:28:19 +03001658/* Calculate the maximum primary/sprite plane watermark */
1659static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1660 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001661 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001662 enum intel_ddb_partitioning ddb_partitioning,
1663 bool is_sprite)
1664{
1665 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001666
1667 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001668 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001669 return 0;
1670
1671 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001672 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001673 fifo_size /= INTEL_INFO(dev)->num_pipes;
1674
1675 /*
1676 * For some reason the non self refresh
1677 * FIFO size is only half of the self
1678 * refresh FIFO size on ILK/SNB.
1679 */
1680 if (INTEL_INFO(dev)->gen <= 6)
1681 fifo_size /= 2;
1682 }
1683
Ville Syrjälä240264f2013-08-07 13:29:12 +03001684 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001685 /* level 0 is always calculated with 1:1 split */
1686 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1687 if (is_sprite)
1688 fifo_size *= 5;
1689 fifo_size /= 6;
1690 } else {
1691 fifo_size /= 2;
1692 }
1693 }
1694
1695 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001696 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001697}
1698
1699/* Calculate the maximum cursor plane watermark */
1700static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001701 int level,
1702 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001703{
1704 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001705 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001706 return 64;
1707
1708 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001709 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001710}
1711
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001712static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001713 int level,
1714 const struct intel_wm_config *config,
1715 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001716 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001717{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001718 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1719 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1720 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001721 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001722}
1723
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001724static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1725 int level,
1726 struct ilk_wm_maximums *max)
1727{
1728 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1729 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1730 max->cur = ilk_cursor_wm_reg_max(dev, level);
1731 max->fbc = ilk_fbc_wm_reg_max(dev);
1732}
1733
Ville Syrjäläd9395652013-10-09 19:18:10 +03001734static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001735 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001736 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001737{
1738 bool ret;
1739
1740 /* already determined to be invalid? */
1741 if (!result->enable)
1742 return false;
1743
1744 result->enable = result->pri_val <= max->pri &&
1745 result->spr_val <= max->spr &&
1746 result->cur_val <= max->cur;
1747
1748 ret = result->enable;
1749
1750 /*
1751 * HACK until we can pre-compute everything,
1752 * and thus fail gracefully if LP0 watermarks
1753 * are exceeded...
1754 */
1755 if (level == 0 && !result->enable) {
1756 if (result->pri_val > max->pri)
1757 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1758 level, result->pri_val, max->pri);
1759 if (result->spr_val > max->spr)
1760 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1761 level, result->spr_val, max->spr);
1762 if (result->cur_val > max->cur)
1763 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1764 level, result->cur_val, max->cur);
1765
1766 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1767 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1768 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1769 result->enable = true;
1770 }
1771
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001772 return ret;
1773}
1774
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001775static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001776 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001777 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001778 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001779{
1780 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1781 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1782 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1783
1784 /* WM1+ latency values stored in 0.5us units */
1785 if (level > 0) {
1786 pri_latency *= 5;
1787 spr_latency *= 5;
1788 cur_latency *= 5;
1789 }
1790
1791 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1792 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1793 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1794 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1795 result->enable = true;
1796}
1797
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001798static uint32_t
1799hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001800{
1801 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001803 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001804 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001805
Matt Roper3ef00282015-03-09 10:19:24 -07001806 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001807 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001808
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001809 /* The WM are computed with base on how long it takes to fill a single
1810 * row at the given clock rate, multiplied by 8.
1811 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001812 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1813 mode->crtc_clock);
1814 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001815 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001816
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001817 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1818 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001819}
1820
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001821static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001822{
1823 struct drm_i915_private *dev_priv = dev->dev_private;
1824
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001825 if (IS_GEN9(dev)) {
1826 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001827 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001828 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001829
1830 /* read the first set of memory latencies[0:3] */
1831 val = 0; /* data0 to be programmed to 0 for first set */
1832 mutex_lock(&dev_priv->rps.hw_lock);
1833 ret = sandybridge_pcode_read(dev_priv,
1834 GEN9_PCODE_READ_MEM_LATENCY,
1835 &val);
1836 mutex_unlock(&dev_priv->rps.hw_lock);
1837
1838 if (ret) {
1839 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1840 return;
1841 }
1842
1843 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1844 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1845 GEN9_MEM_LATENCY_LEVEL_MASK;
1846 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1847 GEN9_MEM_LATENCY_LEVEL_MASK;
1848 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1849 GEN9_MEM_LATENCY_LEVEL_MASK;
1850
1851 /* read the second set of memory latencies[4:7] */
1852 val = 1; /* data0 to be programmed to 1 for second set */
1853 mutex_lock(&dev_priv->rps.hw_lock);
1854 ret = sandybridge_pcode_read(dev_priv,
1855 GEN9_PCODE_READ_MEM_LATENCY,
1856 &val);
1857 mutex_unlock(&dev_priv->rps.hw_lock);
1858 if (ret) {
1859 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1860 return;
1861 }
1862
1863 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1864 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1865 GEN9_MEM_LATENCY_LEVEL_MASK;
1866 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1867 GEN9_MEM_LATENCY_LEVEL_MASK;
1868 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1869 GEN9_MEM_LATENCY_LEVEL_MASK;
1870
Vandana Kannan367294b2014-11-04 17:06:46 +00001871 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001872 * WaWmMemoryReadLatency:skl
1873 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001874 * punit doesn't take into account the read latency so we need
1875 * to add 2us to the various latency levels we retrieve from
1876 * the punit.
1877 * - W0 is a bit special in that it's the only level that
1878 * can't be disabled if we want to have display working, so
1879 * we always add 2us there.
1880 * - For levels >=1, punit returns 0us latency when they are
1881 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001882 *
1883 * Additionally, if a level n (n > 1) has a 0us latency, all
1884 * levels m (m >= n) need to be disabled. We make sure to
1885 * sanitize the values out of the punit to satisfy this
1886 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001887 */
1888 wm[0] += 2;
1889 for (level = 1; level <= max_level; level++)
1890 if (wm[level] != 0)
1891 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001892 else {
1893 for (i = level + 1; i <= max_level; i++)
1894 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001895
Vandana Kannan4f947382014-11-04 17:06:47 +00001896 break;
1897 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001898 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001899 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1900
1901 wm[0] = (sskpd >> 56) & 0xFF;
1902 if (wm[0] == 0)
1903 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001904 wm[1] = (sskpd >> 4) & 0xFF;
1905 wm[2] = (sskpd >> 12) & 0xFF;
1906 wm[3] = (sskpd >> 20) & 0x1FF;
1907 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001908 } else if (INTEL_INFO(dev)->gen >= 6) {
1909 uint32_t sskpd = I915_READ(MCH_SSKPD);
1910
1911 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1912 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1913 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1914 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001915 } else if (INTEL_INFO(dev)->gen >= 5) {
1916 uint32_t mltr = I915_READ(MLTR_ILK);
1917
1918 /* ILK primary LP0 latency is 700 ns */
1919 wm[0] = 7;
1920 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1921 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001922 }
1923}
1924
Ville Syrjälä53615a52013-08-01 16:18:50 +03001925static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1926{
1927 /* ILK sprite LP0 latency is 1300 ns */
1928 if (INTEL_INFO(dev)->gen == 5)
1929 wm[0] = 13;
1930}
1931
1932static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1933{
1934 /* ILK cursor LP0 latency is 1300 ns */
1935 if (INTEL_INFO(dev)->gen == 5)
1936 wm[0] = 13;
1937
1938 /* WaDoubleCursorLP3Latency:ivb */
1939 if (IS_IVYBRIDGE(dev))
1940 wm[3] *= 2;
1941}
1942
Damien Lespiau546c81f2014-05-13 15:30:26 +01001943int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001944{
1945 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001946 if (IS_GEN9(dev))
1947 return 7;
1948 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001949 return 4;
1950 else if (INTEL_INFO(dev)->gen >= 6)
1951 return 3;
1952 else
1953 return 2;
1954}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001955
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001956static void intel_print_wm_latency(struct drm_device *dev,
1957 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001958 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001959{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001960 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001961
1962 for (level = 0; level <= max_level; level++) {
1963 unsigned int latency = wm[level];
1964
1965 if (latency == 0) {
1966 DRM_ERROR("%s WM%d latency not provided\n",
1967 name, level);
1968 continue;
1969 }
1970
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001971 /*
1972 * - latencies are in us on gen9.
1973 * - before then, WM1+ latency values are in 0.5us units
1974 */
1975 if (IS_GEN9(dev))
1976 latency *= 10;
1977 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001978 latency *= 5;
1979
1980 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1981 name, level, wm[level],
1982 latency / 10, latency % 10);
1983 }
1984}
1985
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001986static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1987 uint16_t wm[5], uint16_t min)
1988{
1989 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1990
1991 if (wm[0] >= min)
1992 return false;
1993
1994 wm[0] = max(wm[0], min);
1995 for (level = 1; level <= max_level; level++)
1996 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1997
1998 return true;
1999}
2000
2001static void snb_wm_latency_quirk(struct drm_device *dev)
2002{
2003 struct drm_i915_private *dev_priv = dev->dev_private;
2004 bool changed;
2005
2006 /*
2007 * The BIOS provided WM memory latency values are often
2008 * inadequate for high resolution displays. Adjust them.
2009 */
2010 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2011 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2012 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2013
2014 if (!changed)
2015 return;
2016
2017 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2018 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2019 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2020 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2021}
2022
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002023static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002024{
2025 struct drm_i915_private *dev_priv = dev->dev_private;
2026
2027 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2028
2029 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2030 sizeof(dev_priv->wm.pri_latency));
2031 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2032 sizeof(dev_priv->wm.pri_latency));
2033
2034 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2035 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002036
2037 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2038 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2039 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002040
2041 if (IS_GEN6(dev))
2042 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002043}
2044
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002045static void skl_setup_wm_latency(struct drm_device *dev)
2046{
2047 struct drm_i915_private *dev_priv = dev->dev_private;
2048
2049 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2050 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2051}
2052
Imre Deak820c1982013-12-17 14:46:36 +02002053static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002054 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002055{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002056 struct drm_device *dev = crtc->dev;
2057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2058 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002059 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002060
Matt Roper3ef00282015-03-09 10:19:24 -07002061 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002062 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002063
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002064 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002065 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002066 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roperc9f038a2015-03-09 11:06:02 -07002067
2068 if (crtc->primary->state->fb) {
2069 p->pri.enabled = true;
2070 p->pri.bytes_per_pixel =
2071 crtc->primary->state->fb->bits_per_pixel / 8;
2072 } else {
2073 p->pri.enabled = false;
2074 p->pri.bytes_per_pixel = 0;
2075 }
2076
2077 if (crtc->cursor->state->fb) {
2078 p->cur.enabled = true;
2079 p->cur.bytes_per_pixel = 4;
2080 } else {
2081 p->cur.enabled = false;
2082 p->cur.bytes_per_pixel = 0;
2083 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002084 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08002085 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002086
Matt Roperaf2b6532014-04-01 15:22:32 -07002087 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002088 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002089
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002090 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03002091 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002092 break;
2093 }
2094 }
2095}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002096
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002097static void ilk_compute_wm_config(struct drm_device *dev,
2098 struct intel_wm_config *config)
2099{
2100 struct intel_crtc *intel_crtc;
2101
2102 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002103 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002104 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2105
2106 if (!wm->pipe_enabled)
2107 continue;
2108
2109 config->sprites_enabled |= wm->sprites_enabled;
2110 config->sprites_scaled |= wm->sprites_scaled;
2111 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002112 }
2113}
2114
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002115/* Compute new watermarks for the pipe */
2116static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002117 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002118 struct intel_pipe_wm *pipe_wm)
2119{
2120 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002121 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002122 int level, max_level = ilk_wm_max_level(dev);
2123 /* LP0 watermark maximums depend on this pipe alone */
2124 struct intel_wm_config config = {
2125 .num_pipes_active = 1,
2126 .sprites_enabled = params->spr.enabled,
2127 .sprites_scaled = params->spr.scaled,
2128 };
Imre Deak820c1982013-12-17 14:46:36 +02002129 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002130
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002131 pipe_wm->pipe_enabled = params->active;
2132 pipe_wm->sprites_enabled = params->spr.enabled;
2133 pipe_wm->sprites_scaled = params->spr.scaled;
2134
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002135 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2136 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2137 max_level = 1;
2138
2139 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2140 if (params->spr.scaled)
2141 max_level = 0;
2142
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002143 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002144
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002145 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002146 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002147
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002148 /* LP0 watermarks always use 1/2 DDB partitioning */
2149 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2150
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002151 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002152 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2153 return false;
2154
2155 ilk_compute_wm_reg_maximums(dev, 1, &max);
2156
2157 for (level = 1; level <= max_level; level++) {
2158 struct intel_wm_level wm = {};
2159
2160 ilk_compute_wm_level(dev_priv, level, params, &wm);
2161
2162 /*
2163 * Disable any watermark level that exceeds the
2164 * register maximums since such watermarks are
2165 * always invalid.
2166 */
2167 if (!ilk_validate_wm_level(level, &max, &wm))
2168 break;
2169
2170 pipe_wm->wm[level] = wm;
2171 }
2172
2173 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002174}
2175
2176/*
2177 * Merge the watermarks from all active pipes for a specific level.
2178 */
2179static void ilk_merge_wm_level(struct drm_device *dev,
2180 int level,
2181 struct intel_wm_level *ret_wm)
2182{
2183 const struct intel_crtc *intel_crtc;
2184
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002185 ret_wm->enable = true;
2186
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002187 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002188 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2189 const struct intel_wm_level *wm = &active->wm[level];
2190
2191 if (!active->pipe_enabled)
2192 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002193
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002194 /*
2195 * The watermark values may have been used in the past,
2196 * so we must maintain them in the registers for some
2197 * time even if the level is now disabled.
2198 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002199 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002200 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002201
2202 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2203 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2204 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2205 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2206 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002207}
2208
2209/*
2210 * Merge all low power watermarks for all active pipes.
2211 */
2212static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002213 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002214 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002215 struct intel_pipe_wm *merged)
2216{
2217 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002218 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002219
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002220 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2221 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2222 config->num_pipes_active > 1)
2223 return;
2224
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002225 /* ILK: FBC WM must be disabled always */
2226 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002227
2228 /* merge each WM1+ level */
2229 for (level = 1; level <= max_level; level++) {
2230 struct intel_wm_level *wm = &merged->wm[level];
2231
2232 ilk_merge_wm_level(dev, level, wm);
2233
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002234 if (level > last_enabled_level)
2235 wm->enable = false;
2236 else if (!ilk_validate_wm_level(level, max, wm))
2237 /* make sure all following levels get disabled */
2238 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002239
2240 /*
2241 * The spec says it is preferred to disable
2242 * FBC WMs instead of disabling a WM level.
2243 */
2244 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002245 if (wm->enable)
2246 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002247 wm->fbc_val = 0;
2248 }
2249 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002250
2251 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2252 /*
2253 * FIXME this is racy. FBC might get enabled later.
2254 * What we should check here is whether FBC can be
2255 * enabled sometime later.
2256 */
2257 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2258 for (level = 2; level <= max_level; level++) {
2259 struct intel_wm_level *wm = &merged->wm[level];
2260
2261 wm->enable = false;
2262 }
2263 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002264}
2265
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002266static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2267{
2268 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2269 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2270}
2271
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002272/* The value we need to program into the WM_LPx latency field */
2273static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2274{
2275 struct drm_i915_private *dev_priv = dev->dev_private;
2276
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002277 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002278 return 2 * level;
2279 else
2280 return dev_priv->wm.pri_latency[level];
2281}
2282
Imre Deak820c1982013-12-17 14:46:36 +02002283static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002284 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002285 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002286 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002287{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002288 struct intel_crtc *intel_crtc;
2289 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002290
Ville Syrjälä0362c782013-10-09 19:17:57 +03002291 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002292 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002293
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002294 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002295 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002296 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002297
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002298 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002299
Ville Syrjälä0362c782013-10-09 19:17:57 +03002300 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002301
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002302 /*
2303 * Maintain the watermark values even if the level is
2304 * disabled. Doing otherwise could cause underruns.
2305 */
2306 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002307 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002308 (r->pri_val << WM1_LP_SR_SHIFT) |
2309 r->cur_val;
2310
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002311 if (r->enable)
2312 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2313
Ville Syrjälä416f4722013-11-02 21:07:46 -07002314 if (INTEL_INFO(dev)->gen >= 8)
2315 results->wm_lp[wm_lp - 1] |=
2316 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2317 else
2318 results->wm_lp[wm_lp - 1] |=
2319 r->fbc_val << WM1_LP_FBC_SHIFT;
2320
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002321 /*
2322 * Always set WM1S_LP_EN when spr_val != 0, even if the
2323 * level is disabled. Doing otherwise could cause underruns.
2324 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002325 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2326 WARN_ON(wm_lp != 1);
2327 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2328 } else
2329 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002330 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002331
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002332 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002333 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002334 enum pipe pipe = intel_crtc->pipe;
2335 const struct intel_wm_level *r =
2336 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002337
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002338 if (WARN_ON(!r->enable))
2339 continue;
2340
2341 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2342
2343 results->wm_pipe[pipe] =
2344 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2345 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2346 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002347 }
2348}
2349
Paulo Zanoni861f3382013-05-31 10:19:21 -03002350/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2351 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002352static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002353 struct intel_pipe_wm *r1,
2354 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002355{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002356 int level, max_level = ilk_wm_max_level(dev);
2357 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002358
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002359 for (level = 1; level <= max_level; level++) {
2360 if (r1->wm[level].enable)
2361 level1 = level;
2362 if (r2->wm[level].enable)
2363 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002364 }
2365
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002366 if (level1 == level2) {
2367 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002368 return r2;
2369 else
2370 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002371 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002372 return r1;
2373 } else {
2374 return r2;
2375 }
2376}
2377
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002378/* dirty bits used to track which watermarks need changes */
2379#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2380#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2381#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2382#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2383#define WM_DIRTY_FBC (1 << 24)
2384#define WM_DIRTY_DDB (1 << 25)
2385
Damien Lespiau055e3932014-08-18 13:49:10 +01002386static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002387 const struct ilk_wm_values *old,
2388 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002389{
2390 unsigned int dirty = 0;
2391 enum pipe pipe;
2392 int wm_lp;
2393
Damien Lespiau055e3932014-08-18 13:49:10 +01002394 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002395 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2396 dirty |= WM_DIRTY_LINETIME(pipe);
2397 /* Must disable LP1+ watermarks too */
2398 dirty |= WM_DIRTY_LP_ALL;
2399 }
2400
2401 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2402 dirty |= WM_DIRTY_PIPE(pipe);
2403 /* Must disable LP1+ watermarks too */
2404 dirty |= WM_DIRTY_LP_ALL;
2405 }
2406 }
2407
2408 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2409 dirty |= WM_DIRTY_FBC;
2410 /* Must disable LP1+ watermarks too */
2411 dirty |= WM_DIRTY_LP_ALL;
2412 }
2413
2414 if (old->partitioning != new->partitioning) {
2415 dirty |= WM_DIRTY_DDB;
2416 /* Must disable LP1+ watermarks too */
2417 dirty |= WM_DIRTY_LP_ALL;
2418 }
2419
2420 /* LP1+ watermarks already deemed dirty, no need to continue */
2421 if (dirty & WM_DIRTY_LP_ALL)
2422 return dirty;
2423
2424 /* Find the lowest numbered LP1+ watermark in need of an update... */
2425 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2426 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2427 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2428 break;
2429 }
2430
2431 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2432 for (; wm_lp <= 3; wm_lp++)
2433 dirty |= WM_DIRTY_LP(wm_lp);
2434
2435 return dirty;
2436}
2437
Ville Syrjälä8553c182013-12-05 15:51:39 +02002438static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2439 unsigned int dirty)
2440{
Imre Deak820c1982013-12-17 14:46:36 +02002441 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002442 bool changed = false;
2443
2444 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2445 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2446 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2447 changed = true;
2448 }
2449 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2450 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2451 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2452 changed = true;
2453 }
2454 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2455 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2456 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2457 changed = true;
2458 }
2459
2460 /*
2461 * Don't touch WM1S_LP_EN here.
2462 * Doing so could cause underruns.
2463 */
2464
2465 return changed;
2466}
2467
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002468/*
2469 * The spec says we shouldn't write when we don't need, because every write
2470 * causes WMs to be re-evaluated, expending some power.
2471 */
Imre Deak820c1982013-12-17 14:46:36 +02002472static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2473 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002474{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002475 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002476 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002477 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002478 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002479
Damien Lespiau055e3932014-08-18 13:49:10 +01002480 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002481 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002482 return;
2483
Ville Syrjälä8553c182013-12-05 15:51:39 +02002484 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002485
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002486 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002487 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002488 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002489 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002490 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002491 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2492
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002493 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002494 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002495 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002496 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002497 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002498 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2499
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002500 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002501 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002502 val = I915_READ(WM_MISC);
2503 if (results->partitioning == INTEL_DDB_PART_1_2)
2504 val &= ~WM_MISC_DATA_PARTITION_5_6;
2505 else
2506 val |= WM_MISC_DATA_PARTITION_5_6;
2507 I915_WRITE(WM_MISC, val);
2508 } else {
2509 val = I915_READ(DISP_ARB_CTL2);
2510 if (results->partitioning == INTEL_DDB_PART_1_2)
2511 val &= ~DISP_DATA_PARTITION_5_6;
2512 else
2513 val |= DISP_DATA_PARTITION_5_6;
2514 I915_WRITE(DISP_ARB_CTL2, val);
2515 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002516 }
2517
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002518 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002519 val = I915_READ(DISP_ARB_CTL);
2520 if (results->enable_fbc_wm)
2521 val &= ~DISP_FBC_WM_DIS;
2522 else
2523 val |= DISP_FBC_WM_DIS;
2524 I915_WRITE(DISP_ARB_CTL, val);
2525 }
2526
Imre Deak954911e2013-12-17 14:46:34 +02002527 if (dirty & WM_DIRTY_LP(1) &&
2528 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2529 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2530
2531 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002532 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2533 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2534 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2535 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2536 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002537
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002538 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002539 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002540 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002541 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002542 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002543 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002544
2545 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002546}
2547
Ville Syrjälä8553c182013-12-05 15:51:39 +02002548static bool ilk_disable_lp_wm(struct drm_device *dev)
2549{
2550 struct drm_i915_private *dev_priv = dev->dev_private;
2551
2552 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2553}
2554
Damien Lespiaub9cec072014-11-04 17:06:43 +00002555/*
2556 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2557 * different active planes.
2558 */
2559
2560#define SKL_DDB_SIZE 896 /* in blocks */
Damien Lespiau43d735a2015-03-17 11:39:34 +02002561#define BXT_DDB_SIZE 512
Damien Lespiaub9cec072014-11-04 17:06:43 +00002562
2563static void
2564skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2565 struct drm_crtc *for_crtc,
2566 const struct intel_wm_config *config,
2567 const struct skl_pipe_wm_parameters *params,
2568 struct skl_ddb_entry *alloc /* out */)
2569{
2570 struct drm_crtc *crtc;
2571 unsigned int pipe_size, ddb_size;
2572 int nth_active_pipe;
2573
2574 if (!params->active) {
2575 alloc->start = 0;
2576 alloc->end = 0;
2577 return;
2578 }
2579
Damien Lespiau43d735a2015-03-17 11:39:34 +02002580 if (IS_BROXTON(dev))
2581 ddb_size = BXT_DDB_SIZE;
2582 else
2583 ddb_size = SKL_DDB_SIZE;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002584
2585 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2586
2587 nth_active_pipe = 0;
2588 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002589 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002590 continue;
2591
2592 if (crtc == for_crtc)
2593 break;
2594
2595 nth_active_pipe++;
2596 }
2597
2598 pipe_size = ddb_size / config->num_pipes_active;
2599 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002600 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002601}
2602
2603static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2604{
2605 if (config->num_pipes_active == 1)
2606 return 32;
2607
2608 return 8;
2609}
2610
Damien Lespiaua269c582014-11-04 17:06:49 +00002611static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2612{
2613 entry->start = reg & 0x3ff;
2614 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002615 if (entry->end)
2616 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002617}
2618
Damien Lespiau08db6652014-11-04 17:06:52 +00002619void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2620 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002621{
Damien Lespiaua269c582014-11-04 17:06:49 +00002622 enum pipe pipe;
2623 int plane;
2624 u32 val;
2625
2626 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002627 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002628 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2629 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2630 val);
2631 }
2632
2633 val = I915_READ(CUR_BUF_CFG(pipe));
2634 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2635 }
2636}
2637
Damien Lespiaub9cec072014-11-04 17:06:43 +00002638static unsigned int
2639skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2640{
2641 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2642}
2643
2644/*
2645 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2646 * a 8192x4096@32bpp framebuffer:
2647 * 3 * 4096 * 8192 * 4 < 2^32
2648 */
2649static unsigned int
2650skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2651 const struct skl_pipe_wm_parameters *params)
2652{
2653 unsigned int total_data_rate = 0;
2654 int plane;
2655
2656 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2657 const struct intel_plane_wm_parameters *p;
2658
2659 p = &params->plane[plane];
2660 if (!p->enabled)
2661 continue;
2662
2663 total_data_rate += skl_plane_relative_data_rate(p);
2664 }
2665
2666 return total_data_rate;
2667}
2668
2669static void
2670skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2671 const struct intel_wm_config *config,
2672 const struct skl_pipe_wm_parameters *params,
2673 struct skl_ddb_allocation *ddb /* out */)
2674{
2675 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002676 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2678 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002679 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002680 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002681 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002682 unsigned int total_data_rate;
2683 int plane;
2684
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002685 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2686 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002687 if (alloc_size == 0) {
2688 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2689 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2690 return;
2691 }
2692
2693 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002694 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2695 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002696
2697 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002698 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002699
Damien Lespiau80958152015-02-09 13:35:10 +00002700 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002701 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002702 const struct intel_plane_wm_parameters *p;
2703
2704 p = &params->plane[plane];
2705 if (!p->enabled)
2706 continue;
2707
2708 minimum[plane] = 8;
2709 alloc_size -= minimum[plane];
2710 }
2711
Damien Lespiaub9cec072014-11-04 17:06:43 +00002712 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002713 * 2. Distribute the remaining space in proportion to the amount of
2714 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002715 *
2716 * FIXME: we may not allocate every single block here.
2717 */
2718 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2719
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002720 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002721 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2722 const struct intel_plane_wm_parameters *p;
2723 unsigned int data_rate;
2724 uint16_t plane_blocks;
2725
2726 p = &params->plane[plane];
2727 if (!p->enabled)
2728 continue;
2729
2730 data_rate = skl_plane_relative_data_rate(p);
2731
2732 /*
2733 * promote the expression to 64 bits to avoid overflowing, the
2734 * result is < available as data_rate / total_data_rate < 1
2735 */
Damien Lespiau80958152015-02-09 13:35:10 +00002736 plane_blocks = minimum[plane];
2737 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2738 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002739
2740 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002741 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002742
2743 start += plane_blocks;
2744 }
2745
2746}
2747
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002748static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002749{
2750 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002751 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002752}
2753
2754/*
2755 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2756 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2757 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2758 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2759*/
2760static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2761 uint32_t latency)
2762{
2763 uint32_t wm_intermediate_val, ret;
2764
2765 if (latency == 0)
2766 return UINT_MAX;
2767
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002768 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002769 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2770
2771 return ret;
2772}
2773
2774static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2775 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002776 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002777{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002778 uint32_t ret;
2779 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2780 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002781
2782 if (latency == 0)
2783 return UINT_MAX;
2784
2785 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002786
2787 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2788 tiling == I915_FORMAT_MOD_Yf_TILED) {
2789 plane_bytes_per_line *= 4;
2790 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2791 plane_blocks_per_line /= 4;
2792 } else {
2793 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2794 }
2795
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002796 wm_intermediate_val = latency * pixel_rate;
2797 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002798 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002799
2800 return ret;
2801}
2802
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002803static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2804 const struct intel_crtc *intel_crtc)
2805{
2806 struct drm_device *dev = intel_crtc->base.dev;
2807 struct drm_i915_private *dev_priv = dev->dev_private;
2808 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2809 enum pipe pipe = intel_crtc->pipe;
2810
2811 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2812 sizeof(new_ddb->plane[pipe])))
2813 return true;
2814
2815 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2816 sizeof(new_ddb->cursor[pipe])))
2817 return true;
2818
2819 return false;
2820}
2821
2822static void skl_compute_wm_global_parameters(struct drm_device *dev,
2823 struct intel_wm_config *config)
2824{
2825 struct drm_crtc *crtc;
2826 struct drm_plane *plane;
2827
2828 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07002829 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002830
2831 /* FIXME: I don't think we need those two global parameters on SKL */
2832 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2833 struct intel_plane *intel_plane = to_intel_plane(plane);
2834
2835 config->sprites_enabled |= intel_plane->wm.enabled;
2836 config->sprites_scaled |= intel_plane->wm.scaled;
2837 }
2838}
2839
2840static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2841 struct skl_pipe_wm_parameters *p)
2842{
2843 struct drm_device *dev = crtc->dev;
2844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2845 enum pipe pipe = intel_crtc->pipe;
2846 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002847 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002848 int i = 1; /* Index for sprite planes start */
2849
Matt Roper3ef00282015-03-09 10:19:24 -07002850 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002851 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002852 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2853 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002854
Matt Roperc9f038a2015-03-09 11:06:02 -07002855 fb = crtc->primary->state->fb;
2856 if (fb) {
2857 p->plane[0].enabled = true;
2858 p->plane[0].bytes_per_pixel = fb->bits_per_pixel / 8;
2859 p->plane[0].tiling = fb->modifier[0];
2860 } else {
2861 p->plane[0].enabled = false;
2862 p->plane[0].bytes_per_pixel = 0;
2863 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2864 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002865 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2866 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00002867 p->plane[0].rotation = crtc->primary->state->rotation;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002868
Matt Roperc9f038a2015-03-09 11:06:02 -07002869 fb = crtc->cursor->state->fb;
2870 if (fb) {
2871 p->cursor.enabled = true;
2872 p->cursor.bytes_per_pixel = fb->bits_per_pixel / 8;
2873 p->cursor.horiz_pixels = crtc->cursor->state->crtc_w;
2874 p->cursor.vert_pixels = crtc->cursor->state->crtc_h;
2875 } else {
2876 p->cursor.enabled = false;
2877 p->cursor.bytes_per_pixel = 0;
2878 p->cursor.horiz_pixels = 64;
2879 p->cursor.vert_pixels = 64;
2880 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002881 }
2882
2883 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2884 struct intel_plane *intel_plane = to_intel_plane(plane);
2885
Sonika Jindala712f8e2014-12-09 10:59:15 +05302886 if (intel_plane->pipe == pipe &&
2887 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002888 p->plane[i++] = intel_plane->wm;
2889 }
2890}
2891
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002892static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2893 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002894 struct intel_plane_wm_parameters *p_params,
2895 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002896 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002897 uint16_t *out_blocks, /* out */
2898 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002899{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002900 uint32_t latency = dev_priv->wm.skl_latency[level];
2901 uint32_t method1, method2;
2902 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2903 uint32_t res_blocks, res_lines;
2904 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002905
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002906 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002907 return false;
2908
2909 method1 = skl_wm_method1(p->pixel_rate,
2910 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002911 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002912 method2 = skl_wm_method2(p->pixel_rate,
2913 p->pipe_htotal,
2914 p_params->horiz_pixels,
2915 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002916 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002917 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002918
2919 plane_bytes_per_line = p_params->horiz_pixels *
2920 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002921 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002922
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002923 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2924 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00002925 uint32_t min_scanlines = 4;
2926 uint32_t y_tile_minimum;
2927 if (intel_rotation_90_or_270(p_params->rotation)) {
2928 switch (p_params->bytes_per_pixel) {
2929 case 1:
2930 min_scanlines = 16;
2931 break;
2932 case 2:
2933 min_scanlines = 8;
2934 break;
2935 case 8:
2936 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08002937 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00002938 }
2939 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002940 selected_result = max(method2, y_tile_minimum);
2941 } else {
2942 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2943 selected_result = min(method1, method2);
2944 else
2945 selected_result = method1;
2946 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002947
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002948 res_blocks = selected_result + 1;
2949 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002950
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002951 if (level >= 1 && level <= 7) {
2952 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2953 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2954 res_lines += 4;
2955 else
2956 res_blocks++;
2957 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002958
2959 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002960 return false;
2961
2962 *out_blocks = res_blocks;
2963 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002964
2965 return true;
2966}
2967
2968static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2969 struct skl_ddb_allocation *ddb,
2970 struct skl_pipe_wm_parameters *p,
2971 enum pipe pipe,
2972 int level,
2973 int num_planes,
2974 struct skl_wm_level *result)
2975{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002976 uint16_t ddb_blocks;
2977 int i;
2978
2979 for (i = 0; i < num_planes; i++) {
2980 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2981
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002982 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2983 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002984 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002985 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002986 &result->plane_res_b[i],
2987 &result->plane_res_l[i]);
2988 }
2989
2990 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002991 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2992 ddb_blocks, level,
2993 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002994 &result->cursor_res_l);
2995}
2996
Damien Lespiau407b50f2014-11-04 17:06:57 +00002997static uint32_t
2998skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2999{
Matt Roper3ef00282015-03-09 10:19:24 -07003000 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003001 return 0;
3002
3003 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
3004
3005}
3006
3007static void skl_compute_transition_wm(struct drm_crtc *crtc,
3008 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00003009 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003010{
Damien Lespiau9414f562014-11-04 17:06:58 +00003011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3012 int i;
3013
Damien Lespiau407b50f2014-11-04 17:06:57 +00003014 if (!params->active)
3015 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003016
3017 /* Until we know more, just disable transition WMs */
3018 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3019 trans_wm->plane_en[i] = false;
3020 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003021}
3022
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003023static void skl_compute_pipe_wm(struct drm_crtc *crtc,
3024 struct skl_ddb_allocation *ddb,
3025 struct skl_pipe_wm_parameters *params,
3026 struct skl_pipe_wm *pipe_wm)
3027{
3028 struct drm_device *dev = crtc->dev;
3029 const struct drm_i915_private *dev_priv = dev->dev_private;
3030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3031 int level, max_level = ilk_wm_max_level(dev);
3032
3033 for (level = 0; level <= max_level; level++) {
3034 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
3035 level, intel_num_planes(intel_crtc),
3036 &pipe_wm->wm[level]);
3037 }
3038 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
3039
Damien Lespiau9414f562014-11-04 17:06:58 +00003040 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003041}
3042
3043static void skl_compute_wm_results(struct drm_device *dev,
3044 struct skl_pipe_wm_parameters *p,
3045 struct skl_pipe_wm *p_wm,
3046 struct skl_wm_values *r,
3047 struct intel_crtc *intel_crtc)
3048{
3049 int level, max_level = ilk_wm_max_level(dev);
3050 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003051 uint32_t temp;
3052 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003053
3054 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003055 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3056 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003057
3058 temp |= p_wm->wm[level].plane_res_l[i] <<
3059 PLANE_WM_LINES_SHIFT;
3060 temp |= p_wm->wm[level].plane_res_b[i];
3061 if (p_wm->wm[level].plane_en[i])
3062 temp |= PLANE_WM_EN;
3063
3064 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003065 }
3066
3067 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003068
3069 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
3070 temp |= p_wm->wm[level].cursor_res_b;
3071
3072 if (p_wm->wm[level].cursor_en)
3073 temp |= PLANE_WM_EN;
3074
3075 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003076
3077 }
3078
Damien Lespiau9414f562014-11-04 17:06:58 +00003079 /* transition WMs */
3080 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3081 temp = 0;
3082 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3083 temp |= p_wm->trans_wm.plane_res_b[i];
3084 if (p_wm->trans_wm.plane_en[i])
3085 temp |= PLANE_WM_EN;
3086
3087 r->plane_trans[pipe][i] = temp;
3088 }
3089
3090 temp = 0;
3091 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
3092 temp |= p_wm->trans_wm.cursor_res_b;
3093 if (p_wm->trans_wm.cursor_en)
3094 temp |= PLANE_WM_EN;
3095
3096 r->cursor_trans[pipe] = temp;
3097
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003098 r->wm_linetime[pipe] = p_wm->linetime;
3099}
3100
Damien Lespiau16160e32014-11-04 17:06:53 +00003101static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
3102 const struct skl_ddb_entry *entry)
3103{
3104 if (entry->end)
3105 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3106 else
3107 I915_WRITE(reg, 0);
3108}
3109
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003110static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3111 const struct skl_wm_values *new)
3112{
3113 struct drm_device *dev = dev_priv->dev;
3114 struct intel_crtc *crtc;
3115
3116 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3117 int i, level, max_level = ilk_wm_max_level(dev);
3118 enum pipe pipe = crtc->pipe;
3119
Damien Lespiau5d374d92014-11-04 17:07:00 +00003120 if (!new->dirty[pipe])
3121 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003122
Damien Lespiau5d374d92014-11-04 17:07:00 +00003123 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3124
3125 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003126 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003127 I915_WRITE(PLANE_WM(pipe, i, level),
3128 new->plane[pipe][i][level]);
3129 I915_WRITE(CUR_WM(pipe, level),
3130 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003131 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003132 for (i = 0; i < intel_num_planes(crtc); i++)
3133 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3134 new->plane_trans[pipe][i]);
3135 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3136
3137 for (i = 0; i < intel_num_planes(crtc); i++)
3138 skl_ddb_entry_write(dev_priv,
3139 PLANE_BUF_CFG(pipe, i),
3140 &new->ddb.plane[pipe][i]);
3141
3142 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3143 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003144 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003145}
3146
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003147/*
3148 * When setting up a new DDB allocation arrangement, we need to correctly
3149 * sequence the times at which the new allocations for the pipes are taken into
3150 * account or we'll have pipes fetching from space previously allocated to
3151 * another pipe.
3152 *
3153 * Roughly the sequence looks like:
3154 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3155 * overlapping with a previous light-up pipe (another way to put it is:
3156 * pipes with their new allocation strickly included into their old ones).
3157 * 2. re-allocate the other pipes that get their allocation reduced
3158 * 3. allocate the pipes having their allocation increased
3159 *
3160 * Steps 1. and 2. are here to take care of the following case:
3161 * - Initially DDB looks like this:
3162 * | B | C |
3163 * - enable pipe A.
3164 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3165 * allocation
3166 * | A | B | C |
3167 *
3168 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3169 */
3170
Damien Lespiaud21b7952014-11-04 17:07:03 +00003171static void
3172skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003173{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003174 int plane;
3175
Damien Lespiaud21b7952014-11-04 17:07:03 +00003176 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3177
Damien Lespiaudd740782015-02-28 14:54:08 +00003178 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003179 I915_WRITE(PLANE_SURF(pipe, plane),
3180 I915_READ(PLANE_SURF(pipe, plane)));
3181 }
3182 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3183}
3184
3185static bool
3186skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3187 const struct skl_ddb_allocation *new,
3188 enum pipe pipe)
3189{
3190 uint16_t old_size, new_size;
3191
3192 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3193 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3194
3195 return old_size != new_size &&
3196 new->pipe[pipe].start >= old->pipe[pipe].start &&
3197 new->pipe[pipe].end <= old->pipe[pipe].end;
3198}
3199
3200static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3201 struct skl_wm_values *new_values)
3202{
3203 struct drm_device *dev = dev_priv->dev;
3204 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3205 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3206 struct intel_crtc *crtc;
3207 enum pipe pipe;
3208
3209 new_ddb = &new_values->ddb;
3210 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3211
3212 /*
3213 * First pass: flush the pipes with the new allocation contained into
3214 * the old space.
3215 *
3216 * We'll wait for the vblank on those pipes to ensure we can safely
3217 * re-allocate the freed space without this pipe fetching from it.
3218 */
3219 for_each_intel_crtc(dev, crtc) {
3220 if (!crtc->active)
3221 continue;
3222
3223 pipe = crtc->pipe;
3224
3225 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3226 continue;
3227
Damien Lespiaud21b7952014-11-04 17:07:03 +00003228 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003229 intel_wait_for_vblank(dev, pipe);
3230
3231 reallocated[pipe] = true;
3232 }
3233
3234
3235 /*
3236 * Second pass: flush the pipes that are having their allocation
3237 * reduced, but overlapping with a previous allocation.
3238 *
3239 * Here as well we need to wait for the vblank to make sure the freed
3240 * space is not used anymore.
3241 */
3242 for_each_intel_crtc(dev, crtc) {
3243 if (!crtc->active)
3244 continue;
3245
3246 pipe = crtc->pipe;
3247
3248 if (reallocated[pipe])
3249 continue;
3250
3251 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3252 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003253 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003254 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303255 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003256 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003257 }
3258
3259 /*
3260 * Third pass: flush the pipes that got more space allocated.
3261 *
3262 * We don't need to actively wait for the update here, next vblank
3263 * will just get more DDB space with the correct WM values.
3264 */
3265 for_each_intel_crtc(dev, crtc) {
3266 if (!crtc->active)
3267 continue;
3268
3269 pipe = crtc->pipe;
3270
3271 /*
3272 * At this point, only the pipes more space than before are
3273 * left to re-allocate.
3274 */
3275 if (reallocated[pipe])
3276 continue;
3277
Damien Lespiaud21b7952014-11-04 17:07:03 +00003278 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003279 }
3280}
3281
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003282static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3283 struct skl_pipe_wm_parameters *params,
3284 struct intel_wm_config *config,
3285 struct skl_ddb_allocation *ddb, /* out */
3286 struct skl_pipe_wm *pipe_wm /* out */)
3287{
3288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3289
3290 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003291 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003292 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3293
3294 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3295 return false;
3296
3297 intel_crtc->wm.skl_active = *pipe_wm;
3298 return true;
3299}
3300
3301static void skl_update_other_pipe_wm(struct drm_device *dev,
3302 struct drm_crtc *crtc,
3303 struct intel_wm_config *config,
3304 struct skl_wm_values *r)
3305{
3306 struct intel_crtc *intel_crtc;
3307 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3308
3309 /*
3310 * If the WM update hasn't changed the allocation for this_crtc (the
3311 * crtc we are currently computing the new WM values for), other
3312 * enabled crtcs will keep the same allocation and we don't need to
3313 * recompute anything for them.
3314 */
3315 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3316 return;
3317
3318 /*
3319 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3320 * other active pipes need new DDB allocation and WM values.
3321 */
3322 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3323 base.head) {
3324 struct skl_pipe_wm_parameters params = {};
3325 struct skl_pipe_wm pipe_wm = {};
3326 bool wm_changed;
3327
3328 if (this_crtc->pipe == intel_crtc->pipe)
3329 continue;
3330
3331 if (!intel_crtc->active)
3332 continue;
3333
3334 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3335 &params, config,
3336 &r->ddb, &pipe_wm);
3337
3338 /*
3339 * If we end up re-computing the other pipe WM values, it's
3340 * because it was really needed, so we expect the WM values to
3341 * be different.
3342 */
3343 WARN_ON(!wm_changed);
3344
3345 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3346 r->dirty[intel_crtc->pipe] = true;
3347 }
3348}
3349
3350static void skl_update_wm(struct drm_crtc *crtc)
3351{
3352 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3353 struct drm_device *dev = crtc->dev;
3354 struct drm_i915_private *dev_priv = dev->dev_private;
3355 struct skl_pipe_wm_parameters params = {};
3356 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3357 struct skl_pipe_wm pipe_wm = {};
3358 struct intel_wm_config config = {};
3359
3360 memset(results, 0, sizeof(*results));
3361
3362 skl_compute_wm_global_parameters(dev, &config);
3363
3364 if (!skl_update_pipe_wm(crtc, &params, &config,
3365 &results->ddb, &pipe_wm))
3366 return;
3367
3368 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3369 results->dirty[intel_crtc->pipe] = true;
3370
3371 skl_update_other_pipe_wm(dev, crtc, &config, results);
3372 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003373 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003374
3375 /* store the new configuration */
3376 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003377}
3378
3379static void
3380skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3381 uint32_t sprite_width, uint32_t sprite_height,
3382 int pixel_size, bool enabled, bool scaled)
3383{
3384 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003385 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003386
3387 intel_plane->wm.enabled = enabled;
3388 intel_plane->wm.scaled = scaled;
3389 intel_plane->wm.horiz_pixels = sprite_width;
3390 intel_plane->wm.vert_pixels = sprite_height;
3391 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003392 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3393 /*
3394 * Framebuffer can be NULL on plane disable, but it does not
3395 * matter for watermarks if we assume no tiling in that case.
3396 */
3397 if (fb)
3398 intel_plane->wm.tiling = fb->modifier[0];
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003399 intel_plane->wm.rotation = plane->state->rotation;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003400
3401 skl_update_wm(crtc);
3402}
3403
Imre Deak820c1982013-12-17 14:46:36 +02003404static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003405{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003407 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003408 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003409 struct ilk_wm_maximums max;
3410 struct ilk_pipe_wm_parameters params = {};
3411 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003412 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003413 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003414 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003415 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003416
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003417 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003418
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003419 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3420
3421 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3422 return;
3423
3424 intel_crtc->wm.active = pipe_wm;
3425
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003426 ilk_compute_wm_config(dev, &config);
3427
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003428 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003429 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003430
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003431 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003432 if (INTEL_INFO(dev)->gen >= 7 &&
3433 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003434 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003435 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003436
Imre Deak820c1982013-12-17 14:46:36 +02003437 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003438 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003439 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003440 }
3441
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003442 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003443 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003444
Imre Deak820c1982013-12-17 14:46:36 +02003445 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003446
Imre Deak820c1982013-12-17 14:46:36 +02003447 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003448}
3449
Damien Lespiaued57cb82014-07-15 09:21:24 +02003450static void
3451ilk_update_sprite_wm(struct drm_plane *plane,
3452 struct drm_crtc *crtc,
3453 uint32_t sprite_width, uint32_t sprite_height,
3454 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003455{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003456 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003457 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003458
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003459 intel_plane->wm.enabled = enabled;
3460 intel_plane->wm.scaled = scaled;
3461 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003462 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003463 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003464
Ville Syrjälä8553c182013-12-05 15:51:39 +02003465 /*
3466 * IVB workaround: must disable low power watermarks for at least
3467 * one frame before enabling scaling. LP watermarks can be re-enabled
3468 * when scaling is disabled.
3469 *
3470 * WaCxSRDisabledForSpriteScaling:ivb
3471 */
3472 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3473 intel_wait_for_vblank(dev, intel_plane->pipe);
3474
Imre Deak820c1982013-12-17 14:46:36 +02003475 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003476}
3477
Pradeep Bhat30789992014-11-04 17:06:45 +00003478static void skl_pipe_wm_active_state(uint32_t val,
3479 struct skl_pipe_wm *active,
3480 bool is_transwm,
3481 bool is_cursor,
3482 int i,
3483 int level)
3484{
3485 bool is_enabled = (val & PLANE_WM_EN) != 0;
3486
3487 if (!is_transwm) {
3488 if (!is_cursor) {
3489 active->wm[level].plane_en[i] = is_enabled;
3490 active->wm[level].plane_res_b[i] =
3491 val & PLANE_WM_BLOCKS_MASK;
3492 active->wm[level].plane_res_l[i] =
3493 (val >> PLANE_WM_LINES_SHIFT) &
3494 PLANE_WM_LINES_MASK;
3495 } else {
3496 active->wm[level].cursor_en = is_enabled;
3497 active->wm[level].cursor_res_b =
3498 val & PLANE_WM_BLOCKS_MASK;
3499 active->wm[level].cursor_res_l =
3500 (val >> PLANE_WM_LINES_SHIFT) &
3501 PLANE_WM_LINES_MASK;
3502 }
3503 } else {
3504 if (!is_cursor) {
3505 active->trans_wm.plane_en[i] = is_enabled;
3506 active->trans_wm.plane_res_b[i] =
3507 val & PLANE_WM_BLOCKS_MASK;
3508 active->trans_wm.plane_res_l[i] =
3509 (val >> PLANE_WM_LINES_SHIFT) &
3510 PLANE_WM_LINES_MASK;
3511 } else {
3512 active->trans_wm.cursor_en = is_enabled;
3513 active->trans_wm.cursor_res_b =
3514 val & PLANE_WM_BLOCKS_MASK;
3515 active->trans_wm.cursor_res_l =
3516 (val >> PLANE_WM_LINES_SHIFT) &
3517 PLANE_WM_LINES_MASK;
3518 }
3519 }
3520}
3521
3522static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3523{
3524 struct drm_device *dev = crtc->dev;
3525 struct drm_i915_private *dev_priv = dev->dev_private;
3526 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3527 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3528 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3529 enum pipe pipe = intel_crtc->pipe;
3530 int level, i, max_level;
3531 uint32_t temp;
3532
3533 max_level = ilk_wm_max_level(dev);
3534
3535 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3536
3537 for (level = 0; level <= max_level; level++) {
3538 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3539 hw->plane[pipe][i][level] =
3540 I915_READ(PLANE_WM(pipe, i, level));
3541 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3542 }
3543
3544 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3545 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3546 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3547
Matt Roper3ef00282015-03-09 10:19:24 -07003548 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003549 return;
3550
3551 hw->dirty[pipe] = true;
3552
3553 active->linetime = hw->wm_linetime[pipe];
3554
3555 for (level = 0; level <= max_level; level++) {
3556 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3557 temp = hw->plane[pipe][i][level];
3558 skl_pipe_wm_active_state(temp, active, false,
3559 false, i, level);
3560 }
3561 temp = hw->cursor[pipe][level];
3562 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3563 }
3564
3565 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3566 temp = hw->plane_trans[pipe][i];
3567 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3568 }
3569
3570 temp = hw->cursor_trans[pipe];
3571 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3572}
3573
3574void skl_wm_get_hw_state(struct drm_device *dev)
3575{
Damien Lespiaua269c582014-11-04 17:06:49 +00003576 struct drm_i915_private *dev_priv = dev->dev_private;
3577 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003578 struct drm_crtc *crtc;
3579
Damien Lespiaua269c582014-11-04 17:06:49 +00003580 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003581 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3582 skl_pipe_wm_get_hw_state(crtc);
3583}
3584
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003585static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3586{
3587 struct drm_device *dev = crtc->dev;
3588 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003589 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3591 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3592 enum pipe pipe = intel_crtc->pipe;
3593 static const unsigned int wm0_pipe_reg[] = {
3594 [PIPE_A] = WM0_PIPEA_ILK,
3595 [PIPE_B] = WM0_PIPEB_ILK,
3596 [PIPE_C] = WM0_PIPEC_IVB,
3597 };
3598
3599 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003600 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003601 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003602
Matt Roper3ef00282015-03-09 10:19:24 -07003603 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003604
3605 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003606 u32 tmp = hw->wm_pipe[pipe];
3607
3608 /*
3609 * For active pipes LP0 watermark is marked as
3610 * enabled, and LP1+ watermaks as disabled since
3611 * we can't really reverse compute them in case
3612 * multiple pipes are active.
3613 */
3614 active->wm[0].enable = true;
3615 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3616 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3617 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3618 active->linetime = hw->wm_linetime[pipe];
3619 } else {
3620 int level, max_level = ilk_wm_max_level(dev);
3621
3622 /*
3623 * For inactive pipes, all watermark levels
3624 * should be marked as enabled but zeroed,
3625 * which is what we'd compute them to.
3626 */
3627 for (level = 0; level <= max_level; level++)
3628 active->wm[level].enable = true;
3629 }
3630}
3631
3632void ilk_wm_get_hw_state(struct drm_device *dev)
3633{
3634 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003635 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003636 struct drm_crtc *crtc;
3637
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003638 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003639 ilk_pipe_wm_get_hw_state(crtc);
3640
3641 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3642 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3643 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3644
3645 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003646 if (INTEL_INFO(dev)->gen >= 7) {
3647 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3648 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3649 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003650
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003651 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003652 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3653 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3654 else if (IS_IVYBRIDGE(dev))
3655 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3656 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003657
3658 hw->enable_fbc_wm =
3659 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3660}
3661
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003662/**
3663 * intel_update_watermarks - update FIFO watermark values based on current modes
3664 *
3665 * Calculate watermark values for the various WM regs based on current mode
3666 * and plane configuration.
3667 *
3668 * There are several cases to deal with here:
3669 * - normal (i.e. non-self-refresh)
3670 * - self-refresh (SR) mode
3671 * - lines are large relative to FIFO size (buffer can hold up to 2)
3672 * - lines are small relative to FIFO size (buffer can hold more than 2
3673 * lines), so need to account for TLB latency
3674 *
3675 * The normal calculation is:
3676 * watermark = dotclock * bytes per pixel * latency
3677 * where latency is platform & configuration dependent (we assume pessimal
3678 * values here).
3679 *
3680 * The SR calculation is:
3681 * watermark = (trunc(latency/line time)+1) * surface width *
3682 * bytes per pixel
3683 * where
3684 * line time = htotal / dotclock
3685 * surface width = hdisplay for normal plane and 64 for cursor
3686 * and latency is assumed to be high, as above.
3687 *
3688 * The final value programmed to the register should always be rounded up,
3689 * and include an extra 2 entries to account for clock crossings.
3690 *
3691 * We don't use the sprite, so we can ignore that. And on Crestline we have
3692 * to set the non-SR watermarks to 8.
3693 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003694void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003695{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003696 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003697
3698 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003699 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003700}
3701
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003702void intel_update_sprite_watermarks(struct drm_plane *plane,
3703 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003704 uint32_t sprite_width,
3705 uint32_t sprite_height,
3706 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003707 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003708{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003709 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003710
3711 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003712 dev_priv->display.update_sprite_wm(plane, crtc,
3713 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003714 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003715}
3716
Daniel Vetter92703882012-08-09 16:46:01 +02003717/**
3718 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003719 */
3720DEFINE_SPINLOCK(mchdev_lock);
3721
3722/* Global for IPS driver to get at the current i915 device. Protected by
3723 * mchdev_lock. */
3724static struct drm_i915_private *i915_mch_dev;
3725
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003726bool ironlake_set_drps(struct drm_device *dev, u8 val)
3727{
3728 struct drm_i915_private *dev_priv = dev->dev_private;
3729 u16 rgvswctl;
3730
Daniel Vetter92703882012-08-09 16:46:01 +02003731 assert_spin_locked(&mchdev_lock);
3732
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003733 rgvswctl = I915_READ16(MEMSWCTL);
3734 if (rgvswctl & MEMCTL_CMD_STS) {
3735 DRM_DEBUG("gpu busy, RCS change rejected\n");
3736 return false; /* still busy with another command */
3737 }
3738
3739 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3740 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3741 I915_WRITE16(MEMSWCTL, rgvswctl);
3742 POSTING_READ16(MEMSWCTL);
3743
3744 rgvswctl |= MEMCTL_CMD_STS;
3745 I915_WRITE16(MEMSWCTL, rgvswctl);
3746
3747 return true;
3748}
3749
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003750static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003751{
3752 struct drm_i915_private *dev_priv = dev->dev_private;
3753 u32 rgvmodectl = I915_READ(MEMMODECTL);
3754 u8 fmax, fmin, fstart, vstart;
3755
Daniel Vetter92703882012-08-09 16:46:01 +02003756 spin_lock_irq(&mchdev_lock);
3757
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003758 /* Enable temp reporting */
3759 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3760 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3761
3762 /* 100ms RC evaluation intervals */
3763 I915_WRITE(RCUPEI, 100000);
3764 I915_WRITE(RCDNEI, 100000);
3765
3766 /* Set max/min thresholds to 90ms and 80ms respectively */
3767 I915_WRITE(RCBMAXAVG, 90000);
3768 I915_WRITE(RCBMINAVG, 80000);
3769
3770 I915_WRITE(MEMIHYST, 1);
3771
3772 /* Set up min, max, and cur for interrupt handling */
3773 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3774 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3775 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3776 MEMMODE_FSTART_SHIFT;
3777
3778 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3779 PXVFREQ_PX_SHIFT;
3780
Daniel Vetter20e4d402012-08-08 23:35:39 +02003781 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3782 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003783
Daniel Vetter20e4d402012-08-08 23:35:39 +02003784 dev_priv->ips.max_delay = fstart;
3785 dev_priv->ips.min_delay = fmin;
3786 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003787
3788 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3789 fmax, fmin, fstart);
3790
3791 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3792
3793 /*
3794 * Interrupts will be enabled in ironlake_irq_postinstall
3795 */
3796
3797 I915_WRITE(VIDSTART, vstart);
3798 POSTING_READ(VIDSTART);
3799
3800 rgvmodectl |= MEMMODE_SWMODE_EN;
3801 I915_WRITE(MEMMODECTL, rgvmodectl);
3802
Daniel Vetter92703882012-08-09 16:46:01 +02003803 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003804 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003805 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003806
3807 ironlake_set_drps(dev, fstart);
3808
Daniel Vetter20e4d402012-08-08 23:35:39 +02003809 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003810 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003811 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3812 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003813 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003814
3815 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003816}
3817
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003818static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003819{
3820 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003821 u16 rgvswctl;
3822
3823 spin_lock_irq(&mchdev_lock);
3824
3825 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003826
3827 /* Ack interrupts, disable EFC interrupt */
3828 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3829 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3830 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3831 I915_WRITE(DEIIR, DE_PCU_EVENT);
3832 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3833
3834 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003835 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003836 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003837 rgvswctl |= MEMCTL_CMD_STS;
3838 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003839 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003840
Daniel Vetter92703882012-08-09 16:46:01 +02003841 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003842}
3843
Daniel Vetteracbe9472012-07-26 11:50:05 +02003844/* There's a funny hw issue where the hw returns all 0 when reading from
3845 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3846 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3847 * all limits and the gpu stuck at whatever frequency it is at atm).
3848 */
Akash Goel74ef1172015-03-06 11:07:19 +05303849static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003850{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003851 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003852
Daniel Vetter20b46e52012-07-26 11:16:14 +02003853 /* Only set the down limit when we've reached the lowest level to avoid
3854 * getting more interrupts, otherwise leave this clear. This prevents a
3855 * race in the hw when coming out of rc6: There's a tiny window where
3856 * the hw runs at the minimal clock before selecting the desired
3857 * frequency, if the down threshold expires in that window we will not
3858 * receive a down interrupt. */
Akash Goel74ef1172015-03-06 11:07:19 +05303859 if (IS_GEN9(dev_priv->dev)) {
3860 limits = (dev_priv->rps.max_freq_softlimit) << 23;
3861 if (val <= dev_priv->rps.min_freq_softlimit)
3862 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
3863 } else {
3864 limits = dev_priv->rps.max_freq_softlimit << 24;
3865 if (val <= dev_priv->rps.min_freq_softlimit)
3866 limits |= dev_priv->rps.min_freq_softlimit << 16;
3867 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02003868
3869 return limits;
3870}
3871
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003872static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3873{
3874 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05303875 u32 threshold_up = 0, threshold_down = 0; /* in % */
3876 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003877
3878 new_power = dev_priv->rps.power;
3879 switch (dev_priv->rps.power) {
3880 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003881 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003882 new_power = BETWEEN;
3883 break;
3884
3885 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003886 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003887 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003888 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003889 new_power = HIGH_POWER;
3890 break;
3891
3892 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003893 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 +01003894 new_power = BETWEEN;
3895 break;
3896 }
3897 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00003898 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003899 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00003900 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003901 new_power = HIGH_POWER;
3902 if (new_power == dev_priv->rps.power)
3903 return;
3904
3905 /* Note the units here are not exactly 1us, but 1280ns. */
3906 switch (new_power) {
3907 case LOW_POWER:
3908 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05303909 ei_up = 16000;
3910 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003911
3912 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05303913 ei_down = 32000;
3914 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003915 break;
3916
3917 case BETWEEN:
3918 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05303919 ei_up = 13000;
3920 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003921
3922 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05303923 ei_down = 32000;
3924 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003925 break;
3926
3927 case HIGH_POWER:
3928 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05303929 ei_up = 10000;
3930 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003931
3932 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05303933 ei_down = 32000;
3934 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003935 break;
3936 }
3937
Akash Goel8a586432015-03-06 11:07:18 +05303938 I915_WRITE(GEN6_RP_UP_EI,
3939 GT_INTERVAL_FROM_US(dev_priv, ei_up));
3940 I915_WRITE(GEN6_RP_UP_THRESHOLD,
3941 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
3942
3943 I915_WRITE(GEN6_RP_DOWN_EI,
3944 GT_INTERVAL_FROM_US(dev_priv, ei_down));
3945 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
3946 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
3947
3948 I915_WRITE(GEN6_RP_CONTROL,
3949 GEN6_RP_MEDIA_TURBO |
3950 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3951 GEN6_RP_MEDIA_IS_GFX |
3952 GEN6_RP_ENABLE |
3953 GEN6_RP_UP_BUSY_AVG |
3954 GEN6_RP_DOWN_IDLE_AVG);
3955
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003956 dev_priv->rps.power = new_power;
3957 dev_priv->rps.last_adj = 0;
3958}
3959
Chris Wilson2876ce72014-03-28 08:03:34 +00003960static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3961{
3962 u32 mask = 0;
3963
3964 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00003965 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00003966 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00003967 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00003968
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003969 mask &= dev_priv->pm_rps_events;
3970
Imre Deak59d02a12014-12-19 19:33:26 +02003971 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003972}
3973
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003974/* gen6_set_rps is called to update the frequency request, but should also be
3975 * called when the range (min_delay and max_delay) is modified so that we can
3976 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003977static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003978{
3979 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003980
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003981 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00003982 WARN_ON(val > dev_priv->rps.max_freq);
3983 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02003984
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003985 /* min/max delay may still have been modified so be sure to
3986 * write the limits value.
3987 */
3988 if (val != dev_priv->rps.cur_freq) {
3989 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003990
Akash Goel57041952015-03-06 11:07:17 +05303991 if (IS_GEN9(dev))
3992 I915_WRITE(GEN6_RPNSWREQ,
3993 GEN9_FREQUENCY(val));
3994 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003995 I915_WRITE(GEN6_RPNSWREQ,
3996 HSW_FREQUENCY(val));
3997 else
3998 I915_WRITE(GEN6_RPNSWREQ,
3999 GEN6_FREQUENCY(val) |
4000 GEN6_OFFSET(0) |
4001 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004002 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004003
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004004 /* Make sure we continue to get interrupts
4005 * until we hit the minimum or maximum frequencies.
4006 */
Akash Goel74ef1172015-03-06 11:07:19 +05304007 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004008 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004009
Ben Widawskyd5570a72012-09-07 19:43:41 -07004010 POSTING_READ(GEN6_RPNSWREQ);
4011
Ben Widawskyb39fb292014-03-19 18:31:11 -07004012 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02004013 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004014}
4015
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004016static void valleyview_set_rps(struct drm_device *dev, u8 val)
4017{
4018 struct drm_i915_private *dev_priv = dev->dev_private;
4019
4020 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004021 WARN_ON(val > dev_priv->rps.max_freq);
4022 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004023
4024 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4025 "Odd GPU freq value\n"))
4026 val &= ~1;
4027
4028 if (val != dev_priv->rps.cur_freq)
4029 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
4030
4031 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4032
4033 dev_priv->rps.cur_freq = val;
4034 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4035}
4036
Deepak S76c3552f2014-01-30 23:08:16 +05304037/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
4038 *
4039 * * If Gfx is Idle, then
4040 * 1. Mask Turbo interrupts
4041 * 2. Bring up Gfx clock
4042 * 3. Change the freq to Rpn and wait till P-Unit updates freq
4043 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
4044 * 5. Unmask Turbo interrupts
4045*/
4046static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4047{
Deepak S5549d252014-06-28 11:26:11 +05304048 struct drm_device *dev = dev_priv->dev;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004049 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304050
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004051 /* CHV and latest VLV don't need to force the gfx clock */
4052 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Chris Wilsonaed242f2015-03-18 09:48:21 +00004053 valleyview_set_rps(dev_priv->dev, val);
Deepak S5549d252014-06-28 11:26:11 +05304054 return;
4055 }
4056
Deepak S76c3552f2014-01-30 23:08:16 +05304057 /*
4058 * When we are idle. Drop to min voltage state.
4059 */
4060
Chris Wilsonaed242f2015-03-18 09:48:21 +00004061 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304062 return;
4063
4064 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02004065 I915_WRITE(GEN6_PMINTRMSK,
4066 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05304067
Imre Deak650ad972014-04-18 16:35:02 +03004068 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05304069
Chris Wilsonaed242f2015-03-18 09:48:21 +00004070 dev_priv->rps.cur_freq = val;
Deepak S76c3552f2014-01-30 23:08:16 +05304071
Chris Wilsonaed242f2015-03-18 09:48:21 +00004072 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Deepak S76c3552f2014-01-30 23:08:16 +05304073
4074 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02004075 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05304076 DRM_ERROR("timed out waiting for Punit\n");
4077
Imre Deak650ad972014-04-18 16:35:02 +03004078 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05304079
Chris Wilsonaed242f2015-03-18 09:48:21 +00004080 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Deepak S76c3552f2014-01-30 23:08:16 +05304081}
4082
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004083void gen6_rps_busy(struct drm_i915_private *dev_priv)
4084{
4085 mutex_lock(&dev_priv->rps.hw_lock);
4086 if (dev_priv->rps.enabled) {
4087 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4088 gen6_rps_reset_ei(dev_priv);
4089 I915_WRITE(GEN6_PMINTRMSK,
4090 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4091 }
4092 mutex_unlock(&dev_priv->rps.hw_lock);
4093}
4094
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004095void gen6_rps_idle(struct drm_i915_private *dev_priv)
4096{
Damien Lespiau691bb712013-12-12 14:36:36 +00004097 struct drm_device *dev = dev_priv->dev;
4098
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004099 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004100 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02004101 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304102 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004103 else
Chris Wilsonaed242f2015-03-18 09:48:21 +00004104 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004105 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004106 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004107 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004108 mutex_unlock(&dev_priv->rps.hw_lock);
4109}
4110
4111void gen6_rps_boost(struct drm_i915_private *dev_priv)
4112{
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004113 u32 val;
4114
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004115 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004116 val = dev_priv->rps.max_freq_softlimit;
4117 if (dev_priv->rps.enabled &&
4118 dev_priv->mm.busy &&
4119 dev_priv->rps.cur_freq < val) {
4120 intel_set_rps(dev_priv->dev, val);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004121 dev_priv->rps.last_adj = 0;
4122 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004123 mutex_unlock(&dev_priv->rps.hw_lock);
4124}
4125
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004126void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004127{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004128 if (IS_VALLEYVIEW(dev))
4129 valleyview_set_rps(dev, val);
4130 else
4131 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004132}
4133
Zhe Wang20e49362014-11-04 17:07:05 +00004134static void gen9_disable_rps(struct drm_device *dev)
4135{
4136 struct drm_i915_private *dev_priv = dev->dev_private;
4137
4138 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004139 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004140}
4141
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004142static void gen6_disable_rps(struct drm_device *dev)
4143{
4144 struct drm_i915_private *dev_priv = dev->dev_private;
4145
4146 I915_WRITE(GEN6_RC_CONTROL, 0);
4147 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004148}
4149
Deepak S38807742014-05-23 21:00:15 +05304150static void cherryview_disable_rps(struct drm_device *dev)
4151{
4152 struct drm_i915_private *dev_priv = dev->dev_private;
4153
4154 I915_WRITE(GEN6_RC_CONTROL, 0);
4155}
4156
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004157static void valleyview_disable_rps(struct drm_device *dev)
4158{
4159 struct drm_i915_private *dev_priv = dev->dev_private;
4160
Deepak S98a2e5f2014-08-18 10:35:27 -07004161 /* we're doing forcewake before Disabling RC6,
4162 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004163 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004164
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004165 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004166
Mika Kuoppala59bad942015-01-16 11:34:40 +02004167 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004168}
4169
Ben Widawskydc39fff2013-10-18 12:32:07 -07004170static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4171{
Imre Deak91ca6892014-04-14 20:24:25 +03004172 if (IS_VALLEYVIEW(dev)) {
4173 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4174 mode = GEN6_RC_CTL_RC6_ENABLE;
4175 else
4176 mode = 0;
4177 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004178 if (HAS_RC6p(dev))
4179 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4180 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4181 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4182 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4183
4184 else
4185 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4186 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004187}
4188
Imre Deake6069ca2014-04-18 16:01:02 +03004189static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004190{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004191 /* No RC6 before Ironlake */
4192 if (INTEL_INFO(dev)->gen < 5)
4193 return 0;
4194
Imre Deake6069ca2014-04-18 16:01:02 +03004195 /* RC6 is only on Ironlake mobile not on desktop */
4196 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4197 return 0;
4198
Daniel Vetter456470e2012-08-08 23:35:40 +02004199 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004200 if (enable_rc6 >= 0) {
4201 int mask;
4202
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004203 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004204 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4205 INTEL_RC6pp_ENABLE;
4206 else
4207 mask = INTEL_RC6_ENABLE;
4208
4209 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004210 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4211 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004212
4213 return enable_rc6 & mask;
4214 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004215
Chris Wilson6567d742012-11-10 10:00:06 +00004216 /* Disable RC6 on Ironlake */
4217 if (INTEL_INFO(dev)->gen == 5)
4218 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004219
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004220 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004221 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004222
4223 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004224}
4225
Imre Deake6069ca2014-04-18 16:01:02 +03004226int intel_enable_rc6(const struct drm_device *dev)
4227{
4228 return i915.enable_rc6;
4229}
4230
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004231static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004232{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004233 struct drm_i915_private *dev_priv = dev->dev_private;
4234 uint32_t rp_state_cap;
4235 u32 ddcc_status = 0;
4236 int ret;
4237
4238 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004239 /* All of these values are in units of 50MHz */
4240 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004241 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004242 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004243 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004244 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Akash Goelcee991c2015-03-06 11:07:16 +05304245 if (IS_SKYLAKE(dev)) {
4246 /* Store the frequency values in 16.66 MHZ units, which is
4247 the natural hardware unit for SKL */
4248 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4249 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4250 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4251 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004252 /* hw_max = RP0 until we check for overclocking */
4253 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4254
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004255 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4256 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4257 ret = sandybridge_pcode_read(dev_priv,
4258 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4259 &ddcc_status);
4260 if (0 == ret)
4261 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004262 clamp_t(u8,
4263 ((ddcc_status >> 8) & 0xff),
4264 dev_priv->rps.min_freq,
4265 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004266 }
4267
Chris Wilsonaed242f2015-03-18 09:48:21 +00004268 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4269
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004270 /* Preserve min/max settings in case of re-init */
4271 if (dev_priv->rps.max_freq_softlimit == 0)
4272 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4273
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004274 if (dev_priv->rps.min_freq_softlimit == 0) {
4275 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4276 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004277 /* max(RPe, 450 MHz) */
4278 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004279 else
4280 dev_priv->rps.min_freq_softlimit =
4281 dev_priv->rps.min_freq;
4282 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004283}
4284
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004285/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004286static void gen9_enable_rps(struct drm_device *dev)
4287{
4288 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004289
4290 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4291
Damien Lespiauba1c5542015-01-16 18:07:26 +00004292 gen6_init_rps_frequencies(dev);
4293
Akash Goel0beb0592015-03-06 11:07:20 +05304294 /* Program defaults and thresholds for RPS*/
4295 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4296 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004297
Akash Goel0beb0592015-03-06 11:07:20 +05304298 /* 1 second timeout*/
4299 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4300 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4301
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004302 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004303
Akash Goel0beb0592015-03-06 11:07:20 +05304304 /* Leaning on the below call to gen6_set_rps to program/setup the
4305 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4306 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4307 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4308 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004309
4310 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4311}
4312
4313static void gen9_enable_rc6(struct drm_device *dev)
4314{
4315 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004316 struct intel_engine_cs *ring;
4317 uint32_t rc6_mask = 0;
4318 int unused;
4319
4320 /* 1a: Software RC state - RC0 */
4321 I915_WRITE(GEN6_RC_STATE, 0);
4322
4323 /* 1b: Get forcewake during program sequence. Although the driver
4324 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004325 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004326
4327 /* 2a: Disable RC states. */
4328 I915_WRITE(GEN6_RC_CONTROL, 0);
4329
4330 /* 2b: Program RC6 thresholds.*/
4331 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4332 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4333 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4334 for_each_ring(ring, dev_priv, unused)
4335 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4336 I915_WRITE(GEN6_RC_SLEEP, 0);
4337 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4338
Zhe Wang38c23522015-01-20 12:23:04 +00004339 /* 2c: Program Coarse Power Gating Policies. */
4340 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4341 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4342
Zhe Wang20e49362014-11-04 17:07:05 +00004343 /* 3a: Enable RC6 */
4344 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4345 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4346 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4347 "on" : "off");
4348 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4349 GEN6_RC_CTL_EI_MODE(1) |
4350 rc6_mask);
4351
Zhe Wang38c23522015-01-20 12:23:04 +00004352 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4353 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4354
Mika Kuoppala59bad942015-01-16 11:34:40 +02004355 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004356
4357}
4358
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004359static void gen8_enable_rps(struct drm_device *dev)
4360{
4361 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004362 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004363 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004364 int unused;
4365
4366 /* 1a: Software RC state - RC0 */
4367 I915_WRITE(GEN6_RC_STATE, 0);
4368
4369 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4370 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004371 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004372
4373 /* 2a: Disable RC states. */
4374 I915_WRITE(GEN6_RC_CONTROL, 0);
4375
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004376 /* Initialize rps frequencies */
4377 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004378
4379 /* 2b: Program RC6 thresholds.*/
4380 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4381 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4382 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4383 for_each_ring(ring, dev_priv, unused)
4384 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4385 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004386 if (IS_BROADWELL(dev))
4387 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4388 else
4389 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004390
4391 /* 3: Enable RC6 */
4392 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4393 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004394 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004395 if (IS_BROADWELL(dev))
4396 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4397 GEN7_RC_CTL_TO_MODE |
4398 rc6_mask);
4399 else
4400 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4401 GEN6_RC_CTL_EI_MODE(1) |
4402 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004403
4404 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004405 I915_WRITE(GEN6_RPNSWREQ,
4406 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4407 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4408 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004409 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4410 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004411
Daniel Vetter7526ed72014-09-29 15:07:19 +02004412 /* Docs recommend 900MHz, and 300 MHz respectively */
4413 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4414 dev_priv->rps.max_freq_softlimit << 24 |
4415 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004416
Daniel Vetter7526ed72014-09-29 15:07:19 +02004417 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4418 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4419 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4420 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004421
Daniel Vetter7526ed72014-09-29 15:07:19 +02004422 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004423
4424 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004425 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);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004432
Daniel Vetter7526ed72014-09-29 15:07:19 +02004433 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004434
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004435 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004436 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004437
Mika Kuoppala59bad942015-01-16 11:34:40 +02004438 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004439}
4440
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004441static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004442{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004443 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004444 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004445 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004446 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004447 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004448 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004449
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004450 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004451
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004452 /* Here begins a magic sequence of register writes to enable
4453 * auto-downclocking.
4454 *
4455 * Perhaps there might be some value in exposing these to
4456 * userspace...
4457 */
4458 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004459
4460 /* Clear the DBG now so we don't confuse earlier errors */
4461 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4462 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4463 I915_WRITE(GTFIFODBG, gtfifodbg);
4464 }
4465
Mika Kuoppala59bad942015-01-16 11:34:40 +02004466 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004467
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004468 /* Initialize rps frequencies */
4469 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004470
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004471 /* disable the counters and set deterministic thresholds */
4472 I915_WRITE(GEN6_RC_CONTROL, 0);
4473
4474 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4475 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4476 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4477 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4478 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4479
Chris Wilsonb4519512012-05-11 14:29:30 +01004480 for_each_ring(ring, dev_priv, i)
4481 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004482
4483 I915_WRITE(GEN6_RC_SLEEP, 0);
4484 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004485 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004486 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4487 else
4488 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004489 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004490 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4491
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004492 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004493 rc6_mode = intel_enable_rc6(dev_priv->dev);
4494 if (rc6_mode & INTEL_RC6_ENABLE)
4495 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4496
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004497 /* We don't use those on Haswell */
4498 if (!IS_HASWELL(dev)) {
4499 if (rc6_mode & INTEL_RC6p_ENABLE)
4500 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004501
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004502 if (rc6_mode & INTEL_RC6pp_ENABLE)
4503 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4504 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004505
Ben Widawskydc39fff2013-10-18 12:32:07 -07004506 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004507
4508 I915_WRITE(GEN6_RC_CONTROL,
4509 rc6_mask |
4510 GEN6_RC_CTL_EI_MODE(1) |
4511 GEN6_RC_CTL_HW_ENABLE);
4512
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004513 /* Power down if completely idle for over 50ms */
4514 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004515 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004516
Ben Widawsky42c05262012-09-26 10:34:00 -07004517 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004518 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004519 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004520
4521 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4522 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4523 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004524 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004525 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004526 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004527 }
4528
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004529 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004530 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004531
Ben Widawsky31643d52012-09-26 10:34:01 -07004532 rc6vids = 0;
4533 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4534 if (IS_GEN6(dev) && ret) {
4535 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4536 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4537 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4538 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4539 rc6vids &= 0xffff00;
4540 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4541 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4542 if (ret)
4543 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4544 }
4545
Mika Kuoppala59bad942015-01-16 11:34:40 +02004546 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004547}
4548
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004549static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004550{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004551 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004552 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004553 unsigned int gpu_freq;
4554 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004555 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004556 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004557
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004558 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004559
Ben Widawskyeda79642013-10-07 17:15:48 -03004560 policy = cpufreq_cpu_get(0);
4561 if (policy) {
4562 max_ia_freq = policy->cpuinfo.max_freq;
4563 cpufreq_cpu_put(policy);
4564 } else {
4565 /*
4566 * Default to measured freq if none found, PCU will ensure we
4567 * don't go over
4568 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004569 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004570 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004571
4572 /* Convert from kHz to MHz */
4573 max_ia_freq /= 1000;
4574
Ben Widawsky153b4b952013-10-22 22:05:09 -07004575 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004576 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4577 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004578
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004579 /*
4580 * For each potential GPU frequency, load a ring frequency we'd like
4581 * to use for memory access. We do this by specifying the IA frequency
4582 * the PCU should use as a reference to determine the ring frequency.
4583 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004584 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004585 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004586 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004587 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004588
Ben Widawsky46c764d2013-11-02 21:07:49 -07004589 if (INTEL_INFO(dev)->gen >= 8) {
4590 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4591 ring_freq = max(min_ring_freq, gpu_freq);
4592 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004593 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004594 ring_freq = max(min_ring_freq, ring_freq);
4595 /* leave ia_freq as the default, chosen by cpufreq */
4596 } else {
4597 /* On older processors, there is no separate ring
4598 * clock domain, so in order to boost the bandwidth
4599 * of the ring, we need to upclock the CPU (ia_freq).
4600 *
4601 * For GPU frequencies less than 750MHz,
4602 * just use the lowest ring freq.
4603 */
4604 if (gpu_freq < min_freq)
4605 ia_freq = 800;
4606 else
4607 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4608 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4609 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004610
Ben Widawsky42c05262012-09-26 10:34:00 -07004611 sandybridge_pcode_write(dev_priv,
4612 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004613 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4614 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4615 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004616 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004617}
4618
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004619void gen6_update_ring_freq(struct drm_device *dev)
4620{
4621 struct drm_i915_private *dev_priv = dev->dev_private;
4622
4623 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4624 return;
4625
4626 mutex_lock(&dev_priv->rps.hw_lock);
4627 __gen6_update_ring_freq(dev);
4628 mutex_unlock(&dev_priv->rps.hw_lock);
4629}
4630
Ville Syrjälä03af2042014-06-28 02:03:53 +03004631static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304632{
Deepak S095acd52015-01-17 11:05:59 +05304633 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304634 u32 val, rp0;
4635
Deepak S095acd52015-01-17 11:05:59 +05304636 if (dev->pdev->revision >= 0x20) {
4637 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304638
Deepak S095acd52015-01-17 11:05:59 +05304639 switch (INTEL_INFO(dev)->eu_total) {
4640 case 8:
4641 /* (2 * 4) config */
4642 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4643 break;
4644 case 12:
4645 /* (2 * 6) config */
4646 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4647 break;
4648 case 16:
4649 /* (2 * 8) config */
4650 default:
4651 /* Setting (2 * 8) Min RP0 for any other combination */
4652 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4653 break;
4654 }
4655 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4656 } else {
4657 /* For pre-production hardware */
4658 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4659 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4660 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4661 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304662 return rp0;
4663}
4664
4665static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4666{
4667 u32 val, rpe;
4668
4669 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4670 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4671
4672 return rpe;
4673}
4674
Deepak S7707df42014-07-12 18:46:14 +05304675static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4676{
Deepak S095acd52015-01-17 11:05:59 +05304677 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304678 u32 val, rp1;
4679
Deepak S095acd52015-01-17 11:05:59 +05304680 if (dev->pdev->revision >= 0x20) {
4681 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4682 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4683 } else {
4684 /* For pre-production hardware */
4685 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4686 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4687 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4688 }
Deepak S7707df42014-07-12 18:46:14 +05304689 return rp1;
4690}
4691
Ville Syrjälä03af2042014-06-28 02:03:53 +03004692static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304693{
Deepak S095acd52015-01-17 11:05:59 +05304694 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304695 u32 val, rpn;
4696
Deepak S095acd52015-01-17 11:05:59 +05304697 if (dev->pdev->revision >= 0x20) {
4698 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4699 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4700 FB_GFX_FREQ_FUSE_MASK);
4701 } else { /* For pre-production hardware */
4702 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4703 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4704 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4705 }
4706
Deepak S2b6b3a02014-05-27 15:59:30 +05304707 return rpn;
4708}
4709
Deepak Sf8f2b002014-07-10 13:16:21 +05304710static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4711{
4712 u32 val, rp1;
4713
4714 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4715
4716 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4717
4718 return rp1;
4719}
4720
Ville Syrjälä03af2042014-06-28 02:03:53 +03004721static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004722{
4723 u32 val, rp0;
4724
Jani Nikula64936252013-05-22 15:36:20 +03004725 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004726
4727 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4728 /* Clamp to max */
4729 rp0 = min_t(u32, rp0, 0xea);
4730
4731 return rp0;
4732}
4733
4734static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4735{
4736 u32 val, rpe;
4737
Jani Nikula64936252013-05-22 15:36:20 +03004738 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004739 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004740 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004741 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4742
4743 return rpe;
4744}
4745
Ville Syrjälä03af2042014-06-28 02:03:53 +03004746static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004747{
Jani Nikula64936252013-05-22 15:36:20 +03004748 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004749}
4750
Imre Deakae484342014-03-31 15:10:44 +03004751/* Check that the pctx buffer wasn't move under us. */
4752static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4753{
4754 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4755
4756 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4757 dev_priv->vlv_pctx->stolen->start);
4758}
4759
Deepak S38807742014-05-23 21:00:15 +05304760
4761/* Check that the pcbr address is not empty. */
4762static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4763{
4764 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4765
4766 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4767}
4768
4769static void cherryview_setup_pctx(struct drm_device *dev)
4770{
4771 struct drm_i915_private *dev_priv = dev->dev_private;
4772 unsigned long pctx_paddr, paddr;
4773 struct i915_gtt *gtt = &dev_priv->gtt;
4774 u32 pcbr;
4775 int pctx_size = 32*1024;
4776
4777 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4778
4779 pcbr = I915_READ(VLV_PCBR);
4780 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004781 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304782 paddr = (dev_priv->mm.stolen_base +
4783 (gtt->stolen_size - pctx_size));
4784
4785 pctx_paddr = (paddr & (~4095));
4786 I915_WRITE(VLV_PCBR, pctx_paddr);
4787 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004788
4789 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304790}
4791
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004792static void valleyview_setup_pctx(struct drm_device *dev)
4793{
4794 struct drm_i915_private *dev_priv = dev->dev_private;
4795 struct drm_i915_gem_object *pctx;
4796 unsigned long pctx_paddr;
4797 u32 pcbr;
4798 int pctx_size = 24*1024;
4799
Imre Deak17b0c1f2014-02-11 21:39:06 +02004800 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4801
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004802 pcbr = I915_READ(VLV_PCBR);
4803 if (pcbr) {
4804 /* BIOS set it up already, grab the pre-alloc'd space */
4805 int pcbr_offset;
4806
4807 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4808 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4809 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004810 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004811 pctx_size);
4812 goto out;
4813 }
4814
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004815 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4816
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004817 /*
4818 * From the Gunit register HAS:
4819 * The Gfx driver is expected to program this register and ensure
4820 * proper allocation within Gfx stolen memory. For example, this
4821 * register should be programmed such than the PCBR range does not
4822 * overlap with other ranges, such as the frame buffer, protected
4823 * memory, or any other relevant ranges.
4824 */
4825 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4826 if (!pctx) {
4827 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4828 return;
4829 }
4830
4831 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4832 I915_WRITE(VLV_PCBR, pctx_paddr);
4833
4834out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004835 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004836 dev_priv->vlv_pctx = pctx;
4837}
4838
Imre Deakae484342014-03-31 15:10:44 +03004839static void valleyview_cleanup_pctx(struct drm_device *dev)
4840{
4841 struct drm_i915_private *dev_priv = dev->dev_private;
4842
4843 if (WARN_ON(!dev_priv->vlv_pctx))
4844 return;
4845
4846 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4847 dev_priv->vlv_pctx = NULL;
4848}
4849
Imre Deak4e805192014-04-14 20:24:41 +03004850static void valleyview_init_gt_powersave(struct drm_device *dev)
4851{
4852 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004853 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004854
4855 valleyview_setup_pctx(dev);
4856
4857 mutex_lock(&dev_priv->rps.hw_lock);
4858
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004859 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4860 switch ((val >> 6) & 3) {
4861 case 0:
4862 case 1:
4863 dev_priv->mem_freq = 800;
4864 break;
4865 case 2:
4866 dev_priv->mem_freq = 1066;
4867 break;
4868 case 3:
4869 dev_priv->mem_freq = 1333;
4870 break;
4871 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004872 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004873
Imre Deak4e805192014-04-14 20:24:41 +03004874 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4875 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4876 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004877 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004878 dev_priv->rps.max_freq);
4879
4880 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4881 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004882 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004883 dev_priv->rps.efficient_freq);
4884
Deepak Sf8f2b002014-07-10 13:16:21 +05304885 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4886 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004887 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304888 dev_priv->rps.rp1_freq);
4889
Imre Deak4e805192014-04-14 20:24:41 +03004890 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4891 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004892 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004893 dev_priv->rps.min_freq);
4894
Chris Wilsonaed242f2015-03-18 09:48:21 +00004895 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4896
Imre Deak4e805192014-04-14 20:24:41 +03004897 /* Preserve min/max settings in case of re-init */
4898 if (dev_priv->rps.max_freq_softlimit == 0)
4899 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4900
4901 if (dev_priv->rps.min_freq_softlimit == 0)
4902 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4903
4904 mutex_unlock(&dev_priv->rps.hw_lock);
4905}
4906
Deepak S38807742014-05-23 21:00:15 +05304907static void cherryview_init_gt_powersave(struct drm_device *dev)
4908{
Deepak S2b6b3a02014-05-27 15:59:30 +05304909 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004910 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304911
Deepak S38807742014-05-23 21:00:15 +05304912 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304913
4914 mutex_lock(&dev_priv->rps.hw_lock);
4915
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004916 mutex_lock(&dev_priv->dpio_lock);
4917 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4918 mutex_unlock(&dev_priv->dpio_lock);
4919
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004920 switch ((val >> 2) & 0x7) {
4921 case 0:
4922 case 1:
4923 dev_priv->rps.cz_freq = 200;
4924 dev_priv->mem_freq = 1600;
4925 break;
4926 case 2:
4927 dev_priv->rps.cz_freq = 267;
4928 dev_priv->mem_freq = 1600;
4929 break;
4930 case 3:
4931 dev_priv->rps.cz_freq = 333;
4932 dev_priv->mem_freq = 2000;
4933 break;
4934 case 4:
4935 dev_priv->rps.cz_freq = 320;
4936 dev_priv->mem_freq = 1600;
4937 break;
4938 case 5:
4939 dev_priv->rps.cz_freq = 400;
4940 dev_priv->mem_freq = 1600;
4941 break;
4942 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004943 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004944
Deepak S2b6b3a02014-05-27 15:59:30 +05304945 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4946 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4947 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004948 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304949 dev_priv->rps.max_freq);
4950
4951 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4952 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004953 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304954 dev_priv->rps.efficient_freq);
4955
Deepak S7707df42014-07-12 18:46:14 +05304956 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4957 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004958 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304959 dev_priv->rps.rp1_freq);
4960
Deepak S2b6b3a02014-05-27 15:59:30 +05304961 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4962 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004963 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304964 dev_priv->rps.min_freq);
4965
Ville Syrjälä1c147622014-08-18 14:42:43 +03004966 WARN_ONCE((dev_priv->rps.max_freq |
4967 dev_priv->rps.efficient_freq |
4968 dev_priv->rps.rp1_freq |
4969 dev_priv->rps.min_freq) & 1,
4970 "Odd GPU freq values\n");
4971
Chris Wilsonaed242f2015-03-18 09:48:21 +00004972 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4973
Deepak S2b6b3a02014-05-27 15:59:30 +05304974 /* Preserve min/max settings in case of re-init */
4975 if (dev_priv->rps.max_freq_softlimit == 0)
4976 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4977
4978 if (dev_priv->rps.min_freq_softlimit == 0)
4979 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4980
4981 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304982}
4983
Imre Deak4e805192014-04-14 20:24:41 +03004984static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4985{
4986 valleyview_cleanup_pctx(dev);
4987}
4988
Deepak S38807742014-05-23 21:00:15 +05304989static void cherryview_enable_rps(struct drm_device *dev)
4990{
4991 struct drm_i915_private *dev_priv = dev->dev_private;
4992 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304993 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304994 int i;
4995
4996 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4997
4998 gtfifodbg = I915_READ(GTFIFODBG);
4999 if (gtfifodbg) {
5000 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5001 gtfifodbg);
5002 I915_WRITE(GTFIFODBG, gtfifodbg);
5003 }
5004
5005 cherryview_check_pctx(dev_priv);
5006
5007 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5008 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005009 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305010
Ville Syrjälä160614a2015-01-19 13:50:47 +02005011 /* Disable RC states. */
5012 I915_WRITE(GEN6_RC_CONTROL, 0);
5013
Deepak S38807742014-05-23 21:00:15 +05305014 /* 2a: Program RC6 thresholds.*/
5015 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5016 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5017 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5018
5019 for_each_ring(ring, dev_priv, i)
5020 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5021 I915_WRITE(GEN6_RC_SLEEP, 0);
5022
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005023 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
5024 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05305025
5026 /* allows RC6 residency counter to work */
5027 I915_WRITE(VLV_COUNTER_CONTROL,
5028 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5029 VLV_MEDIA_RC6_COUNT_EN |
5030 VLV_RENDER_RC6_COUNT_EN));
5031
5032 /* For now we assume BIOS is allocating and populating the PCBR */
5033 pcbr = I915_READ(VLV_PCBR);
5034
Deepak S38807742014-05-23 21:00:15 +05305035 /* 3: Enable RC6 */
5036 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5037 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005038 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305039
5040 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5041
Deepak S2b6b3a02014-05-27 15:59:30 +05305042 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005043 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305044 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5045 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5046 I915_WRITE(GEN6_RP_UP_EI, 66000);
5047 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5048
5049 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5050
5051 /* 5: Enable RPS */
5052 I915_WRITE(GEN6_RP_CONTROL,
5053 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005054 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305055 GEN6_RP_ENABLE |
5056 GEN6_RP_UP_BUSY_AVG |
5057 GEN6_RP_DOWN_IDLE_AVG);
5058
5059 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5060
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005061 /* RPS code assumes GPLL is used */
5062 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5063
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005064 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05305065 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5066
5067 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5068 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005069 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305070 dev_priv->rps.cur_freq);
5071
5072 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005073 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305074 dev_priv->rps.efficient_freq);
5075
5076 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5077
Mika Kuoppala59bad942015-01-16 11:34:40 +02005078 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305079}
5080
Jesse Barnes0a073b82013-04-17 15:54:58 -07005081static void valleyview_enable_rps(struct drm_device *dev)
5082{
5083 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005084 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005085 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005086 int i;
5087
5088 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5089
Imre Deakae484342014-03-31 15:10:44 +03005090 valleyview_check_pctx(dev_priv);
5091
Jesse Barnes0a073b82013-04-17 15:54:58 -07005092 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005093 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5094 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005095 I915_WRITE(GTFIFODBG, gtfifodbg);
5096 }
5097
Deepak Sc8d9a592013-11-23 14:55:42 +05305098 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005099 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005100
Ville Syrjälä160614a2015-01-19 13:50:47 +02005101 /* Disable RC states. */
5102 I915_WRITE(GEN6_RC_CONTROL, 0);
5103
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005104 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005105 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5106 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5107 I915_WRITE(GEN6_RP_UP_EI, 66000);
5108 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5109
5110 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5111
5112 I915_WRITE(GEN6_RP_CONTROL,
5113 GEN6_RP_MEDIA_TURBO |
5114 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5115 GEN6_RP_MEDIA_IS_GFX |
5116 GEN6_RP_ENABLE |
5117 GEN6_RP_UP_BUSY_AVG |
5118 GEN6_RP_DOWN_IDLE_CONT);
5119
5120 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5121 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5122 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5123
5124 for_each_ring(ring, dev_priv, i)
5125 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5126
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08005127 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005128
5129 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005130 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005131 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5132 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005133 VLV_MEDIA_RC6_COUNT_EN |
5134 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005135
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005136 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005137 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005138
5139 intel_print_rc6_info(dev, rc6_mode);
5140
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005141 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005142
Jani Nikula64936252013-05-22 15:36:20 +03005143 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005144
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005145 /* RPS code assumes GPLL is used */
5146 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5147
Ville Syrjäläc8e96272014-11-07 21:33:44 +02005148 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07005149 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5150
Ben Widawskyb39fb292014-03-19 18:31:11 -07005151 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005152 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005153 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005154 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005155
Ville Syrjälä73008b92013-06-25 19:21:01 +03005156 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005157 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005158 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005159
Ben Widawskyb39fb292014-03-19 18:31:11 -07005160 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005161
Mika Kuoppala59bad942015-01-16 11:34:40 +02005162 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005163}
5164
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005165static unsigned long intel_pxfreq(u32 vidfreq)
5166{
5167 unsigned long freq;
5168 int div = (vidfreq & 0x3f0000) >> 16;
5169 int post = (vidfreq & 0x3000) >> 12;
5170 int pre = (vidfreq & 0x7);
5171
5172 if (!pre)
5173 return 0;
5174
5175 freq = ((div * 133333) / ((1<<post) * pre));
5176
5177 return freq;
5178}
5179
Daniel Vettereb48eb02012-04-26 23:28:12 +02005180static const struct cparams {
5181 u16 i;
5182 u16 t;
5183 u16 m;
5184 u16 c;
5185} cparams[] = {
5186 { 1, 1333, 301, 28664 },
5187 { 1, 1066, 294, 24460 },
5188 { 1, 800, 294, 25192 },
5189 { 0, 1333, 276, 27605 },
5190 { 0, 1066, 276, 27605 },
5191 { 0, 800, 231, 23784 },
5192};
5193
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005194static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005195{
5196 u64 total_count, diff, ret;
5197 u32 count1, count2, count3, m = 0, c = 0;
5198 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5199 int i;
5200
Daniel Vetter02d71952012-08-09 16:44:54 +02005201 assert_spin_locked(&mchdev_lock);
5202
Daniel Vetter20e4d402012-08-08 23:35:39 +02005203 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005204
5205 /* Prevent division-by-zero if we are asking too fast.
5206 * Also, we don't get interesting results if we are polling
5207 * faster than once in 10ms, so just return the saved value
5208 * in such cases.
5209 */
5210 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005211 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005212
5213 count1 = I915_READ(DMIEC);
5214 count2 = I915_READ(DDREC);
5215 count3 = I915_READ(CSIEC);
5216
5217 total_count = count1 + count2 + count3;
5218
5219 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005220 if (total_count < dev_priv->ips.last_count1) {
5221 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005222 diff += total_count;
5223 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005224 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005225 }
5226
5227 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005228 if (cparams[i].i == dev_priv->ips.c_m &&
5229 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005230 m = cparams[i].m;
5231 c = cparams[i].c;
5232 break;
5233 }
5234 }
5235
5236 diff = div_u64(diff, diff1);
5237 ret = ((m * diff) + c);
5238 ret = div_u64(ret, 10);
5239
Daniel Vetter20e4d402012-08-08 23:35:39 +02005240 dev_priv->ips.last_count1 = total_count;
5241 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005242
Daniel Vetter20e4d402012-08-08 23:35:39 +02005243 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005244
5245 return ret;
5246}
5247
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005248unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5249{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005250 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005251 unsigned long val;
5252
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005253 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005254 return 0;
5255
5256 spin_lock_irq(&mchdev_lock);
5257
5258 val = __i915_chipset_val(dev_priv);
5259
5260 spin_unlock_irq(&mchdev_lock);
5261
5262 return val;
5263}
5264
Daniel Vettereb48eb02012-04-26 23:28:12 +02005265unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5266{
5267 unsigned long m, x, b;
5268 u32 tsfs;
5269
5270 tsfs = I915_READ(TSFS);
5271
5272 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5273 x = I915_READ8(TR1);
5274
5275 b = tsfs & TSFS_INTR_MASK;
5276
5277 return ((m * x) / 127) - b;
5278}
5279
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005280static int _pxvid_to_vd(u8 pxvid)
5281{
5282 if (pxvid == 0)
5283 return 0;
5284
5285 if (pxvid >= 8 && pxvid < 31)
5286 pxvid = 31;
5287
5288 return (pxvid + 2) * 125;
5289}
5290
5291static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005292{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005293 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005294 const int vd = _pxvid_to_vd(pxvid);
5295 const int vm = vd - 1125;
5296
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005297 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005298 return vm > 0 ? vm : 0;
5299
5300 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005301}
5302
Daniel Vetter02d71952012-08-09 16:44:54 +02005303static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005304{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005305 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005306 u32 count;
5307
Daniel Vetter02d71952012-08-09 16:44:54 +02005308 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005309
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005310 now = ktime_get_raw_ns();
5311 diffms = now - dev_priv->ips.last_time2;
5312 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005313
5314 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005315 if (!diffms)
5316 return;
5317
5318 count = I915_READ(GFXEC);
5319
Daniel Vetter20e4d402012-08-08 23:35:39 +02005320 if (count < dev_priv->ips.last_count2) {
5321 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005322 diff += count;
5323 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005324 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005325 }
5326
Daniel Vetter20e4d402012-08-08 23:35:39 +02005327 dev_priv->ips.last_count2 = count;
5328 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005329
5330 /* More magic constants... */
5331 diff = diff * 1181;
5332 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005333 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005334}
5335
Daniel Vetter02d71952012-08-09 16:44:54 +02005336void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5337{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005338 struct drm_device *dev = dev_priv->dev;
5339
5340 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005341 return;
5342
Daniel Vetter92703882012-08-09 16:46:01 +02005343 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005344
5345 __i915_update_gfx_val(dev_priv);
5346
Daniel Vetter92703882012-08-09 16:46:01 +02005347 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005348}
5349
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005350static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005351{
5352 unsigned long t, corr, state1, corr2, state2;
5353 u32 pxvid, ext_v;
5354
Daniel Vetter02d71952012-08-09 16:44:54 +02005355 assert_spin_locked(&mchdev_lock);
5356
Ben Widawskyb39fb292014-03-19 18:31:11 -07005357 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005358 pxvid = (pxvid >> 24) & 0x7f;
5359 ext_v = pvid_to_extvid(dev_priv, pxvid);
5360
5361 state1 = ext_v;
5362
5363 t = i915_mch_val(dev_priv);
5364
5365 /* Revel in the empirically derived constants */
5366
5367 /* Correction factor in 1/100000 units */
5368 if (t > 80)
5369 corr = ((t * 2349) + 135940);
5370 else if (t >= 50)
5371 corr = ((t * 964) + 29317);
5372 else /* < 50 */
5373 corr = ((t * 301) + 1004);
5374
5375 corr = corr * ((150142 * state1) / 10000 - 78642);
5376 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005377 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005378
5379 state2 = (corr2 * state1) / 10000;
5380 state2 /= 100; /* convert to mW */
5381
Daniel Vetter02d71952012-08-09 16:44:54 +02005382 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005383
Daniel Vetter20e4d402012-08-08 23:35:39 +02005384 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005385}
5386
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005387unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5388{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005389 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005390 unsigned long val;
5391
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005392 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005393 return 0;
5394
5395 spin_lock_irq(&mchdev_lock);
5396
5397 val = __i915_gfx_val(dev_priv);
5398
5399 spin_unlock_irq(&mchdev_lock);
5400
5401 return val;
5402}
5403
Daniel Vettereb48eb02012-04-26 23:28:12 +02005404/**
5405 * i915_read_mch_val - return value for IPS use
5406 *
5407 * Calculate and return a value for the IPS driver to use when deciding whether
5408 * we have thermal and power headroom to increase CPU or GPU power budget.
5409 */
5410unsigned long i915_read_mch_val(void)
5411{
5412 struct drm_i915_private *dev_priv;
5413 unsigned long chipset_val, graphics_val, ret = 0;
5414
Daniel Vetter92703882012-08-09 16:46:01 +02005415 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005416 if (!i915_mch_dev)
5417 goto out_unlock;
5418 dev_priv = i915_mch_dev;
5419
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005420 chipset_val = __i915_chipset_val(dev_priv);
5421 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005422
5423 ret = chipset_val + graphics_val;
5424
5425out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005426 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005427
5428 return ret;
5429}
5430EXPORT_SYMBOL_GPL(i915_read_mch_val);
5431
5432/**
5433 * i915_gpu_raise - raise GPU frequency limit
5434 *
5435 * Raise the limit; IPS indicates we have thermal headroom.
5436 */
5437bool i915_gpu_raise(void)
5438{
5439 struct drm_i915_private *dev_priv;
5440 bool ret = true;
5441
Daniel Vetter92703882012-08-09 16:46:01 +02005442 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005443 if (!i915_mch_dev) {
5444 ret = false;
5445 goto out_unlock;
5446 }
5447 dev_priv = i915_mch_dev;
5448
Daniel Vetter20e4d402012-08-08 23:35:39 +02005449 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5450 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005451
5452out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005453 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005454
5455 return ret;
5456}
5457EXPORT_SYMBOL_GPL(i915_gpu_raise);
5458
5459/**
5460 * i915_gpu_lower - lower GPU frequency limit
5461 *
5462 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5463 * frequency maximum.
5464 */
5465bool i915_gpu_lower(void)
5466{
5467 struct drm_i915_private *dev_priv;
5468 bool ret = true;
5469
Daniel Vetter92703882012-08-09 16:46:01 +02005470 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005471 if (!i915_mch_dev) {
5472 ret = false;
5473 goto out_unlock;
5474 }
5475 dev_priv = i915_mch_dev;
5476
Daniel Vetter20e4d402012-08-08 23:35:39 +02005477 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5478 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005479
5480out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005481 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005482
5483 return ret;
5484}
5485EXPORT_SYMBOL_GPL(i915_gpu_lower);
5486
5487/**
5488 * i915_gpu_busy - indicate GPU business to IPS
5489 *
5490 * Tell the IPS driver whether or not the GPU is busy.
5491 */
5492bool i915_gpu_busy(void)
5493{
5494 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005495 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005496 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005497 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005498
Daniel Vetter92703882012-08-09 16:46:01 +02005499 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005500 if (!i915_mch_dev)
5501 goto out_unlock;
5502 dev_priv = i915_mch_dev;
5503
Chris Wilsonf047e392012-07-21 12:31:41 +01005504 for_each_ring(ring, dev_priv, i)
5505 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005506
5507out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005508 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005509
5510 return ret;
5511}
5512EXPORT_SYMBOL_GPL(i915_gpu_busy);
5513
5514/**
5515 * i915_gpu_turbo_disable - disable graphics turbo
5516 *
5517 * Disable graphics turbo by resetting the max frequency and setting the
5518 * current frequency to the default.
5519 */
5520bool i915_gpu_turbo_disable(void)
5521{
5522 struct drm_i915_private *dev_priv;
5523 bool ret = true;
5524
Daniel Vetter92703882012-08-09 16:46:01 +02005525 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005526 if (!i915_mch_dev) {
5527 ret = false;
5528 goto out_unlock;
5529 }
5530 dev_priv = i915_mch_dev;
5531
Daniel Vetter20e4d402012-08-08 23:35:39 +02005532 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005533
Daniel Vetter20e4d402012-08-08 23:35:39 +02005534 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005535 ret = false;
5536
5537out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005538 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005539
5540 return ret;
5541}
5542EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5543
5544/**
5545 * Tells the intel_ips driver that the i915 driver is now loaded, if
5546 * IPS got loaded first.
5547 *
5548 * This awkward dance is so that neither module has to depend on the
5549 * other in order for IPS to do the appropriate communication of
5550 * GPU turbo limits to i915.
5551 */
5552static void
5553ips_ping_for_i915_load(void)
5554{
5555 void (*link)(void);
5556
5557 link = symbol_get(ips_link_to_i915_driver);
5558 if (link) {
5559 link();
5560 symbol_put(ips_link_to_i915_driver);
5561 }
5562}
5563
5564void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5565{
Daniel Vetter02d71952012-08-09 16:44:54 +02005566 /* We only register the i915 ips part with intel-ips once everything is
5567 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005568 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005569 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005570 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005571
5572 ips_ping_for_i915_load();
5573}
5574
5575void intel_gpu_ips_teardown(void)
5576{
Daniel Vetter92703882012-08-09 16:46:01 +02005577 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005578 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005579 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005580}
Deepak S76c3552f2014-01-30 23:08:16 +05305581
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005582static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005583{
5584 struct drm_i915_private *dev_priv = dev->dev_private;
5585 u32 lcfuse;
5586 u8 pxw[16];
5587 int i;
5588
5589 /* Disable to program */
5590 I915_WRITE(ECR, 0);
5591 POSTING_READ(ECR);
5592
5593 /* Program energy weights for various events */
5594 I915_WRITE(SDEW, 0x15040d00);
5595 I915_WRITE(CSIEW0, 0x007f0000);
5596 I915_WRITE(CSIEW1, 0x1e220004);
5597 I915_WRITE(CSIEW2, 0x04000004);
5598
5599 for (i = 0; i < 5; i++)
5600 I915_WRITE(PEW + (i * 4), 0);
5601 for (i = 0; i < 3; i++)
5602 I915_WRITE(DEW + (i * 4), 0);
5603
5604 /* Program P-state weights to account for frequency power adjustment */
5605 for (i = 0; i < 16; i++) {
5606 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5607 unsigned long freq = intel_pxfreq(pxvidfreq);
5608 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5609 PXVFREQ_PX_SHIFT;
5610 unsigned long val;
5611
5612 val = vid * vid;
5613 val *= (freq / 1000);
5614 val *= 255;
5615 val /= (127*127*900);
5616 if (val > 0xff)
5617 DRM_ERROR("bad pxval: %ld\n", val);
5618 pxw[i] = val;
5619 }
5620 /* Render standby states get 0 weight */
5621 pxw[14] = 0;
5622 pxw[15] = 0;
5623
5624 for (i = 0; i < 4; i++) {
5625 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5626 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5627 I915_WRITE(PXW + (i * 4), val);
5628 }
5629
5630 /* Adjust magic regs to magic values (more experimental results) */
5631 I915_WRITE(OGW0, 0);
5632 I915_WRITE(OGW1, 0);
5633 I915_WRITE(EG0, 0x00007f00);
5634 I915_WRITE(EG1, 0x0000000e);
5635 I915_WRITE(EG2, 0x000e0000);
5636 I915_WRITE(EG3, 0x68000300);
5637 I915_WRITE(EG4, 0x42000000);
5638 I915_WRITE(EG5, 0x00140031);
5639 I915_WRITE(EG6, 0);
5640 I915_WRITE(EG7, 0);
5641
5642 for (i = 0; i < 8; i++)
5643 I915_WRITE(PXWL + (i * 4), 0);
5644
5645 /* Enable PMON + select events */
5646 I915_WRITE(ECR, 0x80000019);
5647
5648 lcfuse = I915_READ(LCFUSE02);
5649
Daniel Vetter20e4d402012-08-08 23:35:39 +02005650 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005651}
5652
Imre Deakae484342014-03-31 15:10:44 +03005653void intel_init_gt_powersave(struct drm_device *dev)
5654{
Imre Deake6069ca2014-04-18 16:01:02 +03005655 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5656
Deepak S38807742014-05-23 21:00:15 +05305657 if (IS_CHERRYVIEW(dev))
5658 cherryview_init_gt_powersave(dev);
5659 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005660 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005661}
5662
5663void intel_cleanup_gt_powersave(struct drm_device *dev)
5664{
Deepak S38807742014-05-23 21:00:15 +05305665 if (IS_CHERRYVIEW(dev))
5666 return;
5667 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005668 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005669}
5670
Imre Deakdbea3ce2014-12-15 18:59:28 +02005671static void gen6_suspend_rps(struct drm_device *dev)
5672{
5673 struct drm_i915_private *dev_priv = dev->dev_private;
5674
5675 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5676
Akash Goel4c2a8892015-03-06 11:07:24 +05305677 gen6_disable_rps_interrupts(dev);
Imre Deakdbea3ce2014-12-15 18:59:28 +02005678}
5679
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005680/**
5681 * intel_suspend_gt_powersave - suspend PM work and helper threads
5682 * @dev: drm device
5683 *
5684 * We don't want to disable RC6 or other features here, we just want
5685 * to make sure any work we've queued has finished and won't bother
5686 * us while we're suspended.
5687 */
5688void intel_suspend_gt_powersave(struct drm_device *dev)
5689{
5690 struct drm_i915_private *dev_priv = dev->dev_private;
5691
Imre Deakd4d70aa2014-11-19 15:30:04 +02005692 if (INTEL_INFO(dev)->gen < 6)
5693 return;
5694
Imre Deakdbea3ce2014-12-15 18:59:28 +02005695 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305696
5697 /* Force GPU to min freq during suspend */
5698 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005699}
5700
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005701void intel_disable_gt_powersave(struct drm_device *dev)
5702{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005703 struct drm_i915_private *dev_priv = dev->dev_private;
5704
Daniel Vetter930ebb42012-06-29 23:32:16 +02005705 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005706 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305707 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005708 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005709
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005710 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005711 if (INTEL_INFO(dev)->gen >= 9)
5712 gen9_disable_rps(dev);
5713 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305714 cherryview_disable_rps(dev);
5715 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005716 valleyview_disable_rps(dev);
5717 else
5718 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005719
Chris Wilsonc0951f02013-10-10 21:58:50 +01005720 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005721 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005722 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005723}
5724
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005725static void intel_gen6_powersave_work(struct work_struct *work)
5726{
5727 struct drm_i915_private *dev_priv =
5728 container_of(work, struct drm_i915_private,
5729 rps.delayed_resume_work.work);
5730 struct drm_device *dev = dev_priv->dev;
5731
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005732 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005733
Akash Goel4c2a8892015-03-06 11:07:24 +05305734 gen6_reset_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02005735
Deepak S38807742014-05-23 21:00:15 +05305736 if (IS_CHERRYVIEW(dev)) {
5737 cherryview_enable_rps(dev);
5738 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005739 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005740 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005741 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005742 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005743 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005744 } else if (IS_BROADWELL(dev)) {
5745 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005746 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005747 } else {
5748 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005749 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005750 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00005751
5752 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
5753 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
5754
5755 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
5756 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
5757
Chris Wilsonc0951f02013-10-10 21:58:50 +01005758 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005759
Akash Goel4c2a8892015-03-06 11:07:24 +05305760 gen6_enable_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02005761
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005762 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005763
5764 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005765}
5766
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005767void intel_enable_gt_powersave(struct drm_device *dev)
5768{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005769 struct drm_i915_private *dev_priv = dev->dev_private;
5770
Yu Zhangf61018b2015-02-10 19:05:52 +08005771 /* Powersaving is controlled by the host when inside a VM */
5772 if (intel_vgpu_active(dev))
5773 return;
5774
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005775 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005776 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005777 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005778 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005779 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305780 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005781 /*
5782 * PCU communication is slow and this doesn't need to be
5783 * done at any specific time, so do this out of our fast path
5784 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005785 *
5786 * We depend on the HW RC6 power context save/restore
5787 * mechanism when entering D3 through runtime PM suspend. So
5788 * disable RPM until RPS/RC6 is properly setup. We can only
5789 * get here via the driver load/system resume/runtime resume
5790 * paths, so the _noresume version is enough (and in case of
5791 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005792 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005793 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5794 round_jiffies_up_relative(HZ)))
5795 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005796 }
5797}
5798
Imre Deakc6df39b2014-04-14 20:24:29 +03005799void intel_reset_gt_powersave(struct drm_device *dev)
5800{
5801 struct drm_i915_private *dev_priv = dev->dev_private;
5802
Imre Deakdbea3ce2014-12-15 18:59:28 +02005803 if (INTEL_INFO(dev)->gen < 6)
5804 return;
5805
5806 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005807 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005808}
5809
Daniel Vetter3107bd42012-10-31 22:52:31 +01005810static void ibx_init_clock_gating(struct drm_device *dev)
5811{
5812 struct drm_i915_private *dev_priv = dev->dev_private;
5813
5814 /*
5815 * On Ibex Peak and Cougar Point, we need to disable clock
5816 * gating for the panel power sequencer or it will fail to
5817 * start up when no ports are active.
5818 */
5819 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5820}
5821
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005822static void g4x_disable_trickle_feed(struct drm_device *dev)
5823{
5824 struct drm_i915_private *dev_priv = dev->dev_private;
5825 int pipe;
5826
Damien Lespiau055e3932014-08-18 13:49:10 +01005827 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005828 I915_WRITE(DSPCNTR(pipe),
5829 I915_READ(DSPCNTR(pipe)) |
5830 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005831 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005832 }
5833}
5834
Ville Syrjälä017636c2013-12-05 15:51:37 +02005835static void ilk_init_lp_watermarks(struct drm_device *dev)
5836{
5837 struct drm_i915_private *dev_priv = dev->dev_private;
5838
5839 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5840 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5841 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5842
5843 /*
5844 * Don't touch WM1S_LP_EN here.
5845 * Doing so could cause underruns.
5846 */
5847}
5848
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005849static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005850{
5851 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005852 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005853
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005854 /*
5855 * Required for FBC
5856 * WaFbcDisableDpfcClockGating:ilk
5857 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005858 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5859 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5860 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005861
5862 I915_WRITE(PCH_3DCGDIS0,
5863 MARIUNIT_CLOCK_GATE_DISABLE |
5864 SVSMUNIT_CLOCK_GATE_DISABLE);
5865 I915_WRITE(PCH_3DCGDIS1,
5866 VFMUNIT_CLOCK_GATE_DISABLE);
5867
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005868 /*
5869 * According to the spec the following bits should be set in
5870 * order to enable memory self-refresh
5871 * The bit 22/21 of 0x42004
5872 * The bit 5 of 0x42020
5873 * The bit 15 of 0x45000
5874 */
5875 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5876 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5877 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005878 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005879 I915_WRITE(DISP_ARB_CTL,
5880 (I915_READ(DISP_ARB_CTL) |
5881 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005882
5883 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005884
5885 /*
5886 * Based on the document from hardware guys the following bits
5887 * should be set unconditionally in order to enable FBC.
5888 * The bit 22 of 0x42000
5889 * The bit 22 of 0x42004
5890 * The bit 7,8,9 of 0x42020.
5891 */
5892 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005893 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005894 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5895 I915_READ(ILK_DISPLAY_CHICKEN1) |
5896 ILK_FBCQ_DIS);
5897 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5898 I915_READ(ILK_DISPLAY_CHICKEN2) |
5899 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005900 }
5901
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005902 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5903
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005904 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5905 I915_READ(ILK_DISPLAY_CHICKEN2) |
5906 ILK_ELPIN_409_SELECT);
5907 I915_WRITE(_3D_CHICKEN2,
5908 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5909 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005910
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005911 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005912 I915_WRITE(CACHE_MODE_0,
5913 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005914
Akash Goel4e046322014-04-04 17:14:38 +05305915 /* WaDisable_RenderCache_OperationalFlush:ilk */
5916 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5917
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005918 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005919
Daniel Vetter3107bd42012-10-31 22:52:31 +01005920 ibx_init_clock_gating(dev);
5921}
5922
5923static void cpt_init_clock_gating(struct drm_device *dev)
5924{
5925 struct drm_i915_private *dev_priv = dev->dev_private;
5926 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005927 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005928
5929 /*
5930 * On Ibex Peak and Cougar Point, we need to disable clock
5931 * gating for the panel power sequencer or it will fail to
5932 * start up when no ports are active.
5933 */
Jesse Barnescd664072013-10-02 10:34:19 -07005934 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5935 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5936 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005937 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5938 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005939 /* The below fixes the weird display corruption, a few pixels shifted
5940 * downward, on (only) LVDS of some HP laptops with IVY.
5941 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005942 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005943 val = I915_READ(TRANS_CHICKEN2(pipe));
5944 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5945 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005946 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005947 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005948 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5949 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5950 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005951 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5952 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005953 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005954 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005955 I915_WRITE(TRANS_CHICKEN1(pipe),
5956 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5957 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005958}
5959
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005960static void gen6_check_mch_setup(struct drm_device *dev)
5961{
5962 struct drm_i915_private *dev_priv = dev->dev_private;
5963 uint32_t tmp;
5964
5965 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005966 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5967 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5968 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005969}
5970
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005971static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005972{
5973 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005974 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005975
Damien Lespiau231e54f2012-10-19 17:55:41 +01005976 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005977
5978 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5979 I915_READ(ILK_DISPLAY_CHICKEN2) |
5980 ILK_ELPIN_409_SELECT);
5981
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005982 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005983 I915_WRITE(_3D_CHICKEN,
5984 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5985
Akash Goel4e046322014-04-04 17:14:38 +05305986 /* WaDisable_RenderCache_OperationalFlush:snb */
5987 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5988
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005989 /*
5990 * BSpec recoomends 8x4 when MSAA is used,
5991 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005992 *
5993 * Note that PS/WM thread counts depend on the WIZ hashing
5994 * disable bit, which we don't touch here, but it's good
5995 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005996 */
5997 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005998 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005999
Ville Syrjälä017636c2013-12-05 15:51:37 +02006000 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006001
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006002 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006003 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006004
6005 I915_WRITE(GEN6_UCGCTL1,
6006 I915_READ(GEN6_UCGCTL1) |
6007 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6008 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6009
6010 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6011 * gating disable must be set. Failure to set it results in
6012 * flickering pixels due to Z write ordering failures after
6013 * some amount of runtime in the Mesa "fire" demo, and Unigine
6014 * Sanctuary and Tropics, and apparently anything else with
6015 * alpha test or pixel discard.
6016 *
6017 * According to the spec, bit 11 (RCCUNIT) must also be set,
6018 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006019 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006020 * WaDisableRCCUnitClockGating:snb
6021 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006022 */
6023 I915_WRITE(GEN6_UCGCTL2,
6024 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6025 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6026
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006027 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006028 I915_WRITE(_3D_CHICKEN3,
6029 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006030
6031 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006032 * Bspec says:
6033 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6034 * 3DSTATE_SF number of SF output attributes is more than 16."
6035 */
6036 I915_WRITE(_3D_CHICKEN3,
6037 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6038
6039 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006040 * According to the spec the following bits should be
6041 * set in order to enable memory self-refresh and fbc:
6042 * The bit21 and bit22 of 0x42000
6043 * The bit21 and bit22 of 0x42004
6044 * The bit5 and bit7 of 0x42020
6045 * The bit14 of 0x70180
6046 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006047 *
6048 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006049 */
6050 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6051 I915_READ(ILK_DISPLAY_CHICKEN1) |
6052 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6053 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6054 I915_READ(ILK_DISPLAY_CHICKEN2) |
6055 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006056 I915_WRITE(ILK_DSPCLK_GATE_D,
6057 I915_READ(ILK_DSPCLK_GATE_D) |
6058 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6059 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006060
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006061 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006062
Daniel Vetter3107bd42012-10-31 22:52:31 +01006063 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006064
6065 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006066}
6067
6068static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6069{
6070 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6071
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006072 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006073 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006074 *
6075 * This actually overrides the dispatch
6076 * mode for all thread types.
6077 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006078 reg &= ~GEN7_FF_SCHED_MASK;
6079 reg |= GEN7_FF_TS_SCHED_HW;
6080 reg |= GEN7_FF_VS_SCHED_HW;
6081 reg |= GEN7_FF_DS_SCHED_HW;
6082
6083 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6084}
6085
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006086static void lpt_init_clock_gating(struct drm_device *dev)
6087{
6088 struct drm_i915_private *dev_priv = dev->dev_private;
6089
6090 /*
6091 * TODO: this bit should only be enabled when really needed, then
6092 * disabled when not needed anymore in order to save power.
6093 */
6094 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
6095 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6096 I915_READ(SOUTH_DSPCLK_GATE_D) |
6097 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006098
6099 /* WADPOClockGatingDisable:hsw */
6100 I915_WRITE(_TRANSA_CHICKEN1,
6101 I915_READ(_TRANSA_CHICKEN1) |
6102 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006103}
6104
Imre Deak7d708ee2013-04-17 14:04:50 +03006105static void lpt_suspend_hw(struct drm_device *dev)
6106{
6107 struct drm_i915_private *dev_priv = dev->dev_private;
6108
6109 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
6110 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6111
6112 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6113 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6114 }
6115}
6116
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006117static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006118{
6119 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006120 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006121
6122 I915_WRITE(WM3_LP_ILK, 0);
6123 I915_WRITE(WM2_LP_ILK, 0);
6124 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006125
Ben Widawskyab57fff2013-12-12 15:28:04 -08006126 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006127 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006128
Ben Widawskyab57fff2013-12-12 15:28:04 -08006129 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006130 I915_WRITE(CHICKEN_PAR1_1,
6131 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6132
Ben Widawskyab57fff2013-12-12 15:28:04 -08006133 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006134 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006135 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006136 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006137 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006138 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006139
Ben Widawskyab57fff2013-12-12 15:28:04 -08006140 /* WaVSRefCountFullforceMissDisable:bdw */
6141 /* WaDSRefCountFullforceMissDisable:bdw */
6142 I915_WRITE(GEN7_FF_THREAD_MODE,
6143 I915_READ(GEN7_FF_THREAD_MODE) &
6144 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006145
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006146 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6147 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006148
6149 /* WaDisableSDEUnitClockGating:bdw */
6150 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6151 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006152
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006153 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006154}
6155
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006156static void haswell_init_clock_gating(struct drm_device *dev)
6157{
6158 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006159
Ville Syrjälä017636c2013-12-05 15:51:37 +02006160 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006161
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006162 /* L3 caching of data atomics doesn't work -- disable it. */
6163 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6164 I915_WRITE(HSW_ROW_CHICKEN3,
6165 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6166
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006167 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006168 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6169 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6170 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6171
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006172 /* WaVSRefCountFullforceMissDisable:hsw */
6173 I915_WRITE(GEN7_FF_THREAD_MODE,
6174 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006175
Akash Goel4e046322014-04-04 17:14:38 +05306176 /* WaDisable_RenderCache_OperationalFlush:hsw */
6177 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6178
Chia-I Wufe27c602014-01-28 13:29:33 +08006179 /* enable HiZ Raw Stall Optimization */
6180 I915_WRITE(CACHE_MODE_0_GEN7,
6181 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6182
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006183 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006184 I915_WRITE(CACHE_MODE_1,
6185 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006186
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006187 /*
6188 * BSpec recommends 8x4 when MSAA is used,
6189 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006190 *
6191 * Note that PS/WM thread counts depend on the WIZ hashing
6192 * disable bit, which we don't touch here, but it's good
6193 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006194 */
6195 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006196 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006197
Kenneth Graunke94411592014-12-31 16:23:00 -08006198 /* WaSampleCChickenBitEnable:hsw */
6199 I915_WRITE(HALF_SLICE_CHICKEN3,
6200 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6201
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006202 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006203 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6204
Paulo Zanoni90a88642013-05-03 17:23:45 -03006205 /* WaRsPkgCStateDisplayPMReq:hsw */
6206 I915_WRITE(CHICKEN_PAR1_1,
6207 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006208
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006209 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006210}
6211
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006212static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006213{
6214 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006215 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006216
Ville Syrjälä017636c2013-12-05 15:51:37 +02006217 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006218
Damien Lespiau231e54f2012-10-19 17:55:41 +01006219 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006220
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006221 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006222 I915_WRITE(_3D_CHICKEN3,
6223 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6224
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006225 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006226 I915_WRITE(IVB_CHICKEN3,
6227 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6228 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6229
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006230 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006231 if (IS_IVB_GT1(dev))
6232 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6233 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006234
Akash Goel4e046322014-04-04 17:14:38 +05306235 /* WaDisable_RenderCache_OperationalFlush:ivb */
6236 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6237
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006238 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006239 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6240 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6241
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006242 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006243 I915_WRITE(GEN7_L3CNTLREG1,
6244 GEN7_WA_FOR_GEN7_L3_CONTROL);
6245 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006246 GEN7_WA_L3_CHICKEN_MODE);
6247 if (IS_IVB_GT1(dev))
6248 I915_WRITE(GEN7_ROW_CHICKEN2,
6249 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006250 else {
6251 /* must write both registers */
6252 I915_WRITE(GEN7_ROW_CHICKEN2,
6253 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006254 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6255 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006256 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006257
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006258 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006259 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6260 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6261
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006262 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006263 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006264 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006265 */
6266 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006267 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006268
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006269 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006270 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6271 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6272 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6273
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006274 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006275
6276 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006277
Chris Wilson22721342014-03-04 09:41:43 +00006278 if (0) { /* causes HiZ corruption on ivb:gt1 */
6279 /* enable HiZ Raw Stall Optimization */
6280 I915_WRITE(CACHE_MODE_0_GEN7,
6281 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6282 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006283
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006284 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006285 I915_WRITE(CACHE_MODE_1,
6286 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006287
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006288 /*
6289 * BSpec recommends 8x4 when MSAA is used,
6290 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006291 *
6292 * Note that PS/WM thread counts depend on the WIZ hashing
6293 * disable bit, which we don't touch here, but it's good
6294 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006295 */
6296 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006297 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006298
Ben Widawsky20848222012-05-04 18:58:59 -07006299 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6300 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6301 snpcr |= GEN6_MBC_SNPCR_MED;
6302 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006303
Ben Widawskyab5c6082013-04-05 13:12:41 -07006304 if (!HAS_PCH_NOP(dev))
6305 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006306
6307 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006308}
6309
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006310static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6311{
6312 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6313
6314 /*
6315 * Disable trickle feed and enable pnd deadline calculation
6316 */
6317 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6318 I915_WRITE(CBR1_VLV, 0);
6319}
6320
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006321static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006322{
6323 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006324
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006325 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006326
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006327 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006328 I915_WRITE(_3D_CHICKEN3,
6329 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6330
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006331 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006332 I915_WRITE(IVB_CHICKEN3,
6333 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6334 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6335
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006336 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006337 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006338 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006339 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6340 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006341
Akash Goel4e046322014-04-04 17:14:38 +05306342 /* WaDisable_RenderCache_OperationalFlush:vlv */
6343 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6344
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006345 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006346 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6347 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6348
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006349 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006350 I915_WRITE(GEN7_ROW_CHICKEN2,
6351 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6352
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006353 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006354 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6355 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6356 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6357
Ville Syrjälä46680e02014-01-22 21:33:01 +02006358 gen7_setup_fixed_func_scheduler(dev_priv);
6359
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006360 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006361 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006362 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006363 */
6364 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006365 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006366
Akash Goelc98f5062014-03-24 23:00:07 +05306367 /* WaDisableL3Bank2xClockGate:vlv
6368 * Disabling L3 clock gating- MMIO 940c[25] = 1
6369 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6370 I915_WRITE(GEN7_UCGCTL4,
6371 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006372
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006373 /*
6374 * BSpec says this must be set, even though
6375 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6376 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006377 I915_WRITE(CACHE_MODE_1,
6378 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006379
6380 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006381 * BSpec recommends 8x4 when MSAA is used,
6382 * however in practice 16x4 seems fastest.
6383 *
6384 * Note that PS/WM thread counts depend on the WIZ hashing
6385 * disable bit, which we don't touch here, but it's good
6386 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6387 */
6388 I915_WRITE(GEN7_GT_MODE,
6389 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6390
6391 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006392 * WaIncreaseL3CreditsForVLVB0:vlv
6393 * This is the hardware default actually.
6394 */
6395 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6396
6397 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006398 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006399 * Disable clock gating on th GCFG unit to prevent a delay
6400 * in the reporting of vblank events.
6401 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006402 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006403}
6404
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006405static void cherryview_init_clock_gating(struct drm_device *dev)
6406{
6407 struct drm_i915_private *dev_priv = dev->dev_private;
6408
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006409 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006410
Ville Syrjälä232ce332014-04-09 13:28:35 +03006411 /* WaVSRefCountFullforceMissDisable:chv */
6412 /* WaDSRefCountFullforceMissDisable:chv */
6413 I915_WRITE(GEN7_FF_THREAD_MODE,
6414 I915_READ(GEN7_FF_THREAD_MODE) &
6415 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006416
6417 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6418 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6419 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006420
6421 /* WaDisableCSUnitClockGating:chv */
6422 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6423 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006424
6425 /* WaDisableSDEUnitClockGating:chv */
6426 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6427 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006428}
6429
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006430static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006431{
6432 struct drm_i915_private *dev_priv = dev->dev_private;
6433 uint32_t dspclk_gate;
6434
6435 I915_WRITE(RENCLK_GATE_D1, 0);
6436 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6437 GS_UNIT_CLOCK_GATE_DISABLE |
6438 CL_UNIT_CLOCK_GATE_DISABLE);
6439 I915_WRITE(RAMCLK_GATE_D, 0);
6440 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6441 OVRUNIT_CLOCK_GATE_DISABLE |
6442 OVCUNIT_CLOCK_GATE_DISABLE;
6443 if (IS_GM45(dev))
6444 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6445 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006446
6447 /* WaDisableRenderCachePipelinedFlush */
6448 I915_WRITE(CACHE_MODE_0,
6449 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006450
Akash Goel4e046322014-04-04 17:14:38 +05306451 /* WaDisable_RenderCache_OperationalFlush:g4x */
6452 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6453
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006454 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006455}
6456
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006457static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006458{
6459 struct drm_i915_private *dev_priv = dev->dev_private;
6460
6461 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6462 I915_WRITE(RENCLK_GATE_D2, 0);
6463 I915_WRITE(DSPCLK_GATE_D, 0);
6464 I915_WRITE(RAMCLK_GATE_D, 0);
6465 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006466 I915_WRITE(MI_ARB_STATE,
6467 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306468
6469 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6470 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006471}
6472
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006473static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006474{
6475 struct drm_i915_private *dev_priv = dev->dev_private;
6476
6477 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6478 I965_RCC_CLOCK_GATE_DISABLE |
6479 I965_RCPB_CLOCK_GATE_DISABLE |
6480 I965_ISC_CLOCK_GATE_DISABLE |
6481 I965_FBC_CLOCK_GATE_DISABLE);
6482 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006483 I915_WRITE(MI_ARB_STATE,
6484 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306485
6486 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6487 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006488}
6489
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006490static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006491{
6492 struct drm_i915_private *dev_priv = dev->dev_private;
6493 u32 dstate = I915_READ(D_STATE);
6494
6495 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6496 DSTATE_DOT_CLOCK_GATING;
6497 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006498
6499 if (IS_PINEVIEW(dev))
6500 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006501
6502 /* IIR "flip pending" means done if this bit is set */
6503 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006504
6505 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006506 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006507
6508 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6509 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006510
6511 I915_WRITE(MI_ARB_STATE,
6512 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006513}
6514
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006515static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006516{
6517 struct drm_i915_private *dev_priv = dev->dev_private;
6518
6519 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006520
6521 /* interrupts should cause a wake up from C3 */
6522 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6523 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006524
6525 I915_WRITE(MEM_MODE,
6526 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006527}
6528
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006529static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006530{
6531 struct drm_i915_private *dev_priv = dev->dev_private;
6532
6533 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006534
6535 I915_WRITE(MEM_MODE,
6536 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6537 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006538}
6539
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006540void intel_init_clock_gating(struct drm_device *dev)
6541{
6542 struct drm_i915_private *dev_priv = dev->dev_private;
6543
Damien Lespiauc57e3552015-02-09 19:33:05 +00006544 if (dev_priv->display.init_clock_gating)
6545 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006546}
6547
Imre Deak7d708ee2013-04-17 14:04:50 +03006548void intel_suspend_hw(struct drm_device *dev)
6549{
6550 if (HAS_PCH_LPT(dev))
6551 lpt_suspend_hw(dev);
6552}
6553
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006554/* Set up chip specific power management-related functions */
6555void intel_init_pm(struct drm_device *dev)
6556{
6557 struct drm_i915_private *dev_priv = dev->dev_private;
6558
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006559 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006560
Daniel Vetterc921aba2012-04-26 23:28:17 +02006561 /* For cxsr */
6562 if (IS_PINEVIEW(dev))
6563 i915_pineview_get_mem_freq(dev);
6564 else if (IS_GEN5(dev))
6565 i915_ironlake_get_mem_freq(dev);
6566
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006567 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006568 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006569 skl_setup_wm_latency(dev);
6570
Imre Deaka82abe42015-03-27 14:00:04 +02006571 if (IS_BROXTON(dev))
6572 dev_priv->display.init_clock_gating =
6573 bxt_init_clock_gating;
6574 else if (IS_SKYLAKE(dev))
6575 dev_priv->display.init_clock_gating =
6576 skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006577 dev_priv->display.update_wm = skl_update_wm;
6578 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306579 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006580 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006581
Ville Syrjäläbd602542014-01-07 16:14:10 +02006582 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6583 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6584 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6585 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6586 dev_priv->display.update_wm = ilk_update_wm;
6587 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6588 } else {
6589 DRM_DEBUG_KMS("Failed to read display plane latency. "
6590 "Disable CxSR\n");
6591 }
6592
6593 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006594 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006595 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006596 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006597 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006598 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006599 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006600 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006601 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006602 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006603 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläae801522015-03-05 21:19:49 +02006604 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306605 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006606 dev_priv->display.init_clock_gating =
6607 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006608 } else if (IS_VALLEYVIEW(dev)) {
6609 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306610 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006611 dev_priv->display.init_clock_gating =
6612 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006613 } else if (IS_PINEVIEW(dev)) {
6614 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6615 dev_priv->is_ddr3,
6616 dev_priv->fsb_freq,
6617 dev_priv->mem_freq)) {
6618 DRM_INFO("failed to find known CxSR latency "
6619 "(found ddr%s fsb freq %d, mem freq %d), "
6620 "disabling CxSR\n",
6621 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6622 dev_priv->fsb_freq, dev_priv->mem_freq);
6623 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006624 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006625 dev_priv->display.update_wm = NULL;
6626 } else
6627 dev_priv->display.update_wm = pineview_update_wm;
6628 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6629 } else if (IS_G4X(dev)) {
6630 dev_priv->display.update_wm = g4x_update_wm;
6631 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6632 } else if (IS_GEN4(dev)) {
6633 dev_priv->display.update_wm = i965_update_wm;
6634 if (IS_CRESTLINE(dev))
6635 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6636 else if (IS_BROADWATER(dev))
6637 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6638 } else if (IS_GEN3(dev)) {
6639 dev_priv->display.update_wm = i9xx_update_wm;
6640 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6641 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006642 } else if (IS_GEN2(dev)) {
6643 if (INTEL_INFO(dev)->num_pipes == 1) {
6644 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006645 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006646 } else {
6647 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006648 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006649 }
6650
6651 if (IS_I85X(dev) || IS_I865G(dev))
6652 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6653 else
6654 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6655 } else {
6656 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006657 }
6658}
6659
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006660int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006661{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006662 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006663
6664 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6665 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6666 return -EAGAIN;
6667 }
6668
6669 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006670 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006671 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6672
6673 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6674 500)) {
6675 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6676 return -ETIMEDOUT;
6677 }
6678
6679 *val = I915_READ(GEN6_PCODE_DATA);
6680 I915_WRITE(GEN6_PCODE_DATA, 0);
6681
6682 return 0;
6683}
6684
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006685int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006686{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006687 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006688
6689 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6690 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6691 return -EAGAIN;
6692 }
6693
6694 I915_WRITE(GEN6_PCODE_DATA, val);
6695 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6696
6697 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6698 500)) {
6699 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6700 return -ETIMEDOUT;
6701 }
6702
6703 I915_WRITE(GEN6_PCODE_DATA, 0);
6704
6705 return 0;
6706}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006707
Ville Syrjälädd06f882014-11-10 22:55:12 +02006708static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006709{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006710 switch (czclk_freq) {
6711 case 200:
6712 return 10;
6713 case 267:
6714 return 12;
6715 case 320:
6716 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006717 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006718 case 400:
6719 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006720 default:
6721 return -1;
6722 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006723}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006724
Ville Syrjälädd06f882014-11-10 22:55:12 +02006725static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6726{
6727 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6728
6729 div = vlv_gpu_freq_div(czclk_freq);
6730 if (div < 0)
6731 return div;
6732
6733 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006734}
6735
Fengguang Wub55dd642014-07-12 11:21:39 +02006736static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006737{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006738 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006739
Ville Syrjälädd06f882014-11-10 22:55:12 +02006740 mul = vlv_gpu_freq_div(czclk_freq);
6741 if (mul < 0)
6742 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006743
Ville Syrjälädd06f882014-11-10 22:55:12 +02006744 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006745}
6746
Fengguang Wub55dd642014-07-12 11:21:39 +02006747static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306748{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006749 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306750
Ville Syrjälädd06f882014-11-10 22:55:12 +02006751 div = vlv_gpu_freq_div(czclk_freq) / 2;
6752 if (div < 0)
6753 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306754
Ville Syrjälädd06f882014-11-10 22:55:12 +02006755 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306756}
6757
Fengguang Wub55dd642014-07-12 11:21:39 +02006758static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306759{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006760 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306761
Ville Syrjälädd06f882014-11-10 22:55:12 +02006762 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6763 if (mul < 0)
6764 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306765
Ville Syrjälä1c147622014-08-18 14:42:43 +03006766 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006767 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306768}
6769
Ville Syrjälä616bc822015-01-23 21:04:25 +02006770int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6771{
Akash Goel80b6dda2015-03-06 11:07:15 +05306772 if (IS_GEN9(dev_priv->dev))
6773 return (val * GT_FREQUENCY_MULTIPLIER) / GEN9_FREQ_SCALER;
6774 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006775 return chv_gpu_freq(dev_priv, val);
6776 else if (IS_VALLEYVIEW(dev_priv->dev))
6777 return byt_gpu_freq(dev_priv, val);
6778 else
6779 return val * GT_FREQUENCY_MULTIPLIER;
6780}
6781
Ville Syrjälä616bc822015-01-23 21:04:25 +02006782int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6783{
Akash Goel80b6dda2015-03-06 11:07:15 +05306784 if (IS_GEN9(dev_priv->dev))
6785 return (val * GEN9_FREQ_SCALER) / GT_FREQUENCY_MULTIPLIER;
6786 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006787 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306788 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006789 return byt_freq_opcode(dev_priv, val);
6790 else
6791 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306792}
6793
Daniel Vetterf742a552013-12-06 10:17:53 +01006794void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006795{
6796 struct drm_i915_private *dev_priv = dev->dev_private;
6797
Daniel Vetterf742a552013-12-06 10:17:53 +01006798 mutex_init(&dev_priv->rps.hw_lock);
6799
Chris Wilson907b28c2013-07-19 20:36:52 +01006800 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6801 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006802
Paulo Zanoni33688d92014-03-07 20:08:19 -03006803 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006804}