blob: 7ae5bfdbf569f7cc1f9f73679f2299a46ab1d4d1 [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/**
Jani Nikula18afd442016-01-18 09:19:48 +020035 * DOC: RC6
36 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070037 * RC6 is a special power stage which allows the GPU to enter an very
38 * low-voltage mode when idle, using down to 0V while at this stage. This
39 * stage is entered automatically when the GPU is idle when RC6 support is
40 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
41 *
42 * There are different RC6 modes available in Intel GPU, which differentiate
43 * among each other with the latency required to enter and leave RC6 and
44 * voltage consumed by the GPU in different states.
45 *
46 * The combination of the following flags define which states GPU is allowed
47 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
48 * RC6pp is deepest RC6. Their support by hardware varies according to the
49 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
50 * which brings the most power savings; deeper states save more power, but
51 * require higher latency to switch to and wake up.
52 */
53#define INTEL_RC6_ENABLE (1<<0)
54#define INTEL_RC6p_ENABLE (1<<1)
55#define INTEL_RC6pp_ENABLE (1<<2)
56
Mika Kuoppala11b28342016-06-07 17:19:04 +030057static void gen9_init_clock_gating(struct drm_device *dev)
58{
59 struct drm_i915_private *dev_priv = dev->dev_private;
60
61 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
62 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
65 I915_WRITE(GEN8_CONFIG0,
66 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
67}
68
Imre Deaka82abe42015-03-27 14:00:04 +020069static void bxt_init_clock_gating(struct drm_device *dev)
70{
Imre Deak32608ca2015-03-11 11:10:27 +020071 struct drm_i915_private *dev_priv = dev->dev_private;
72
Mika Kuoppala11b28342016-06-07 17:19:04 +030073 gen9_init_clock_gating(dev);
Daniel Vetterda0a0ac2016-05-19 09:14:20 +020074
Nick Hoatha7546152015-06-29 14:07:32 +010075 /* WaDisableSDEUnitClockGating:bxt */
76 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
77 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
78
Imre Deak32608ca2015-03-11 11:10:27 +020079 /*
80 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020081 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020082 */
Imre Deak32608ca2015-03-11 11:10:27 +020083 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020084 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7ac2015-12-01 10:23:52 +020085
86 /*
87 * Wa: Backlight PWM may stop in the asserted state, causing backlight
88 * to stay fully on.
89 */
90 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
91 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
92 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +020093}
94
Daniel Vetterc921aba2012-04-26 23:28:17 +020095static void i915_pineview_get_mem_freq(struct drm_device *dev)
96{
Jani Nikula50227e12014-03-31 14:27:21 +030097 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +020098 u32 tmp;
99
100 tmp = I915_READ(CLKCFG);
101
102 switch (tmp & CLKCFG_FSB_MASK) {
103 case CLKCFG_FSB_533:
104 dev_priv->fsb_freq = 533; /* 133*4 */
105 break;
106 case CLKCFG_FSB_800:
107 dev_priv->fsb_freq = 800; /* 200*4 */
108 break;
109 case CLKCFG_FSB_667:
110 dev_priv->fsb_freq = 667; /* 167*4 */
111 break;
112 case CLKCFG_FSB_400:
113 dev_priv->fsb_freq = 400; /* 100*4 */
114 break;
115 }
116
117 switch (tmp & CLKCFG_MEM_MASK) {
118 case CLKCFG_MEM_533:
119 dev_priv->mem_freq = 533;
120 break;
121 case CLKCFG_MEM_667:
122 dev_priv->mem_freq = 667;
123 break;
124 case CLKCFG_MEM_800:
125 dev_priv->mem_freq = 800;
126 break;
127 }
128
129 /* detect pineview DDR3 setting */
130 tmp = I915_READ(CSHRDDR3CTL);
131 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
132}
133
134static void i915_ironlake_get_mem_freq(struct drm_device *dev)
135{
Jani Nikula50227e12014-03-31 14:27:21 +0300136 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200137 u16 ddrpll, csipll;
138
139 ddrpll = I915_READ16(DDRMPLL1);
140 csipll = I915_READ16(CSIPLL0);
141
142 switch (ddrpll & 0xff) {
143 case 0xc:
144 dev_priv->mem_freq = 800;
145 break;
146 case 0x10:
147 dev_priv->mem_freq = 1066;
148 break;
149 case 0x14:
150 dev_priv->mem_freq = 1333;
151 break;
152 case 0x18:
153 dev_priv->mem_freq = 1600;
154 break;
155 default:
156 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
157 ddrpll & 0xff);
158 dev_priv->mem_freq = 0;
159 break;
160 }
161
Daniel Vetter20e4d402012-08-08 23:35:39 +0200162 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200163
164 switch (csipll & 0x3ff) {
165 case 0x00c:
166 dev_priv->fsb_freq = 3200;
167 break;
168 case 0x00e:
169 dev_priv->fsb_freq = 3733;
170 break;
171 case 0x010:
172 dev_priv->fsb_freq = 4266;
173 break;
174 case 0x012:
175 dev_priv->fsb_freq = 4800;
176 break;
177 case 0x014:
178 dev_priv->fsb_freq = 5333;
179 break;
180 case 0x016:
181 dev_priv->fsb_freq = 5866;
182 break;
183 case 0x018:
184 dev_priv->fsb_freq = 6400;
185 break;
186 default:
187 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
188 csipll & 0x3ff);
189 dev_priv->fsb_freq = 0;
190 break;
191 }
192
193 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200194 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200196 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200197 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200198 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200199 }
200}
201
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300202static const struct cxsr_latency cxsr_latency_table[] = {
203 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
204 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
205 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
206 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
207 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
208
209 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
210 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
211 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
212 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
213 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
214
215 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
216 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
217 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
218 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
219 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
220
221 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
222 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
223 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
224 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
225 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
226
227 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
228 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
229 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
230 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
231 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
232
233 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
234 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
235 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
236 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
237 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
238};
239
Daniel Vetter63c62272012-04-21 23:17:55 +0200240static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300241 int is_ddr3,
242 int fsb,
243 int mem)
244{
245 const struct cxsr_latency *latency;
246 int i;
247
248 if (fsb == 0 || mem == 0)
249 return NULL;
250
251 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
252 latency = &cxsr_latency_table[i];
253 if (is_desktop == latency->is_desktop &&
254 is_ddr3 == latency->is_ddr3 &&
255 fsb == latency->fsb_freq && mem == latency->mem_freq)
256 return latency;
257 }
258
259 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
260
261 return NULL;
262}
263
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200264static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
265{
266 u32 val;
267
268 mutex_lock(&dev_priv->rps.hw_lock);
269
270 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
271 if (enable)
272 val &= ~FORCE_DDR_HIGH_FREQ;
273 else
274 val |= FORCE_DDR_HIGH_FREQ;
275 val &= ~FORCE_DDR_LOW_FREQ;
276 val |= FORCE_DDR_FREQ_REQ_ACK;
277 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
278
279 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
280 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
281 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
282
283 mutex_unlock(&dev_priv->rps.hw_lock);
284}
285
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200286static void chv_set_memory_pm5(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_DSPFREQ);
293 if (enable)
294 val |= DSP_MAXFIFO_PM5_ENABLE;
295 else
296 val &= ~DSP_MAXFIFO_PM5_ENABLE;
297 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
298
299 mutex_unlock(&dev_priv->rps.hw_lock);
300}
301
Ville Syrjäläf4998962015-03-10 17:02:21 +0200302#define FW_WM(value, plane) \
303 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
304
Imre Deak5209b1f2014-07-01 12:36:17 +0300305void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300306{
Imre Deak5209b1f2014-07-01 12:36:17 +0300307 struct drm_device *dev = dev_priv->dev;
308 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300309
Wayne Boyer666a4532015-12-09 12:29:35 -0800310 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300311 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300312 POSTING_READ(FW_BLC_SELF_VLV);
Ville Syrjälä852eb002015-06-24 22:00:07 +0300313 dev_priv->wm.vlv.cxsr = enable;
Imre Deak5209b1f2014-07-01 12:36:17 +0300314 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
315 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300316 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300317 } else if (IS_PINEVIEW(dev)) {
318 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
319 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
320 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300321 POSTING_READ(DSPFW3);
Imre Deak5209b1f2014-07-01 12:36:17 +0300322 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
323 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
324 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
325 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300326 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300327 } else if (IS_I915GM(dev)) {
328 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
329 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
330 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300331 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300332 } else {
333 return;
334 }
335
336 DRM_DEBUG_KMS("memory self-refresh is %s\n",
337 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300338}
339
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200340
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300341/*
342 * Latency for FIFO fetches is dependent on several factors:
343 * - memory configuration (speed, channels)
344 * - chipset
345 * - current MCH state
346 * It can be fairly high in some situations, so here we assume a fairly
347 * pessimal value. It's a tradeoff between extra memory fetches (if we
348 * set this value too high, the FIFO will fetch frequently to stay full)
349 * and power consumption (set it too low to save power and we might see
350 * FIFO underruns and display "flicker").
351 *
352 * A value of 5us seems to be a good balance; safe for very low end
353 * platforms but not overly aggressive on lower latency configs.
354 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100355static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300356
Ville Syrjäläb5004722015-03-05 21:19:47 +0200357#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
358 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
359
360static int vlv_get_fifo_size(struct drm_device *dev,
361 enum pipe pipe, int plane)
362{
363 struct drm_i915_private *dev_priv = dev->dev_private;
364 int sprite0_start, sprite1_start, size;
365
366 switch (pipe) {
367 uint32_t dsparb, dsparb2, dsparb3;
368 case PIPE_A:
369 dsparb = I915_READ(DSPARB);
370 dsparb2 = I915_READ(DSPARB2);
371 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
372 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
373 break;
374 case PIPE_B:
375 dsparb = I915_READ(DSPARB);
376 dsparb2 = I915_READ(DSPARB2);
377 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
378 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
379 break;
380 case PIPE_C:
381 dsparb2 = I915_READ(DSPARB2);
382 dsparb3 = I915_READ(DSPARB3);
383 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
384 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
385 break;
386 default:
387 return 0;
388 }
389
390 switch (plane) {
391 case 0:
392 size = sprite0_start;
393 break;
394 case 1:
395 size = sprite1_start - sprite0_start;
396 break;
397 case 2:
398 size = 512 - 1 - sprite1_start;
399 break;
400 default:
401 return 0;
402 }
403
404 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
405 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
406 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
407 size);
408
409 return size;
410}
411
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300412static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300413{
414 struct drm_i915_private *dev_priv = dev->dev_private;
415 uint32_t dsparb = I915_READ(DSPARB);
416 int size;
417
418 size = dsparb & 0x7f;
419 if (plane)
420 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
421
422 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
423 plane ? "B" : "A", size);
424
425 return size;
426}
427
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200428static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300429{
430 struct drm_i915_private *dev_priv = dev->dev_private;
431 uint32_t dsparb = I915_READ(DSPARB);
432 int size;
433
434 size = dsparb & 0x1ff;
435 if (plane)
436 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
437 size >>= 1; /* Convert to cachelines */
438
439 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
440 plane ? "B" : "A", size);
441
442 return size;
443}
444
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300445static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300446{
447 struct drm_i915_private *dev_priv = dev->dev_private;
448 uint32_t dsparb = I915_READ(DSPARB);
449 int size;
450
451 size = dsparb & 0x7f;
452 size >>= 2; /* Convert to cachelines */
453
454 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
455 plane ? "B" : "A",
456 size);
457
458 return size;
459}
460
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300461/* Pineview has different values for various configs */
462static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300463 .fifo_size = PINEVIEW_DISPLAY_FIFO,
464 .max_wm = PINEVIEW_MAX_WM,
465 .default_wm = PINEVIEW_DFT_WM,
466 .guard_size = PINEVIEW_GUARD_WM,
467 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300468};
469static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300470 .fifo_size = PINEVIEW_DISPLAY_FIFO,
471 .max_wm = PINEVIEW_MAX_WM,
472 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
473 .guard_size = PINEVIEW_GUARD_WM,
474 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300475};
476static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300477 .fifo_size = PINEVIEW_CURSOR_FIFO,
478 .max_wm = PINEVIEW_CURSOR_MAX_WM,
479 .default_wm = PINEVIEW_CURSOR_DFT_WM,
480 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
481 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482};
483static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300484 .fifo_size = PINEVIEW_CURSOR_FIFO,
485 .max_wm = PINEVIEW_CURSOR_MAX_WM,
486 .default_wm = PINEVIEW_CURSOR_DFT_WM,
487 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
488 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300489};
490static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300491 .fifo_size = G4X_FIFO_SIZE,
492 .max_wm = G4X_MAX_WM,
493 .default_wm = G4X_MAX_WM,
494 .guard_size = 2,
495 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300496};
497static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300498 .fifo_size = I965_CURSOR_FIFO,
499 .max_wm = I965_CURSOR_MAX_WM,
500 .default_wm = I965_CURSOR_DFT_WM,
501 .guard_size = 2,
502 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300503};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300504static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300505 .fifo_size = I965_CURSOR_FIFO,
506 .max_wm = I965_CURSOR_MAX_WM,
507 .default_wm = I965_CURSOR_DFT_WM,
508 .guard_size = 2,
509 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300510};
511static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300512 .fifo_size = I945_FIFO_SIZE,
513 .max_wm = I915_MAX_WM,
514 .default_wm = 1,
515 .guard_size = 2,
516 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517};
518static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300519 .fifo_size = I915_FIFO_SIZE,
520 .max_wm = I915_MAX_WM,
521 .default_wm = 1,
522 .guard_size = 2,
523 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300524};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300525static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300526 .fifo_size = I855GM_FIFO_SIZE,
527 .max_wm = I915_MAX_WM,
528 .default_wm = 1,
529 .guard_size = 2,
530 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300532static const struct intel_watermark_params i830_bc_wm_info = {
533 .fifo_size = I855GM_FIFO_SIZE,
534 .max_wm = I915_MAX_WM/2,
535 .default_wm = 1,
536 .guard_size = 2,
537 .cacheline_size = I830_FIFO_LINE_SIZE,
538};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200539static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300540 .fifo_size = I830_FIFO_SIZE,
541 .max_wm = I915_MAX_WM,
542 .default_wm = 1,
543 .guard_size = 2,
544 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545};
546
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300547/**
548 * intel_calculate_wm - calculate watermark level
549 * @clock_in_khz: pixel clock
550 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200551 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300552 * @latency_ns: memory latency for the platform
553 *
554 * Calculate the watermark level (the level at which the display plane will
555 * start fetching from memory again). Each chip has a different display
556 * FIFO size and allocation, so the caller needs to figure that out and pass
557 * in the correct intel_watermark_params structure.
558 *
559 * As the pixel clock runs, the FIFO will be drained at a rate that depends
560 * on the pixel size. When it reaches the watermark level, it'll start
561 * fetching FIFO line sized based chunks from memory until the FIFO fills
562 * past the watermark point. If the FIFO drains completely, a FIFO underrun
563 * will occur, and a display engine hang could result.
564 */
565static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
566 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200567 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568 unsigned long latency_ns)
569{
570 long entries_required, wm_size;
571
572 /*
573 * Note: we need to make sure we don't overflow for various clock &
574 * latency values.
575 * clocks go from a few thousand to several hundred thousand.
576 * latency is usually a few thousand
577 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200578 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579 1000;
580 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
581
582 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
583
584 wm_size = fifo_size - (entries_required + wm->guard_size);
585
586 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
587
588 /* Don't promote wm_size to unsigned... */
589 if (wm_size > (long)wm->max_wm)
590 wm_size = wm->max_wm;
591 if (wm_size <= 0)
592 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300593
594 /*
595 * Bspec seems to indicate that the value shouldn't be lower than
596 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
597 * Lets go for 8 which is the burst size since certain platforms
598 * already use a hardcoded 8 (which is what the spec says should be
599 * done).
600 */
601 if (wm_size <= 8)
602 wm_size = 8;
603
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300604 return wm_size;
605}
606
607static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
608{
609 struct drm_crtc *crtc, *enabled = NULL;
610
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100611 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000612 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300613 if (enabled)
614 return NULL;
615 enabled = crtc;
616 }
617 }
618
619 return enabled;
620}
621
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300622static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300623{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300624 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300625 struct drm_i915_private *dev_priv = dev->dev_private;
626 struct drm_crtc *crtc;
627 const struct cxsr_latency *latency;
628 u32 reg;
629 unsigned long wm;
630
631 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
632 dev_priv->fsb_freq, dev_priv->mem_freq);
633 if (!latency) {
634 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300635 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300636 return;
637 }
638
639 crtc = single_enabled_crtc(dev);
640 if (crtc) {
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300641 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +0200642 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300643 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300644
645 /* Display SR */
646 wm = intel_calculate_wm(clock, &pineview_display_wm,
647 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200648 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300649 reg = I915_READ(DSPFW1);
650 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200651 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300652 I915_WRITE(DSPFW1, reg);
653 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
654
655 /* cursor SR */
656 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
657 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200658 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300659 reg = I915_READ(DSPFW3);
660 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200661 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300662 I915_WRITE(DSPFW3, reg);
663
664 /* Display HPLL off SR */
665 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
666 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200667 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300668 reg = I915_READ(DSPFW3);
669 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200670 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300671 I915_WRITE(DSPFW3, reg);
672
673 /* cursor HPLL off SR */
674 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
675 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200676 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300677 reg = I915_READ(DSPFW3);
678 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200679 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300680 I915_WRITE(DSPFW3, reg);
681 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
682
Imre Deak5209b1f2014-07-01 12:36:17 +0300683 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300684 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300685 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300686 }
687}
688
689static bool g4x_compute_wm0(struct drm_device *dev,
690 int plane,
691 const struct intel_watermark_params *display,
692 int display_latency_ns,
693 const struct intel_watermark_params *cursor,
694 int cursor_latency_ns,
695 int *plane_wm,
696 int *cursor_wm)
697{
698 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300699 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +0200700 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300701 int line_time_us, line_count;
702 int entries, tlb_miss;
703
704 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000705 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300706 *cursor_wm = cursor->guard_size;
707 *plane_wm = display->guard_size;
708 return false;
709 }
710
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200711 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100712 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800713 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200714 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +0200715 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300716
717 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200718 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300719 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
720 if (tlb_miss > 0)
721 entries += tlb_miss;
722 entries = DIV_ROUND_UP(entries, display->cacheline_size);
723 *plane_wm = entries + display->guard_size;
724 if (*plane_wm > (int)display->max_wm)
725 *plane_wm = display->max_wm;
726
727 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200728 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300729 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200730 entries = line_count * crtc->cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300731 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
732 if (tlb_miss > 0)
733 entries += tlb_miss;
734 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
735 *cursor_wm = entries + cursor->guard_size;
736 if (*cursor_wm > (int)cursor->max_wm)
737 *cursor_wm = (int)cursor->max_wm;
738
739 return true;
740}
741
742/*
743 * Check the wm result.
744 *
745 * If any calculated watermark values is larger than the maximum value that
746 * can be programmed into the associated watermark register, that watermark
747 * must be disabled.
748 */
749static bool g4x_check_srwm(struct drm_device *dev,
750 int display_wm, int cursor_wm,
751 const struct intel_watermark_params *display,
752 const struct intel_watermark_params *cursor)
753{
754 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
755 display_wm, cursor_wm);
756
757 if (display_wm > display->max_wm) {
758 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
759 display_wm, display->max_wm);
760 return false;
761 }
762
763 if (cursor_wm > cursor->max_wm) {
764 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
765 cursor_wm, cursor->max_wm);
766 return false;
767 }
768
769 if (!(display_wm || cursor_wm)) {
770 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
771 return false;
772 }
773
774 return true;
775}
776
777static bool g4x_compute_srwm(struct drm_device *dev,
778 int plane,
779 int latency_ns,
780 const struct intel_watermark_params *display,
781 const struct intel_watermark_params *cursor,
782 int *display_wm, int *cursor_wm)
783{
784 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300785 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +0200786 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300787 unsigned long line_time_us;
788 int line_count, line_size;
789 int small, large;
790 int entries;
791
792 if (!latency_ns) {
793 *display_wm = *cursor_wm = 0;
794 return false;
795 }
796
797 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200798 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100799 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800800 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200801 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +0200802 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300803
Ville Syrjälä922044c2014-02-14 14:18:57 +0200804 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300805 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200806 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300807
808 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200809 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300810 large = line_count * line_size;
811
812 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
813 *display_wm = entries + display->guard_size;
814
815 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläac484962016-01-20 21:05:26 +0200816 entries = line_count * cpp * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300817 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
818 *cursor_wm = entries + cursor->guard_size;
819
820 return g4x_check_srwm(dev,
821 *display_wm, *cursor_wm,
822 display, cursor);
823}
824
Ville Syrjälä15665972015-03-10 16:16:28 +0200825#define FW_WM_VLV(value, plane) \
826 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
827
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200828static void vlv_write_wm_values(struct intel_crtc *crtc,
829 const struct vlv_wm_values *wm)
830{
831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
832 enum pipe pipe = crtc->pipe;
833
834 I915_WRITE(VLV_DDL(pipe),
835 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
836 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
837 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
838 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
839
Ville Syrjäläae801522015-03-05 21:19:49 +0200840 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200841 FW_WM(wm->sr.plane, SR) |
842 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
843 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
844 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200845 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200846 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
847 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
848 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200849 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200850 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200851
852 if (IS_CHERRYVIEW(dev_priv)) {
853 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200854 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
855 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200856 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200857 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
858 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200859 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200860 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
861 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200862 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200863 FW_WM(wm->sr.plane >> 9, SR_HI) |
864 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
865 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
866 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
867 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
868 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
869 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
870 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
871 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
872 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200873 } else {
874 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200875 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
876 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200877 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200878 FW_WM(wm->sr.plane >> 9, SR_HI) |
879 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
880 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
881 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
882 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
883 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
884 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200885 }
886
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300887 /* zero (unused) WM1 watermarks */
888 I915_WRITE(DSPFW4, 0);
889 I915_WRITE(DSPFW5, 0);
890 I915_WRITE(DSPFW6, 0);
891 I915_WRITE(DSPHOWM1, 0);
892
Ville Syrjäläae801522015-03-05 21:19:49 +0200893 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200894}
895
Ville Syrjälä15665972015-03-10 16:16:28 +0200896#undef FW_WM_VLV
897
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300898enum vlv_wm_level {
899 VLV_WM_LEVEL_PM2,
900 VLV_WM_LEVEL_PM5,
901 VLV_WM_LEVEL_DDR_DVFS,
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300902};
903
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300904/* latency must be in 0.1us units. */
905static unsigned int vlv_wm_method2(unsigned int pixel_rate,
906 unsigned int pipe_htotal,
907 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200908 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300909 unsigned int latency)
910{
911 unsigned int ret;
912
913 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200914 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300915 ret = DIV_ROUND_UP(ret, 64);
916
917 return ret;
918}
919
920static void vlv_setup_wm_latency(struct drm_device *dev)
921{
922 struct drm_i915_private *dev_priv = dev->dev_private;
923
924 /* all latencies in usec */
925 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
926
Ville Syrjälä58590c12015-09-08 21:05:12 +0300927 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
928
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300929 if (IS_CHERRYVIEW(dev_priv)) {
930 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
931 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300932
933 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300934 }
935}
936
937static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
938 struct intel_crtc *crtc,
939 const struct intel_plane_state *state,
940 int level)
941{
942 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläac484962016-01-20 21:05:26 +0200943 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300944
945 if (dev_priv->wm.pri_latency[level] == 0)
946 return USHRT_MAX;
947
948 if (!state->visible)
949 return 0;
950
Ville Syrjäläac484962016-01-20 21:05:26 +0200951 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300952 clock = crtc->config->base.adjusted_mode.crtc_clock;
953 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
954 width = crtc->config->pipe_src_w;
955 if (WARN_ON(htotal == 0))
956 htotal = 1;
957
958 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
959 /*
960 * FIXME the formula gives values that are
961 * too big for the cursor FIFO, and hence we
962 * would never be able to use cursors. For
963 * now just hardcode the watermark.
964 */
965 wm = 63;
966 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +0200967 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300968 dev_priv->wm.pri_latency[level] * 10);
969 }
970
971 return min_t(int, wm, USHRT_MAX);
972}
973
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +0300974static void vlv_compute_fifo(struct intel_crtc *crtc)
975{
976 struct drm_device *dev = crtc->base.dev;
977 struct vlv_wm_state *wm_state = &crtc->wm_state;
978 struct intel_plane *plane;
979 unsigned int total_rate = 0;
980 const int fifo_size = 512 - 1;
981 int fifo_extra, fifo_left = fifo_size;
982
983 for_each_intel_plane_on_crtc(dev, crtc, plane) {
984 struct intel_plane_state *state =
985 to_intel_plane_state(plane->base.state);
986
987 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
988 continue;
989
990 if (state->visible) {
991 wm_state->num_active_planes++;
992 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
993 }
994 }
995
996 for_each_intel_plane_on_crtc(dev, crtc, plane) {
997 struct intel_plane_state *state =
998 to_intel_plane_state(plane->base.state);
999 unsigned int rate;
1000
1001 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1002 plane->wm.fifo_size = 63;
1003 continue;
1004 }
1005
1006 if (!state->visible) {
1007 plane->wm.fifo_size = 0;
1008 continue;
1009 }
1010
1011 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1012 plane->wm.fifo_size = fifo_size * rate / total_rate;
1013 fifo_left -= plane->wm.fifo_size;
1014 }
1015
1016 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1017
1018 /* spread the remainder evenly */
1019 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1020 int plane_extra;
1021
1022 if (fifo_left == 0)
1023 break;
1024
1025 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1026 continue;
1027
1028 /* give it all to the first plane if none are active */
1029 if (plane->wm.fifo_size == 0 &&
1030 wm_state->num_active_planes)
1031 continue;
1032
1033 plane_extra = min(fifo_extra, fifo_left);
1034 plane->wm.fifo_size += plane_extra;
1035 fifo_left -= plane_extra;
1036 }
1037
1038 WARN_ON(fifo_left != 0);
1039}
1040
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001041static void vlv_invert_wms(struct intel_crtc *crtc)
1042{
1043 struct vlv_wm_state *wm_state = &crtc->wm_state;
1044 int level;
1045
1046 for (level = 0; level < wm_state->num_levels; level++) {
1047 struct drm_device *dev = crtc->base.dev;
1048 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1049 struct intel_plane *plane;
1050
1051 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1052 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1053
1054 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1055 switch (plane->base.type) {
1056 int sprite;
1057 case DRM_PLANE_TYPE_CURSOR:
1058 wm_state->wm[level].cursor = plane->wm.fifo_size -
1059 wm_state->wm[level].cursor;
1060 break;
1061 case DRM_PLANE_TYPE_PRIMARY:
1062 wm_state->wm[level].primary = plane->wm.fifo_size -
1063 wm_state->wm[level].primary;
1064 break;
1065 case DRM_PLANE_TYPE_OVERLAY:
1066 sprite = plane->plane;
1067 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1068 wm_state->wm[level].sprite[sprite];
1069 break;
1070 }
1071 }
1072 }
1073}
1074
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001075static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001076{
1077 struct drm_device *dev = crtc->base.dev;
1078 struct vlv_wm_state *wm_state = &crtc->wm_state;
1079 struct intel_plane *plane;
1080 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1081 int level;
1082
1083 memset(wm_state, 0, sizeof(*wm_state));
1084
Ville Syrjälä852eb002015-06-24 22:00:07 +03001085 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001086 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001087
1088 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001089
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001090 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001091
1092 if (wm_state->num_active_planes != 1)
1093 wm_state->cxsr = false;
1094
1095 if (wm_state->cxsr) {
1096 for (level = 0; level < wm_state->num_levels; level++) {
1097 wm_state->sr[level].plane = sr_fifo_size;
1098 wm_state->sr[level].cursor = 63;
1099 }
1100 }
1101
1102 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1103 struct intel_plane_state *state =
1104 to_intel_plane_state(plane->base.state);
1105
1106 if (!state->visible)
1107 continue;
1108
1109 /* normal watermarks */
1110 for (level = 0; level < wm_state->num_levels; level++) {
1111 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1112 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1113
1114 /* hack */
1115 if (WARN_ON(level == 0 && wm > max_wm))
1116 wm = max_wm;
1117
1118 if (wm > plane->wm.fifo_size)
1119 break;
1120
1121 switch (plane->base.type) {
1122 int sprite;
1123 case DRM_PLANE_TYPE_CURSOR:
1124 wm_state->wm[level].cursor = wm;
1125 break;
1126 case DRM_PLANE_TYPE_PRIMARY:
1127 wm_state->wm[level].primary = wm;
1128 break;
1129 case DRM_PLANE_TYPE_OVERLAY:
1130 sprite = plane->plane;
1131 wm_state->wm[level].sprite[sprite] = wm;
1132 break;
1133 }
1134 }
1135
1136 wm_state->num_levels = level;
1137
1138 if (!wm_state->cxsr)
1139 continue;
1140
1141 /* maxfifo watermarks */
1142 switch (plane->base.type) {
1143 int sprite, level;
1144 case DRM_PLANE_TYPE_CURSOR:
1145 for (level = 0; level < wm_state->num_levels; level++)
1146 wm_state->sr[level].cursor =
Thomas Daniel5a37ed02015-10-23 14:55:38 +01001147 wm_state->wm[level].cursor;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001148 break;
1149 case DRM_PLANE_TYPE_PRIMARY:
1150 for (level = 0; level < wm_state->num_levels; level++)
1151 wm_state->sr[level].plane =
1152 min(wm_state->sr[level].plane,
1153 wm_state->wm[level].primary);
1154 break;
1155 case DRM_PLANE_TYPE_OVERLAY:
1156 sprite = plane->plane;
1157 for (level = 0; level < wm_state->num_levels; level++)
1158 wm_state->sr[level].plane =
1159 min(wm_state->sr[level].plane,
1160 wm_state->wm[level].sprite[sprite]);
1161 break;
1162 }
1163 }
1164
1165 /* clear any (partially) filled invalid levels */
Ville Syrjälä58590c12015-09-08 21:05:12 +03001166 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001167 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1168 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1169 }
1170
1171 vlv_invert_wms(crtc);
1172}
1173
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001174#define VLV_FIFO(plane, value) \
1175 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1176
1177static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1178{
1179 struct drm_device *dev = crtc->base.dev;
1180 struct drm_i915_private *dev_priv = to_i915(dev);
1181 struct intel_plane *plane;
1182 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1183
1184 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1185 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1186 WARN_ON(plane->wm.fifo_size != 63);
1187 continue;
1188 }
1189
1190 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1191 sprite0_start = plane->wm.fifo_size;
1192 else if (plane->plane == 0)
1193 sprite1_start = sprite0_start + plane->wm.fifo_size;
1194 else
1195 fifo_size = sprite1_start + plane->wm.fifo_size;
1196 }
1197
1198 WARN_ON(fifo_size != 512 - 1);
1199
1200 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1201 pipe_name(crtc->pipe), sprite0_start,
1202 sprite1_start, fifo_size);
1203
1204 switch (crtc->pipe) {
1205 uint32_t dsparb, dsparb2, dsparb3;
1206 case PIPE_A:
1207 dsparb = I915_READ(DSPARB);
1208 dsparb2 = I915_READ(DSPARB2);
1209
1210 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1211 VLV_FIFO(SPRITEB, 0xff));
1212 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1213 VLV_FIFO(SPRITEB, sprite1_start));
1214
1215 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1216 VLV_FIFO(SPRITEB_HI, 0x1));
1217 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1218 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1219
1220 I915_WRITE(DSPARB, dsparb);
1221 I915_WRITE(DSPARB2, dsparb2);
1222 break;
1223 case PIPE_B:
1224 dsparb = I915_READ(DSPARB);
1225 dsparb2 = I915_READ(DSPARB2);
1226
1227 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1228 VLV_FIFO(SPRITED, 0xff));
1229 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1230 VLV_FIFO(SPRITED, sprite1_start));
1231
1232 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1233 VLV_FIFO(SPRITED_HI, 0xff));
1234 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1235 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1236
1237 I915_WRITE(DSPARB, dsparb);
1238 I915_WRITE(DSPARB2, dsparb2);
1239 break;
1240 case PIPE_C:
1241 dsparb3 = I915_READ(DSPARB3);
1242 dsparb2 = I915_READ(DSPARB2);
1243
1244 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1245 VLV_FIFO(SPRITEF, 0xff));
1246 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1247 VLV_FIFO(SPRITEF, sprite1_start));
1248
1249 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1250 VLV_FIFO(SPRITEF_HI, 0xff));
1251 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1252 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1253
1254 I915_WRITE(DSPARB3, dsparb3);
1255 I915_WRITE(DSPARB2, dsparb2);
1256 break;
1257 default:
1258 break;
1259 }
1260}
1261
1262#undef VLV_FIFO
1263
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001264static void vlv_merge_wm(struct drm_device *dev,
1265 struct vlv_wm_values *wm)
1266{
1267 struct intel_crtc *crtc;
1268 int num_active_crtcs = 0;
1269
Ville Syrjälä58590c12015-09-08 21:05:12 +03001270 wm->level = to_i915(dev)->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001271 wm->cxsr = true;
1272
1273 for_each_intel_crtc(dev, crtc) {
1274 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1275
1276 if (!crtc->active)
1277 continue;
1278
1279 if (!wm_state->cxsr)
1280 wm->cxsr = false;
1281
1282 num_active_crtcs++;
1283 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1284 }
1285
1286 if (num_active_crtcs != 1)
1287 wm->cxsr = false;
1288
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001289 if (num_active_crtcs > 1)
1290 wm->level = VLV_WM_LEVEL_PM2;
1291
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001292 for_each_intel_crtc(dev, crtc) {
1293 struct vlv_wm_state *wm_state = &crtc->wm_state;
1294 enum pipe pipe = crtc->pipe;
1295
1296 if (!crtc->active)
1297 continue;
1298
1299 wm->pipe[pipe] = wm_state->wm[wm->level];
1300 if (wm->cxsr)
1301 wm->sr = wm_state->sr[wm->level];
1302
1303 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1304 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1305 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1306 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1307 }
1308}
1309
1310static void vlv_update_wm(struct drm_crtc *crtc)
1311{
1312 struct drm_device *dev = crtc->dev;
1313 struct drm_i915_private *dev_priv = dev->dev_private;
1314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1315 enum pipe pipe = intel_crtc->pipe;
1316 struct vlv_wm_values wm = {};
1317
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001318 vlv_compute_wm(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001319 vlv_merge_wm(dev, &wm);
1320
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001321 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1322 /* FIXME should be part of crtc atomic commit */
1323 vlv_pipe_set_fifo_size(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001324 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001325 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001326
1327 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1328 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1329 chv_set_memory_dvfs(dev_priv, false);
1330
1331 if (wm.level < VLV_WM_LEVEL_PM5 &&
1332 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1333 chv_set_memory_pm5(dev_priv, false);
1334
Ville Syrjälä852eb002015-06-24 22:00:07 +03001335 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001336 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001337
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001338 /* FIXME should be part of crtc atomic commit */
1339 vlv_pipe_set_fifo_size(intel_crtc);
1340
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001341 vlv_write_wm_values(intel_crtc, &wm);
1342
1343 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1344 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1345 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1346 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1347 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1348
Ville Syrjälä852eb002015-06-24 22:00:07 +03001349 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001350 intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001351
1352 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1353 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1354 chv_set_memory_pm5(dev_priv, true);
1355
1356 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1357 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1358 chv_set_memory_dvfs(dev_priv, true);
1359
1360 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001361}
1362
Ville Syrjäläae801522015-03-05 21:19:49 +02001363#define single_plane_enabled(mask) is_power_of_2(mask)
1364
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001365static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001366{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001367 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001368 static const int sr_latency_ns = 12000;
1369 struct drm_i915_private *dev_priv = dev->dev_private;
1370 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1371 int plane_sr, cursor_sr;
1372 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001373 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001374
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001375 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001376 &g4x_wm_info, pessimal_latency_ns,
1377 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001379 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001380
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001381 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001382 &g4x_wm_info, pessimal_latency_ns,
1383 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001384 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001385 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001386
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001387 if (single_plane_enabled(enabled) &&
1388 g4x_compute_srwm(dev, ffs(enabled) - 1,
1389 sr_latency_ns,
1390 &g4x_wm_info,
1391 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001392 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001393 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001394 } else {
Imre Deak98584252014-06-13 14:54:20 +03001395 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001396 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001397 plane_sr = cursor_sr = 0;
1398 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001399
Ville Syrjäläa5043452014-06-28 02:04:18 +03001400 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1401 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001402 planea_wm, cursora_wm,
1403 planeb_wm, cursorb_wm,
1404 plane_sr, cursor_sr);
1405
1406 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001407 FW_WM(plane_sr, SR) |
1408 FW_WM(cursorb_wm, CURSORB) |
1409 FW_WM(planeb_wm, PLANEB) |
1410 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001411 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001412 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001413 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001414 /* HPLL off in SR has some issues on G4x... disable it */
1415 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001416 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001417 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001418
1419 if (cxsr_enabled)
1420 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001421}
1422
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001423static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001424{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001425 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001426 struct drm_i915_private *dev_priv = dev->dev_private;
1427 struct drm_crtc *crtc;
1428 int srwm = 1;
1429 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001430 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001431
1432 /* Calc sr entries for one plane configs */
1433 crtc = single_enabled_crtc(dev);
1434 if (crtc) {
1435 /* self-refresh has much higher latency */
1436 static const int sr_latency_ns = 12000;
Ville Syrjälä124abe02015-09-08 13:40:45 +03001437 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001438 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001439 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001440 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +02001441 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001442 unsigned long line_time_us;
1443 int entries;
1444
Ville Syrjälä922044c2014-02-14 14:18:57 +02001445 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001446
1447 /* Use ns/us then divide to preserve precision */
1448 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001449 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001450 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1451 srwm = I965_FIFO_SIZE - entries;
1452 if (srwm < 0)
1453 srwm = 1;
1454 srwm &= 0x1ff;
1455 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1456 entries, srwm);
1457
1458 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001459 cpp * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001460 entries = DIV_ROUND_UP(entries,
1461 i965_cursor_wm_info.cacheline_size);
1462 cursor_sr = i965_cursor_wm_info.fifo_size -
1463 (entries + i965_cursor_wm_info.guard_size);
1464
1465 if (cursor_sr > i965_cursor_wm_info.max_wm)
1466 cursor_sr = i965_cursor_wm_info.max_wm;
1467
1468 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1469 "cursor %d\n", srwm, cursor_sr);
1470
Imre Deak98584252014-06-13 14:54:20 +03001471 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001472 } else {
Imre Deak98584252014-06-13 14:54:20 +03001473 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001474 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001475 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001476 }
1477
1478 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1479 srwm);
1480
1481 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001482 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1483 FW_WM(8, CURSORB) |
1484 FW_WM(8, PLANEB) |
1485 FW_WM(8, PLANEA));
1486 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1487 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001488 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001489 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001490
1491 if (cxsr_enabled)
1492 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001493}
1494
Ville Syrjäläf4998962015-03-10 17:02:21 +02001495#undef FW_WM
1496
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001497static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001498{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001499 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001500 struct drm_i915_private *dev_priv = dev->dev_private;
1501 const struct intel_watermark_params *wm_info;
1502 uint32_t fwater_lo;
1503 uint32_t fwater_hi;
1504 int cwm, srwm = 1;
1505 int fifo_size;
1506 int planea_wm, planeb_wm;
1507 struct drm_crtc *crtc, *enabled = NULL;
1508
1509 if (IS_I945GM(dev))
1510 wm_info = &i945_wm_info;
1511 else if (!IS_GEN2(dev))
1512 wm_info = &i915_wm_info;
1513 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001514 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001515
1516 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1517 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001518 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001519 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001520 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001521 if (IS_GEN2(dev))
1522 cpp = 4;
1523
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001524 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001525 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001526 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001527 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001528 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001529 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001530 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001531 if (planea_wm > (long)wm_info->max_wm)
1532 planea_wm = wm_info->max_wm;
1533 }
1534
1535 if (IS_GEN2(dev))
1536 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001537
1538 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1539 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001540 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001541 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001542 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001543 if (IS_GEN2(dev))
1544 cpp = 4;
1545
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001546 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001547 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001548 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001549 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001550 if (enabled == NULL)
1551 enabled = crtc;
1552 else
1553 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001554 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001555 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001556 if (planeb_wm > (long)wm_info->max_wm)
1557 planeb_wm = wm_info->max_wm;
1558 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001559
1560 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1561
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001562 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001563 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001564
Matt Roper59bea882015-02-27 10:12:01 -08001565 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001566
1567 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001568 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001569 enabled = NULL;
1570 }
1571
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001572 /*
1573 * Overlay gets an aggressive default since video jitter is bad.
1574 */
1575 cwm = 2;
1576
1577 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001578 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001579
1580 /* Calc sr entries for one plane configs */
1581 if (HAS_FW_BLC(dev) && enabled) {
1582 /* self-refresh has much higher latency */
1583 static const int sr_latency_ns = 6000;
Ville Syrjälä124abe02015-09-08 13:40:45 +03001584 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001585 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001586 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001587 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +02001588 int cpp = drm_format_plane_cpp(enabled->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001589 unsigned long line_time_us;
1590 int entries;
1591
Ville Syrjälä922044c2014-02-14 14:18:57 +02001592 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001593
1594 /* Use ns/us then divide to preserve precision */
1595 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001596 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001597 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1598 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1599 srwm = wm_info->fifo_size - entries;
1600 if (srwm < 0)
1601 srwm = 1;
1602
1603 if (IS_I945G(dev) || IS_I945GM(dev))
1604 I915_WRITE(FW_BLC_SELF,
1605 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1606 else if (IS_I915GM(dev))
1607 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1608 }
1609
1610 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1611 planea_wm, planeb_wm, cwm, srwm);
1612
1613 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1614 fwater_hi = (cwm & 0x1f);
1615
1616 /* Set request length to 8 cachelines per fetch */
1617 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1618 fwater_hi = fwater_hi | (1 << 8);
1619
1620 I915_WRITE(FW_BLC, fwater_lo);
1621 I915_WRITE(FW_BLC2, fwater_hi);
1622
Imre Deak5209b1f2014-07-01 12:36:17 +03001623 if (enabled)
1624 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001625}
1626
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001627static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001628{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001629 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001630 struct drm_i915_private *dev_priv = dev->dev_private;
1631 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001632 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001633 uint32_t fwater_lo;
1634 int planea_wm;
1635
1636 crtc = single_enabled_crtc(dev);
1637 if (crtc == NULL)
1638 return;
1639
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001640 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001641 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001642 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001643 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001644 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001645 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1646 fwater_lo |= (3<<8) | planea_wm;
1647
1648 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1649
1650 I915_WRITE(FW_BLC, fwater_lo);
1651}
1652
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001653uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001654{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001655 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001656
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001657 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001658
1659 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1660 * adjust the pixel_rate here. */
1661
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001662 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001663 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001664 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001665
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001666 pipe_w = pipe_config->pipe_src_w;
1667 pipe_h = pipe_config->pipe_src_h;
1668
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001669 pfit_w = (pfit_size >> 16) & 0xFFFF;
1670 pfit_h = pfit_size & 0xFFFF;
1671 if (pipe_w < pfit_w)
1672 pipe_w = pfit_w;
1673 if (pipe_h < pfit_h)
1674 pipe_h = pfit_h;
1675
Matt Roper15126882015-12-03 11:37:40 -08001676 if (WARN_ON(!pfit_w || !pfit_h))
1677 return pixel_rate;
1678
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001679 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1680 pfit_w * pfit_h);
1681 }
1682
1683 return pixel_rate;
1684}
1685
Ville Syrjälä37126462013-08-01 16:18:55 +03001686/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001687static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001688{
1689 uint64_t ret;
1690
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001691 if (WARN(latency == 0, "Latency value missing\n"))
1692 return UINT_MAX;
1693
Ville Syrjäläac484962016-01-20 21:05:26 +02001694 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001695 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1696
1697 return ret;
1698}
1699
Ville Syrjälä37126462013-08-01 16:18:55 +03001700/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001701static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001702 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001703 uint32_t latency)
1704{
1705 uint32_t ret;
1706
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001707 if (WARN(latency == 0, "Latency value missing\n"))
1708 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001709 if (WARN_ON(!pipe_htotal))
1710 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001711
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001712 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001713 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001714 ret = DIV_ROUND_UP(ret, 64) + 2;
1715 return ret;
1716}
1717
Ville Syrjälä23297042013-07-05 11:57:17 +03001718static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001719 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001720{
Matt Roper15126882015-12-03 11:37:40 -08001721 /*
1722 * Neither of these should be possible since this function shouldn't be
1723 * called if the CRTC is off or the plane is invisible. But let's be
1724 * extra paranoid to avoid a potential divide-by-zero if we screw up
1725 * elsewhere in the driver.
1726 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001727 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001728 return 0;
1729 if (WARN_ON(!horiz_pixels))
1730 return 0;
1731
Ville Syrjäläac484962016-01-20 21:05:26 +02001732 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001733}
1734
Imre Deak820c1982013-12-17 14:46:36 +02001735struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001736 uint16_t pri;
1737 uint16_t spr;
1738 uint16_t cur;
1739 uint16_t fbc;
1740};
1741
Ville Syrjälä37126462013-08-01 16:18:55 +03001742/*
1743 * For both WM_PIPE and WM_LP.
1744 * mem_value must be in 0.1us units.
1745 */
Matt Roper7221fc32015-09-24 15:53:08 -07001746static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001747 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001748 uint32_t mem_value,
1749 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001750{
Ville Syrjäläac484962016-01-20 21:05:26 +02001751 int cpp = pstate->base.fb ?
1752 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001753 uint32_t method1, method2;
1754
Matt Roper7221fc32015-09-24 15:53:08 -07001755 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001756 return 0;
1757
Ville Syrjäläac484962016-01-20 21:05:26 +02001758 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001759
1760 if (!is_lp)
1761 return method1;
1762
Matt Roper7221fc32015-09-24 15:53:08 -07001763 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1764 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001765 drm_rect_width(&pstate->dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001766 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001767
1768 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001769}
1770
Ville Syrjälä37126462013-08-01 16:18:55 +03001771/*
1772 * For both WM_PIPE and WM_LP.
1773 * mem_value must be in 0.1us units.
1774 */
Matt Roper7221fc32015-09-24 15:53:08 -07001775static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001776 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001777 uint32_t mem_value)
1778{
Ville Syrjäläac484962016-01-20 21:05:26 +02001779 int cpp = pstate->base.fb ?
1780 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001781 uint32_t method1, method2;
1782
Matt Roper7221fc32015-09-24 15:53:08 -07001783 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001784 return 0;
1785
Ville Syrjäläac484962016-01-20 21:05:26 +02001786 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Matt Roper7221fc32015-09-24 15:53:08 -07001787 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1788 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001789 drm_rect_width(&pstate->dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001790 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001791 return min(method1, method2);
1792}
1793
Ville Syrjälä37126462013-08-01 16:18:55 +03001794/*
1795 * For both WM_PIPE and WM_LP.
1796 * mem_value must be in 0.1us units.
1797 */
Matt Roper7221fc32015-09-24 15:53:08 -07001798static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001799 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001800 uint32_t mem_value)
1801{
Matt Roperb2435692016-02-02 22:06:51 -08001802 /*
1803 * We treat the cursor plane as always-on for the purposes of watermark
1804 * calculation. Until we have two-stage watermark programming merged,
1805 * this is necessary to avoid flickering.
1806 */
1807 int cpp = 4;
1808 int width = pstate->visible ? pstate->base.crtc_w : 64;
Matt Roper43d59ed2015-09-24 15:53:07 -07001809
Matt Roperb2435692016-02-02 22:06:51 -08001810 if (!cstate->base.active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001811 return 0;
1812
Matt Roper7221fc32015-09-24 15:53:08 -07001813 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1814 cstate->base.adjusted_mode.crtc_htotal,
Matt Roperb2435692016-02-02 22:06:51 -08001815 width, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001816}
1817
Paulo Zanonicca32e92013-05-31 11:45:06 -03001818/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07001819static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001820 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001821 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001822{
Ville Syrjäläac484962016-01-20 21:05:26 +02001823 int cpp = pstate->base.fb ?
1824 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Matt Roper43d59ed2015-09-24 15:53:07 -07001825
Matt Roper7221fc32015-09-24 15:53:08 -07001826 if (!cstate->base.active || !pstate->visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001827 return 0;
1828
Ville Syrjäläac484962016-01-20 21:05:26 +02001829 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001830}
1831
Ville Syrjälä158ae642013-08-07 13:28:19 +03001832static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1833{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001834 if (INTEL_INFO(dev)->gen >= 8)
1835 return 3072;
1836 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001837 return 768;
1838 else
1839 return 512;
1840}
1841
Ville Syrjälä4e975082014-03-07 18:32:11 +02001842static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1843 int level, bool is_sprite)
1844{
1845 if (INTEL_INFO(dev)->gen >= 8)
1846 /* BDW primary/sprite plane watermarks */
1847 return level == 0 ? 255 : 2047;
1848 else if (INTEL_INFO(dev)->gen >= 7)
1849 /* IVB/HSW primary/sprite plane watermarks */
1850 return level == 0 ? 127 : 1023;
1851 else if (!is_sprite)
1852 /* ILK/SNB primary plane watermarks */
1853 return level == 0 ? 127 : 511;
1854 else
1855 /* ILK/SNB sprite plane watermarks */
1856 return level == 0 ? 63 : 255;
1857}
1858
1859static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1860 int level)
1861{
1862 if (INTEL_INFO(dev)->gen >= 7)
1863 return level == 0 ? 63 : 255;
1864 else
1865 return level == 0 ? 31 : 63;
1866}
1867
1868static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1869{
1870 if (INTEL_INFO(dev)->gen >= 8)
1871 return 31;
1872 else
1873 return 15;
1874}
1875
Ville Syrjälä158ae642013-08-07 13:28:19 +03001876/* Calculate the maximum primary/sprite plane watermark */
1877static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1878 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001879 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001880 enum intel_ddb_partitioning ddb_partitioning,
1881 bool is_sprite)
1882{
1883 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001884
1885 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001886 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001887 return 0;
1888
1889 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001890 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001891 fifo_size /= INTEL_INFO(dev)->num_pipes;
1892
1893 /*
1894 * For some reason the non self refresh
1895 * FIFO size is only half of the self
1896 * refresh FIFO size on ILK/SNB.
1897 */
1898 if (INTEL_INFO(dev)->gen <= 6)
1899 fifo_size /= 2;
1900 }
1901
Ville Syrjälä240264f2013-08-07 13:29:12 +03001902 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001903 /* level 0 is always calculated with 1:1 split */
1904 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1905 if (is_sprite)
1906 fifo_size *= 5;
1907 fifo_size /= 6;
1908 } else {
1909 fifo_size /= 2;
1910 }
1911 }
1912
1913 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001914 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001915}
1916
1917/* Calculate the maximum cursor plane watermark */
1918static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001919 int level,
1920 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001921{
1922 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001923 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001924 return 64;
1925
1926 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001927 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001928}
1929
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001930static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001931 int level,
1932 const struct intel_wm_config *config,
1933 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001934 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001935{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001936 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1937 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1938 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001939 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001940}
1941
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001942static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1943 int level,
1944 struct ilk_wm_maximums *max)
1945{
1946 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1947 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1948 max->cur = ilk_cursor_wm_reg_max(dev, level);
1949 max->fbc = ilk_fbc_wm_reg_max(dev);
1950}
1951
Ville Syrjäläd9395652013-10-09 19:18:10 +03001952static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001953 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001954 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001955{
1956 bool ret;
1957
1958 /* already determined to be invalid? */
1959 if (!result->enable)
1960 return false;
1961
1962 result->enable = result->pri_val <= max->pri &&
1963 result->spr_val <= max->spr &&
1964 result->cur_val <= max->cur;
1965
1966 ret = result->enable;
1967
1968 /*
1969 * HACK until we can pre-compute everything,
1970 * and thus fail gracefully if LP0 watermarks
1971 * are exceeded...
1972 */
1973 if (level == 0 && !result->enable) {
1974 if (result->pri_val > max->pri)
1975 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1976 level, result->pri_val, max->pri);
1977 if (result->spr_val > max->spr)
1978 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1979 level, result->spr_val, max->spr);
1980 if (result->cur_val > max->cur)
1981 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1982 level, result->cur_val, max->cur);
1983
1984 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1985 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1986 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1987 result->enable = true;
1988 }
1989
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001990 return ret;
1991}
1992
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001993static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07001994 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001995 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07001996 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07001997 struct intel_plane_state *pristate,
1998 struct intel_plane_state *sprstate,
1999 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002000 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002001{
2002 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2003 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2004 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2005
2006 /* WM1+ latency values stored in 0.5us units */
2007 if (level > 0) {
2008 pri_latency *= 5;
2009 spr_latency *= 5;
2010 cur_latency *= 5;
2011 }
2012
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002013 if (pristate) {
2014 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2015 pri_latency, level);
2016 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2017 }
2018
2019 if (sprstate)
2020 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2021
2022 if (curstate)
2023 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2024
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002025 result->enable = true;
2026}
2027
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002028static uint32_t
Matt Roperee91a152015-12-03 11:37:39 -08002029hsw_compute_linetime_wm(struct drm_device *dev,
2030 struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002031{
2032 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperee91a152015-12-03 11:37:39 -08002033 const struct drm_display_mode *adjusted_mode =
2034 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002035 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002036
Matt Roperee91a152015-12-03 11:37:39 -08002037 if (!cstate->base.active)
2038 return 0;
2039 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2040 return 0;
2041 if (WARN_ON(dev_priv->cdclk_freq == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002042 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002043
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002044 /* The WM are computed with base on how long it takes to fill a single
2045 * row at the given clock rate, multiplied by 8.
2046 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002047 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2048 adjusted_mode->crtc_clock);
2049 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjälä05024da2015-06-03 15:45:08 +03002050 dev_priv->cdclk_freq);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002051
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002052 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2053 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002054}
2055
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002056static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002057{
2058 struct drm_i915_private *dev_priv = dev->dev_private;
2059
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002060 if (IS_GEN9(dev)) {
2061 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002062 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00002063 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002064
2065 /* read the first set of memory latencies[0:3] */
2066 val = 0; /* data0 to be programmed to 0 for first set */
2067 mutex_lock(&dev_priv->rps.hw_lock);
2068 ret = sandybridge_pcode_read(dev_priv,
2069 GEN9_PCODE_READ_MEM_LATENCY,
2070 &val);
2071 mutex_unlock(&dev_priv->rps.hw_lock);
2072
2073 if (ret) {
2074 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2075 return;
2076 }
2077
2078 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2079 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2080 GEN9_MEM_LATENCY_LEVEL_MASK;
2081 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2082 GEN9_MEM_LATENCY_LEVEL_MASK;
2083 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2084 GEN9_MEM_LATENCY_LEVEL_MASK;
2085
2086 /* read the second set of memory latencies[4:7] */
2087 val = 1; /* data0 to be programmed to 1 for second set */
2088 mutex_lock(&dev_priv->rps.hw_lock);
2089 ret = sandybridge_pcode_read(dev_priv,
2090 GEN9_PCODE_READ_MEM_LATENCY,
2091 &val);
2092 mutex_unlock(&dev_priv->rps.hw_lock);
2093 if (ret) {
2094 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2095 return;
2096 }
2097
2098 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2099 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2100 GEN9_MEM_LATENCY_LEVEL_MASK;
2101 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2102 GEN9_MEM_LATENCY_LEVEL_MASK;
2103 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2104 GEN9_MEM_LATENCY_LEVEL_MASK;
2105
Vandana Kannan367294b2014-11-04 17:06:46 +00002106 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002107 * WaWmMemoryReadLatency:skl
2108 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002109 * punit doesn't take into account the read latency so we need
2110 * to add 2us to the various latency levels we retrieve from
2111 * the punit.
2112 * - W0 is a bit special in that it's the only level that
2113 * can't be disabled if we want to have display working, so
2114 * we always add 2us there.
2115 * - For levels >=1, punit returns 0us latency when they are
2116 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00002117 *
2118 * Additionally, if a level n (n > 1) has a 0us latency, all
2119 * levels m (m >= n) need to be disabled. We make sure to
2120 * sanitize the values out of the punit to satisfy this
2121 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00002122 */
2123 wm[0] += 2;
2124 for (level = 1; level <= max_level; level++)
2125 if (wm[level] != 0)
2126 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002127 else {
2128 for (i = level + 1; i <= max_level; i++)
2129 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00002130
Vandana Kannan4f947382014-11-04 17:06:47 +00002131 break;
2132 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002133 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002134 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2135
2136 wm[0] = (sskpd >> 56) & 0xFF;
2137 if (wm[0] == 0)
2138 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002139 wm[1] = (sskpd >> 4) & 0xFF;
2140 wm[2] = (sskpd >> 12) & 0xFF;
2141 wm[3] = (sskpd >> 20) & 0x1FF;
2142 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002143 } else if (INTEL_INFO(dev)->gen >= 6) {
2144 uint32_t sskpd = I915_READ(MCH_SSKPD);
2145
2146 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2147 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2148 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2149 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002150 } else if (INTEL_INFO(dev)->gen >= 5) {
2151 uint32_t mltr = I915_READ(MLTR_ILK);
2152
2153 /* ILK primary LP0 latency is 700 ns */
2154 wm[0] = 7;
2155 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2156 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002157 }
2158}
2159
Ville Syrjälä53615a52013-08-01 16:18:50 +03002160static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2161{
2162 /* ILK sprite LP0 latency is 1300 ns */
2163 if (INTEL_INFO(dev)->gen == 5)
2164 wm[0] = 13;
2165}
2166
2167static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2168{
2169 /* ILK cursor LP0 latency is 1300 ns */
2170 if (INTEL_INFO(dev)->gen == 5)
2171 wm[0] = 13;
2172
2173 /* WaDoubleCursorLP3Latency:ivb */
2174 if (IS_IVYBRIDGE(dev))
2175 wm[3] *= 2;
2176}
2177
Damien Lespiau546c81f2014-05-13 15:30:26 +01002178int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002179{
2180 /* how many WM levels are we expecting */
Damien Lespiaub6e742f2015-05-09 02:05:55 +01002181 if (INTEL_INFO(dev)->gen >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002182 return 7;
2183 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002184 return 4;
2185 else if (INTEL_INFO(dev)->gen >= 6)
2186 return 3;
2187 else
2188 return 2;
2189}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002190
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002191static void intel_print_wm_latency(struct drm_device *dev,
2192 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002193 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002194{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002195 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002196
2197 for (level = 0; level <= max_level; level++) {
2198 unsigned int latency = wm[level];
2199
2200 if (latency == 0) {
2201 DRM_ERROR("%s WM%d latency not provided\n",
2202 name, level);
2203 continue;
2204 }
2205
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002206 /*
2207 * - latencies are in us on gen9.
2208 * - before then, WM1+ latency values are in 0.5us units
2209 */
2210 if (IS_GEN9(dev))
2211 latency *= 10;
2212 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002213 latency *= 5;
2214
2215 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2216 name, level, wm[level],
2217 latency / 10, latency % 10);
2218 }
2219}
2220
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002221static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2222 uint16_t wm[5], uint16_t min)
2223{
2224 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2225
2226 if (wm[0] >= min)
2227 return false;
2228
2229 wm[0] = max(wm[0], min);
2230 for (level = 1; level <= max_level; level++)
2231 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2232
2233 return true;
2234}
2235
2236static void snb_wm_latency_quirk(struct drm_device *dev)
2237{
2238 struct drm_i915_private *dev_priv = dev->dev_private;
2239 bool changed;
2240
2241 /*
2242 * The BIOS provided WM memory latency values are often
2243 * inadequate for high resolution displays. Adjust them.
2244 */
2245 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2246 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2247 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2248
2249 if (!changed)
2250 return;
2251
2252 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2253 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2254 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2255 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2256}
2257
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002258static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002259{
2260 struct drm_i915_private *dev_priv = dev->dev_private;
2261
2262 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2263
2264 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2265 sizeof(dev_priv->wm.pri_latency));
2266 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2267 sizeof(dev_priv->wm.pri_latency));
2268
2269 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2270 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002271
2272 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2273 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2274 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002275
2276 if (IS_GEN6(dev))
2277 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002278}
2279
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002280static void skl_setup_wm_latency(struct drm_device *dev)
2281{
2282 struct drm_i915_private *dev_priv = dev->dev_private;
2283
2284 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2285 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2286}
2287
Matt Ropered4a6a72016-02-23 17:20:13 -08002288static bool ilk_validate_pipe_wm(struct drm_device *dev,
2289 struct intel_pipe_wm *pipe_wm)
Matt Roper261a27d2015-10-08 15:28:25 -07002290{
Matt Roperbf220452016-01-19 11:43:04 -08002291 /* LP0 watermark maximums depend on this pipe alone */
Matt Ropered4a6a72016-02-23 17:20:13 -08002292 const struct intel_wm_config config = {
Matt Roperbf220452016-01-19 11:43:04 -08002293 .num_pipes_active = 1,
Matt Ropered4a6a72016-02-23 17:20:13 -08002294 .sprites_enabled = pipe_wm->sprites_enabled,
2295 .sprites_scaled = pipe_wm->sprites_scaled,
Matt Roperbf220452016-01-19 11:43:04 -08002296 };
Imre Deak820c1982013-12-17 14:46:36 +02002297 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002298
Matt Roperbf220452016-01-19 11:43:04 -08002299 /* LP0 watermarks always use 1/2 DDB partitioning */
2300 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2301
2302 /* At least LP0 must be valid */
Matt Ropered4a6a72016-02-23 17:20:13 -08002303 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2304 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2305 return false;
2306 }
2307
2308 return true;
2309}
2310
Ville Syrjälä158ae642013-08-07 13:28:19 +03002311/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002312static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002313{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002314 struct drm_atomic_state *state = cstate->base.state;
2315 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002316 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002317 struct drm_device *dev = state->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002318 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002319 struct intel_plane *intel_plane;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002320 struct intel_plane_state *pristate = NULL;
2321 struct intel_plane_state *sprstate = NULL;
2322 struct intel_plane_state *curstate = NULL;
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002323 int level, max_level = ilk_wm_max_level(dev), usable_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002324 struct ilk_wm_maximums max;
2325
Matt Roper86c8bbb2015-09-24 15:53:16 -07002326 pipe_wm = &cstate->wm.optimal.ilk;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002327
2328 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002329 struct intel_plane_state *ps;
2330
2331 ps = intel_atomic_get_existing_plane_state(state,
2332 intel_plane);
2333 if (!ps)
2334 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002335
2336 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002337 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002338 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002339 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002340 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002341 curstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002342 }
2343
Matt Ropered4a6a72016-02-23 17:20:13 -08002344 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002345 if (sprstate) {
2346 pipe_wm->sprites_enabled = sprstate->visible;
2347 pipe_wm->sprites_scaled = sprstate->visible &&
2348 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2349 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2350 }
2351
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002352 usable_level = max_level;
2353
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002354 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002355 if (INTEL_INFO(dev)->gen <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002356 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002357
2358 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002359 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002360 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002361
Matt Roper86c8bbb2015-09-24 15:53:16 -07002362 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002363 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2364
2365 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2366 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002367
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002368 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Matt Roperee91a152015-12-03 11:37:39 -08002369 pipe_wm->linetime = hsw_compute_linetime_wm(dev, cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002370
Matt Ropered4a6a72016-02-23 17:20:13 -08002371 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Matt Roperbf220452016-01-19 11:43:04 -08002372 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002373
2374 ilk_compute_wm_reg_maximums(dev, 1, &max);
2375
2376 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002377 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002378
Matt Roper86c8bbb2015-09-24 15:53:16 -07002379 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002380 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002381
2382 /*
2383 * Disable any watermark level that exceeds the
2384 * register maximums since such watermarks are
2385 * always invalid.
2386 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002387 if (level > usable_level)
2388 continue;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002389
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002390 if (ilk_validate_wm_level(level, &max, wm))
2391 pipe_wm->wm[level] = *wm;
2392 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002393 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002394 }
2395
Matt Roper86c8bbb2015-09-24 15:53:16 -07002396 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002397}
2398
2399/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002400 * Build a set of 'intermediate' watermark values that satisfy both the old
2401 * state and the new state. These can be programmed to the hardware
2402 * immediately.
2403 */
2404static int ilk_compute_intermediate_wm(struct drm_device *dev,
2405 struct intel_crtc *intel_crtc,
2406 struct intel_crtc_state *newstate)
2407{
2408 struct intel_pipe_wm *a = &newstate->wm.intermediate;
2409 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
2410 int level, max_level = ilk_wm_max_level(dev);
2411
2412 /*
2413 * Start with the final, target watermarks, then combine with the
2414 * currently active watermarks to get values that are safe both before
2415 * and after the vblank.
2416 */
2417 *a = newstate->wm.optimal.ilk;
2418 a->pipe_enabled |= b->pipe_enabled;
2419 a->sprites_enabled |= b->sprites_enabled;
2420 a->sprites_scaled |= b->sprites_scaled;
2421
2422 for (level = 0; level <= max_level; level++) {
2423 struct intel_wm_level *a_wm = &a->wm[level];
2424 const struct intel_wm_level *b_wm = &b->wm[level];
2425
2426 a_wm->enable &= b_wm->enable;
2427 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2428 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2429 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2430 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2431 }
2432
2433 /*
2434 * We need to make sure that these merged watermark values are
2435 * actually a valid configuration themselves. If they're not,
2436 * there's no safe way to transition from the old state to
2437 * the new state, so we need to fail the atomic transaction.
2438 */
2439 if (!ilk_validate_pipe_wm(dev, a))
2440 return -EINVAL;
2441
2442 /*
2443 * If our intermediate WM are identical to the final WM, then we can
2444 * omit the post-vblank programming; only update if it's different.
2445 */
2446 if (memcmp(a, &newstate->wm.optimal.ilk, sizeof(*a)) == 0)
2447 newstate->wm.need_postvbl_update = false;
2448
2449 return 0;
2450}
2451
2452/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002453 * Merge the watermarks from all active pipes for a specific level.
2454 */
2455static void ilk_merge_wm_level(struct drm_device *dev,
2456 int level,
2457 struct intel_wm_level *ret_wm)
2458{
2459 const struct intel_crtc *intel_crtc;
2460
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002461 ret_wm->enable = true;
2462
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002463 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002464 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002465 const struct intel_wm_level *wm = &active->wm[level];
2466
2467 if (!active->pipe_enabled)
2468 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002469
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002470 /*
2471 * The watermark values may have been used in the past,
2472 * so we must maintain them in the registers for some
2473 * time even if the level is now disabled.
2474 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002475 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002476 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002477
2478 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2479 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2480 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2481 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2482 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002483}
2484
2485/*
2486 * Merge all low power watermarks for all active pipes.
2487 */
2488static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002489 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002490 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002491 struct intel_pipe_wm *merged)
2492{
Paulo Zanoni7733b492015-07-07 15:26:04 -03002493 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002494 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002495 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002496
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002497 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2498 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2499 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002500 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002501
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002502 /* ILK: FBC WM must be disabled always */
2503 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002504
2505 /* merge each WM1+ level */
2506 for (level = 1; level <= max_level; level++) {
2507 struct intel_wm_level *wm = &merged->wm[level];
2508
2509 ilk_merge_wm_level(dev, level, wm);
2510
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002511 if (level > last_enabled_level)
2512 wm->enable = false;
2513 else if (!ilk_validate_wm_level(level, max, wm))
2514 /* make sure all following levels get disabled */
2515 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002516
2517 /*
2518 * The spec says it is preferred to disable
2519 * FBC WMs instead of disabling a WM level.
2520 */
2521 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002522 if (wm->enable)
2523 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002524 wm->fbc_val = 0;
2525 }
2526 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002527
2528 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2529 /*
2530 * FIXME this is racy. FBC might get enabled later.
2531 * What we should check here is whether FBC can be
2532 * enabled sometime later.
2533 */
Paulo Zanoni7733b492015-07-07 15:26:04 -03002534 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002535 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002536 for (level = 2; level <= max_level; level++) {
2537 struct intel_wm_level *wm = &merged->wm[level];
2538
2539 wm->enable = false;
2540 }
2541 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002542}
2543
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002544static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2545{
2546 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2547 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2548}
2549
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002550/* The value we need to program into the WM_LPx latency field */
2551static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2552{
2553 struct drm_i915_private *dev_priv = dev->dev_private;
2554
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002555 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002556 return 2 * level;
2557 else
2558 return dev_priv->wm.pri_latency[level];
2559}
2560
Imre Deak820c1982013-12-17 14:46:36 +02002561static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002562 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002563 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002564 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002565{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002566 struct intel_crtc *intel_crtc;
2567 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002568
Ville Syrjälä0362c782013-10-09 19:17:57 +03002569 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002570 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002571
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002572 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002573 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002574 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002575
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002576 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002577
Ville Syrjälä0362c782013-10-09 19:17:57 +03002578 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002579
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002580 /*
2581 * Maintain the watermark values even if the level is
2582 * disabled. Doing otherwise could cause underruns.
2583 */
2584 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002585 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002586 (r->pri_val << WM1_LP_SR_SHIFT) |
2587 r->cur_val;
2588
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002589 if (r->enable)
2590 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2591
Ville Syrjälä416f4722013-11-02 21:07:46 -07002592 if (INTEL_INFO(dev)->gen >= 8)
2593 results->wm_lp[wm_lp - 1] |=
2594 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2595 else
2596 results->wm_lp[wm_lp - 1] |=
2597 r->fbc_val << WM1_LP_FBC_SHIFT;
2598
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002599 /*
2600 * Always set WM1S_LP_EN when spr_val != 0, even if the
2601 * level is disabled. Doing otherwise could cause underruns.
2602 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002603 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2604 WARN_ON(wm_lp != 1);
2605 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2606 } else
2607 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002608 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002609
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002610 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002611 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002612 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002613 const struct intel_wm_level *r =
2614 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002615
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002616 if (WARN_ON(!r->enable))
2617 continue;
2618
Matt Ropered4a6a72016-02-23 17:20:13 -08002619 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002620
2621 results->wm_pipe[pipe] =
2622 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2623 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2624 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002625 }
2626}
2627
Paulo Zanoni861f3382013-05-31 10:19:21 -03002628/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2629 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002630static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002631 struct intel_pipe_wm *r1,
2632 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002633{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002634 int level, max_level = ilk_wm_max_level(dev);
2635 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002636
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002637 for (level = 1; level <= max_level; level++) {
2638 if (r1->wm[level].enable)
2639 level1 = level;
2640 if (r2->wm[level].enable)
2641 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002642 }
2643
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002644 if (level1 == level2) {
2645 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002646 return r2;
2647 else
2648 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002649 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002650 return r1;
2651 } else {
2652 return r2;
2653 }
2654}
2655
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002656/* dirty bits used to track which watermarks need changes */
2657#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2658#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2659#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2660#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2661#define WM_DIRTY_FBC (1 << 24)
2662#define WM_DIRTY_DDB (1 << 25)
2663
Damien Lespiau055e3932014-08-18 13:49:10 +01002664static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002665 const struct ilk_wm_values *old,
2666 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002667{
2668 unsigned int dirty = 0;
2669 enum pipe pipe;
2670 int wm_lp;
2671
Damien Lespiau055e3932014-08-18 13:49:10 +01002672 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002673 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2674 dirty |= WM_DIRTY_LINETIME(pipe);
2675 /* Must disable LP1+ watermarks too */
2676 dirty |= WM_DIRTY_LP_ALL;
2677 }
2678
2679 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2680 dirty |= WM_DIRTY_PIPE(pipe);
2681 /* Must disable LP1+ watermarks too */
2682 dirty |= WM_DIRTY_LP_ALL;
2683 }
2684 }
2685
2686 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2687 dirty |= WM_DIRTY_FBC;
2688 /* Must disable LP1+ watermarks too */
2689 dirty |= WM_DIRTY_LP_ALL;
2690 }
2691
2692 if (old->partitioning != new->partitioning) {
2693 dirty |= WM_DIRTY_DDB;
2694 /* Must disable LP1+ watermarks too */
2695 dirty |= WM_DIRTY_LP_ALL;
2696 }
2697
2698 /* LP1+ watermarks already deemed dirty, no need to continue */
2699 if (dirty & WM_DIRTY_LP_ALL)
2700 return dirty;
2701
2702 /* Find the lowest numbered LP1+ watermark in need of an update... */
2703 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2704 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2705 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2706 break;
2707 }
2708
2709 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2710 for (; wm_lp <= 3; wm_lp++)
2711 dirty |= WM_DIRTY_LP(wm_lp);
2712
2713 return dirty;
2714}
2715
Ville Syrjälä8553c182013-12-05 15:51:39 +02002716static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2717 unsigned int dirty)
2718{
Imre Deak820c1982013-12-17 14:46:36 +02002719 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002720 bool changed = false;
2721
2722 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2723 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2724 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2725 changed = true;
2726 }
2727 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2728 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2729 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2730 changed = true;
2731 }
2732 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2733 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2734 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2735 changed = true;
2736 }
2737
2738 /*
2739 * Don't touch WM1S_LP_EN here.
2740 * Doing so could cause underruns.
2741 */
2742
2743 return changed;
2744}
2745
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002746/*
2747 * The spec says we shouldn't write when we don't need, because every write
2748 * causes WMs to be re-evaluated, expending some power.
2749 */
Imre Deak820c1982013-12-17 14:46:36 +02002750static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2751 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002752{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002753 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002754 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002755 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002756 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002757
Damien Lespiau055e3932014-08-18 13:49:10 +01002758 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002759 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002760 return;
2761
Ville Syrjälä8553c182013-12-05 15:51:39 +02002762 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002763
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002764 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002765 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002766 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002767 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002768 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002769 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2770
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002771 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002772 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002773 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002774 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002775 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002776 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2777
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002778 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002779 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002780 val = I915_READ(WM_MISC);
2781 if (results->partitioning == INTEL_DDB_PART_1_2)
2782 val &= ~WM_MISC_DATA_PARTITION_5_6;
2783 else
2784 val |= WM_MISC_DATA_PARTITION_5_6;
2785 I915_WRITE(WM_MISC, val);
2786 } else {
2787 val = I915_READ(DISP_ARB_CTL2);
2788 if (results->partitioning == INTEL_DDB_PART_1_2)
2789 val &= ~DISP_DATA_PARTITION_5_6;
2790 else
2791 val |= DISP_DATA_PARTITION_5_6;
2792 I915_WRITE(DISP_ARB_CTL2, val);
2793 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002794 }
2795
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002796 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002797 val = I915_READ(DISP_ARB_CTL);
2798 if (results->enable_fbc_wm)
2799 val &= ~DISP_FBC_WM_DIS;
2800 else
2801 val |= DISP_FBC_WM_DIS;
2802 I915_WRITE(DISP_ARB_CTL, val);
2803 }
2804
Imre Deak954911e2013-12-17 14:46:34 +02002805 if (dirty & WM_DIRTY_LP(1) &&
2806 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2807 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2808
2809 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002810 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2811 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2812 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2813 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2814 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002815
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002816 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002817 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002818 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002819 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002820 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002821 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002822
2823 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002824}
2825
Matt Ropered4a6a72016-02-23 17:20:13 -08002826bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02002827{
2828 struct drm_i915_private *dev_priv = dev->dev_private;
2829
2830 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2831}
2832
Damien Lespiaub9cec072014-11-04 17:06:43 +00002833/*
2834 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2835 * different active planes.
2836 */
2837
2838#define SKL_DDB_SIZE 896 /* in blocks */
Damien Lespiau43d735a2015-03-17 11:39:34 +02002839#define BXT_DDB_SIZE 512
Damien Lespiaub9cec072014-11-04 17:06:43 +00002840
Matt Roper024c9042015-09-24 15:53:11 -07002841/*
2842 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2843 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2844 * other universal planes are in indices 1..n. Note that this may leave unused
2845 * indices between the top "sprite" plane and the cursor.
2846 */
2847static int
2848skl_wm_plane_id(const struct intel_plane *plane)
2849{
2850 switch (plane->base.type) {
2851 case DRM_PLANE_TYPE_PRIMARY:
2852 return 0;
2853 case DRM_PLANE_TYPE_CURSOR:
2854 return PLANE_CURSOR;
2855 case DRM_PLANE_TYPE_OVERLAY:
2856 return plane->plane + 1;
2857 default:
2858 MISSING_CASE(plane->base.type);
2859 return plane->plane;
2860 }
2861}
2862
Damien Lespiaub9cec072014-11-04 17:06:43 +00002863static void
2864skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07002865 const struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00002866 const struct intel_wm_config *config,
Damien Lespiaub9cec072014-11-04 17:06:43 +00002867 struct skl_ddb_entry *alloc /* out */)
2868{
Matt Roper024c9042015-09-24 15:53:11 -07002869 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002870 struct drm_crtc *crtc;
2871 unsigned int pipe_size, ddb_size;
2872 int nth_active_pipe;
2873
Matt Roper024c9042015-09-24 15:53:11 -07002874 if (!cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00002875 alloc->start = 0;
2876 alloc->end = 0;
2877 return;
2878 }
2879
Damien Lespiau43d735a2015-03-17 11:39:34 +02002880 if (IS_BROXTON(dev))
2881 ddb_size = BXT_DDB_SIZE;
2882 else
2883 ddb_size = SKL_DDB_SIZE;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002884
2885 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2886
2887 nth_active_pipe = 0;
2888 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002889 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002890 continue;
2891
2892 if (crtc == for_crtc)
2893 break;
2894
2895 nth_active_pipe++;
2896 }
2897
2898 pipe_size = ddb_size / config->num_pipes_active;
2899 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002900 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002901}
2902
2903static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2904{
2905 if (config->num_pipes_active == 1)
2906 return 32;
2907
2908 return 8;
2909}
2910
Damien Lespiaua269c582014-11-04 17:06:49 +00002911static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2912{
2913 entry->start = reg & 0x3ff;
2914 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002915 if (entry->end)
2916 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002917}
2918
Damien Lespiau08db6652014-11-04 17:06:52 +00002919void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2920 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002921{
Damien Lespiaua269c582014-11-04 17:06:49 +00002922 enum pipe pipe;
2923 int plane;
2924 u32 val;
2925
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02002926 memset(ddb, 0, sizeof(*ddb));
2927
Damien Lespiaua269c582014-11-04 17:06:49 +00002928 for_each_pipe(dev_priv, pipe) {
Imre Deak4d800032016-02-17 16:31:29 +02002929 enum intel_display_power_domain power_domain;
2930
2931 power_domain = POWER_DOMAIN_PIPE(pipe);
2932 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02002933 continue;
2934
Damien Lespiaudd740782015-02-28 14:54:08 +00002935 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002936 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2937 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2938 val);
2939 }
2940
2941 val = I915_READ(CUR_BUF_CFG(pipe));
Matt Roper4969d332015-09-24 15:53:10 -07002942 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2943 val);
Imre Deak4d800032016-02-17 16:31:29 +02002944
2945 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00002946 }
2947}
2948
Damien Lespiaub9cec072014-11-04 17:06:43 +00002949static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07002950skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
2951 const struct drm_plane_state *pstate,
2952 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002953{
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07002954 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Matt Roper024c9042015-09-24 15:53:11 -07002955 struct drm_framebuffer *fb = pstate->fb;
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07002956 uint32_t width = 0, height = 0;
2957
2958 width = drm_rect_width(&intel_pstate->src) >> 16;
2959 height = drm_rect_height(&intel_pstate->src) >> 16;
2960
2961 if (intel_rotation_90_or_270(pstate->rotation))
2962 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002963
2964 /* for planar format */
Matt Roper024c9042015-09-24 15:53:11 -07002965 if (fb->pixel_format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002966 if (y) /* y-plane data rate */
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07002967 return width * height *
Matt Roper024c9042015-09-24 15:53:11 -07002968 drm_format_plane_cpp(fb->pixel_format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002969 else /* uv-plane data rate */
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07002970 return (width / 2) * (height / 2) *
Matt Roper024c9042015-09-24 15:53:11 -07002971 drm_format_plane_cpp(fb->pixel_format, 1);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002972 }
2973
2974 /* for packed formats */
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07002975 return width * height * drm_format_plane_cpp(fb->pixel_format, 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002976}
2977
2978/*
2979 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2980 * a 8192x4096@32bpp framebuffer:
2981 * 3 * 4096 * 8192 * 4 < 2^32
2982 */
2983static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07002984skl_get_total_relative_data_rate(const struct intel_crtc_state *cstate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002985{
Matt Roper024c9042015-09-24 15:53:11 -07002986 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2987 struct drm_device *dev = intel_crtc->base.dev;
2988 const struct intel_plane *intel_plane;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002989 unsigned int total_data_rate = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002990
Matt Roper024c9042015-09-24 15:53:11 -07002991 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2992 const struct drm_plane_state *pstate = intel_plane->base.state;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002993
Matt Roper024c9042015-09-24 15:53:11 -07002994 if (pstate->fb == NULL)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002995 continue;
2996
Matt Roper024c9042015-09-24 15:53:11 -07002997 if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
2998 continue;
2999
3000 /* packed/uv */
3001 total_data_rate += skl_plane_relative_data_rate(cstate,
3002 pstate,
3003 0);
3004
3005 if (pstate->fb->pixel_format == DRM_FORMAT_NV12)
3006 /* y-plane */
3007 total_data_rate += skl_plane_relative_data_rate(cstate,
3008 pstate,
3009 1);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003010 }
3011
3012 return total_data_rate;
3013}
3014
3015static void
Matt Roper024c9042015-09-24 15:53:11 -07003016skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003017 struct skl_ddb_allocation *ddb /* out */)
3018{
Matt Roper024c9042015-09-24 15:53:11 -07003019 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003020 struct drm_device *dev = crtc->dev;
Matt Roperaa363132015-09-24 15:53:18 -07003021 struct drm_i915_private *dev_priv = to_i915(dev);
3022 struct intel_wm_config *config = &dev_priv->wm.config;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003024 struct intel_plane *intel_plane;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003025 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003026 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003027 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00003028 uint16_t minimum[I915_MAX_PLANES];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003029 uint16_t y_minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003030 unsigned int total_data_rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003031
Matt Roper024c9042015-09-24 15:53:11 -07003032 skl_ddb_get_pipe_allocation_limits(dev, cstate, config, alloc);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003033 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003034 if (alloc_size == 0) {
3035 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roper4969d332015-09-24 15:53:10 -07003036 memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
3037 sizeof(ddb->plane[pipe][PLANE_CURSOR]));
Damien Lespiaub9cec072014-11-04 17:06:43 +00003038 return;
3039 }
3040
3041 cursor_blocks = skl_cursor_allocation(config);
Matt Roper4969d332015-09-24 15:53:10 -07003042 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
3043 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003044
3045 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003046 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003047
Damien Lespiau80958152015-02-09 13:35:10 +00003048 /* 1. Allocate the mininum required blocks for each active plane */
Matt Roper024c9042015-09-24 15:53:11 -07003049 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3050 struct drm_plane *plane = &intel_plane->base;
3051 struct drm_framebuffer *fb = plane->state->fb;
3052 int id = skl_wm_plane_id(intel_plane);
Damien Lespiau80958152015-02-09 13:35:10 +00003053
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003054 if (!to_intel_plane_state(plane->state)->visible)
Matt Roper024c9042015-09-24 15:53:11 -07003055 continue;
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003056
Matt Roper024c9042015-09-24 15:53:11 -07003057 if (plane->type == DRM_PLANE_TYPE_CURSOR)
Damien Lespiau80958152015-02-09 13:35:10 +00003058 continue;
3059
Matt Roper024c9042015-09-24 15:53:11 -07003060 minimum[id] = 8;
3061 alloc_size -= minimum[id];
3062 y_minimum[id] = (fb->pixel_format == DRM_FORMAT_NV12) ? 8 : 0;
3063 alloc_size -= y_minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003064 }
3065
Damien Lespiaub9cec072014-11-04 17:06:43 +00003066 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003067 * 2. Distribute the remaining space in proportion to the amount of
3068 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003069 *
3070 * FIXME: we may not allocate every single block here.
3071 */
Matt Roper024c9042015-09-24 15:53:11 -07003072 total_data_rate = skl_get_total_relative_data_rate(cstate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003073
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003074 start = alloc->start;
Matt Roper024c9042015-09-24 15:53:11 -07003075 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3076 struct drm_plane *plane = &intel_plane->base;
3077 struct drm_plane_state *pstate = intel_plane->base.state;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003078 unsigned int data_rate, y_data_rate;
3079 uint16_t plane_blocks, y_plane_blocks = 0;
Matt Roper024c9042015-09-24 15:53:11 -07003080 int id = skl_wm_plane_id(intel_plane);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003081
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003082 if (!to_intel_plane_state(pstate)->visible)
Matt Roper024c9042015-09-24 15:53:11 -07003083 continue;
3084 if (plane->type == DRM_PLANE_TYPE_CURSOR)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003085 continue;
3086
Matt Roper024c9042015-09-24 15:53:11 -07003087 data_rate = skl_plane_relative_data_rate(cstate, pstate, 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003088
3089 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003090 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003091 * promote the expression to 64 bits to avoid overflowing, the
3092 * result is < available as data_rate / total_data_rate < 1
3093 */
Matt Roper024c9042015-09-24 15:53:11 -07003094 plane_blocks = minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003095 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3096 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003097
Matt Roper024c9042015-09-24 15:53:11 -07003098 ddb->plane[pipe][id].start = start;
3099 ddb->plane[pipe][id].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003100
3101 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003102
3103 /*
3104 * allocation for y_plane part of planar format:
3105 */
Matt Roper024c9042015-09-24 15:53:11 -07003106 if (pstate->fb->pixel_format == DRM_FORMAT_NV12) {
3107 y_data_rate = skl_plane_relative_data_rate(cstate,
3108 pstate,
3109 1);
3110 y_plane_blocks = y_minimum[id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003111 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3112 total_data_rate);
3113
Matt Roper024c9042015-09-24 15:53:11 -07003114 ddb->y_plane[pipe][id].start = start;
3115 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003116
3117 start += y_plane_blocks;
3118 }
3119
Damien Lespiaub9cec072014-11-04 17:06:43 +00003120 }
3121
3122}
3123
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02003124static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003125{
3126 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02003127 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003128}
3129
3130/*
3131 * The max latency should be 257 (max the punit can code is 255 and we add 2us
Ville Syrjäläac484962016-01-20 21:05:26 +02003132 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003133 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3134 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3135*/
Ville Syrjäläac484962016-01-20 21:05:26 +02003136static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003137{
3138 uint32_t wm_intermediate_val, ret;
3139
3140 if (latency == 0)
3141 return UINT_MAX;
3142
Ville Syrjäläac484962016-01-20 21:05:26 +02003143 wm_intermediate_val = latency * pixel_rate * cpp / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003144 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3145
3146 return ret;
3147}
3148
3149static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02003150 uint32_t horiz_pixels, uint8_t cpp,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003151 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003152{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003153 uint32_t ret;
3154 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3155 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003156
3157 if (latency == 0)
3158 return UINT_MAX;
3159
Ville Syrjäläac484962016-01-20 21:05:26 +02003160 plane_bytes_per_line = horiz_pixels * cpp;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003161
3162 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3163 tiling == I915_FORMAT_MOD_Yf_TILED) {
3164 plane_bytes_per_line *= 4;
3165 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3166 plane_blocks_per_line /= 4;
3167 } else {
3168 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3169 }
3170
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003171 wm_intermediate_val = latency * pixel_rate;
3172 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003173 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003174
3175 return ret;
3176}
3177
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003178static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3179 const struct intel_crtc *intel_crtc)
3180{
3181 struct drm_device *dev = intel_crtc->base.dev;
3182 struct drm_i915_private *dev_priv = dev->dev_private;
3183 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003184
Kumar, Maheshe6d90022015-10-23 09:41:34 -07003185 /*
3186 * If ddb allocation of pipes changed, it may require recalculation of
3187 * watermarks
3188 */
3189 if (memcmp(new_ddb->pipe, cur_ddb->pipe, sizeof(new_ddb->pipe)))
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003190 return true;
3191
3192 return false;
3193}
3194
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003195static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
Matt Roper024c9042015-09-24 15:53:11 -07003196 struct intel_crtc_state *cstate,
3197 struct intel_plane *intel_plane,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003198 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003199 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003200 uint16_t *out_blocks, /* out */
3201 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003202{
Matt Roper024c9042015-09-24 15:53:11 -07003203 struct drm_plane *plane = &intel_plane->base;
3204 struct drm_framebuffer *fb = plane->state->fb;
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003205 struct intel_plane_state *intel_pstate =
3206 to_intel_plane_state(plane->state);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003207 uint32_t latency = dev_priv->wm.skl_latency[level];
3208 uint32_t method1, method2;
3209 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3210 uint32_t res_blocks, res_lines;
3211 uint32_t selected_result;
Ville Syrjäläac484962016-01-20 21:05:26 +02003212 uint8_t cpp;
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003213 uint32_t width = 0, height = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003214
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003215 if (latency == 0 || !cstate->base.active || !intel_pstate->visible)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003216 return false;
3217
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003218 width = drm_rect_width(&intel_pstate->src) >> 16;
3219 height = drm_rect_height(&intel_pstate->src) >> 16;
3220
3221 if (intel_rotation_90_or_270(plane->state->rotation))
3222 swap(width, height);
3223
Ville Syrjäläac484962016-01-20 21:05:26 +02003224 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Matt Roper024c9042015-09-24 15:53:11 -07003225 method1 = skl_wm_method1(skl_pipe_pixel_rate(cstate),
Ville Syrjäläac484962016-01-20 21:05:26 +02003226 cpp, latency);
Matt Roper024c9042015-09-24 15:53:11 -07003227 method2 = skl_wm_method2(skl_pipe_pixel_rate(cstate),
3228 cstate->base.adjusted_mode.crtc_htotal,
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003229 width,
3230 cpp,
3231 fb->modifier[0],
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003232 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003233
Kumar, Mahesh9aec6a02016-04-06 08:26:39 -07003234 plane_bytes_per_line = width * cpp;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003235 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003236
Matt Roper024c9042015-09-24 15:53:11 -07003237 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3238 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003239 uint32_t min_scanlines = 4;
3240 uint32_t y_tile_minimum;
Matt Roper024c9042015-09-24 15:53:11 -07003241 if (intel_rotation_90_or_270(plane->state->rotation)) {
Ville Syrjäläac484962016-01-20 21:05:26 +02003242 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
Matt Roper024c9042015-09-24 15:53:11 -07003243 drm_format_plane_cpp(fb->pixel_format, 1) :
3244 drm_format_plane_cpp(fb->pixel_format, 0);
3245
Ville Syrjäläac484962016-01-20 21:05:26 +02003246 switch (cpp) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003247 case 1:
3248 min_scanlines = 16;
3249 break;
3250 case 2:
3251 min_scanlines = 8;
3252 break;
3253 case 8:
3254 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08003255 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003256 }
3257 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003258 selected_result = max(method2, y_tile_minimum);
3259 } else {
3260 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3261 selected_result = min(method1, method2);
3262 else
3263 selected_result = method1;
3264 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003265
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003266 res_blocks = selected_result + 1;
3267 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003268
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003269 if (level >= 1 && level <= 7) {
Matt Roper024c9042015-09-24 15:53:11 -07003270 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3271 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003272 res_lines += 4;
3273 else
3274 res_blocks++;
3275 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003276
3277 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00003278 return false;
3279
3280 *out_blocks = res_blocks;
3281 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003282
3283 return true;
3284}
3285
3286static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3287 struct skl_ddb_allocation *ddb,
Matt Roper024c9042015-09-24 15:53:11 -07003288 struct intel_crtc_state *cstate,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003289 int level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003290 struct skl_wm_level *result)
3291{
Matt Roper024c9042015-09-24 15:53:11 -07003292 struct drm_device *dev = dev_priv->dev;
3293 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
3294 struct intel_plane *intel_plane;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003295 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003296 enum pipe pipe = intel_crtc->pipe;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003297
Matt Roper024c9042015-09-24 15:53:11 -07003298 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3299 int i = skl_wm_plane_id(intel_plane);
3300
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003301 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3302
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003303 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
Matt Roper024c9042015-09-24 15:53:11 -07003304 cstate,
3305 intel_plane,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003306 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003307 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003308 &result->plane_res_b[i],
3309 &result->plane_res_l[i]);
3310 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003311}
3312
Damien Lespiau407b50f2014-11-04 17:06:57 +00003313static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003314skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003315{
Matt Roper024c9042015-09-24 15:53:11 -07003316 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003317 return 0;
3318
Matt Roper024c9042015-09-24 15:53:11 -07003319 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003320 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003321
Matt Roper024c9042015-09-24 15:53:11 -07003322 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3323 skl_pipe_pixel_rate(cstate));
Damien Lespiau407b50f2014-11-04 17:06:57 +00003324}
3325
Matt Roper024c9042015-09-24 15:53:11 -07003326static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003327 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003328{
Matt Roper024c9042015-09-24 15:53:11 -07003329 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiau9414f562014-11-04 17:06:58 +00003330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003331 struct intel_plane *intel_plane;
Damien Lespiau9414f562014-11-04 17:06:58 +00003332
Matt Roper024c9042015-09-24 15:53:11 -07003333 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003334 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003335
3336 /* Until we know more, just disable transition WMs */
Matt Roper024c9042015-09-24 15:53:11 -07003337 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3338 int i = skl_wm_plane_id(intel_plane);
3339
Damien Lespiau9414f562014-11-04 17:06:58 +00003340 trans_wm->plane_en[i] = false;
Matt Roper024c9042015-09-24 15:53:11 -07003341 }
Damien Lespiau407b50f2014-11-04 17:06:57 +00003342}
3343
Matt Roper024c9042015-09-24 15:53:11 -07003344static void skl_compute_pipe_wm(struct intel_crtc_state *cstate,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003345 struct skl_ddb_allocation *ddb,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003346 struct skl_pipe_wm *pipe_wm)
3347{
Matt Roper024c9042015-09-24 15:53:11 -07003348 struct drm_device *dev = cstate->base.crtc->dev;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003349 const struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003350 int level, max_level = ilk_wm_max_level(dev);
3351
3352 for (level = 0; level <= max_level; level++) {
Matt Roper024c9042015-09-24 15:53:11 -07003353 skl_compute_wm_level(dev_priv, ddb, cstate,
3354 level, &pipe_wm->wm[level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003355 }
Matt Roper024c9042015-09-24 15:53:11 -07003356 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003357
Matt Roper024c9042015-09-24 15:53:11 -07003358 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003359}
3360
3361static void skl_compute_wm_results(struct drm_device *dev,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003362 struct skl_pipe_wm *p_wm,
3363 struct skl_wm_values *r,
3364 struct intel_crtc *intel_crtc)
3365{
3366 int level, max_level = ilk_wm_max_level(dev);
3367 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003368 uint32_t temp;
3369 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003370
3371 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003372 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3373 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003374
3375 temp |= p_wm->wm[level].plane_res_l[i] <<
3376 PLANE_WM_LINES_SHIFT;
3377 temp |= p_wm->wm[level].plane_res_b[i];
3378 if (p_wm->wm[level].plane_en[i])
3379 temp |= PLANE_WM_EN;
3380
3381 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003382 }
3383
3384 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003385
Matt Roper4969d332015-09-24 15:53:10 -07003386 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3387 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003388
Matt Roper4969d332015-09-24 15:53:10 -07003389 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003390 temp |= PLANE_WM_EN;
3391
Matt Roper4969d332015-09-24 15:53:10 -07003392 r->plane[pipe][PLANE_CURSOR][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003393
3394 }
3395
Damien Lespiau9414f562014-11-04 17:06:58 +00003396 /* transition WMs */
3397 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3398 temp = 0;
3399 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3400 temp |= p_wm->trans_wm.plane_res_b[i];
3401 if (p_wm->trans_wm.plane_en[i])
3402 temp |= PLANE_WM_EN;
3403
3404 r->plane_trans[pipe][i] = temp;
3405 }
3406
3407 temp = 0;
Matt Roper4969d332015-09-24 15:53:10 -07003408 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3409 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3410 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
Damien Lespiau9414f562014-11-04 17:06:58 +00003411 temp |= PLANE_WM_EN;
3412
Matt Roper4969d332015-09-24 15:53:10 -07003413 r->plane_trans[pipe][PLANE_CURSOR] = temp;
Damien Lespiau9414f562014-11-04 17:06:58 +00003414
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003415 r->wm_linetime[pipe] = p_wm->linetime;
3416}
3417
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003418static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3419 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003420 const struct skl_ddb_entry *entry)
3421{
3422 if (entry->end)
3423 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3424 else
3425 I915_WRITE(reg, 0);
3426}
3427
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003428static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3429 const struct skl_wm_values *new)
3430{
3431 struct drm_device *dev = dev_priv->dev;
3432 struct intel_crtc *crtc;
3433
Jani Nikula19c80542015-12-16 12:48:16 +02003434 for_each_intel_crtc(dev, crtc) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003435 int i, level, max_level = ilk_wm_max_level(dev);
3436 enum pipe pipe = crtc->pipe;
3437
Damien Lespiau5d374d92014-11-04 17:07:00 +00003438 if (!new->dirty[pipe])
3439 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003440
Damien Lespiau5d374d92014-11-04 17:07:00 +00003441 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3442
3443 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003444 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003445 I915_WRITE(PLANE_WM(pipe, i, level),
3446 new->plane[pipe][i][level]);
3447 I915_WRITE(CUR_WM(pipe, level),
Matt Roper4969d332015-09-24 15:53:10 -07003448 new->plane[pipe][PLANE_CURSOR][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003449 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003450 for (i = 0; i < intel_num_planes(crtc); i++)
3451 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3452 new->plane_trans[pipe][i]);
Matt Roper4969d332015-09-24 15:53:10 -07003453 I915_WRITE(CUR_WM_TRANS(pipe),
3454 new->plane_trans[pipe][PLANE_CURSOR]);
Damien Lespiau5d374d92014-11-04 17:07:00 +00003455
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003456 for (i = 0; i < intel_num_planes(crtc); i++) {
Damien Lespiau5d374d92014-11-04 17:07:00 +00003457 skl_ddb_entry_write(dev_priv,
3458 PLANE_BUF_CFG(pipe, i),
3459 &new->ddb.plane[pipe][i]);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003460 skl_ddb_entry_write(dev_priv,
3461 PLANE_NV12_BUF_CFG(pipe, i),
3462 &new->ddb.y_plane[pipe][i]);
3463 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003464
3465 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
Matt Roper4969d332015-09-24 15:53:10 -07003466 &new->ddb.plane[pipe][PLANE_CURSOR]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003467 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003468}
3469
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003470/*
3471 * When setting up a new DDB allocation arrangement, we need to correctly
3472 * sequence the times at which the new allocations for the pipes are taken into
3473 * account or we'll have pipes fetching from space previously allocated to
3474 * another pipe.
3475 *
3476 * Roughly the sequence looks like:
3477 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3478 * overlapping with a previous light-up pipe (another way to put it is:
3479 * pipes with their new allocation strickly included into their old ones).
3480 * 2. re-allocate the other pipes that get their allocation reduced
3481 * 3. allocate the pipes having their allocation increased
3482 *
3483 * Steps 1. and 2. are here to take care of the following case:
3484 * - Initially DDB looks like this:
3485 * | B | C |
3486 * - enable pipe A.
3487 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3488 * allocation
3489 * | A | B | C |
3490 *
3491 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3492 */
3493
Damien Lespiaud21b7952014-11-04 17:07:03 +00003494static void
3495skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003496{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003497 int plane;
3498
Damien Lespiaud21b7952014-11-04 17:07:03 +00003499 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3500
Damien Lespiaudd740782015-02-28 14:54:08 +00003501 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003502 I915_WRITE(PLANE_SURF(pipe, plane),
3503 I915_READ(PLANE_SURF(pipe, plane)));
3504 }
3505 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3506}
3507
3508static bool
3509skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3510 const struct skl_ddb_allocation *new,
3511 enum pipe pipe)
3512{
3513 uint16_t old_size, new_size;
3514
3515 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3516 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3517
3518 return old_size != new_size &&
3519 new->pipe[pipe].start >= old->pipe[pipe].start &&
3520 new->pipe[pipe].end <= old->pipe[pipe].end;
3521}
3522
3523static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3524 struct skl_wm_values *new_values)
3525{
3526 struct drm_device *dev = dev_priv->dev;
3527 struct skl_ddb_allocation *cur_ddb, *new_ddb;
Ville Syrjäläc929cb42015-04-02 18:28:07 +03003528 bool reallocated[I915_MAX_PIPES] = {};
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003529 struct intel_crtc *crtc;
3530 enum pipe pipe;
3531
3532 new_ddb = &new_values->ddb;
3533 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3534
3535 /*
3536 * First pass: flush the pipes with the new allocation contained into
3537 * the old space.
3538 *
3539 * We'll wait for the vblank on those pipes to ensure we can safely
3540 * re-allocate the freed space without this pipe fetching from it.
3541 */
3542 for_each_intel_crtc(dev, crtc) {
3543 if (!crtc->active)
3544 continue;
3545
3546 pipe = crtc->pipe;
3547
3548 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3549 continue;
3550
Damien Lespiaud21b7952014-11-04 17:07:03 +00003551 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003552 intel_wait_for_vblank(dev, pipe);
3553
3554 reallocated[pipe] = true;
3555 }
3556
3557
3558 /*
3559 * Second pass: flush the pipes that are having their allocation
3560 * reduced, but overlapping with a previous allocation.
3561 *
3562 * Here as well we need to wait for the vblank to make sure the freed
3563 * space is not used anymore.
3564 */
3565 for_each_intel_crtc(dev, crtc) {
3566 if (!crtc->active)
3567 continue;
3568
3569 pipe = crtc->pipe;
3570
3571 if (reallocated[pipe])
3572 continue;
3573
3574 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3575 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003576 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003577 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303578 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003579 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003580 }
3581
3582 /*
3583 * Third pass: flush the pipes that got more space allocated.
3584 *
3585 * We don't need to actively wait for the update here, next vblank
3586 * will just get more DDB space with the correct WM values.
3587 */
3588 for_each_intel_crtc(dev, crtc) {
3589 if (!crtc->active)
3590 continue;
3591
3592 pipe = crtc->pipe;
3593
3594 /*
3595 * At this point, only the pipes more space than before are
3596 * left to re-allocate.
3597 */
3598 if (reallocated[pipe])
3599 continue;
3600
Damien Lespiaud21b7952014-11-04 17:07:03 +00003601 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003602 }
3603}
3604
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003605static bool skl_update_pipe_wm(struct drm_crtc *crtc,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003606 struct skl_ddb_allocation *ddb, /* out */
3607 struct skl_pipe_wm *pipe_wm /* out */)
3608{
3609 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003610 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003611
Matt Roperaa363132015-09-24 15:53:18 -07003612 skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper024c9042015-09-24 15:53:11 -07003613 skl_compute_pipe_wm(cstate, ddb, pipe_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003614
Matt Roper4e0963c2015-09-24 15:53:15 -07003615 if (!memcmp(&intel_crtc->wm.active.skl, pipe_wm, sizeof(*pipe_wm)))
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003616 return false;
3617
Matt Roper4e0963c2015-09-24 15:53:15 -07003618 intel_crtc->wm.active.skl = *pipe_wm;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003619
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003620 return true;
3621}
3622
3623static void skl_update_other_pipe_wm(struct drm_device *dev,
3624 struct drm_crtc *crtc,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003625 struct skl_wm_values *r)
3626{
3627 struct intel_crtc *intel_crtc;
3628 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3629
3630 /*
3631 * If the WM update hasn't changed the allocation for this_crtc (the
3632 * crtc we are currently computing the new WM values for), other
3633 * enabled crtcs will keep the same allocation and we don't need to
3634 * recompute anything for them.
3635 */
3636 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3637 return;
3638
3639 /*
3640 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3641 * other active pipes need new DDB allocation and WM values.
3642 */
Jani Nikula19c80542015-12-16 12:48:16 +02003643 for_each_intel_crtc(dev, intel_crtc) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003644 struct skl_pipe_wm pipe_wm = {};
3645 bool wm_changed;
3646
3647 if (this_crtc->pipe == intel_crtc->pipe)
3648 continue;
3649
3650 if (!intel_crtc->active)
3651 continue;
3652
Matt Roperaa363132015-09-24 15:53:18 -07003653 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003654 &r->ddb, &pipe_wm);
3655
3656 /*
3657 * If we end up re-computing the other pipe WM values, it's
3658 * because it was really needed, so we expect the WM values to
3659 * be different.
3660 */
3661 WARN_ON(!wm_changed);
3662
Matt Roper024c9042015-09-24 15:53:11 -07003663 skl_compute_wm_results(dev, &pipe_wm, r, intel_crtc);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003664 r->dirty[intel_crtc->pipe] = true;
3665 }
3666}
3667
Bob Paauweadda50b2015-07-21 10:42:53 -07003668static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
3669{
3670 watermarks->wm_linetime[pipe] = 0;
3671 memset(watermarks->plane[pipe], 0,
3672 sizeof(uint32_t) * 8 * I915_MAX_PLANES);
Bob Paauweadda50b2015-07-21 10:42:53 -07003673 memset(watermarks->plane_trans[pipe],
3674 0, sizeof(uint32_t) * I915_MAX_PLANES);
Matt Roper4969d332015-09-24 15:53:10 -07003675 watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
Bob Paauweadda50b2015-07-21 10:42:53 -07003676
3677 /* Clear ddb entries for pipe */
3678 memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
3679 memset(&watermarks->ddb.plane[pipe], 0,
3680 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3681 memset(&watermarks->ddb.y_plane[pipe], 0,
3682 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
Matt Roper4969d332015-09-24 15:53:10 -07003683 memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
3684 sizeof(struct skl_ddb_entry));
Bob Paauweadda50b2015-07-21 10:42:53 -07003685
3686}
3687
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003688static void skl_update_wm(struct drm_crtc *crtc)
3689{
3690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3691 struct drm_device *dev = crtc->dev;
3692 struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003693 struct skl_wm_values *results = &dev_priv->wm.skl_results;
Matt Roper4e0963c2015-09-24 15:53:15 -07003694 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3695 struct skl_pipe_wm *pipe_wm = &cstate->wm.optimal.skl;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003696
Bob Paauweadda50b2015-07-21 10:42:53 -07003697
3698 /* Clear all dirty flags */
3699 memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
3700
3701 skl_clear_wm(results, intel_crtc->pipe);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003702
Matt Roperaa363132015-09-24 15:53:18 -07003703 if (!skl_update_pipe_wm(crtc, &results->ddb, pipe_wm))
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003704 return;
3705
Matt Roper4e0963c2015-09-24 15:53:15 -07003706 skl_compute_wm_results(dev, pipe_wm, results, intel_crtc);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003707 results->dirty[intel_crtc->pipe] = true;
3708
Matt Roperaa363132015-09-24 15:53:18 -07003709 skl_update_other_pipe_wm(dev, crtc, results);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003710 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003711 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003712
3713 /* store the new configuration */
3714 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003715}
3716
Ville Syrjäläd8905652016-01-14 14:53:35 +02003717static void ilk_compute_wm_config(struct drm_device *dev,
3718 struct intel_wm_config *config)
3719{
3720 struct intel_crtc *crtc;
3721
3722 /* Compute the currently _active_ config */
3723 for_each_intel_crtc(dev, crtc) {
3724 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
3725
3726 if (!wm->pipe_enabled)
3727 continue;
3728
3729 config->sprites_enabled |= wm->sprites_enabled;
3730 config->sprites_scaled |= wm->sprites_scaled;
3731 config->num_pipes_active++;
3732 }
3733}
3734
Matt Ropered4a6a72016-02-23 17:20:13 -08003735static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003736{
Matt Ropered4a6a72016-02-23 17:20:13 -08003737 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003738 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02003739 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02003740 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02003741 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003742 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07003743
Ville Syrjäläd8905652016-01-14 14:53:35 +02003744 ilk_compute_wm_config(dev, &config);
3745
3746 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3747 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003748
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003749 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003750 if (INTEL_INFO(dev)->gen >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02003751 config.num_pipes_active == 1 && config.sprites_enabled) {
3752 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3753 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003754
Imre Deak820c1982013-12-17 14:46:36 +02003755 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003756 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003757 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003758 }
3759
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003760 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003761 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003762
Imre Deak820c1982013-12-17 14:46:36 +02003763 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003764
Imre Deak820c1982013-12-17 14:46:36 +02003765 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003766}
3767
Matt Ropered4a6a72016-02-23 17:20:13 -08003768static void ilk_initial_watermarks(struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003769{
Matt Ropered4a6a72016-02-23 17:20:13 -08003770 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
3771 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003772
Matt Ropered4a6a72016-02-23 17:20:13 -08003773 mutex_lock(&dev_priv->wm.wm_mutex);
3774 intel_crtc->wm.active.ilk = cstate->wm.intermediate;
3775 ilk_program_watermarks(dev_priv);
3776 mutex_unlock(&dev_priv->wm.wm_mutex);
3777}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003778
Matt Ropered4a6a72016-02-23 17:20:13 -08003779static void ilk_optimize_watermarks(struct intel_crtc_state *cstate)
3780{
3781 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
3782 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
3783
3784 mutex_lock(&dev_priv->wm.wm_mutex);
3785 if (cstate->wm.need_postvbl_update) {
3786 intel_crtc->wm.active.ilk = cstate->wm.optimal.ilk;
3787 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003788 }
Matt Ropered4a6a72016-02-23 17:20:13 -08003789 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003790}
3791
Pradeep Bhat30789992014-11-04 17:06:45 +00003792static void skl_pipe_wm_active_state(uint32_t val,
3793 struct skl_pipe_wm *active,
3794 bool is_transwm,
3795 bool is_cursor,
3796 int i,
3797 int level)
3798{
3799 bool is_enabled = (val & PLANE_WM_EN) != 0;
3800
3801 if (!is_transwm) {
3802 if (!is_cursor) {
3803 active->wm[level].plane_en[i] = is_enabled;
3804 active->wm[level].plane_res_b[i] =
3805 val & PLANE_WM_BLOCKS_MASK;
3806 active->wm[level].plane_res_l[i] =
3807 (val >> PLANE_WM_LINES_SHIFT) &
3808 PLANE_WM_LINES_MASK;
3809 } else {
Matt Roper4969d332015-09-24 15:53:10 -07003810 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3811 active->wm[level].plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003812 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07003813 active->wm[level].plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003814 (val >> PLANE_WM_LINES_SHIFT) &
3815 PLANE_WM_LINES_MASK;
3816 }
3817 } else {
3818 if (!is_cursor) {
3819 active->trans_wm.plane_en[i] = is_enabled;
3820 active->trans_wm.plane_res_b[i] =
3821 val & PLANE_WM_BLOCKS_MASK;
3822 active->trans_wm.plane_res_l[i] =
3823 (val >> PLANE_WM_LINES_SHIFT) &
3824 PLANE_WM_LINES_MASK;
3825 } else {
Matt Roper4969d332015-09-24 15:53:10 -07003826 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3827 active->trans_wm.plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003828 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07003829 active->trans_wm.plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003830 (val >> PLANE_WM_LINES_SHIFT) &
3831 PLANE_WM_LINES_MASK;
3832 }
3833 }
3834}
3835
3836static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3837{
3838 struct drm_device *dev = crtc->dev;
3839 struct drm_i915_private *dev_priv = dev->dev_private;
3840 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07003842 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3843 struct skl_pipe_wm *active = &cstate->wm.optimal.skl;
Pradeep Bhat30789992014-11-04 17:06:45 +00003844 enum pipe pipe = intel_crtc->pipe;
3845 int level, i, max_level;
3846 uint32_t temp;
3847
3848 max_level = ilk_wm_max_level(dev);
3849
3850 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3851
3852 for (level = 0; level <= max_level; level++) {
3853 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3854 hw->plane[pipe][i][level] =
3855 I915_READ(PLANE_WM(pipe, i, level));
Matt Roper4969d332015-09-24 15:53:10 -07003856 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
Pradeep Bhat30789992014-11-04 17:06:45 +00003857 }
3858
3859 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3860 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
Matt Roper4969d332015-09-24 15:53:10 -07003861 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00003862
Matt Roper3ef00282015-03-09 10:19:24 -07003863 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003864 return;
3865
3866 hw->dirty[pipe] = true;
3867
3868 active->linetime = hw->wm_linetime[pipe];
3869
3870 for (level = 0; level <= max_level; level++) {
3871 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3872 temp = hw->plane[pipe][i][level];
3873 skl_pipe_wm_active_state(temp, active, false,
3874 false, i, level);
3875 }
Matt Roper4969d332015-09-24 15:53:10 -07003876 temp = hw->plane[pipe][PLANE_CURSOR][level];
Pradeep Bhat30789992014-11-04 17:06:45 +00003877 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3878 }
3879
3880 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3881 temp = hw->plane_trans[pipe][i];
3882 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3883 }
3884
Matt Roper4969d332015-09-24 15:53:10 -07003885 temp = hw->plane_trans[pipe][PLANE_CURSOR];
Pradeep Bhat30789992014-11-04 17:06:45 +00003886 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
Matt Roper4e0963c2015-09-24 15:53:15 -07003887
3888 intel_crtc->wm.active.skl = *active;
Pradeep Bhat30789992014-11-04 17:06:45 +00003889}
3890
3891void skl_wm_get_hw_state(struct drm_device *dev)
3892{
Damien Lespiaua269c582014-11-04 17:06:49 +00003893 struct drm_i915_private *dev_priv = dev->dev_private;
3894 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003895 struct drm_crtc *crtc;
3896
Damien Lespiaua269c582014-11-04 17:06:49 +00003897 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003898 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3899 skl_pipe_wm_get_hw_state(crtc);
3900}
3901
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003902static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3903{
3904 struct drm_device *dev = crtc->dev;
3905 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003906 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003907 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07003908 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3909 struct intel_pipe_wm *active = &cstate->wm.optimal.ilk;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003910 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003911 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003912 [PIPE_A] = WM0_PIPEA_ILK,
3913 [PIPE_B] = WM0_PIPEB_ILK,
3914 [PIPE_C] = WM0_PIPEC_IVB,
3915 };
3916
3917 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003918 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003919 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003920
Ville Syrjälä7045c362016-05-13 17:55:17 +03003921 memset(active, 0, sizeof(*active));
3922
Matt Roper3ef00282015-03-09 10:19:24 -07003923 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003924
3925 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003926 u32 tmp = hw->wm_pipe[pipe];
3927
3928 /*
3929 * For active pipes LP0 watermark is marked as
3930 * enabled, and LP1+ watermaks as disabled since
3931 * we can't really reverse compute them in case
3932 * multiple pipes are active.
3933 */
3934 active->wm[0].enable = true;
3935 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3936 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3937 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3938 active->linetime = hw->wm_linetime[pipe];
3939 } else {
3940 int level, max_level = ilk_wm_max_level(dev);
3941
3942 /*
3943 * For inactive pipes, all watermark levels
3944 * should be marked as enabled but zeroed,
3945 * which is what we'd compute them to.
3946 */
3947 for (level = 0; level <= max_level; level++)
3948 active->wm[level].enable = true;
3949 }
Matt Roper4e0963c2015-09-24 15:53:15 -07003950
3951 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003952}
3953
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03003954#define _FW_WM(value, plane) \
3955 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3956#define _FW_WM_VLV(value, plane) \
3957 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3958
3959static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3960 struct vlv_wm_values *wm)
3961{
3962 enum pipe pipe;
3963 uint32_t tmp;
3964
3965 for_each_pipe(dev_priv, pipe) {
3966 tmp = I915_READ(VLV_DDL(pipe));
3967
3968 wm->ddl[pipe].primary =
3969 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3970 wm->ddl[pipe].cursor =
3971 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3972 wm->ddl[pipe].sprite[0] =
3973 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3974 wm->ddl[pipe].sprite[1] =
3975 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3976 }
3977
3978 tmp = I915_READ(DSPFW1);
3979 wm->sr.plane = _FW_WM(tmp, SR);
3980 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
3981 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
3982 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
3983
3984 tmp = I915_READ(DSPFW2);
3985 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
3986 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
3987 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
3988
3989 tmp = I915_READ(DSPFW3);
3990 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3991
3992 if (IS_CHERRYVIEW(dev_priv)) {
3993 tmp = I915_READ(DSPFW7_CHV);
3994 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3995 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3996
3997 tmp = I915_READ(DSPFW8_CHV);
3998 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
3999 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
4000
4001 tmp = I915_READ(DSPFW9_CHV);
4002 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
4003 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
4004
4005 tmp = I915_READ(DSPHOWM);
4006 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4007 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4008 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4009 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4010 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4011 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4012 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4013 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4014 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4015 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4016 } else {
4017 tmp = I915_READ(DSPFW7);
4018 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4019 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4020
4021 tmp = I915_READ(DSPHOWM);
4022 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4023 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4024 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4025 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4026 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4027 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4028 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4029 }
4030}
4031
4032#undef _FW_WM
4033#undef _FW_WM_VLV
4034
4035void vlv_wm_get_hw_state(struct drm_device *dev)
4036{
4037 struct drm_i915_private *dev_priv = to_i915(dev);
4038 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4039 struct intel_plane *plane;
4040 enum pipe pipe;
4041 u32 val;
4042
4043 vlv_read_wm_values(dev_priv, wm);
4044
4045 for_each_intel_plane(dev, plane) {
4046 switch (plane->base.type) {
4047 int sprite;
4048 case DRM_PLANE_TYPE_CURSOR:
4049 plane->wm.fifo_size = 63;
4050 break;
4051 case DRM_PLANE_TYPE_PRIMARY:
4052 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4053 break;
4054 case DRM_PLANE_TYPE_OVERLAY:
4055 sprite = plane->plane;
4056 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4057 break;
4058 }
4059 }
4060
4061 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4062 wm->level = VLV_WM_LEVEL_PM2;
4063
4064 if (IS_CHERRYVIEW(dev_priv)) {
4065 mutex_lock(&dev_priv->rps.hw_lock);
4066
4067 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4068 if (val & DSP_MAXFIFO_PM5_ENABLE)
4069 wm->level = VLV_WM_LEVEL_PM5;
4070
Ville Syrjälä58590c12015-09-08 21:05:12 +03004071 /*
4072 * If DDR DVFS is disabled in the BIOS, Punit
4073 * will never ack the request. So if that happens
4074 * assume we don't have to enable/disable DDR DVFS
4075 * dynamically. To test that just set the REQ_ACK
4076 * bit to poke the Punit, but don't change the
4077 * HIGH/LOW bits so that we don't actually change
4078 * the current state.
4079 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004080 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004081 val |= FORCE_DDR_FREQ_REQ_ACK;
4082 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4083
4084 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4085 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4086 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4087 "assuming DDR DVFS is disabled\n");
4088 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4089 } else {
4090 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4091 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4092 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4093 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004094
4095 mutex_unlock(&dev_priv->rps.hw_lock);
4096 }
4097
4098 for_each_pipe(dev_priv, pipe)
4099 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4100 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4101 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4102
4103 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4104 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4105}
4106
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004107void ilk_wm_get_hw_state(struct drm_device *dev)
4108{
4109 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004110 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004111 struct drm_crtc *crtc;
4112
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004113 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004114 ilk_pipe_wm_get_hw_state(crtc);
4115
4116 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4117 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4118 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4119
4120 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004121 if (INTEL_INFO(dev)->gen >= 7) {
4122 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4123 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4124 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004125
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004126 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004127 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4128 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4129 else if (IS_IVYBRIDGE(dev))
4130 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4131 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004132
4133 hw->enable_fbc_wm =
4134 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4135}
4136
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004137/**
4138 * intel_update_watermarks - update FIFO watermark values based on current modes
4139 *
4140 * Calculate watermark values for the various WM regs based on current mode
4141 * and plane configuration.
4142 *
4143 * There are several cases to deal with here:
4144 * - normal (i.e. non-self-refresh)
4145 * - self-refresh (SR) mode
4146 * - lines are large relative to FIFO size (buffer can hold up to 2)
4147 * - lines are small relative to FIFO size (buffer can hold more than 2
4148 * lines), so need to account for TLB latency
4149 *
4150 * The normal calculation is:
4151 * watermark = dotclock * bytes per pixel * latency
4152 * where latency is platform & configuration dependent (we assume pessimal
4153 * values here).
4154 *
4155 * The SR calculation is:
4156 * watermark = (trunc(latency/line time)+1) * surface width *
4157 * bytes per pixel
4158 * where
4159 * line time = htotal / dotclock
4160 * surface width = hdisplay for normal plane and 64 for cursor
4161 * and latency is assumed to be high, as above.
4162 *
4163 * The final value programmed to the register should always be rounded up,
4164 * and include an extra 2 entries to account for clock crossings.
4165 *
4166 * We don't use the sprite, so we can ignore that. And on Crestline we have
4167 * to set the non-SR watermarks to 8.
4168 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004169void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004170{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004171 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004172
4173 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004174 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004175}
4176
Jani Nikulae2828912016-01-18 09:19:47 +02004177/*
Daniel Vetter92703882012-08-09 16:46:01 +02004178 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004179 */
4180DEFINE_SPINLOCK(mchdev_lock);
4181
4182/* Global for IPS driver to get at the current i915 device. Protected by
4183 * mchdev_lock. */
4184static struct drm_i915_private *i915_mch_dev;
4185
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004186bool ironlake_set_drps(struct drm_device *dev, u8 val)
4187{
4188 struct drm_i915_private *dev_priv = dev->dev_private;
4189 u16 rgvswctl;
4190
Daniel Vetter92703882012-08-09 16:46:01 +02004191 assert_spin_locked(&mchdev_lock);
4192
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004193 rgvswctl = I915_READ16(MEMSWCTL);
4194 if (rgvswctl & MEMCTL_CMD_STS) {
4195 DRM_DEBUG("gpu busy, RCS change rejected\n");
4196 return false; /* still busy with another command */
4197 }
4198
4199 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4200 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4201 I915_WRITE16(MEMSWCTL, rgvswctl);
4202 POSTING_READ16(MEMSWCTL);
4203
4204 rgvswctl |= MEMCTL_CMD_STS;
4205 I915_WRITE16(MEMSWCTL, rgvswctl);
4206
4207 return true;
4208}
4209
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004210static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004211{
4212 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004213 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004214 u8 fmax, fmin, fstart, vstart;
4215
Daniel Vetter92703882012-08-09 16:46:01 +02004216 spin_lock_irq(&mchdev_lock);
4217
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004218 rgvmodectl = I915_READ(MEMMODECTL);
4219
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004220 /* Enable temp reporting */
4221 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4222 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4223
4224 /* 100ms RC evaluation intervals */
4225 I915_WRITE(RCUPEI, 100000);
4226 I915_WRITE(RCDNEI, 100000);
4227
4228 /* Set max/min thresholds to 90ms and 80ms respectively */
4229 I915_WRITE(RCBMAXAVG, 90000);
4230 I915_WRITE(RCBMINAVG, 80000);
4231
4232 I915_WRITE(MEMIHYST, 1);
4233
4234 /* Set up min, max, and cur for interrupt handling */
4235 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4236 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4237 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4238 MEMMODE_FSTART_SHIFT;
4239
Ville Syrjälä616847e2015-09-18 20:03:19 +03004240 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004241 PXVFREQ_PX_SHIFT;
4242
Daniel Vetter20e4d402012-08-08 23:35:39 +02004243 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4244 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004245
Daniel Vetter20e4d402012-08-08 23:35:39 +02004246 dev_priv->ips.max_delay = fstart;
4247 dev_priv->ips.min_delay = fmin;
4248 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004249
4250 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4251 fmax, fmin, fstart);
4252
4253 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4254
4255 /*
4256 * Interrupts will be enabled in ironlake_irq_postinstall
4257 */
4258
4259 I915_WRITE(VIDSTART, vstart);
4260 POSTING_READ(VIDSTART);
4261
4262 rgvmodectl |= MEMMODE_SWMODE_EN;
4263 I915_WRITE(MEMMODECTL, rgvmodectl);
4264
Daniel Vetter92703882012-08-09 16:46:01 +02004265 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004266 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004267 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004268
4269 ironlake_set_drps(dev, fstart);
4270
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004271 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4272 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004273 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004274 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004275 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004276
4277 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004278}
4279
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004280static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004281{
4282 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02004283 u16 rgvswctl;
4284
4285 spin_lock_irq(&mchdev_lock);
4286
4287 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004288
4289 /* Ack interrupts, disable EFC interrupt */
4290 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4291 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4292 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4293 I915_WRITE(DEIIR, DE_PCU_EVENT);
4294 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4295
4296 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004297 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004298 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004299 rgvswctl |= MEMCTL_CMD_STS;
4300 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004301 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004302
Daniel Vetter92703882012-08-09 16:46:01 +02004303 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004304}
4305
Daniel Vetteracbe9472012-07-26 11:50:05 +02004306/* There's a funny hw issue where the hw returns all 0 when reading from
4307 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4308 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4309 * all limits and the gpu stuck at whatever frequency it is at atm).
4310 */
Akash Goel74ef1172015-03-06 11:07:19 +05304311static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004312{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004313 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004314
Daniel Vetter20b46e52012-07-26 11:16:14 +02004315 /* Only set the down limit when we've reached the lowest level to avoid
4316 * getting more interrupts, otherwise leave this clear. This prevents a
4317 * race in the hw when coming out of rc6: There's a tiny window where
4318 * the hw runs at the minimal clock before selecting the desired
4319 * frequency, if the down threshold expires in that window we will not
4320 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03004321 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05304322 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4323 if (val <= dev_priv->rps.min_freq_softlimit)
4324 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4325 } else {
4326 limits = dev_priv->rps.max_freq_softlimit << 24;
4327 if (val <= dev_priv->rps.min_freq_softlimit)
4328 limits |= dev_priv->rps.min_freq_softlimit << 16;
4329 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004330
4331 return limits;
4332}
4333
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004334static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4335{
4336 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304337 u32 threshold_up = 0, threshold_down = 0; /* in % */
4338 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004339
4340 new_power = dev_priv->rps.power;
4341 switch (dev_priv->rps.power) {
4342 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004343 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004344 new_power = BETWEEN;
4345 break;
4346
4347 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004348 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004349 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004350 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004351 new_power = HIGH_POWER;
4352 break;
4353
4354 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004355 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 +01004356 new_power = BETWEEN;
4357 break;
4358 }
4359 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004360 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004361 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004362 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004363 new_power = HIGH_POWER;
4364 if (new_power == dev_priv->rps.power)
4365 return;
4366
4367 /* Note the units here are not exactly 1us, but 1280ns. */
4368 switch (new_power) {
4369 case LOW_POWER:
4370 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304371 ei_up = 16000;
4372 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004373
4374 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304375 ei_down = 32000;
4376 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004377 break;
4378
4379 case BETWEEN:
4380 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304381 ei_up = 13000;
4382 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004383
4384 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304385 ei_down = 32000;
4386 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004387 break;
4388
4389 case HIGH_POWER:
4390 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304391 ei_up = 10000;
4392 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004393
4394 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304395 ei_down = 32000;
4396 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004397 break;
4398 }
4399
Akash Goel8a586432015-03-06 11:07:18 +05304400 I915_WRITE(GEN6_RP_UP_EI,
4401 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4402 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4403 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4404
4405 I915_WRITE(GEN6_RP_DOWN_EI,
4406 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4407 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4408 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4409
4410 I915_WRITE(GEN6_RP_CONTROL,
4411 GEN6_RP_MEDIA_TURBO |
4412 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4413 GEN6_RP_MEDIA_IS_GFX |
4414 GEN6_RP_ENABLE |
4415 GEN6_RP_UP_BUSY_AVG |
4416 GEN6_RP_DOWN_IDLE_AVG);
4417
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004418 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004419 dev_priv->rps.up_threshold = threshold_up;
4420 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004421 dev_priv->rps.last_adj = 0;
4422}
4423
Chris Wilson2876ce72014-03-28 08:03:34 +00004424static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4425{
4426 u32 mask = 0;
4427
4428 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004429 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004430 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004431 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004432
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004433 mask &= dev_priv->pm_rps_events;
4434
Imre Deak59d02a12014-12-19 19:33:26 +02004435 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004436}
4437
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004438/* gen6_set_rps is called to update the frequency request, but should also be
4439 * called when the range (min_delay and max_delay) is modified so that we can
4440 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004441static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004442{
4443 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004444
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304445 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Jani Nikulae87a0052015-10-20 15:22:02 +03004446 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304447 return;
4448
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004449 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004450 WARN_ON(val > dev_priv->rps.max_freq);
4451 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004452
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004453 /* min/max delay may still have been modified so be sure to
4454 * write the limits value.
4455 */
4456 if (val != dev_priv->rps.cur_freq) {
4457 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004458
Akash Goel57041952015-03-06 11:07:17 +05304459 if (IS_GEN9(dev))
4460 I915_WRITE(GEN6_RPNSWREQ,
4461 GEN9_FREQUENCY(val));
4462 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004463 I915_WRITE(GEN6_RPNSWREQ,
4464 HSW_FREQUENCY(val));
4465 else
4466 I915_WRITE(GEN6_RPNSWREQ,
4467 GEN6_FREQUENCY(val) |
4468 GEN6_OFFSET(0) |
4469 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004470 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004471
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004472 /* Make sure we continue to get interrupts
4473 * until we hit the minimum or maximum frequencies.
4474 */
Akash Goel74ef1172015-03-06 11:07:19 +05304475 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004476 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004477
Ben Widawskyd5570a72012-09-07 19:43:41 -07004478 POSTING_READ(GEN6_RPNSWREQ);
4479
Ben Widawskyb39fb292014-03-19 18:31:11 -07004480 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004481 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004482}
4483
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004484static void valleyview_set_rps(struct drm_device *dev, u8 val)
4485{
4486 struct drm_i915_private *dev_priv = dev->dev_private;
4487
4488 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004489 WARN_ON(val > dev_priv->rps.max_freq);
4490 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004491
4492 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4493 "Odd GPU freq value\n"))
4494 val &= ~1;
4495
Deepak Scd25dd52015-07-10 18:31:40 +05304496 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4497
Chris Wilson8fb55192015-04-07 16:20:28 +01004498 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004499 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01004500 if (!IS_CHERRYVIEW(dev_priv))
4501 gen6_set_rps_thresholds(dev_priv, val);
4502 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004503
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004504 dev_priv->rps.cur_freq = val;
4505 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4506}
4507
Deepak Sa7f6e232015-05-09 18:04:44 +05304508/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05304509 *
4510 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05304511 * 1. Forcewake Media well.
4512 * 2. Request idle freq.
4513 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05304514*/
4515static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4516{
Chris Wilsonaed242f2015-03-18 09:48:21 +00004517 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304518
Chris Wilsonaed242f2015-03-18 09:48:21 +00004519 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304520 return;
4521
Deepak Sa7f6e232015-05-09 18:04:44 +05304522 /* Wake up the media well, as that takes a lot less
4523 * power than the Render well. */
4524 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4525 valleyview_set_rps(dev_priv->dev, val);
4526 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05304527}
4528
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004529void gen6_rps_busy(struct drm_i915_private *dev_priv)
4530{
4531 mutex_lock(&dev_priv->rps.hw_lock);
4532 if (dev_priv->rps.enabled) {
4533 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4534 gen6_rps_reset_ei(dev_priv);
4535 I915_WRITE(GEN6_PMINTRMSK,
4536 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4537 }
4538 mutex_unlock(&dev_priv->rps.hw_lock);
4539}
4540
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004541void gen6_rps_idle(struct drm_i915_private *dev_priv)
4542{
Damien Lespiau691bb712013-12-12 14:36:36 +00004543 struct drm_device *dev = dev_priv->dev;
4544
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004545 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004546 if (dev_priv->rps.enabled) {
Wayne Boyer666a4532015-12-09 12:29:35 -08004547 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304548 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004549 else
Chris Wilsonaed242f2015-03-18 09:48:21 +00004550 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004551 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004552 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004553 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004554 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004555
Chris Wilson8d3afd72015-05-21 21:01:47 +01004556 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004557 while (!list_empty(&dev_priv->rps.clients))
4558 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004559 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004560}
4561
Chris Wilson1854d5c2015-04-07 16:20:32 +01004562void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01004563 struct intel_rps_client *rps,
4564 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004565{
Chris Wilson8d3afd72015-05-21 21:01:47 +01004566 /* This is intentionally racy! We peek at the state here, then
4567 * validate inside the RPS worker.
4568 */
4569 if (!(dev_priv->mm.busy &&
4570 dev_priv->rps.enabled &&
4571 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4572 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004573
Chris Wilsone61b9952015-04-27 13:41:24 +01004574 /* Force a RPS boost (and don't count it against the client) if
4575 * the GPU is severely congested.
4576 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004577 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01004578 rps = NULL;
4579
Chris Wilson8d3afd72015-05-21 21:01:47 +01004580 spin_lock(&dev_priv->rps.client_lock);
4581 if (rps == NULL || list_empty(&rps->link)) {
4582 spin_lock_irq(&dev_priv->irq_lock);
4583 if (dev_priv->rps.interrupts_enabled) {
4584 dev_priv->rps.client_boost = true;
4585 queue_work(dev_priv->wq, &dev_priv->rps.work);
4586 }
4587 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004588
Chris Wilson2e1b8732015-04-27 13:41:22 +01004589 if (rps != NULL) {
4590 list_add(&rps->link, &dev_priv->rps.clients);
4591 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01004592 } else
4593 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01004594 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004595 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004596}
4597
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004598void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004599{
Wayne Boyer666a4532015-12-09 12:29:35 -08004600 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004601 valleyview_set_rps(dev, val);
4602 else
4603 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004604}
4605
Akash Goel2030d682016-04-23 00:05:45 +05304606static void gen9_disable_rc6(struct drm_device *dev)
Zhe Wang20e49362014-11-04 17:07:05 +00004607{
4608 struct drm_i915_private *dev_priv = dev->dev_private;
4609
4610 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004611 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004612}
4613
Akash Goel2030d682016-04-23 00:05:45 +05304614static void gen9_disable_rps(struct drm_device *dev)
4615{
4616 struct drm_i915_private *dev_priv = dev->dev_private;
4617
4618 I915_WRITE(GEN6_RP_CONTROL, 0);
4619}
4620
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004621static void gen6_disable_rps(struct drm_device *dev)
4622{
4623 struct drm_i915_private *dev_priv = dev->dev_private;
4624
4625 I915_WRITE(GEN6_RC_CONTROL, 0);
4626 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05304627 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004628}
4629
Deepak S38807742014-05-23 21:00:15 +05304630static void cherryview_disable_rps(struct drm_device *dev)
4631{
4632 struct drm_i915_private *dev_priv = dev->dev_private;
4633
4634 I915_WRITE(GEN6_RC_CONTROL, 0);
4635}
4636
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004637static void valleyview_disable_rps(struct drm_device *dev)
4638{
4639 struct drm_i915_private *dev_priv = dev->dev_private;
4640
Deepak S98a2e5f2014-08-18 10:35:27 -07004641 /* we're doing forcewake before Disabling RC6,
4642 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004643 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004644
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004645 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004646
Mika Kuoppala59bad942015-01-16 11:34:40 +02004647 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004648}
4649
Ben Widawskydc39fff2013-10-18 12:32:07 -07004650static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4651{
Wayne Boyer666a4532015-12-09 12:29:35 -08004652 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Imre Deak91ca6892014-04-14 20:24:25 +03004653 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4654 mode = GEN6_RC_CTL_RC6_ENABLE;
4655 else
4656 mode = 0;
4657 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004658 if (HAS_RC6p(dev))
4659 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004660 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
4661 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
4662 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004663
4664 else
4665 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004666 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07004667}
4668
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304669static bool bxt_check_bios_rc6_setup(const struct drm_device *dev)
4670{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004671 struct drm_i915_private *dev_priv = to_i915(dev);
4672 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304673 bool enable_rc6 = true;
4674 unsigned long rc6_ctx_base;
4675
4676 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
4677 DRM_DEBUG_KMS("RC6 Base location not set properly.\n");
4678 enable_rc6 = false;
4679 }
4680
4681 /*
4682 * The exact context size is not known for BXT, so assume a page size
4683 * for this check.
4684 */
4685 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004686 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
4687 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
4688 ggtt->stolen_reserved_size))) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304689 DRM_DEBUG_KMS("RC6 Base address not as expected.\n");
4690 enable_rc6 = false;
4691 }
4692
4693 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
4694 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
4695 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
4696 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
4697 DRM_DEBUG_KMS("Engine Idle wait time not set properly.\n");
4698 enable_rc6 = false;
4699 }
4700
4701 if (!(I915_READ(GEN6_RC_CONTROL) & (GEN6_RC_CTL_RC6_ENABLE |
4702 GEN6_RC_CTL_HW_ENABLE)) &&
4703 ((I915_READ(GEN6_RC_CONTROL) & GEN6_RC_CTL_HW_ENABLE) ||
4704 !(I915_READ(GEN6_RC_STATE) & RC6_STATE))) {
4705 DRM_DEBUG_KMS("HW/SW RC6 is not enabled by BIOS.\n");
4706 enable_rc6 = false;
4707 }
4708
4709 return enable_rc6;
4710}
4711
4712int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004713{
Daniel Vettere7d66d82015-06-15 23:23:54 +02004714 /* No RC6 before Ironlake and code is gone for ilk. */
4715 if (INTEL_INFO(dev)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03004716 return 0;
4717
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304718 if (!enable_rc6)
4719 return 0;
4720
4721 if (IS_BROXTON(dev) && !bxt_check_bios_rc6_setup(dev)) {
4722 DRM_INFO("RC6 disabled by BIOS\n");
4723 return 0;
4724 }
4725
Daniel Vetter456470e2012-08-08 23:35:40 +02004726 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004727 if (enable_rc6 >= 0) {
4728 int mask;
4729
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004730 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004731 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4732 INTEL_RC6pp_ENABLE;
4733 else
4734 mask = INTEL_RC6_ENABLE;
4735
4736 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004737 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4738 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004739
4740 return enable_rc6 & mask;
4741 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004742
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004743 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004744 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004745
4746 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004747}
4748
Imre Deake6069ca2014-04-18 16:01:02 +03004749int intel_enable_rc6(const struct drm_device *dev)
4750{
4751 return i915.enable_rc6;
4752}
4753
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004754static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004755{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004756 struct drm_i915_private *dev_priv = dev->dev_private;
4757 uint32_t rp_state_cap;
4758 u32 ddcc_status = 0;
4759 int ret;
4760
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004761 /* All of these values are in units of 50MHz */
4762 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004763 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Bob Paauwe35040562015-06-25 14:54:07 -07004764 if (IS_BROXTON(dev)) {
4765 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4766 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4767 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4768 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4769 } else {
4770 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4771 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4772 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4773 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4774 }
4775
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004776 /* hw_max = RP0 until we check for overclocking */
4777 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4778
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004779 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07004780 if (IS_HASWELL(dev) || IS_BROADWELL(dev) ||
4781 IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004782 ret = sandybridge_pcode_read(dev_priv,
4783 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4784 &ddcc_status);
4785 if (0 == ret)
4786 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004787 clamp_t(u8,
4788 ((ddcc_status >> 8) & 0xff),
4789 dev_priv->rps.min_freq,
4790 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004791 }
4792
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07004793 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Akash Goelc5e06882015-06-29 14:50:19 +05304794 /* Store the frequency values in 16.66 MHZ units, which is
4795 the natural hardware unit for SKL */
4796 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4797 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4798 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4799 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4800 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4801 }
4802
Chris Wilsonaed242f2015-03-18 09:48:21 +00004803 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4804
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004805 /* Preserve min/max settings in case of re-init */
4806 if (dev_priv->rps.max_freq_softlimit == 0)
4807 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4808
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004809 if (dev_priv->rps.min_freq_softlimit == 0) {
4810 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4811 dev_priv->rps.min_freq_softlimit =
Ville Syrjälä813b5e62015-03-25 19:27:16 +02004812 max_t(int, dev_priv->rps.efficient_freq,
4813 intel_freq_opcode(dev_priv, 450));
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004814 else
4815 dev_priv->rps.min_freq_softlimit =
4816 dev_priv->rps.min_freq;
4817 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004818}
4819
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004820/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004821static void gen9_enable_rps(struct drm_device *dev)
4822{
4823 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004824
4825 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4826
Damien Lespiauba1c5542015-01-16 18:07:26 +00004827 gen6_init_rps_frequencies(dev);
4828
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304829 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Jani Nikulae87a0052015-10-20 15:22:02 +03004830 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
Akash Goel2030d682016-04-23 00:05:45 +05304831 /*
4832 * BIOS could leave the Hw Turbo enabled, so need to explicitly
4833 * clear out the Control register just to avoid inconsitency
4834 * with debugfs interface, which will show Turbo as enabled
4835 * only and that is not expected by the User after adding the
4836 * WaGsvDisableTurbo. Apart from this there is no problem even
4837 * if the Turbo is left enabled in the Control register, as the
4838 * Up/Down interrupts would remain masked.
4839 */
4840 gen9_disable_rps(dev);
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304841 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4842 return;
4843 }
4844
Akash Goel0beb0592015-03-06 11:07:20 +05304845 /* Program defaults and thresholds for RPS*/
4846 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4847 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004848
Akash Goel0beb0592015-03-06 11:07:20 +05304849 /* 1 second timeout*/
4850 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4851 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4852
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004853 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004854
Akash Goel0beb0592015-03-06 11:07:20 +05304855 /* Leaning on the below call to gen6_set_rps to program/setup the
4856 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4857 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4858 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02004859 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004860
4861 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4862}
4863
4864static void gen9_enable_rc6(struct drm_device *dev)
4865{
4866 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004867 struct intel_engine_cs *engine;
Zhe Wang20e49362014-11-04 17:07:05 +00004868 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00004869
4870 /* 1a: Software RC state - RC0 */
4871 I915_WRITE(GEN6_RC_STATE, 0);
4872
4873 /* 1b: Get forcewake during program sequence. Although the driver
4874 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004875 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004876
4877 /* 2a: Disable RC states. */
4878 I915_WRITE(GEN6_RC_CONTROL, 0);
4879
4880 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05304881
4882 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Mika Kuoppalae7674b82015-12-07 18:29:45 +02004883 if (IS_SKYLAKE(dev))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05304884 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
4885 else
4886 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00004887 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4888 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004889 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004890 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05304891
4892 if (HAS_GUC_UCODE(dev))
4893 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
4894
Zhe Wang20e49362014-11-04 17:07:05 +00004895 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004896
Zhe Wang38c23522015-01-20 12:23:04 +00004897 /* 2c: Program Coarse Power Gating Policies. */
4898 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4899 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4900
Zhe Wang20e49362014-11-04 17:07:05 +00004901 /* 3a: Enable RC6 */
4902 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4903 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02004904 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304905 /* WaRsUseTimeoutMode */
Jani Nikulae87a0052015-10-20 15:22:02 +03004906 if (IS_SKL_REVID(dev, 0, SKL_REVID_D0) ||
Tim Gorecbdc12a2015-10-26 10:48:58 +00004907 IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304908 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05304909 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4910 GEN7_RC_CTL_TO_MODE |
4911 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304912 } else {
4913 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05304914 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4915 GEN6_RC_CTL_EI_MODE(1) |
4916 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304917 }
Zhe Wang20e49362014-11-04 17:07:05 +00004918
Sagar Kamblecb07bae2015-04-12 11:28:14 +05304919 /*
4920 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05304921 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05304922 */
Mika Kuoppala06e668a2015-12-16 19:18:37 +02004923 if (NEEDS_WaRsDisableCoarsePowerGating(dev))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05304924 I915_WRITE(GEN9_PG_ENABLE, 0);
4925 else
4926 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4927 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004928
Mika Kuoppala59bad942015-01-16 11:34:40 +02004929 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004930
4931}
4932
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004933static void gen8_enable_rps(struct drm_device *dev)
4934{
4935 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004936 struct intel_engine_cs *engine;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004937 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004938
4939 /* 1a: Software RC state - RC0 */
4940 I915_WRITE(GEN6_RC_STATE, 0);
4941
4942 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4943 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004944 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004945
4946 /* 2a: Disable RC states. */
4947 I915_WRITE(GEN6_RC_CONTROL, 0);
4948
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004949 /* Initialize rps frequencies */
4950 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004951
4952 /* 2b: Program RC6 thresholds.*/
4953 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4954 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4955 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00004956 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00004957 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004958 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004959 if (IS_BROADWELL(dev))
4960 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4961 else
4962 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004963
4964 /* 3: Enable RC6 */
4965 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4966 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004967 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004968 if (IS_BROADWELL(dev))
4969 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4970 GEN7_RC_CTL_TO_MODE |
4971 rc6_mask);
4972 else
4973 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4974 GEN6_RC_CTL_EI_MODE(1) |
4975 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004976
4977 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004978 I915_WRITE(GEN6_RPNSWREQ,
4979 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4980 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4981 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004982 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4983 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004984
Daniel Vetter7526ed72014-09-29 15:07:19 +02004985 /* Docs recommend 900MHz, and 300 MHz respectively */
4986 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4987 dev_priv->rps.max_freq_softlimit << 24 |
4988 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004989
Daniel Vetter7526ed72014-09-29 15:07:19 +02004990 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4991 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4992 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4993 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004994
Daniel Vetter7526ed72014-09-29 15:07:19 +02004995 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004996
4997 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004998 I915_WRITE(GEN6_RP_CONTROL,
4999 GEN6_RP_MEDIA_TURBO |
5000 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5001 GEN6_RP_MEDIA_IS_GFX |
5002 GEN6_RP_ENABLE |
5003 GEN6_RP_UP_BUSY_AVG |
5004 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005005
Daniel Vetter7526ed72014-09-29 15:07:19 +02005006 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005007
Tom O'Rourkec7f31532014-11-19 14:21:54 -08005008 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005009 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005010
Mika Kuoppala59bad942015-01-16 11:34:40 +02005011 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005012}
5013
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005014static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005015{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005016 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005017 struct intel_engine_cs *engine;
Ben Widawskyd060c162014-03-19 18:31:08 -07005018 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005019 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005020 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005021 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005022
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005023 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005024
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005025 /* Here begins a magic sequence of register writes to enable
5026 * auto-downclocking.
5027 *
5028 * Perhaps there might be some value in exposing these to
5029 * userspace...
5030 */
5031 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005032
5033 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005034 gtfifodbg = I915_READ(GTFIFODBG);
5035 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005036 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5037 I915_WRITE(GTFIFODBG, gtfifodbg);
5038 }
5039
Mika Kuoppala59bad942015-01-16 11:34:40 +02005040 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005041
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005042 /* Initialize rps frequencies */
5043 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06005044
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005045 /* disable the counters and set deterministic thresholds */
5046 I915_WRITE(GEN6_RC_CONTROL, 0);
5047
5048 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5049 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5050 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5051 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5052 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5053
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005054 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005055 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005056
5057 I915_WRITE(GEN6_RC_SLEEP, 0);
5058 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01005059 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005060 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5061 else
5062 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005063 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005064 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5065
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005066 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005067 rc6_mode = intel_enable_rc6(dev_priv->dev);
5068 if (rc6_mode & INTEL_RC6_ENABLE)
5069 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5070
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005071 /* We don't use those on Haswell */
5072 if (!IS_HASWELL(dev)) {
5073 if (rc6_mode & INTEL_RC6p_ENABLE)
5074 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005075
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005076 if (rc6_mode & INTEL_RC6pp_ENABLE)
5077 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5078 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005079
Ben Widawskydc39fff2013-10-18 12:32:07 -07005080 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005081
5082 I915_WRITE(GEN6_RC_CONTROL,
5083 rc6_mask |
5084 GEN6_RC_CTL_EI_MODE(1) |
5085 GEN6_RC_CTL_HW_ENABLE);
5086
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005087 /* Power down if completely idle for over 50ms */
5088 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005089 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005090
Ben Widawsky42c05262012-09-26 10:34:00 -07005091 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07005092 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07005093 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07005094
5095 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5096 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5097 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07005098 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07005099 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07005100 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005101 }
5102
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005103 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005104 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005105
Ben Widawsky31643d52012-09-26 10:34:01 -07005106 rc6vids = 0;
5107 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5108 if (IS_GEN6(dev) && ret) {
5109 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5110 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5111 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5112 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5113 rc6vids &= 0xffff00;
5114 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5115 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5116 if (ret)
5117 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5118 }
5119
Mika Kuoppala59bad942015-01-16 11:34:40 +02005120 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005121}
5122
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005123static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005124{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005125 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005126 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005127 unsigned int gpu_freq;
5128 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305129 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005130 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005131 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005132
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005133 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005134
Ben Widawskyeda79642013-10-07 17:15:48 -03005135 policy = cpufreq_cpu_get(0);
5136 if (policy) {
5137 max_ia_freq = policy->cpuinfo.max_freq;
5138 cpufreq_cpu_put(policy);
5139 } else {
5140 /*
5141 * Default to measured freq if none found, PCU will ensure we
5142 * don't go over
5143 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005144 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005145 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005146
5147 /* Convert from kHz to MHz */
5148 max_ia_freq /= 1000;
5149
Ben Widawsky153b4b952013-10-22 22:05:09 -07005150 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005151 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5152 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005153
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005154 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305155 /* Convert GT frequency to 50 HZ units */
5156 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5157 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5158 } else {
5159 min_gpu_freq = dev_priv->rps.min_freq;
5160 max_gpu_freq = dev_priv->rps.max_freq;
5161 }
5162
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005163 /*
5164 * For each potential GPU frequency, load a ring frequency we'd like
5165 * to use for memory access. We do this by specifying the IA frequency
5166 * the PCU should use as a reference to determine the ring frequency.
5167 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305168 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5169 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005170 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005171
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005172 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305173 /*
5174 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5175 * No floor required for ring frequency on SKL.
5176 */
5177 ring_freq = gpu_freq;
5178 } else if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005179 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5180 ring_freq = max(min_ring_freq, gpu_freq);
5181 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005182 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005183 ring_freq = max(min_ring_freq, ring_freq);
5184 /* leave ia_freq as the default, chosen by cpufreq */
5185 } else {
5186 /* On older processors, there is no separate ring
5187 * clock domain, so in order to boost the bandwidth
5188 * of the ring, we need to upclock the CPU (ia_freq).
5189 *
5190 * For GPU frequencies less than 750MHz,
5191 * just use the lowest ring freq.
5192 */
5193 if (gpu_freq < min_freq)
5194 ia_freq = 800;
5195 else
5196 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5197 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5198 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005199
Ben Widawsky42c05262012-09-26 10:34:00 -07005200 sandybridge_pcode_write(dev_priv,
5201 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005202 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5203 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5204 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005205 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005206}
5207
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005208void gen6_update_ring_freq(struct drm_device *dev)
5209{
5210 struct drm_i915_private *dev_priv = dev->dev_private;
5211
Akash Goel97d33082015-06-29 14:50:23 +05305212 if (!HAS_CORE_RING_FREQ(dev))
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005213 return;
5214
5215 mutex_lock(&dev_priv->rps.hw_lock);
5216 __gen6_update_ring_freq(dev);
5217 mutex_unlock(&dev_priv->rps.hw_lock);
5218}
5219
Ville Syrjälä03af2042014-06-28 02:03:53 +03005220static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305221{
Deepak S095acd52015-01-17 11:05:59 +05305222 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05305223 u32 val, rp0;
5224
Jani Nikula5b5929c2015-10-07 11:17:46 +03005225 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305226
Jani Nikula5b5929c2015-10-07 11:17:46 +03005227 switch (INTEL_INFO(dev)->eu_total) {
5228 case 8:
5229 /* (2 * 4) config */
5230 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5231 break;
5232 case 12:
5233 /* (2 * 6) config */
5234 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5235 break;
5236 case 16:
5237 /* (2 * 8) config */
5238 default:
5239 /* Setting (2 * 8) Min RP0 for any other combination */
5240 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5241 break;
Deepak S095acd52015-01-17 11:05:59 +05305242 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005243
5244 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5245
Deepak S2b6b3a02014-05-27 15:59:30 +05305246 return rp0;
5247}
5248
5249static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5250{
5251 u32 val, rpe;
5252
5253 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5254 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5255
5256 return rpe;
5257}
5258
Deepak S7707df42014-07-12 18:46:14 +05305259static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5260{
5261 u32 val, rp1;
5262
Jani Nikula5b5929c2015-10-07 11:17:46 +03005263 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5264 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5265
Deepak S7707df42014-07-12 18:46:14 +05305266 return rp1;
5267}
5268
Deepak Sf8f2b002014-07-10 13:16:21 +05305269static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5270{
5271 u32 val, rp1;
5272
5273 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5274
5275 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5276
5277 return rp1;
5278}
5279
Ville Syrjälä03af2042014-06-28 02:03:53 +03005280static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005281{
5282 u32 val, rp0;
5283
Jani Nikula64936252013-05-22 15:36:20 +03005284 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005285
5286 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5287 /* Clamp to max */
5288 rp0 = min_t(u32, rp0, 0xea);
5289
5290 return rp0;
5291}
5292
5293static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5294{
5295 u32 val, rpe;
5296
Jani Nikula64936252013-05-22 15:36:20 +03005297 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005298 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005299 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005300 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5301
5302 return rpe;
5303}
5304
Ville Syrjälä03af2042014-06-28 02:03:53 +03005305static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005306{
Imre Deak36146032014-12-04 18:39:35 +02005307 u32 val;
5308
5309 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5310 /*
5311 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5312 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5313 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5314 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5315 * to make sure it matches what Punit accepts.
5316 */
5317 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005318}
5319
Imre Deakae484342014-03-31 15:10:44 +03005320/* Check that the pctx buffer wasn't move under us. */
5321static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5322{
5323 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5324
5325 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5326 dev_priv->vlv_pctx->stolen->start);
5327}
5328
Deepak S38807742014-05-23 21:00:15 +05305329
5330/* Check that the pcbr address is not empty. */
5331static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5332{
5333 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5334
5335 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5336}
5337
5338static void cherryview_setup_pctx(struct drm_device *dev)
5339{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005340 struct drm_i915_private *dev_priv = to_i915(dev);
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005341 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Deepak S38807742014-05-23 21:00:15 +05305342 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05305343 u32 pcbr;
5344 int pctx_size = 32*1024;
5345
Deepak S38807742014-05-23 21:00:15 +05305346 pcbr = I915_READ(VLV_PCBR);
5347 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005348 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305349 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005350 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05305351
5352 pctx_paddr = (paddr & (~4095));
5353 I915_WRITE(VLV_PCBR, pctx_paddr);
5354 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005355
5356 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305357}
5358
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005359static void valleyview_setup_pctx(struct drm_device *dev)
5360{
5361 struct drm_i915_private *dev_priv = dev->dev_private;
5362 struct drm_i915_gem_object *pctx;
5363 unsigned long pctx_paddr;
5364 u32 pcbr;
5365 int pctx_size = 24*1024;
5366
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005367 mutex_lock(&dev->struct_mutex);
Imre Deak17b0c1f2014-02-11 21:39:06 +02005368
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005369 pcbr = I915_READ(VLV_PCBR);
5370 if (pcbr) {
5371 /* BIOS set it up already, grab the pre-alloc'd space */
5372 int pcbr_offset;
5373
5374 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5375 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5376 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005377 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005378 pctx_size);
5379 goto out;
5380 }
5381
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005382 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5383
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005384 /*
5385 * From the Gunit register HAS:
5386 * The Gfx driver is expected to program this register and ensure
5387 * proper allocation within Gfx stolen memory. For example, this
5388 * register should be programmed such than the PCBR range does not
5389 * overlap with other ranges, such as the frame buffer, protected
5390 * memory, or any other relevant ranges.
5391 */
5392 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5393 if (!pctx) {
5394 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005395 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005396 }
5397
5398 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5399 I915_WRITE(VLV_PCBR, pctx_paddr);
5400
5401out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005402 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005403 dev_priv->vlv_pctx = pctx;
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005404 mutex_unlock(&dev->struct_mutex);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005405}
5406
Imre Deakae484342014-03-31 15:10:44 +03005407static void valleyview_cleanup_pctx(struct drm_device *dev)
5408{
5409 struct drm_i915_private *dev_priv = dev->dev_private;
5410
5411 if (WARN_ON(!dev_priv->vlv_pctx))
5412 return;
5413
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005414 drm_gem_object_unreference_unlocked(&dev_priv->vlv_pctx->base);
Imre Deakae484342014-03-31 15:10:44 +03005415 dev_priv->vlv_pctx = NULL;
5416}
5417
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005418static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5419{
5420 dev_priv->rps.gpll_ref_freq =
5421 vlv_get_cck_clock(dev_priv, "GPLL ref",
5422 CCK_GPLL_CLOCK_CONTROL,
5423 dev_priv->czclk_freq);
5424
5425 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5426 dev_priv->rps.gpll_ref_freq);
5427}
5428
Imre Deak4e805192014-04-14 20:24:41 +03005429static void valleyview_init_gt_powersave(struct drm_device *dev)
5430{
5431 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005432 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005433
5434 valleyview_setup_pctx(dev);
5435
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005436 vlv_init_gpll_ref_freq(dev_priv);
5437
Imre Deak4e805192014-04-14 20:24:41 +03005438 mutex_lock(&dev_priv->rps.hw_lock);
5439
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005440 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5441 switch ((val >> 6) & 3) {
5442 case 0:
5443 case 1:
5444 dev_priv->mem_freq = 800;
5445 break;
5446 case 2:
5447 dev_priv->mem_freq = 1066;
5448 break;
5449 case 3:
5450 dev_priv->mem_freq = 1333;
5451 break;
5452 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005453 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005454
Imre Deak4e805192014-04-14 20:24:41 +03005455 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5456 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5457 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005458 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005459 dev_priv->rps.max_freq);
5460
5461 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5462 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005463 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005464 dev_priv->rps.efficient_freq);
5465
Deepak Sf8f2b002014-07-10 13:16:21 +05305466 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5467 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005468 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305469 dev_priv->rps.rp1_freq);
5470
Imre Deak4e805192014-04-14 20:24:41 +03005471 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5472 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005473 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005474 dev_priv->rps.min_freq);
5475
Chris Wilsonaed242f2015-03-18 09:48:21 +00005476 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5477
Imre Deak4e805192014-04-14 20:24:41 +03005478 /* Preserve min/max settings in case of re-init */
5479 if (dev_priv->rps.max_freq_softlimit == 0)
5480 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5481
5482 if (dev_priv->rps.min_freq_softlimit == 0)
5483 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5484
5485 mutex_unlock(&dev_priv->rps.hw_lock);
5486}
5487
Deepak S38807742014-05-23 21:00:15 +05305488static void cherryview_init_gt_powersave(struct drm_device *dev)
5489{
Deepak S2b6b3a02014-05-27 15:59:30 +05305490 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005491 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305492
Deepak S38807742014-05-23 21:00:15 +05305493 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05305494
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005495 vlv_init_gpll_ref_freq(dev_priv);
5496
Deepak S2b6b3a02014-05-27 15:59:30 +05305497 mutex_lock(&dev_priv->rps.hw_lock);
5498
Ville Syrjäläa5805162015-05-26 20:42:30 +03005499 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005500 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005501 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005502
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005503 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005504 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005505 dev_priv->mem_freq = 2000;
5506 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005507 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005508 dev_priv->mem_freq = 1600;
5509 break;
5510 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005511 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005512
Deepak S2b6b3a02014-05-27 15:59:30 +05305513 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5514 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5515 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005516 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305517 dev_priv->rps.max_freq);
5518
5519 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5520 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005521 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305522 dev_priv->rps.efficient_freq);
5523
Deepak S7707df42014-07-12 18:46:14 +05305524 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5525 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005526 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305527 dev_priv->rps.rp1_freq);
5528
Deepak S5b7c91b2015-05-09 18:15:46 +05305529 /* PUnit validated range is only [RPe, RP0] */
5530 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305531 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005532 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305533 dev_priv->rps.min_freq);
5534
Ville Syrjälä1c147622014-08-18 14:42:43 +03005535 WARN_ONCE((dev_priv->rps.max_freq |
5536 dev_priv->rps.efficient_freq |
5537 dev_priv->rps.rp1_freq |
5538 dev_priv->rps.min_freq) & 1,
5539 "Odd GPU freq values\n");
5540
Chris Wilsonaed242f2015-03-18 09:48:21 +00005541 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5542
Deepak S2b6b3a02014-05-27 15:59:30 +05305543 /* Preserve min/max settings in case of re-init */
5544 if (dev_priv->rps.max_freq_softlimit == 0)
5545 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5546
5547 if (dev_priv->rps.min_freq_softlimit == 0)
5548 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5549
5550 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305551}
5552
Imre Deak4e805192014-04-14 20:24:41 +03005553static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5554{
5555 valleyview_cleanup_pctx(dev);
5556}
5557
Deepak S38807742014-05-23 21:00:15 +05305558static void cherryview_enable_rps(struct drm_device *dev)
5559{
5560 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005561 struct intel_engine_cs *engine;
Deepak S2b6b3a02014-05-27 15:59:30 +05305562 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305563
5564 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5565
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005566 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
5567 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05305568 if (gtfifodbg) {
5569 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5570 gtfifodbg);
5571 I915_WRITE(GTFIFODBG, gtfifodbg);
5572 }
5573
5574 cherryview_check_pctx(dev_priv);
5575
5576 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5577 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005578 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305579
Ville Syrjälä160614a2015-01-19 13:50:47 +02005580 /* Disable RC states. */
5581 I915_WRITE(GEN6_RC_CONTROL, 0);
5582
Deepak S38807742014-05-23 21:00:15 +05305583 /* 2a: Program RC6 thresholds.*/
5584 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5585 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5586 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5587
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005588 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005589 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05305590 I915_WRITE(GEN6_RC_SLEEP, 0);
5591
Deepak Sf4f71c72015-03-28 15:23:35 +05305592 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5593 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05305594
5595 /* allows RC6 residency counter to work */
5596 I915_WRITE(VLV_COUNTER_CONTROL,
5597 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5598 VLV_MEDIA_RC6_COUNT_EN |
5599 VLV_RENDER_RC6_COUNT_EN));
5600
5601 /* For now we assume BIOS is allocating and populating the PCBR */
5602 pcbr = I915_READ(VLV_PCBR);
5603
Deepak S38807742014-05-23 21:00:15 +05305604 /* 3: Enable RC6 */
5605 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5606 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005607 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305608
5609 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5610
Deepak S2b6b3a02014-05-27 15:59:30 +05305611 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005612 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305613 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5614 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5615 I915_WRITE(GEN6_RP_UP_EI, 66000);
5616 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5617
5618 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5619
5620 /* 5: Enable RPS */
5621 I915_WRITE(GEN6_RP_CONTROL,
5622 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005623 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305624 GEN6_RP_ENABLE |
5625 GEN6_RP_UP_BUSY_AVG |
5626 GEN6_RP_DOWN_IDLE_AVG);
5627
Deepak S3ef62342015-04-29 08:36:24 +05305628 /* Setting Fixed Bias */
5629 val = VLV_OVERRIDE_EN |
5630 VLV_SOC_TDP_EN |
5631 CHV_BIAS_CPU_50_SOC_50;
5632 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5633
Deepak S2b6b3a02014-05-27 15:59:30 +05305634 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5635
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005636 /* RPS code assumes GPLL is used */
5637 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5638
Jani Nikula742f4912015-09-03 11:16:09 +03005639 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05305640 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5641
5642 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5643 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005644 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305645 dev_priv->rps.cur_freq);
5646
5647 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005648 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5649 dev_priv->rps.idle_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05305650
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005651 valleyview_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05305652
Mika Kuoppala59bad942015-01-16 11:34:40 +02005653 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305654}
5655
Jesse Barnes0a073b82013-04-17 15:54:58 -07005656static void valleyview_enable_rps(struct drm_device *dev)
5657{
5658 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005659 struct intel_engine_cs *engine;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005660 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005661
5662 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5663
Imre Deakae484342014-03-31 15:10:44 +03005664 valleyview_check_pctx(dev_priv);
5665
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005666 gtfifodbg = I915_READ(GTFIFODBG);
5667 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005668 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5669 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005670 I915_WRITE(GTFIFODBG, gtfifodbg);
5671 }
5672
Deepak Sc8d9a592013-11-23 14:55:42 +05305673 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005674 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005675
Ville Syrjälä160614a2015-01-19 13:50:47 +02005676 /* Disable RC states. */
5677 I915_WRITE(GEN6_RC_CONTROL, 0);
5678
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005679 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005680 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5681 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5682 I915_WRITE(GEN6_RP_UP_EI, 66000);
5683 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5684
5685 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5686
5687 I915_WRITE(GEN6_RP_CONTROL,
5688 GEN6_RP_MEDIA_TURBO |
5689 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5690 GEN6_RP_MEDIA_IS_GFX |
5691 GEN6_RP_ENABLE |
5692 GEN6_RP_UP_BUSY_AVG |
5693 GEN6_RP_DOWN_IDLE_CONT);
5694
5695 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5696 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5697 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5698
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005699 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005700 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005701
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08005702 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005703
5704 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005705 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005706 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5707 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005708 VLV_MEDIA_RC6_COUNT_EN |
5709 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005710
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005711 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005712 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005713
5714 intel_print_rc6_info(dev, rc6_mode);
5715
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005716 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005717
Deepak S3ef62342015-04-29 08:36:24 +05305718 /* Setting Fixed Bias */
5719 val = VLV_OVERRIDE_EN |
5720 VLV_SOC_TDP_EN |
5721 VLV_BIAS_CPU_125_SOC_875;
5722 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5723
Jani Nikula64936252013-05-22 15:36:20 +03005724 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005725
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005726 /* RPS code assumes GPLL is used */
5727 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5728
Jani Nikula742f4912015-09-03 11:16:09 +03005729 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07005730 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5731
Ben Widawskyb39fb292014-03-19 18:31:11 -07005732 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005733 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005734 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005735 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005736
Ville Syrjälä73008b92013-06-25 19:21:01 +03005737 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005738 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5739 dev_priv->rps.idle_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005740
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005741 valleyview_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005742
Mika Kuoppala59bad942015-01-16 11:34:40 +02005743 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005744}
5745
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005746static unsigned long intel_pxfreq(u32 vidfreq)
5747{
5748 unsigned long freq;
5749 int div = (vidfreq & 0x3f0000) >> 16;
5750 int post = (vidfreq & 0x3000) >> 12;
5751 int pre = (vidfreq & 0x7);
5752
5753 if (!pre)
5754 return 0;
5755
5756 freq = ((div * 133333) / ((1<<post) * pre));
5757
5758 return freq;
5759}
5760
Daniel Vettereb48eb02012-04-26 23:28:12 +02005761static const struct cparams {
5762 u16 i;
5763 u16 t;
5764 u16 m;
5765 u16 c;
5766} cparams[] = {
5767 { 1, 1333, 301, 28664 },
5768 { 1, 1066, 294, 24460 },
5769 { 1, 800, 294, 25192 },
5770 { 0, 1333, 276, 27605 },
5771 { 0, 1066, 276, 27605 },
5772 { 0, 800, 231, 23784 },
5773};
5774
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005775static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005776{
5777 u64 total_count, diff, ret;
5778 u32 count1, count2, count3, m = 0, c = 0;
5779 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5780 int i;
5781
Daniel Vetter02d71952012-08-09 16:44:54 +02005782 assert_spin_locked(&mchdev_lock);
5783
Daniel Vetter20e4d402012-08-08 23:35:39 +02005784 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005785
5786 /* Prevent division-by-zero if we are asking too fast.
5787 * Also, we don't get interesting results if we are polling
5788 * faster than once in 10ms, so just return the saved value
5789 * in such cases.
5790 */
5791 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005792 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005793
5794 count1 = I915_READ(DMIEC);
5795 count2 = I915_READ(DDREC);
5796 count3 = I915_READ(CSIEC);
5797
5798 total_count = count1 + count2 + count3;
5799
5800 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005801 if (total_count < dev_priv->ips.last_count1) {
5802 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005803 diff += total_count;
5804 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005805 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005806 }
5807
5808 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005809 if (cparams[i].i == dev_priv->ips.c_m &&
5810 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005811 m = cparams[i].m;
5812 c = cparams[i].c;
5813 break;
5814 }
5815 }
5816
5817 diff = div_u64(diff, diff1);
5818 ret = ((m * diff) + c);
5819 ret = div_u64(ret, 10);
5820
Daniel Vetter20e4d402012-08-08 23:35:39 +02005821 dev_priv->ips.last_count1 = total_count;
5822 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005823
Daniel Vetter20e4d402012-08-08 23:35:39 +02005824 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005825
5826 return ret;
5827}
5828
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005829unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5830{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005831 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005832 unsigned long val;
5833
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005834 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005835 return 0;
5836
5837 spin_lock_irq(&mchdev_lock);
5838
5839 val = __i915_chipset_val(dev_priv);
5840
5841 spin_unlock_irq(&mchdev_lock);
5842
5843 return val;
5844}
5845
Daniel Vettereb48eb02012-04-26 23:28:12 +02005846unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5847{
5848 unsigned long m, x, b;
5849 u32 tsfs;
5850
5851 tsfs = I915_READ(TSFS);
5852
5853 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5854 x = I915_READ8(TR1);
5855
5856 b = tsfs & TSFS_INTR_MASK;
5857
5858 return ((m * x) / 127) - b;
5859}
5860
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005861static int _pxvid_to_vd(u8 pxvid)
5862{
5863 if (pxvid == 0)
5864 return 0;
5865
5866 if (pxvid >= 8 && pxvid < 31)
5867 pxvid = 31;
5868
5869 return (pxvid + 2) * 125;
5870}
5871
5872static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005873{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005874 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005875 const int vd = _pxvid_to_vd(pxvid);
5876 const int vm = vd - 1125;
5877
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005878 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005879 return vm > 0 ? vm : 0;
5880
5881 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005882}
5883
Daniel Vetter02d71952012-08-09 16:44:54 +02005884static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005885{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005886 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005887 u32 count;
5888
Daniel Vetter02d71952012-08-09 16:44:54 +02005889 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005890
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005891 now = ktime_get_raw_ns();
5892 diffms = now - dev_priv->ips.last_time2;
5893 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005894
5895 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005896 if (!diffms)
5897 return;
5898
5899 count = I915_READ(GFXEC);
5900
Daniel Vetter20e4d402012-08-08 23:35:39 +02005901 if (count < dev_priv->ips.last_count2) {
5902 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005903 diff += count;
5904 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005905 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005906 }
5907
Daniel Vetter20e4d402012-08-08 23:35:39 +02005908 dev_priv->ips.last_count2 = count;
5909 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005910
5911 /* More magic constants... */
5912 diff = diff * 1181;
5913 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005914 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005915}
5916
Daniel Vetter02d71952012-08-09 16:44:54 +02005917void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5918{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005919 struct drm_device *dev = dev_priv->dev;
5920
5921 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005922 return;
5923
Daniel Vetter92703882012-08-09 16:46:01 +02005924 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005925
5926 __i915_update_gfx_val(dev_priv);
5927
Daniel Vetter92703882012-08-09 16:46:01 +02005928 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005929}
5930
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005931static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005932{
5933 unsigned long t, corr, state1, corr2, state2;
5934 u32 pxvid, ext_v;
5935
Daniel Vetter02d71952012-08-09 16:44:54 +02005936 assert_spin_locked(&mchdev_lock);
5937
Ville Syrjälä616847e2015-09-18 20:03:19 +03005938 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005939 pxvid = (pxvid >> 24) & 0x7f;
5940 ext_v = pvid_to_extvid(dev_priv, pxvid);
5941
5942 state1 = ext_v;
5943
5944 t = i915_mch_val(dev_priv);
5945
5946 /* Revel in the empirically derived constants */
5947
5948 /* Correction factor in 1/100000 units */
5949 if (t > 80)
5950 corr = ((t * 2349) + 135940);
5951 else if (t >= 50)
5952 corr = ((t * 964) + 29317);
5953 else /* < 50 */
5954 corr = ((t * 301) + 1004);
5955
5956 corr = corr * ((150142 * state1) / 10000 - 78642);
5957 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005958 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005959
5960 state2 = (corr2 * state1) / 10000;
5961 state2 /= 100; /* convert to mW */
5962
Daniel Vetter02d71952012-08-09 16:44:54 +02005963 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005964
Daniel Vetter20e4d402012-08-08 23:35:39 +02005965 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005966}
5967
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005968unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5969{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005970 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005971 unsigned long val;
5972
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005973 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005974 return 0;
5975
5976 spin_lock_irq(&mchdev_lock);
5977
5978 val = __i915_gfx_val(dev_priv);
5979
5980 spin_unlock_irq(&mchdev_lock);
5981
5982 return val;
5983}
5984
Daniel Vettereb48eb02012-04-26 23:28:12 +02005985/**
5986 * i915_read_mch_val - return value for IPS use
5987 *
5988 * Calculate and return a value for the IPS driver to use when deciding whether
5989 * we have thermal and power headroom to increase CPU or GPU power budget.
5990 */
5991unsigned long i915_read_mch_val(void)
5992{
5993 struct drm_i915_private *dev_priv;
5994 unsigned long chipset_val, graphics_val, ret = 0;
5995
Daniel Vetter92703882012-08-09 16:46:01 +02005996 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005997 if (!i915_mch_dev)
5998 goto out_unlock;
5999 dev_priv = i915_mch_dev;
6000
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006001 chipset_val = __i915_chipset_val(dev_priv);
6002 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006003
6004 ret = chipset_val + graphics_val;
6005
6006out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006007 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006008
6009 return ret;
6010}
6011EXPORT_SYMBOL_GPL(i915_read_mch_val);
6012
6013/**
6014 * i915_gpu_raise - raise GPU frequency limit
6015 *
6016 * Raise the limit; IPS indicates we have thermal headroom.
6017 */
6018bool i915_gpu_raise(void)
6019{
6020 struct drm_i915_private *dev_priv;
6021 bool ret = true;
6022
Daniel Vetter92703882012-08-09 16:46:01 +02006023 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006024 if (!i915_mch_dev) {
6025 ret = false;
6026 goto out_unlock;
6027 }
6028 dev_priv = i915_mch_dev;
6029
Daniel Vetter20e4d402012-08-08 23:35:39 +02006030 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6031 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006032
6033out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006034 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006035
6036 return ret;
6037}
6038EXPORT_SYMBOL_GPL(i915_gpu_raise);
6039
6040/**
6041 * i915_gpu_lower - lower GPU frequency limit
6042 *
6043 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6044 * frequency maximum.
6045 */
6046bool i915_gpu_lower(void)
6047{
6048 struct drm_i915_private *dev_priv;
6049 bool ret = true;
6050
Daniel Vetter92703882012-08-09 16:46:01 +02006051 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006052 if (!i915_mch_dev) {
6053 ret = false;
6054 goto out_unlock;
6055 }
6056 dev_priv = i915_mch_dev;
6057
Daniel Vetter20e4d402012-08-08 23:35:39 +02006058 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6059 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006060
6061out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006062 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006063
6064 return ret;
6065}
6066EXPORT_SYMBOL_GPL(i915_gpu_lower);
6067
6068/**
6069 * i915_gpu_busy - indicate GPU business to IPS
6070 *
6071 * Tell the IPS driver whether or not the GPU is busy.
6072 */
6073bool i915_gpu_busy(void)
6074{
6075 struct drm_i915_private *dev_priv;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006076 struct intel_engine_cs *engine;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006077 bool ret = false;
6078
Daniel Vetter92703882012-08-09 16:46:01 +02006079 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006080 if (!i915_mch_dev)
6081 goto out_unlock;
6082 dev_priv = i915_mch_dev;
6083
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006084 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006085 ret |= !list_empty(&engine->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006086
6087out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006088 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006089
6090 return ret;
6091}
6092EXPORT_SYMBOL_GPL(i915_gpu_busy);
6093
6094/**
6095 * i915_gpu_turbo_disable - disable graphics turbo
6096 *
6097 * Disable graphics turbo by resetting the max frequency and setting the
6098 * current frequency to the default.
6099 */
6100bool i915_gpu_turbo_disable(void)
6101{
6102 struct drm_i915_private *dev_priv;
6103 bool ret = true;
6104
Daniel Vetter92703882012-08-09 16:46:01 +02006105 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006106 if (!i915_mch_dev) {
6107 ret = false;
6108 goto out_unlock;
6109 }
6110 dev_priv = i915_mch_dev;
6111
Daniel Vetter20e4d402012-08-08 23:35:39 +02006112 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006113
Daniel Vetter20e4d402012-08-08 23:35:39 +02006114 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006115 ret = false;
6116
6117out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006118 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006119
6120 return ret;
6121}
6122EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6123
6124/**
6125 * Tells the intel_ips driver that the i915 driver is now loaded, if
6126 * IPS got loaded first.
6127 *
6128 * This awkward dance is so that neither module has to depend on the
6129 * other in order for IPS to do the appropriate communication of
6130 * GPU turbo limits to i915.
6131 */
6132static void
6133ips_ping_for_i915_load(void)
6134{
6135 void (*link)(void);
6136
6137 link = symbol_get(ips_link_to_i915_driver);
6138 if (link) {
6139 link();
6140 symbol_put(ips_link_to_i915_driver);
6141 }
6142}
6143
6144void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6145{
Daniel Vetter02d71952012-08-09 16:44:54 +02006146 /* We only register the i915 ips part with intel-ips once everything is
6147 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006148 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006149 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006150 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006151
6152 ips_ping_for_i915_load();
6153}
6154
6155void intel_gpu_ips_teardown(void)
6156{
Daniel Vetter92703882012-08-09 16:46:01 +02006157 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006158 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006159 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006160}
Deepak S76c3552f2014-01-30 23:08:16 +05306161
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006162static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006163{
6164 struct drm_i915_private *dev_priv = dev->dev_private;
6165 u32 lcfuse;
6166 u8 pxw[16];
6167 int i;
6168
6169 /* Disable to program */
6170 I915_WRITE(ECR, 0);
6171 POSTING_READ(ECR);
6172
6173 /* Program energy weights for various events */
6174 I915_WRITE(SDEW, 0x15040d00);
6175 I915_WRITE(CSIEW0, 0x007f0000);
6176 I915_WRITE(CSIEW1, 0x1e220004);
6177 I915_WRITE(CSIEW2, 0x04000004);
6178
6179 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006180 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006181 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006182 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006183
6184 /* Program P-state weights to account for frequency power adjustment */
6185 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006186 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006187 unsigned long freq = intel_pxfreq(pxvidfreq);
6188 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6189 PXVFREQ_PX_SHIFT;
6190 unsigned long val;
6191
6192 val = vid * vid;
6193 val *= (freq / 1000);
6194 val *= 255;
6195 val /= (127*127*900);
6196 if (val > 0xff)
6197 DRM_ERROR("bad pxval: %ld\n", val);
6198 pxw[i] = val;
6199 }
6200 /* Render standby states get 0 weight */
6201 pxw[14] = 0;
6202 pxw[15] = 0;
6203
6204 for (i = 0; i < 4; i++) {
6205 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6206 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006207 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006208 }
6209
6210 /* Adjust magic regs to magic values (more experimental results) */
6211 I915_WRITE(OGW0, 0);
6212 I915_WRITE(OGW1, 0);
6213 I915_WRITE(EG0, 0x00007f00);
6214 I915_WRITE(EG1, 0x0000000e);
6215 I915_WRITE(EG2, 0x000e0000);
6216 I915_WRITE(EG3, 0x68000300);
6217 I915_WRITE(EG4, 0x42000000);
6218 I915_WRITE(EG5, 0x00140031);
6219 I915_WRITE(EG6, 0);
6220 I915_WRITE(EG7, 0);
6221
6222 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006223 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006224
6225 /* Enable PMON + select events */
6226 I915_WRITE(ECR, 0x80000019);
6227
6228 lcfuse = I915_READ(LCFUSE02);
6229
Daniel Vetter20e4d402012-08-08 23:35:39 +02006230 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006231}
6232
Imre Deakae484342014-03-31 15:10:44 +03006233void intel_init_gt_powersave(struct drm_device *dev)
6234{
Imre Deakb268c692015-12-15 20:10:31 +02006235 struct drm_i915_private *dev_priv = dev->dev_private;
6236
Imre Deakb268c692015-12-15 20:10:31 +02006237 /*
6238 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6239 * requirement.
6240 */
6241 if (!i915.enable_rc6) {
6242 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6243 intel_runtime_pm_get(dev_priv);
6244 }
Imre Deake6069ca2014-04-18 16:01:02 +03006245
Deepak S38807742014-05-23 21:00:15 +05306246 if (IS_CHERRYVIEW(dev))
6247 cherryview_init_gt_powersave(dev);
6248 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006249 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03006250}
6251
6252void intel_cleanup_gt_powersave(struct drm_device *dev)
6253{
Imre Deakb268c692015-12-15 20:10:31 +02006254 struct drm_i915_private *dev_priv = dev->dev_private;
6255
Deepak S38807742014-05-23 21:00:15 +05306256 if (IS_CHERRYVIEW(dev))
6257 return;
6258 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006259 valleyview_cleanup_gt_powersave(dev);
Imre Deakb268c692015-12-15 20:10:31 +02006260
6261 if (!i915.enable_rc6)
6262 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006263}
6264
Imre Deakdbea3ce2014-12-15 18:59:28 +02006265static void gen6_suspend_rps(struct drm_device *dev)
6266{
6267 struct drm_i915_private *dev_priv = dev->dev_private;
6268
6269 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6270
Akash Goel4c2a8892015-03-06 11:07:24 +05306271 gen6_disable_rps_interrupts(dev);
Imre Deakdbea3ce2014-12-15 18:59:28 +02006272}
6273
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006274/**
6275 * intel_suspend_gt_powersave - suspend PM work and helper threads
6276 * @dev: drm device
6277 *
6278 * We don't want to disable RC6 or other features here, we just want
6279 * to make sure any work we've queued has finished and won't bother
6280 * us while we're suspended.
6281 */
6282void intel_suspend_gt_powersave(struct drm_device *dev)
6283{
6284 struct drm_i915_private *dev_priv = dev->dev_private;
6285
Imre Deakd4d70aa2014-11-19 15:30:04 +02006286 if (INTEL_INFO(dev)->gen < 6)
6287 return;
6288
Imre Deakdbea3ce2014-12-15 18:59:28 +02006289 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05306290
6291 /* Force GPU to min freq during suspend */
6292 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006293}
6294
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006295void intel_disable_gt_powersave(struct drm_device *dev)
6296{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006297 struct drm_i915_private *dev_priv = dev->dev_private;
6298
Daniel Vetter930ebb42012-06-29 23:32:16 +02006299 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006300 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05306301 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02006302 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03006303
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006304 mutex_lock(&dev_priv->rps.hw_lock);
Akash Goel2030d682016-04-23 00:05:45 +05306305 if (INTEL_INFO(dev)->gen >= 9) {
6306 gen9_disable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006307 gen9_disable_rps(dev);
Akash Goel2030d682016-04-23 00:05:45 +05306308 } else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05306309 cherryview_disable_rps(dev);
6310 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006311 valleyview_disable_rps(dev);
6312 else
6313 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02006314
Chris Wilsonc0951f02013-10-10 21:58:50 +01006315 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006316 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006317 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006318}
6319
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006320static void intel_gen6_powersave_work(struct work_struct *work)
6321{
6322 struct drm_i915_private *dev_priv =
6323 container_of(work, struct drm_i915_private,
6324 rps.delayed_resume_work.work);
6325 struct drm_device *dev = dev_priv->dev;
6326
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006327 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006328
Akash Goel4c2a8892015-03-06 11:07:24 +05306329 gen6_reset_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02006330
Deepak S38807742014-05-23 21:00:15 +05306331 if (IS_CHERRYVIEW(dev)) {
6332 cherryview_enable_rps(dev);
6333 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07006334 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006335 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006336 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006337 gen9_enable_rps(dev);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07006338 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Akash Goelcc017fb42015-06-29 14:50:21 +05306339 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006340 } else if (IS_BROADWELL(dev)) {
6341 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006342 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006343 } else {
6344 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006345 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006346 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006347
6348 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6349 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6350
6351 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6352 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6353
Chris Wilsonc0951f02013-10-10 21:58:50 +01006354 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02006355
Akash Goel4c2a8892015-03-06 11:07:24 +05306356 gen6_enable_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02006357
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006358 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03006359
6360 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006361}
6362
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006363void intel_enable_gt_powersave(struct drm_device *dev)
6364{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006365 struct drm_i915_private *dev_priv = dev->dev_private;
6366
Yu Zhangf61018b2015-02-10 19:05:52 +08006367 /* Powersaving is controlled by the host when inside a VM */
6368 if (intel_vgpu_active(dev))
6369 return;
6370
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006371 if (IS_IRONLAKE_M(dev)) {
6372 ironlake_enable_drps(dev);
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00006373 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006374 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03006375 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05306376 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006377 /*
6378 * PCU communication is slow and this doesn't need to be
6379 * done at any specific time, so do this out of our fast path
6380 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03006381 *
6382 * We depend on the HW RC6 power context save/restore
6383 * mechanism when entering D3 through runtime PM suspend. So
6384 * disable RPM until RPS/RC6 is properly setup. We can only
6385 * get here via the driver load/system resume/runtime resume
6386 * paths, so the _noresume version is enough (and in case of
6387 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006388 */
Imre Deakc6df39b2014-04-14 20:24:29 +03006389 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6390 round_jiffies_up_relative(HZ)))
6391 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006392 }
6393}
6394
Imre Deakc6df39b2014-04-14 20:24:29 +03006395void intel_reset_gt_powersave(struct drm_device *dev)
6396{
6397 struct drm_i915_private *dev_priv = dev->dev_private;
6398
Imre Deakdbea3ce2014-12-15 18:59:28 +02006399 if (INTEL_INFO(dev)->gen < 6)
6400 return;
6401
6402 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03006403 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03006404}
6405
Daniel Vetter3107bd42012-10-31 22:52:31 +01006406static void ibx_init_clock_gating(struct drm_device *dev)
6407{
6408 struct drm_i915_private *dev_priv = dev->dev_private;
6409
6410 /*
6411 * On Ibex Peak and Cougar Point, we need to disable clock
6412 * gating for the panel power sequencer or it will fail to
6413 * start up when no ports are active.
6414 */
6415 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6416}
6417
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006418static void g4x_disable_trickle_feed(struct drm_device *dev)
6419{
6420 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006421 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006422
Damien Lespiau055e3932014-08-18 13:49:10 +01006423 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006424 I915_WRITE(DSPCNTR(pipe),
6425 I915_READ(DSPCNTR(pipe)) |
6426 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006427
6428 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6429 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006430 }
6431}
6432
Ville Syrjälä017636c2013-12-05 15:51:37 +02006433static void ilk_init_lp_watermarks(struct drm_device *dev)
6434{
6435 struct drm_i915_private *dev_priv = dev->dev_private;
6436
6437 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6438 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6439 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6440
6441 /*
6442 * Don't touch WM1S_LP_EN here.
6443 * Doing so could cause underruns.
6444 */
6445}
6446
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006447static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006448{
6449 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006450 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006451
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006452 /*
6453 * Required for FBC
6454 * WaFbcDisableDpfcClockGating:ilk
6455 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006456 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6457 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6458 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006459
6460 I915_WRITE(PCH_3DCGDIS0,
6461 MARIUNIT_CLOCK_GATE_DISABLE |
6462 SVSMUNIT_CLOCK_GATE_DISABLE);
6463 I915_WRITE(PCH_3DCGDIS1,
6464 VFMUNIT_CLOCK_GATE_DISABLE);
6465
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006466 /*
6467 * According to the spec the following bits should be set in
6468 * order to enable memory self-refresh
6469 * The bit 22/21 of 0x42004
6470 * The bit 5 of 0x42020
6471 * The bit 15 of 0x45000
6472 */
6473 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6474 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6475 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006476 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006477 I915_WRITE(DISP_ARB_CTL,
6478 (I915_READ(DISP_ARB_CTL) |
6479 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006480
6481 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006482
6483 /*
6484 * Based on the document from hardware guys the following bits
6485 * should be set unconditionally in order to enable FBC.
6486 * The bit 22 of 0x42000
6487 * The bit 22 of 0x42004
6488 * The bit 7,8,9 of 0x42020.
6489 */
6490 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006491 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006492 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6493 I915_READ(ILK_DISPLAY_CHICKEN1) |
6494 ILK_FBCQ_DIS);
6495 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6496 I915_READ(ILK_DISPLAY_CHICKEN2) |
6497 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006498 }
6499
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006500 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6501
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006502 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6503 I915_READ(ILK_DISPLAY_CHICKEN2) |
6504 ILK_ELPIN_409_SELECT);
6505 I915_WRITE(_3D_CHICKEN2,
6506 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6507 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006508
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006509 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006510 I915_WRITE(CACHE_MODE_0,
6511 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006512
Akash Goel4e046322014-04-04 17:14:38 +05306513 /* WaDisable_RenderCache_OperationalFlush:ilk */
6514 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6515
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006516 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006517
Daniel Vetter3107bd42012-10-31 22:52:31 +01006518 ibx_init_clock_gating(dev);
6519}
6520
6521static void cpt_init_clock_gating(struct drm_device *dev)
6522{
6523 struct drm_i915_private *dev_priv = dev->dev_private;
6524 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006525 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006526
6527 /*
6528 * On Ibex Peak and Cougar Point, we need to disable clock
6529 * gating for the panel power sequencer or it will fail to
6530 * start up when no ports are active.
6531 */
Jesse Barnescd664072013-10-02 10:34:19 -07006532 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6533 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6534 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006535 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6536 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006537 /* The below fixes the weird display corruption, a few pixels shifted
6538 * downward, on (only) LVDS of some HP laptops with IVY.
6539 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006540 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006541 val = I915_READ(TRANS_CHICKEN2(pipe));
6542 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6543 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006544 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006545 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006546 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6547 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6548 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006549 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6550 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006551 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006552 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006553 I915_WRITE(TRANS_CHICKEN1(pipe),
6554 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6555 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006556}
6557
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006558static void gen6_check_mch_setup(struct drm_device *dev)
6559{
6560 struct drm_i915_private *dev_priv = dev->dev_private;
6561 uint32_t tmp;
6562
6563 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006564 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6565 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6566 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006567}
6568
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006569static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006570{
6571 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006572 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006573
Damien Lespiau231e54f2012-10-19 17:55:41 +01006574 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006575
6576 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6577 I915_READ(ILK_DISPLAY_CHICKEN2) |
6578 ILK_ELPIN_409_SELECT);
6579
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006580 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006581 I915_WRITE(_3D_CHICKEN,
6582 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6583
Akash Goel4e046322014-04-04 17:14:38 +05306584 /* WaDisable_RenderCache_OperationalFlush:snb */
6585 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6586
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006587 /*
6588 * BSpec recoomends 8x4 when MSAA is used,
6589 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006590 *
6591 * Note that PS/WM thread counts depend on the WIZ hashing
6592 * disable bit, which we don't touch here, but it's good
6593 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006594 */
6595 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006596 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006597
Ville Syrjälä017636c2013-12-05 15:51:37 +02006598 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006599
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006600 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006601 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006602
6603 I915_WRITE(GEN6_UCGCTL1,
6604 I915_READ(GEN6_UCGCTL1) |
6605 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6606 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6607
6608 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6609 * gating disable must be set. Failure to set it results in
6610 * flickering pixels due to Z write ordering failures after
6611 * some amount of runtime in the Mesa "fire" demo, and Unigine
6612 * Sanctuary and Tropics, and apparently anything else with
6613 * alpha test or pixel discard.
6614 *
6615 * According to the spec, bit 11 (RCCUNIT) must also be set,
6616 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006617 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006618 * WaDisableRCCUnitClockGating:snb
6619 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006620 */
6621 I915_WRITE(GEN6_UCGCTL2,
6622 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6623 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6624
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006625 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006626 I915_WRITE(_3D_CHICKEN3,
6627 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006628
6629 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006630 * Bspec says:
6631 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6632 * 3DSTATE_SF number of SF output attributes is more than 16."
6633 */
6634 I915_WRITE(_3D_CHICKEN3,
6635 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6636
6637 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006638 * According to the spec the following bits should be
6639 * set in order to enable memory self-refresh and fbc:
6640 * The bit21 and bit22 of 0x42000
6641 * The bit21 and bit22 of 0x42004
6642 * The bit5 and bit7 of 0x42020
6643 * The bit14 of 0x70180
6644 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006645 *
6646 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006647 */
6648 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6649 I915_READ(ILK_DISPLAY_CHICKEN1) |
6650 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6651 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6652 I915_READ(ILK_DISPLAY_CHICKEN2) |
6653 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006654 I915_WRITE(ILK_DSPCLK_GATE_D,
6655 I915_READ(ILK_DSPCLK_GATE_D) |
6656 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6657 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006658
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006659 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006660
Daniel Vetter3107bd42012-10-31 22:52:31 +01006661 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006662
6663 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006664}
6665
6666static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6667{
6668 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6669
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006670 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006671 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006672 *
6673 * This actually overrides the dispatch
6674 * mode for all thread types.
6675 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006676 reg &= ~GEN7_FF_SCHED_MASK;
6677 reg |= GEN7_FF_TS_SCHED_HW;
6678 reg |= GEN7_FF_VS_SCHED_HW;
6679 reg |= GEN7_FF_DS_SCHED_HW;
6680
6681 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6682}
6683
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006684static void lpt_init_clock_gating(struct drm_device *dev)
6685{
6686 struct drm_i915_private *dev_priv = dev->dev_private;
6687
6688 /*
6689 * TODO: this bit should only be enabled when really needed, then
6690 * disabled when not needed anymore in order to save power.
6691 */
Ville Syrjäläc2699522015-08-27 23:55:59 +03006692 if (HAS_PCH_LPT_LP(dev))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006693 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6694 I915_READ(SOUTH_DSPCLK_GATE_D) |
6695 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006696
6697 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03006698 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6699 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006700 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006701}
6702
Imre Deak7d708ee2013-04-17 14:04:50 +03006703static void lpt_suspend_hw(struct drm_device *dev)
6704{
6705 struct drm_i915_private *dev_priv = dev->dev_private;
6706
Ville Syrjäläc2699522015-08-27 23:55:59 +03006707 if (HAS_PCH_LPT_LP(dev)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03006708 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6709
6710 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6711 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6712 }
6713}
6714
Mika Kuoppala9146f302016-06-07 17:19:01 +03006715static void kabylake_init_clock_gating(struct drm_device *dev)
6716{
6717 struct drm_i915_private *dev_priv = dev->dev_private;
6718
Mika Kuoppala11b28342016-06-07 17:19:04 +03006719 gen9_init_clock_gating(dev);
Mika Kuoppala9146f302016-06-07 17:19:01 +03006720
6721 /* WaDisableSDEUnitClockGating:kbl */
6722 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
6723 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6724 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6725}
6726
Daniel Vetterda0a0ac2016-05-19 09:14:20 +02006727static void skylake_init_clock_gating(struct drm_device *dev)
6728{
Mika Kuoppala11b28342016-06-07 17:19:04 +03006729 gen9_init_clock_gating(dev);
Daniel Vetterda0a0ac2016-05-19 09:14:20 +02006730}
6731
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006732static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006733{
6734 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006735 enum pipe pipe;
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006736 uint32_t misccpctl;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006737
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03006738 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006739
Ben Widawskyab57fff2013-12-12 15:28:04 -08006740 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006741 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006742
Ben Widawskyab57fff2013-12-12 15:28:04 -08006743 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006744 I915_WRITE(CHICKEN_PAR1_1,
6745 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6746
Ben Widawskyab57fff2013-12-12 15:28:04 -08006747 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006748 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006749 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006750 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006751 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006752 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006753
Ben Widawskyab57fff2013-12-12 15:28:04 -08006754 /* WaVSRefCountFullforceMissDisable:bdw */
6755 /* WaDSRefCountFullforceMissDisable:bdw */
6756 I915_WRITE(GEN7_FF_THREAD_MODE,
6757 I915_READ(GEN7_FF_THREAD_MODE) &
6758 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006759
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006760 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6761 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006762
6763 /* WaDisableSDEUnitClockGating:bdw */
6764 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6765 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006766
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006767 /*
6768 * WaProgramL3SqcReg1Default:bdw
6769 * WaTempDisableDOPClkGating:bdw
6770 */
6771 misccpctl = I915_READ(GEN7_MISCCPCTL);
6772 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6773 I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
Imre Deakd6a862f2016-05-03 15:54:19 +03006774 /*
6775 * Wait at least 100 clocks before re-enabling clock gating. See
6776 * the definition of L3SQCREG1 in BSpec.
6777 */
6778 POSTING_READ(GEN8_L3SQCREG1);
6779 udelay(1);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006780 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6781
Ville Syrjälä6d50b062015-05-19 20:32:57 +03006782 /*
6783 * WaGttCachingOffByDefault:bdw
6784 * GTT cache may not work with big pages, so if those
6785 * are ever enabled GTT cache may need to be disabled.
6786 */
6787 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6788
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006789 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006790}
6791
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006792static void haswell_init_clock_gating(struct drm_device *dev)
6793{
6794 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006795
Ville Syrjälä017636c2013-12-05 15:51:37 +02006796 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006797
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006798 /* L3 caching of data atomics doesn't work -- disable it. */
6799 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6800 I915_WRITE(HSW_ROW_CHICKEN3,
6801 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6802
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006803 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006804 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6805 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6806 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6807
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006808 /* WaVSRefCountFullforceMissDisable:hsw */
6809 I915_WRITE(GEN7_FF_THREAD_MODE,
6810 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006811
Akash Goel4e046322014-04-04 17:14:38 +05306812 /* WaDisable_RenderCache_OperationalFlush:hsw */
6813 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6814
Chia-I Wufe27c602014-01-28 13:29:33 +08006815 /* enable HiZ Raw Stall Optimization */
6816 I915_WRITE(CACHE_MODE_0_GEN7,
6817 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6818
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006819 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006820 I915_WRITE(CACHE_MODE_1,
6821 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006822
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006823 /*
6824 * BSpec recommends 8x4 when MSAA is used,
6825 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006826 *
6827 * Note that PS/WM thread counts depend on the WIZ hashing
6828 * disable bit, which we don't touch here, but it's good
6829 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006830 */
6831 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006832 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006833
Kenneth Graunke94411592014-12-31 16:23:00 -08006834 /* WaSampleCChickenBitEnable:hsw */
6835 I915_WRITE(HALF_SLICE_CHICKEN3,
6836 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6837
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006838 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006839 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6840
Paulo Zanoni90a88642013-05-03 17:23:45 -03006841 /* WaRsPkgCStateDisplayPMReq:hsw */
6842 I915_WRITE(CHICKEN_PAR1_1,
6843 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006844
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006845 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006846}
6847
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006848static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006849{
6850 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006851 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006852
Ville Syrjälä017636c2013-12-05 15:51:37 +02006853 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006854
Damien Lespiau231e54f2012-10-19 17:55:41 +01006855 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006856
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006857 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006858 I915_WRITE(_3D_CHICKEN3,
6859 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6860
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006861 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006862 I915_WRITE(IVB_CHICKEN3,
6863 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6864 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6865
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006866 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006867 if (IS_IVB_GT1(dev))
6868 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6869 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006870
Akash Goel4e046322014-04-04 17:14:38 +05306871 /* WaDisable_RenderCache_OperationalFlush:ivb */
6872 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6873
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006874 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006875 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6876 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6877
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006878 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006879 I915_WRITE(GEN7_L3CNTLREG1,
6880 GEN7_WA_FOR_GEN7_L3_CONTROL);
6881 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006882 GEN7_WA_L3_CHICKEN_MODE);
6883 if (IS_IVB_GT1(dev))
6884 I915_WRITE(GEN7_ROW_CHICKEN2,
6885 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006886 else {
6887 /* must write both registers */
6888 I915_WRITE(GEN7_ROW_CHICKEN2,
6889 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006890 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6891 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006892 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006893
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006894 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006895 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6896 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6897
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006898 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006899 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006900 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006901 */
6902 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006903 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006904
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006905 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006906 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6907 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6908 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6909
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006910 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006911
6912 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006913
Chris Wilson22721342014-03-04 09:41:43 +00006914 if (0) { /* causes HiZ corruption on ivb:gt1 */
6915 /* enable HiZ Raw Stall Optimization */
6916 I915_WRITE(CACHE_MODE_0_GEN7,
6917 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6918 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006919
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006920 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006921 I915_WRITE(CACHE_MODE_1,
6922 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006923
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006924 /*
6925 * BSpec recommends 8x4 when MSAA is used,
6926 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006927 *
6928 * Note that PS/WM thread counts depend on the WIZ hashing
6929 * disable bit, which we don't touch here, but it's good
6930 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006931 */
6932 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006933 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006934
Ben Widawsky20848222012-05-04 18:58:59 -07006935 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6936 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6937 snpcr |= GEN6_MBC_SNPCR_MED;
6938 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006939
Ben Widawskyab5c6082013-04-05 13:12:41 -07006940 if (!HAS_PCH_NOP(dev))
6941 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006942
6943 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006944}
6945
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006946static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006947{
6948 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006949
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006950 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006951 I915_WRITE(_3D_CHICKEN3,
6952 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6953
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006954 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006955 I915_WRITE(IVB_CHICKEN3,
6956 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6957 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6958
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006959 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006960 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006961 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006962 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6963 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006964
Akash Goel4e046322014-04-04 17:14:38 +05306965 /* WaDisable_RenderCache_OperationalFlush:vlv */
6966 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6967
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006968 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006969 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6970 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6971
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006972 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006973 I915_WRITE(GEN7_ROW_CHICKEN2,
6974 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6975
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006976 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006977 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6978 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6979 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6980
Ville Syrjälä46680e02014-01-22 21:33:01 +02006981 gen7_setup_fixed_func_scheduler(dev_priv);
6982
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006983 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006984 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006985 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006986 */
6987 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006988 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006989
Akash Goelc98f5062014-03-24 23:00:07 +05306990 /* WaDisableL3Bank2xClockGate:vlv
6991 * Disabling L3 clock gating- MMIO 940c[25] = 1
6992 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6993 I915_WRITE(GEN7_UCGCTL4,
6994 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006995
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006996 /*
6997 * BSpec says this must be set, even though
6998 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6999 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007000 I915_WRITE(CACHE_MODE_1,
7001 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007002
7003 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007004 * BSpec recommends 8x4 when MSAA is used,
7005 * however in practice 16x4 seems fastest.
7006 *
7007 * Note that PS/WM thread counts depend on the WIZ hashing
7008 * disable bit, which we don't touch here, but it's good
7009 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7010 */
7011 I915_WRITE(GEN7_GT_MODE,
7012 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7013
7014 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007015 * WaIncreaseL3CreditsForVLVB0:vlv
7016 * This is the hardware default actually.
7017 */
7018 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7019
7020 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007021 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007022 * Disable clock gating on th GCFG unit to prevent a delay
7023 * in the reporting of vblank events.
7024 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007025 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007026}
7027
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007028static void cherryview_init_clock_gating(struct drm_device *dev)
7029{
7030 struct drm_i915_private *dev_priv = dev->dev_private;
7031
Ville Syrjälä232ce332014-04-09 13:28:35 +03007032 /* WaVSRefCountFullforceMissDisable:chv */
7033 /* WaDSRefCountFullforceMissDisable:chv */
7034 I915_WRITE(GEN7_FF_THREAD_MODE,
7035 I915_READ(GEN7_FF_THREAD_MODE) &
7036 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007037
7038 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7039 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7040 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007041
7042 /* WaDisableCSUnitClockGating:chv */
7043 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7044 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007045
7046 /* WaDisableSDEUnitClockGating:chv */
7047 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7048 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007049
7050 /*
7051 * GTT cache may not work with big pages, so if those
7052 * are ever enabled GTT cache may need to be disabled.
7053 */
7054 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007055}
7056
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007057static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007058{
7059 struct drm_i915_private *dev_priv = dev->dev_private;
7060 uint32_t dspclk_gate;
7061
7062 I915_WRITE(RENCLK_GATE_D1, 0);
7063 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7064 GS_UNIT_CLOCK_GATE_DISABLE |
7065 CL_UNIT_CLOCK_GATE_DISABLE);
7066 I915_WRITE(RAMCLK_GATE_D, 0);
7067 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7068 OVRUNIT_CLOCK_GATE_DISABLE |
7069 OVCUNIT_CLOCK_GATE_DISABLE;
7070 if (IS_GM45(dev))
7071 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7072 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007073
7074 /* WaDisableRenderCachePipelinedFlush */
7075 I915_WRITE(CACHE_MODE_0,
7076 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007077
Akash Goel4e046322014-04-04 17:14:38 +05307078 /* WaDisable_RenderCache_OperationalFlush:g4x */
7079 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7080
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007081 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007082}
7083
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007084static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007085{
7086 struct drm_i915_private *dev_priv = dev->dev_private;
7087
7088 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7089 I915_WRITE(RENCLK_GATE_D2, 0);
7090 I915_WRITE(DSPCLK_GATE_D, 0);
7091 I915_WRITE(RAMCLK_GATE_D, 0);
7092 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007093 I915_WRITE(MI_ARB_STATE,
7094 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307095
7096 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7097 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007098}
7099
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007100static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007101{
7102 struct drm_i915_private *dev_priv = dev->dev_private;
7103
7104 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7105 I965_RCC_CLOCK_GATE_DISABLE |
7106 I965_RCPB_CLOCK_GATE_DISABLE |
7107 I965_ISC_CLOCK_GATE_DISABLE |
7108 I965_FBC_CLOCK_GATE_DISABLE);
7109 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007110 I915_WRITE(MI_ARB_STATE,
7111 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307112
7113 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7114 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007115}
7116
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007117static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007118{
7119 struct drm_i915_private *dev_priv = dev->dev_private;
7120 u32 dstate = I915_READ(D_STATE);
7121
7122 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7123 DSTATE_DOT_CLOCK_GATING;
7124 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007125
7126 if (IS_PINEVIEW(dev))
7127 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007128
7129 /* IIR "flip pending" means done if this bit is set */
7130 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007131
7132 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007133 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007134
7135 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7136 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007137
7138 I915_WRITE(MI_ARB_STATE,
7139 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007140}
7141
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007142static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007143{
7144 struct drm_i915_private *dev_priv = dev->dev_private;
7145
7146 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007147
7148 /* interrupts should cause a wake up from C3 */
7149 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7150 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007151
7152 I915_WRITE(MEM_MODE,
7153 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007154}
7155
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007156static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007157{
7158 struct drm_i915_private *dev_priv = dev->dev_private;
7159
7160 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007161
7162 I915_WRITE(MEM_MODE,
7163 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7164 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007165}
7166
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007167void intel_init_clock_gating(struct drm_device *dev)
7168{
7169 struct drm_i915_private *dev_priv = dev->dev_private;
7170
Imre Deakbb400da2016-03-16 13:38:54 +02007171 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007172}
7173
Imre Deak7d708ee2013-04-17 14:04:50 +03007174void intel_suspend_hw(struct drm_device *dev)
7175{
7176 if (HAS_PCH_LPT(dev))
7177 lpt_suspend_hw(dev);
7178}
7179
Imre Deakbb400da2016-03-16 13:38:54 +02007180static void nop_init_clock_gating(struct drm_device *dev)
7181{
7182 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7183}
7184
7185/**
7186 * intel_init_clock_gating_hooks - setup the clock gating hooks
7187 * @dev_priv: device private
7188 *
7189 * Setup the hooks that configure which clocks of a given platform can be
7190 * gated and also apply various GT and display specific workarounds for these
7191 * platforms. Note that some GT specific workarounds are applied separately
7192 * when GPU contexts or batchbuffers start their execution.
7193 */
7194void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7195{
7196 if (IS_SKYLAKE(dev_priv))
Daniel Vetterda0a0ac2016-05-19 09:14:20 +02007197 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007198 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9146f302016-06-07 17:19:01 +03007199 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007200 else if (IS_BROXTON(dev_priv))
7201 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7202 else if (IS_BROADWELL(dev_priv))
7203 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7204 else if (IS_CHERRYVIEW(dev_priv))
7205 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7206 else if (IS_HASWELL(dev_priv))
7207 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7208 else if (IS_IVYBRIDGE(dev_priv))
7209 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7210 else if (IS_VALLEYVIEW(dev_priv))
7211 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7212 else if (IS_GEN6(dev_priv))
7213 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7214 else if (IS_GEN5(dev_priv))
7215 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7216 else if (IS_G4X(dev_priv))
7217 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7218 else if (IS_CRESTLINE(dev_priv))
7219 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7220 else if (IS_BROADWATER(dev_priv))
7221 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7222 else if (IS_GEN3(dev_priv))
7223 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7224 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7225 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7226 else if (IS_GEN2(dev_priv))
7227 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7228 else {
7229 MISSING_CASE(INTEL_DEVID(dev_priv));
7230 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7231 }
7232}
7233
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007234/* Set up chip specific power management-related functions */
7235void intel_init_pm(struct drm_device *dev)
7236{
7237 struct drm_i915_private *dev_priv = dev->dev_private;
7238
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007239 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007240
Daniel Vetterc921aba2012-04-26 23:28:17 +02007241 /* For cxsr */
7242 if (IS_PINEVIEW(dev))
7243 i915_pineview_get_mem_freq(dev);
7244 else if (IS_GEN5(dev))
7245 i915_ironlake_get_mem_freq(dev);
7246
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007247 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007248 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007249 skl_setup_wm_latency(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007250 dev_priv->display.update_wm = skl_update_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05307251 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007252 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007253
Ville Syrjäläbd602542014-01-07 16:14:10 +02007254 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7255 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7256 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7257 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007258 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007259 dev_priv->display.compute_intermediate_wm =
7260 ilk_compute_intermediate_wm;
7261 dev_priv->display.initial_watermarks =
7262 ilk_initial_watermarks;
7263 dev_priv->display.optimize_watermarks =
7264 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007265 } else {
7266 DRM_DEBUG_KMS("Failed to read display plane latency. "
7267 "Disable CxSR\n");
7268 }
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007269 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007270 vlv_setup_wm_latency(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007271 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007272 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007273 vlv_setup_wm_latency(dev);
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007274 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007275 } else if (IS_PINEVIEW(dev)) {
7276 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7277 dev_priv->is_ddr3,
7278 dev_priv->fsb_freq,
7279 dev_priv->mem_freq)) {
7280 DRM_INFO("failed to find known CxSR latency "
7281 "(found ddr%s fsb freq %d, mem freq %d), "
7282 "disabling CxSR\n",
7283 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7284 dev_priv->fsb_freq, dev_priv->mem_freq);
7285 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007286 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007287 dev_priv->display.update_wm = NULL;
7288 } else
7289 dev_priv->display.update_wm = pineview_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007290 } else if (IS_G4X(dev)) {
7291 dev_priv->display.update_wm = g4x_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007292 } else if (IS_GEN4(dev)) {
7293 dev_priv->display.update_wm = i965_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007294 } else if (IS_GEN3(dev)) {
7295 dev_priv->display.update_wm = i9xx_update_wm;
7296 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007297 } else if (IS_GEN2(dev)) {
7298 if (INTEL_INFO(dev)->num_pipes == 1) {
7299 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007300 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007301 } else {
7302 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007303 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007304 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007305 } else {
7306 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007307 }
7308}
7309
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007310int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007311{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007312 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007313
7314 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7315 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7316 return -EAGAIN;
7317 }
7318
7319 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00007320 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007321 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7322
7323 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7324 500)) {
7325 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7326 return -ETIMEDOUT;
7327 }
7328
7329 *val = I915_READ(GEN6_PCODE_DATA);
7330 I915_WRITE(GEN6_PCODE_DATA, 0);
7331
7332 return 0;
7333}
7334
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007335int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007336{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007337 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007338
7339 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7340 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7341 return -EAGAIN;
7342 }
7343
7344 I915_WRITE(GEN6_PCODE_DATA, val);
7345 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7346
7347 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7348 500)) {
7349 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7350 return -ETIMEDOUT;
7351 }
7352
7353 I915_WRITE(GEN6_PCODE_DATA, 0);
7354
7355 return 0;
7356}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007357
Ville Syrjälädd06f882014-11-10 22:55:12 +02007358static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7359{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007360 /*
7361 * N = val - 0xb7
7362 * Slow = Fast = GPLL ref * N
7363 */
7364 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007365}
7366
Fengguang Wub55dd642014-07-12 11:21:39 +02007367static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007368{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007369 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007370}
7371
Fengguang Wub55dd642014-07-12 11:21:39 +02007372static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307373{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007374 /*
7375 * N = val / 2
7376 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7377 */
7378 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307379}
7380
Fengguang Wub55dd642014-07-12 11:21:39 +02007381static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307382{
Ville Syrjälä1c147622014-08-18 14:42:43 +03007383 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007384 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307385}
7386
Ville Syrjälä616bc822015-01-23 21:04:25 +02007387int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7388{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007389 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007390 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7391 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007392 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007393 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007394 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007395 return byt_gpu_freq(dev_priv, val);
7396 else
7397 return val * GT_FREQUENCY_MULTIPLIER;
7398}
7399
Ville Syrjälä616bc822015-01-23 21:04:25 +02007400int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7401{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007402 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007403 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7404 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007405 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007406 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007407 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007408 return byt_freq_opcode(dev_priv, val);
7409 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007410 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05307411}
7412
Chris Wilson6ad790c2015-04-07 16:20:31 +01007413struct request_boost {
7414 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02007415 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007416};
7417
7418static void __intel_rps_boost_work(struct work_struct *work)
7419{
7420 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01007421 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007422
Chris Wilsone61b9952015-04-27 13:41:24 +01007423 if (!i915_gem_request_completed(req, true))
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +00007424 gen6_rps_boost(to_i915(req->engine->dev), NULL,
Chris Wilsone61b9952015-04-27 13:41:24 +01007425 req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007426
Chris Wilsone61b9952015-04-27 13:41:24 +01007427 i915_gem_request_unreference__unlocked(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007428 kfree(boost);
7429}
7430
7431void intel_queue_rps_boost_for_request(struct drm_device *dev,
Daniel Vettereed29a52015-05-21 14:21:25 +02007432 struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007433{
7434 struct request_boost *boost;
7435
Daniel Vettereed29a52015-05-21 14:21:25 +02007436 if (req == NULL || INTEL_INFO(dev)->gen < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007437 return;
7438
Chris Wilsone61b9952015-04-27 13:41:24 +01007439 if (i915_gem_request_completed(req, true))
7440 return;
7441
Chris Wilson6ad790c2015-04-07 16:20:31 +01007442 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7443 if (boost == NULL)
7444 return;
7445
Daniel Vettereed29a52015-05-21 14:21:25 +02007446 i915_gem_request_reference(req);
7447 boost->req = req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007448
7449 INIT_WORK(&boost->work, __intel_rps_boost_work);
7450 queue_work(to_i915(dev)->wq, &boost->work);
7451}
7452
Daniel Vetterf742a552013-12-06 10:17:53 +01007453void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007454{
7455 struct drm_i915_private *dev_priv = dev->dev_private;
7456
Daniel Vetterf742a552013-12-06 10:17:53 +01007457 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01007458 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01007459
Chris Wilson907b28c2013-07-19 20:36:52 +01007460 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7461 intel_gen6_powersave_work);
Chris Wilson1854d5c2015-04-07 16:20:32 +01007462 INIT_LIST_HEAD(&dev_priv->rps.clients);
Chris Wilson2e1b8732015-04-27 13:41:22 +01007463 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7464 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007465
Paulo Zanoni33688d92014-03-07 20:08:19 -03007466 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02007467 atomic_set(&dev_priv->pm.wakeref_count, 0);
Imre Deak2b19efe2015-12-15 20:10:37 +02007468 atomic_set(&dev_priv->pm.atomic_seq, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007469}