blob: ceb1eecd9dadd8ec68e25c50030e53f63ed1369b [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>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030034
Ben Widawskydc39fff2013-10-18 12:32:07 -070035/**
Jani Nikula18afd442016-01-18 09:19:48 +020036 * DOC: RC6
37 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070038 * RC6 is a special power stage which allows the GPU to enter an very
39 * low-voltage mode when idle, using down to 0V while at this stage. This
40 * stage is entered automatically when the GPU is idle when RC6 support is
41 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
42 *
43 * There are different RC6 modes available in Intel GPU, which differentiate
44 * among each other with the latency required to enter and leave RC6 and
45 * voltage consumed by the GPU in different states.
46 *
47 * The combination of the following flags define which states GPU is allowed
48 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
49 * RC6pp is deepest RC6. Their support by hardware varies according to the
50 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
51 * which brings the most power savings; deeper states save more power, but
52 * require higher latency to switch to and wake up.
53 */
54#define INTEL_RC6_ENABLE (1<<0)
55#define INTEL_RC6p_ENABLE (1<<1)
56#define INTEL_RC6pp_ENABLE (1<<2)
57
Imre Deaka82abe42015-03-27 14:00:04 +020058static void bxt_init_clock_gating(struct drm_device *dev)
59{
Imre Deak32608ca2015-03-11 11:10:27 +020060 struct drm_i915_private *dev_priv = dev->dev_private;
61
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020062 /* See Bspec note for PSR2_CTL bit 31, Wa#828:bxt */
63 I915_WRITE(CHICKEN_PAR1_1,
64 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
65
Nick Hoatha7546152015-06-29 14:07:32 +010066 /* WaDisableSDEUnitClockGating:bxt */
67 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
68 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
69
Imre Deak32608ca2015-03-11 11:10:27 +020070 /*
71 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020072 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020073 */
Imre Deak32608ca2015-03-11 11:10:27 +020074 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020075 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7a2015-12-01 10:23:52 +020076
77 /*
78 * Wa: Backlight PWM may stop in the asserted state, causing backlight
79 * to stay fully on.
80 */
81 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
82 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
83 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +020084}
85
Daniel Vetterc921aba2012-04-26 23:28:17 +020086static void i915_pineview_get_mem_freq(struct drm_device *dev)
87{
Jani Nikula50227e12014-03-31 14:27:21 +030088 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +020089 u32 tmp;
90
91 tmp = I915_READ(CLKCFG);
92
93 switch (tmp & CLKCFG_FSB_MASK) {
94 case CLKCFG_FSB_533:
95 dev_priv->fsb_freq = 533; /* 133*4 */
96 break;
97 case CLKCFG_FSB_800:
98 dev_priv->fsb_freq = 800; /* 200*4 */
99 break;
100 case CLKCFG_FSB_667:
101 dev_priv->fsb_freq = 667; /* 167*4 */
102 break;
103 case CLKCFG_FSB_400:
104 dev_priv->fsb_freq = 400; /* 100*4 */
105 break;
106 }
107
108 switch (tmp & CLKCFG_MEM_MASK) {
109 case CLKCFG_MEM_533:
110 dev_priv->mem_freq = 533;
111 break;
112 case CLKCFG_MEM_667:
113 dev_priv->mem_freq = 667;
114 break;
115 case CLKCFG_MEM_800:
116 dev_priv->mem_freq = 800;
117 break;
118 }
119
120 /* detect pineview DDR3 setting */
121 tmp = I915_READ(CSHRDDR3CTL);
122 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
123}
124
125static void i915_ironlake_get_mem_freq(struct drm_device *dev)
126{
Jani Nikula50227e12014-03-31 14:27:21 +0300127 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200128 u16 ddrpll, csipll;
129
130 ddrpll = I915_READ16(DDRMPLL1);
131 csipll = I915_READ16(CSIPLL0);
132
133 switch (ddrpll & 0xff) {
134 case 0xc:
135 dev_priv->mem_freq = 800;
136 break;
137 case 0x10:
138 dev_priv->mem_freq = 1066;
139 break;
140 case 0x14:
141 dev_priv->mem_freq = 1333;
142 break;
143 case 0x18:
144 dev_priv->mem_freq = 1600;
145 break;
146 default:
147 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
148 ddrpll & 0xff);
149 dev_priv->mem_freq = 0;
150 break;
151 }
152
Daniel Vetter20e4d402012-08-08 23:35:39 +0200153 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200154
155 switch (csipll & 0x3ff) {
156 case 0x00c:
157 dev_priv->fsb_freq = 3200;
158 break;
159 case 0x00e:
160 dev_priv->fsb_freq = 3733;
161 break;
162 case 0x010:
163 dev_priv->fsb_freq = 4266;
164 break;
165 case 0x012:
166 dev_priv->fsb_freq = 4800;
167 break;
168 case 0x014:
169 dev_priv->fsb_freq = 5333;
170 break;
171 case 0x016:
172 dev_priv->fsb_freq = 5866;
173 break;
174 case 0x018:
175 dev_priv->fsb_freq = 6400;
176 break;
177 default:
178 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
179 csipll & 0x3ff);
180 dev_priv->fsb_freq = 0;
181 break;
182 }
183
184 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200185 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200186 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200187 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200188 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200189 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200190 }
191}
192
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300193static const struct cxsr_latency cxsr_latency_table[] = {
194 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
195 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
196 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
197 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
198 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
199
200 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
201 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
202 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
203 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
204 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
205
206 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
207 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
208 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
209 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
210 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
211
212 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
213 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
214 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
215 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
216 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
217
218 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
219 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
220 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
221 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
222 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
223
224 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
225 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
226 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
227 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
228 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
229};
230
Daniel Vetter63c62272012-04-21 23:17:55 +0200231static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300232 int is_ddr3,
233 int fsb,
234 int mem)
235{
236 const struct cxsr_latency *latency;
237 int i;
238
239 if (fsb == 0 || mem == 0)
240 return NULL;
241
242 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
243 latency = &cxsr_latency_table[i];
244 if (is_desktop == latency->is_desktop &&
245 is_ddr3 == latency->is_ddr3 &&
246 fsb == latency->fsb_freq && mem == latency->mem_freq)
247 return latency;
248 }
249
250 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
251
252 return NULL;
253}
254
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200255static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
256{
257 u32 val;
258
259 mutex_lock(&dev_priv->rps.hw_lock);
260
261 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
262 if (enable)
263 val &= ~FORCE_DDR_HIGH_FREQ;
264 else
265 val |= FORCE_DDR_HIGH_FREQ;
266 val &= ~FORCE_DDR_LOW_FREQ;
267 val |= FORCE_DDR_FREQ_REQ_ACK;
268 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
269
270 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
271 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
272 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
273
274 mutex_unlock(&dev_priv->rps.hw_lock);
275}
276
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200277static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
278{
279 u32 val;
280
281 mutex_lock(&dev_priv->rps.hw_lock);
282
283 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
284 if (enable)
285 val |= DSP_MAXFIFO_PM5_ENABLE;
286 else
287 val &= ~DSP_MAXFIFO_PM5_ENABLE;
288 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
289
290 mutex_unlock(&dev_priv->rps.hw_lock);
291}
292
Ville Syrjäläf4998962015-03-10 17:02:21 +0200293#define FW_WM(value, plane) \
294 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
295
Imre Deak5209b1f2014-07-01 12:36:17 +0300296void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300297{
Imre Deak5209b1f2014-07-01 12:36:17 +0300298 struct drm_device *dev = dev_priv->dev;
299 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300300
Wayne Boyer666a4532015-12-09 12:29:35 -0800301 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300302 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300303 POSTING_READ(FW_BLC_SELF_VLV);
Ville Syrjälä852eb002015-06-24 22:00:07 +0300304 dev_priv->wm.vlv.cxsr = enable;
Imre Deak5209b1f2014-07-01 12:36:17 +0300305 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
306 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300307 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300308 } else if (IS_PINEVIEW(dev)) {
309 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
310 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
311 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300312 POSTING_READ(DSPFW3);
Imre Deak5209b1f2014-07-01 12:36:17 +0300313 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
314 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
315 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
316 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300317 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300318 } else if (IS_I915GM(dev)) {
319 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
320 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
321 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300322 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300323 } else {
324 return;
325 }
326
327 DRM_DEBUG_KMS("memory self-refresh is %s\n",
328 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300329}
330
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200331
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300332/*
333 * Latency for FIFO fetches is dependent on several factors:
334 * - memory configuration (speed, channels)
335 * - chipset
336 * - current MCH state
337 * It can be fairly high in some situations, so here we assume a fairly
338 * pessimal value. It's a tradeoff between extra memory fetches (if we
339 * set this value too high, the FIFO will fetch frequently to stay full)
340 * and power consumption (set it too low to save power and we might see
341 * FIFO underruns and display "flicker").
342 *
343 * A value of 5us seems to be a good balance; safe for very low end
344 * platforms but not overly aggressive on lower latency configs.
345 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100346static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300347
Ville Syrjäläb5004722015-03-05 21:19:47 +0200348#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
349 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
350
351static int vlv_get_fifo_size(struct drm_device *dev,
352 enum pipe pipe, int plane)
353{
354 struct drm_i915_private *dev_priv = dev->dev_private;
355 int sprite0_start, sprite1_start, size;
356
357 switch (pipe) {
358 uint32_t dsparb, dsparb2, dsparb3;
359 case PIPE_A:
360 dsparb = I915_READ(DSPARB);
361 dsparb2 = I915_READ(DSPARB2);
362 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
363 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
364 break;
365 case PIPE_B:
366 dsparb = I915_READ(DSPARB);
367 dsparb2 = I915_READ(DSPARB2);
368 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
369 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
370 break;
371 case PIPE_C:
372 dsparb2 = I915_READ(DSPARB2);
373 dsparb3 = I915_READ(DSPARB3);
374 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
375 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
376 break;
377 default:
378 return 0;
379 }
380
381 switch (plane) {
382 case 0:
383 size = sprite0_start;
384 break;
385 case 1:
386 size = sprite1_start - sprite0_start;
387 break;
388 case 2:
389 size = 512 - 1 - sprite1_start;
390 break;
391 default:
392 return 0;
393 }
394
395 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
396 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
397 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
398 size);
399
400 return size;
401}
402
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300403static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300404{
405 struct drm_i915_private *dev_priv = dev->dev_private;
406 uint32_t dsparb = I915_READ(DSPARB);
407 int size;
408
409 size = dsparb & 0x7f;
410 if (plane)
411 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
412
413 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
414 plane ? "B" : "A", size);
415
416 return size;
417}
418
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200419static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300420{
421 struct drm_i915_private *dev_priv = dev->dev_private;
422 uint32_t dsparb = I915_READ(DSPARB);
423 int size;
424
425 size = dsparb & 0x1ff;
426 if (plane)
427 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
428 size >>= 1; /* Convert to cachelines */
429
430 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
431 plane ? "B" : "A", size);
432
433 return size;
434}
435
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300436static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300437{
438 struct drm_i915_private *dev_priv = dev->dev_private;
439 uint32_t dsparb = I915_READ(DSPARB);
440 int size;
441
442 size = dsparb & 0x7f;
443 size >>= 2; /* Convert to cachelines */
444
445 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
446 plane ? "B" : "A",
447 size);
448
449 return size;
450}
451
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300452/* Pineview has different values for various configs */
453static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300454 .fifo_size = PINEVIEW_DISPLAY_FIFO,
455 .max_wm = PINEVIEW_MAX_WM,
456 .default_wm = PINEVIEW_DFT_WM,
457 .guard_size = PINEVIEW_GUARD_WM,
458 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300459};
460static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300461 .fifo_size = PINEVIEW_DISPLAY_FIFO,
462 .max_wm = PINEVIEW_MAX_WM,
463 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
464 .guard_size = PINEVIEW_GUARD_WM,
465 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300466};
467static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300468 .fifo_size = PINEVIEW_CURSOR_FIFO,
469 .max_wm = PINEVIEW_CURSOR_MAX_WM,
470 .default_wm = PINEVIEW_CURSOR_DFT_WM,
471 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
472 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300473};
474static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300475 .fifo_size = PINEVIEW_CURSOR_FIFO,
476 .max_wm = PINEVIEW_CURSOR_MAX_WM,
477 .default_wm = PINEVIEW_CURSOR_DFT_WM,
478 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
479 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300480};
481static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300482 .fifo_size = G4X_FIFO_SIZE,
483 .max_wm = G4X_MAX_WM,
484 .default_wm = G4X_MAX_WM,
485 .guard_size = 2,
486 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300487};
488static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300489 .fifo_size = I965_CURSOR_FIFO,
490 .max_wm = I965_CURSOR_MAX_WM,
491 .default_wm = I965_CURSOR_DFT_WM,
492 .guard_size = 2,
493 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300494};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300495static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300496 .fifo_size = I965_CURSOR_FIFO,
497 .max_wm = I965_CURSOR_MAX_WM,
498 .default_wm = I965_CURSOR_DFT_WM,
499 .guard_size = 2,
500 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300501};
502static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300503 .fifo_size = I945_FIFO_SIZE,
504 .max_wm = I915_MAX_WM,
505 .default_wm = 1,
506 .guard_size = 2,
507 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300508};
509static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300510 .fifo_size = I915_FIFO_SIZE,
511 .max_wm = I915_MAX_WM,
512 .default_wm = 1,
513 .guard_size = 2,
514 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300516static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300517 .fifo_size = I855GM_FIFO_SIZE,
518 .max_wm = I915_MAX_WM,
519 .default_wm = 1,
520 .guard_size = 2,
521 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300522};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300523static const struct intel_watermark_params i830_bc_wm_info = {
524 .fifo_size = I855GM_FIFO_SIZE,
525 .max_wm = I915_MAX_WM/2,
526 .default_wm = 1,
527 .guard_size = 2,
528 .cacheline_size = I830_FIFO_LINE_SIZE,
529};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200530static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300531 .fifo_size = I830_FIFO_SIZE,
532 .max_wm = I915_MAX_WM,
533 .default_wm = 1,
534 .guard_size = 2,
535 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300536};
537
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300538/**
539 * intel_calculate_wm - calculate watermark level
540 * @clock_in_khz: pixel clock
541 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200542 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300543 * @latency_ns: memory latency for the platform
544 *
545 * Calculate the watermark level (the level at which the display plane will
546 * start fetching from memory again). Each chip has a different display
547 * FIFO size and allocation, so the caller needs to figure that out and pass
548 * in the correct intel_watermark_params structure.
549 *
550 * As the pixel clock runs, the FIFO will be drained at a rate that depends
551 * on the pixel size. When it reaches the watermark level, it'll start
552 * fetching FIFO line sized based chunks from memory until the FIFO fills
553 * past the watermark point. If the FIFO drains completely, a FIFO underrun
554 * will occur, and a display engine hang could result.
555 */
556static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
557 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200558 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300559 unsigned long latency_ns)
560{
561 long entries_required, wm_size;
562
563 /*
564 * Note: we need to make sure we don't overflow for various clock &
565 * latency values.
566 * clocks go from a few thousand to several hundred thousand.
567 * latency is usually a few thousand
568 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200569 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300570 1000;
571 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
572
573 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
574
575 wm_size = fifo_size - (entries_required + wm->guard_size);
576
577 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
578
579 /* Don't promote wm_size to unsigned... */
580 if (wm_size > (long)wm->max_wm)
581 wm_size = wm->max_wm;
582 if (wm_size <= 0)
583 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300584
585 /*
586 * Bspec seems to indicate that the value shouldn't be lower than
587 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
588 * Lets go for 8 which is the burst size since certain platforms
589 * already use a hardcoded 8 (which is what the spec says should be
590 * done).
591 */
592 if (wm_size <= 8)
593 wm_size = 8;
594
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595 return wm_size;
596}
597
598static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
599{
600 struct drm_crtc *crtc, *enabled = NULL;
601
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100602 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000603 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300604 if (enabled)
605 return NULL;
606 enabled = crtc;
607 }
608 }
609
610 return enabled;
611}
612
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300613static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300615 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300616 struct drm_i915_private *dev_priv = dev->dev_private;
617 struct drm_crtc *crtc;
618 const struct cxsr_latency *latency;
619 u32 reg;
620 unsigned long wm;
621
622 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
623 dev_priv->fsb_freq, dev_priv->mem_freq);
624 if (!latency) {
625 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300626 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300627 return;
628 }
629
630 crtc = single_enabled_crtc(dev);
631 if (crtc) {
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300632 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +0200633 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300634 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300635
636 /* Display SR */
637 wm = intel_calculate_wm(clock, &pineview_display_wm,
638 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200639 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300640 reg = I915_READ(DSPFW1);
641 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200642 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300643 I915_WRITE(DSPFW1, reg);
644 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
645
646 /* cursor SR */
647 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
648 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200649 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300650 reg = I915_READ(DSPFW3);
651 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200652 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300653 I915_WRITE(DSPFW3, reg);
654
655 /* Display HPLL off SR */
656 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
657 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200658 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300659 reg = I915_READ(DSPFW3);
660 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200661 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300662 I915_WRITE(DSPFW3, reg);
663
664 /* cursor HPLL off SR */
665 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
666 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200667 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300668 reg = I915_READ(DSPFW3);
669 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200670 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300671 I915_WRITE(DSPFW3, reg);
672 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
673
Imre Deak5209b1f2014-07-01 12:36:17 +0300674 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300675 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300676 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300677 }
678}
679
680static bool g4x_compute_wm0(struct drm_device *dev,
681 int plane,
682 const struct intel_watermark_params *display,
683 int display_latency_ns,
684 const struct intel_watermark_params *cursor,
685 int cursor_latency_ns,
686 int *plane_wm,
687 int *cursor_wm)
688{
689 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300690 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +0200691 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300692 int line_time_us, line_count;
693 int entries, tlb_miss;
694
695 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000696 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300697 *cursor_wm = cursor->guard_size;
698 *plane_wm = display->guard_size;
699 return false;
700 }
701
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200702 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100703 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800704 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200705 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +0200706 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300707
708 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200709 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300710 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
711 if (tlb_miss > 0)
712 entries += tlb_miss;
713 entries = DIV_ROUND_UP(entries, display->cacheline_size);
714 *plane_wm = entries + display->guard_size;
715 if (*plane_wm > (int)display->max_wm)
716 *plane_wm = display->max_wm;
717
718 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200719 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300720 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200721 entries = line_count * crtc->cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
723 if (tlb_miss > 0)
724 entries += tlb_miss;
725 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
726 *cursor_wm = entries + cursor->guard_size;
727 if (*cursor_wm > (int)cursor->max_wm)
728 *cursor_wm = (int)cursor->max_wm;
729
730 return true;
731}
732
733/*
734 * Check the wm result.
735 *
736 * If any calculated watermark values is larger than the maximum value that
737 * can be programmed into the associated watermark register, that watermark
738 * must be disabled.
739 */
740static bool g4x_check_srwm(struct drm_device *dev,
741 int display_wm, int cursor_wm,
742 const struct intel_watermark_params *display,
743 const struct intel_watermark_params *cursor)
744{
745 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
746 display_wm, cursor_wm);
747
748 if (display_wm > display->max_wm) {
749 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
750 display_wm, display->max_wm);
751 return false;
752 }
753
754 if (cursor_wm > cursor->max_wm) {
755 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
756 cursor_wm, cursor->max_wm);
757 return false;
758 }
759
760 if (!(display_wm || cursor_wm)) {
761 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
762 return false;
763 }
764
765 return true;
766}
767
768static bool g4x_compute_srwm(struct drm_device *dev,
769 int plane,
770 int latency_ns,
771 const struct intel_watermark_params *display,
772 const struct intel_watermark_params *cursor,
773 int *display_wm, int *cursor_wm)
774{
775 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300776 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +0200777 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300778 unsigned long line_time_us;
779 int line_count, line_size;
780 int small, large;
781 int entries;
782
783 if (!latency_ns) {
784 *display_wm = *cursor_wm = 0;
785 return false;
786 }
787
788 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200789 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100790 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800791 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200792 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +0200793 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300794
Ville Syrjälä922044c2014-02-14 14:18:57 +0200795 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300796 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200797 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300798
799 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200800 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300801 large = line_count * line_size;
802
803 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
804 *display_wm = entries + display->guard_size;
805
806 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläac484962016-01-20 21:05:26 +0200807 entries = line_count * cpp * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300808 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
809 *cursor_wm = entries + cursor->guard_size;
810
811 return g4x_check_srwm(dev,
812 *display_wm, *cursor_wm,
813 display, cursor);
814}
815
Ville Syrjälä15665972015-03-10 16:16:28 +0200816#define FW_WM_VLV(value, plane) \
817 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
818
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200819static void vlv_write_wm_values(struct intel_crtc *crtc,
820 const struct vlv_wm_values *wm)
821{
822 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
823 enum pipe pipe = crtc->pipe;
824
825 I915_WRITE(VLV_DDL(pipe),
826 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
827 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
828 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
829 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
830
Ville Syrjäläae801522015-03-05 21:19:49 +0200831 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200832 FW_WM(wm->sr.plane, SR) |
833 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
834 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
835 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200836 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200837 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
838 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
839 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200840 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200841 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200842
843 if (IS_CHERRYVIEW(dev_priv)) {
844 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200845 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
846 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200847 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200848 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
849 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200850 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200851 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
852 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200853 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200854 FW_WM(wm->sr.plane >> 9, SR_HI) |
855 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
856 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
857 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
858 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
859 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
860 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
861 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
862 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
863 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200864 } else {
865 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200866 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
867 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200868 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200869 FW_WM(wm->sr.plane >> 9, SR_HI) |
870 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
871 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
872 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
873 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
874 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
875 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200876 }
877
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300878 /* zero (unused) WM1 watermarks */
879 I915_WRITE(DSPFW4, 0);
880 I915_WRITE(DSPFW5, 0);
881 I915_WRITE(DSPFW6, 0);
882 I915_WRITE(DSPHOWM1, 0);
883
Ville Syrjäläae801522015-03-05 21:19:49 +0200884 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200885}
886
Ville Syrjälä15665972015-03-10 16:16:28 +0200887#undef FW_WM_VLV
888
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300889enum vlv_wm_level {
890 VLV_WM_LEVEL_PM2,
891 VLV_WM_LEVEL_PM5,
892 VLV_WM_LEVEL_DDR_DVFS,
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300893};
894
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300895/* latency must be in 0.1us units. */
896static unsigned int vlv_wm_method2(unsigned int pixel_rate,
897 unsigned int pipe_htotal,
898 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200899 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300900 unsigned int latency)
901{
902 unsigned int ret;
903
904 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200905 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300906 ret = DIV_ROUND_UP(ret, 64);
907
908 return ret;
909}
910
911static void vlv_setup_wm_latency(struct drm_device *dev)
912{
913 struct drm_i915_private *dev_priv = dev->dev_private;
914
915 /* all latencies in usec */
916 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
917
Ville Syrjälä58590c12015-09-08 21:05:12 +0300918 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
919
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300920 if (IS_CHERRYVIEW(dev_priv)) {
921 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
922 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300923
924 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300925 }
926}
927
928static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
929 struct intel_crtc *crtc,
930 const struct intel_plane_state *state,
931 int level)
932{
933 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläac484962016-01-20 21:05:26 +0200934 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300935
936 if (dev_priv->wm.pri_latency[level] == 0)
937 return USHRT_MAX;
938
939 if (!state->visible)
940 return 0;
941
Ville Syrjäläac484962016-01-20 21:05:26 +0200942 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300943 clock = crtc->config->base.adjusted_mode.crtc_clock;
944 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
945 width = crtc->config->pipe_src_w;
946 if (WARN_ON(htotal == 0))
947 htotal = 1;
948
949 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
950 /*
951 * FIXME the formula gives values that are
952 * too big for the cursor FIFO, and hence we
953 * would never be able to use cursors. For
954 * now just hardcode the watermark.
955 */
956 wm = 63;
957 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +0200958 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300959 dev_priv->wm.pri_latency[level] * 10);
960 }
961
962 return min_t(int, wm, USHRT_MAX);
963}
964
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +0300965static void vlv_compute_fifo(struct intel_crtc *crtc)
966{
967 struct drm_device *dev = crtc->base.dev;
968 struct vlv_wm_state *wm_state = &crtc->wm_state;
969 struct intel_plane *plane;
970 unsigned int total_rate = 0;
971 const int fifo_size = 512 - 1;
972 int fifo_extra, fifo_left = fifo_size;
973
974 for_each_intel_plane_on_crtc(dev, crtc, plane) {
975 struct intel_plane_state *state =
976 to_intel_plane_state(plane->base.state);
977
978 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
979 continue;
980
981 if (state->visible) {
982 wm_state->num_active_planes++;
983 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
984 }
985 }
986
987 for_each_intel_plane_on_crtc(dev, crtc, plane) {
988 struct intel_plane_state *state =
989 to_intel_plane_state(plane->base.state);
990 unsigned int rate;
991
992 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
993 plane->wm.fifo_size = 63;
994 continue;
995 }
996
997 if (!state->visible) {
998 plane->wm.fifo_size = 0;
999 continue;
1000 }
1001
1002 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1003 plane->wm.fifo_size = fifo_size * rate / total_rate;
1004 fifo_left -= plane->wm.fifo_size;
1005 }
1006
1007 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1008
1009 /* spread the remainder evenly */
1010 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1011 int plane_extra;
1012
1013 if (fifo_left == 0)
1014 break;
1015
1016 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1017 continue;
1018
1019 /* give it all to the first plane if none are active */
1020 if (plane->wm.fifo_size == 0 &&
1021 wm_state->num_active_planes)
1022 continue;
1023
1024 plane_extra = min(fifo_extra, fifo_left);
1025 plane->wm.fifo_size += plane_extra;
1026 fifo_left -= plane_extra;
1027 }
1028
1029 WARN_ON(fifo_left != 0);
1030}
1031
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001032static void vlv_invert_wms(struct intel_crtc *crtc)
1033{
1034 struct vlv_wm_state *wm_state = &crtc->wm_state;
1035 int level;
1036
1037 for (level = 0; level < wm_state->num_levels; level++) {
1038 struct drm_device *dev = crtc->base.dev;
1039 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1040 struct intel_plane *plane;
1041
1042 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1043 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1044
1045 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1046 switch (plane->base.type) {
1047 int sprite;
1048 case DRM_PLANE_TYPE_CURSOR:
1049 wm_state->wm[level].cursor = plane->wm.fifo_size -
1050 wm_state->wm[level].cursor;
1051 break;
1052 case DRM_PLANE_TYPE_PRIMARY:
1053 wm_state->wm[level].primary = plane->wm.fifo_size -
1054 wm_state->wm[level].primary;
1055 break;
1056 case DRM_PLANE_TYPE_OVERLAY:
1057 sprite = plane->plane;
1058 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1059 wm_state->wm[level].sprite[sprite];
1060 break;
1061 }
1062 }
1063 }
1064}
1065
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001066static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001067{
1068 struct drm_device *dev = crtc->base.dev;
1069 struct vlv_wm_state *wm_state = &crtc->wm_state;
1070 struct intel_plane *plane;
1071 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1072 int level;
1073
1074 memset(wm_state, 0, sizeof(*wm_state));
1075
Ville Syrjälä852eb002015-06-24 22:00:07 +03001076 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001077 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001078
1079 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001080
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001081 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001082
1083 if (wm_state->num_active_planes != 1)
1084 wm_state->cxsr = false;
1085
1086 if (wm_state->cxsr) {
1087 for (level = 0; level < wm_state->num_levels; level++) {
1088 wm_state->sr[level].plane = sr_fifo_size;
1089 wm_state->sr[level].cursor = 63;
1090 }
1091 }
1092
1093 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1094 struct intel_plane_state *state =
1095 to_intel_plane_state(plane->base.state);
1096
1097 if (!state->visible)
1098 continue;
1099
1100 /* normal watermarks */
1101 for (level = 0; level < wm_state->num_levels; level++) {
1102 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1103 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1104
1105 /* hack */
1106 if (WARN_ON(level == 0 && wm > max_wm))
1107 wm = max_wm;
1108
1109 if (wm > plane->wm.fifo_size)
1110 break;
1111
1112 switch (plane->base.type) {
1113 int sprite;
1114 case DRM_PLANE_TYPE_CURSOR:
1115 wm_state->wm[level].cursor = wm;
1116 break;
1117 case DRM_PLANE_TYPE_PRIMARY:
1118 wm_state->wm[level].primary = wm;
1119 break;
1120 case DRM_PLANE_TYPE_OVERLAY:
1121 sprite = plane->plane;
1122 wm_state->wm[level].sprite[sprite] = wm;
1123 break;
1124 }
1125 }
1126
1127 wm_state->num_levels = level;
1128
1129 if (!wm_state->cxsr)
1130 continue;
1131
1132 /* maxfifo watermarks */
1133 switch (plane->base.type) {
1134 int sprite, level;
1135 case DRM_PLANE_TYPE_CURSOR:
1136 for (level = 0; level < wm_state->num_levels; level++)
1137 wm_state->sr[level].cursor =
Thomas Daniel5a37ed02015-10-23 14:55:38 +01001138 wm_state->wm[level].cursor;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001139 break;
1140 case DRM_PLANE_TYPE_PRIMARY:
1141 for (level = 0; level < wm_state->num_levels; level++)
1142 wm_state->sr[level].plane =
1143 min(wm_state->sr[level].plane,
1144 wm_state->wm[level].primary);
1145 break;
1146 case DRM_PLANE_TYPE_OVERLAY:
1147 sprite = plane->plane;
1148 for (level = 0; level < wm_state->num_levels; level++)
1149 wm_state->sr[level].plane =
1150 min(wm_state->sr[level].plane,
1151 wm_state->wm[level].sprite[sprite]);
1152 break;
1153 }
1154 }
1155
1156 /* clear any (partially) filled invalid levels */
Ville Syrjälä58590c12015-09-08 21:05:12 +03001157 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001158 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1159 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1160 }
1161
1162 vlv_invert_wms(crtc);
1163}
1164
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001165#define VLV_FIFO(plane, value) \
1166 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1167
1168static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1169{
1170 struct drm_device *dev = crtc->base.dev;
1171 struct drm_i915_private *dev_priv = to_i915(dev);
1172 struct intel_plane *plane;
1173 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1174
1175 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1176 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1177 WARN_ON(plane->wm.fifo_size != 63);
1178 continue;
1179 }
1180
1181 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1182 sprite0_start = plane->wm.fifo_size;
1183 else if (plane->plane == 0)
1184 sprite1_start = sprite0_start + plane->wm.fifo_size;
1185 else
1186 fifo_size = sprite1_start + plane->wm.fifo_size;
1187 }
1188
1189 WARN_ON(fifo_size != 512 - 1);
1190
1191 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1192 pipe_name(crtc->pipe), sprite0_start,
1193 sprite1_start, fifo_size);
1194
1195 switch (crtc->pipe) {
1196 uint32_t dsparb, dsparb2, dsparb3;
1197 case PIPE_A:
1198 dsparb = I915_READ(DSPARB);
1199 dsparb2 = I915_READ(DSPARB2);
1200
1201 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1202 VLV_FIFO(SPRITEB, 0xff));
1203 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1204 VLV_FIFO(SPRITEB, sprite1_start));
1205
1206 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1207 VLV_FIFO(SPRITEB_HI, 0x1));
1208 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1209 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1210
1211 I915_WRITE(DSPARB, dsparb);
1212 I915_WRITE(DSPARB2, dsparb2);
1213 break;
1214 case PIPE_B:
1215 dsparb = I915_READ(DSPARB);
1216 dsparb2 = I915_READ(DSPARB2);
1217
1218 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1219 VLV_FIFO(SPRITED, 0xff));
1220 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1221 VLV_FIFO(SPRITED, sprite1_start));
1222
1223 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1224 VLV_FIFO(SPRITED_HI, 0xff));
1225 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1226 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1227
1228 I915_WRITE(DSPARB, dsparb);
1229 I915_WRITE(DSPARB2, dsparb2);
1230 break;
1231 case PIPE_C:
1232 dsparb3 = I915_READ(DSPARB3);
1233 dsparb2 = I915_READ(DSPARB2);
1234
1235 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1236 VLV_FIFO(SPRITEF, 0xff));
1237 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1238 VLV_FIFO(SPRITEF, sprite1_start));
1239
1240 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1241 VLV_FIFO(SPRITEF_HI, 0xff));
1242 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1243 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1244
1245 I915_WRITE(DSPARB3, dsparb3);
1246 I915_WRITE(DSPARB2, dsparb2);
1247 break;
1248 default:
1249 break;
1250 }
1251}
1252
1253#undef VLV_FIFO
1254
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001255static void vlv_merge_wm(struct drm_device *dev,
1256 struct vlv_wm_values *wm)
1257{
1258 struct intel_crtc *crtc;
1259 int num_active_crtcs = 0;
1260
Ville Syrjälä58590c12015-09-08 21:05:12 +03001261 wm->level = to_i915(dev)->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001262 wm->cxsr = true;
1263
1264 for_each_intel_crtc(dev, crtc) {
1265 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1266
1267 if (!crtc->active)
1268 continue;
1269
1270 if (!wm_state->cxsr)
1271 wm->cxsr = false;
1272
1273 num_active_crtcs++;
1274 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1275 }
1276
1277 if (num_active_crtcs != 1)
1278 wm->cxsr = false;
1279
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001280 if (num_active_crtcs > 1)
1281 wm->level = VLV_WM_LEVEL_PM2;
1282
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001283 for_each_intel_crtc(dev, crtc) {
1284 struct vlv_wm_state *wm_state = &crtc->wm_state;
1285 enum pipe pipe = crtc->pipe;
1286
1287 if (!crtc->active)
1288 continue;
1289
1290 wm->pipe[pipe] = wm_state->wm[wm->level];
1291 if (wm->cxsr)
1292 wm->sr = wm_state->sr[wm->level];
1293
1294 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1295 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1296 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1297 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1298 }
1299}
1300
1301static void vlv_update_wm(struct drm_crtc *crtc)
1302{
1303 struct drm_device *dev = crtc->dev;
1304 struct drm_i915_private *dev_priv = dev->dev_private;
1305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1306 enum pipe pipe = intel_crtc->pipe;
1307 struct vlv_wm_values wm = {};
1308
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001309 vlv_compute_wm(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001310 vlv_merge_wm(dev, &wm);
1311
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001312 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1313 /* FIXME should be part of crtc atomic commit */
1314 vlv_pipe_set_fifo_size(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001315 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001316 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001317
1318 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1319 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1320 chv_set_memory_dvfs(dev_priv, false);
1321
1322 if (wm.level < VLV_WM_LEVEL_PM5 &&
1323 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1324 chv_set_memory_pm5(dev_priv, false);
1325
Ville Syrjälä852eb002015-06-24 22:00:07 +03001326 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001327 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001328
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001329 /* FIXME should be part of crtc atomic commit */
1330 vlv_pipe_set_fifo_size(intel_crtc);
1331
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001332 vlv_write_wm_values(intel_crtc, &wm);
1333
1334 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1335 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1336 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1337 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1338 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1339
Ville Syrjälä852eb002015-06-24 22:00:07 +03001340 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001341 intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001342
1343 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1344 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1345 chv_set_memory_pm5(dev_priv, true);
1346
1347 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1348 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1349 chv_set_memory_dvfs(dev_priv, true);
1350
1351 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001352}
1353
Ville Syrjäläae801522015-03-05 21:19:49 +02001354#define single_plane_enabled(mask) is_power_of_2(mask)
1355
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001356static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001357{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001358 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001359 static const int sr_latency_ns = 12000;
1360 struct drm_i915_private *dev_priv = dev->dev_private;
1361 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1362 int plane_sr, cursor_sr;
1363 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001364 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001365
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001366 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001367 &g4x_wm_info, pessimal_latency_ns,
1368 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001369 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001370 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001371
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001372 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001373 &g4x_wm_info, pessimal_latency_ns,
1374 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001375 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001376 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001377
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001378 if (single_plane_enabled(enabled) &&
1379 g4x_compute_srwm(dev, ffs(enabled) - 1,
1380 sr_latency_ns,
1381 &g4x_wm_info,
1382 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001383 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001384 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001385 } else {
Imre Deak98584252014-06-13 14:54:20 +03001386 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001387 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001388 plane_sr = cursor_sr = 0;
1389 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001390
Ville Syrjäläa5043452014-06-28 02:04:18 +03001391 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1392 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001393 planea_wm, cursora_wm,
1394 planeb_wm, cursorb_wm,
1395 plane_sr, cursor_sr);
1396
1397 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001398 FW_WM(plane_sr, SR) |
1399 FW_WM(cursorb_wm, CURSORB) |
1400 FW_WM(planeb_wm, PLANEB) |
1401 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001402 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001403 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001404 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001405 /* HPLL off in SR has some issues on G4x... disable it */
1406 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001407 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001408 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001409
1410 if (cxsr_enabled)
1411 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001412}
1413
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001414static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001416 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001417 struct drm_i915_private *dev_priv = dev->dev_private;
1418 struct drm_crtc *crtc;
1419 int srwm = 1;
1420 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001421 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001422
1423 /* Calc sr entries for one plane configs */
1424 crtc = single_enabled_crtc(dev);
1425 if (crtc) {
1426 /* self-refresh has much higher latency */
1427 static const int sr_latency_ns = 12000;
Ville Syrjälä124abe02015-09-08 13:40:45 +03001428 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001429 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001430 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001431 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +02001432 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001433 unsigned long line_time_us;
1434 int entries;
1435
Ville Syrjälä922044c2014-02-14 14:18:57 +02001436 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001437
1438 /* Use ns/us then divide to preserve precision */
1439 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001440 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001441 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1442 srwm = I965_FIFO_SIZE - entries;
1443 if (srwm < 0)
1444 srwm = 1;
1445 srwm &= 0x1ff;
1446 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1447 entries, srwm);
1448
1449 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001450 cpp * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001451 entries = DIV_ROUND_UP(entries,
1452 i965_cursor_wm_info.cacheline_size);
1453 cursor_sr = i965_cursor_wm_info.fifo_size -
1454 (entries + i965_cursor_wm_info.guard_size);
1455
1456 if (cursor_sr > i965_cursor_wm_info.max_wm)
1457 cursor_sr = i965_cursor_wm_info.max_wm;
1458
1459 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1460 "cursor %d\n", srwm, cursor_sr);
1461
Imre Deak98584252014-06-13 14:54:20 +03001462 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001463 } else {
Imre Deak98584252014-06-13 14:54:20 +03001464 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001465 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001466 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001467 }
1468
1469 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1470 srwm);
1471
1472 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001473 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1474 FW_WM(8, CURSORB) |
1475 FW_WM(8, PLANEB) |
1476 FW_WM(8, PLANEA));
1477 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1478 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001479 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001480 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001481
1482 if (cxsr_enabled)
1483 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001484}
1485
Ville Syrjäläf4998962015-03-10 17:02:21 +02001486#undef FW_WM
1487
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001488static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001489{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001490 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001491 struct drm_i915_private *dev_priv = dev->dev_private;
1492 const struct intel_watermark_params *wm_info;
1493 uint32_t fwater_lo;
1494 uint32_t fwater_hi;
1495 int cwm, srwm = 1;
1496 int fifo_size;
1497 int planea_wm, planeb_wm;
1498 struct drm_crtc *crtc, *enabled = NULL;
1499
1500 if (IS_I945GM(dev))
1501 wm_info = &i945_wm_info;
1502 else if (!IS_GEN2(dev))
1503 wm_info = &i915_wm_info;
1504 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001505 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001506
1507 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1508 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001509 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001510 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001511 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001512 if (IS_GEN2(dev))
1513 cpp = 4;
1514
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001515 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001516 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001517 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001518 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001519 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001520 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001521 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001522 if (planea_wm > (long)wm_info->max_wm)
1523 planea_wm = wm_info->max_wm;
1524 }
1525
1526 if (IS_GEN2(dev))
1527 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001528
1529 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1530 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001531 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001532 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001533 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001534 if (IS_GEN2(dev))
1535 cpp = 4;
1536
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001537 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001538 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001539 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001540 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001541 if (enabled == NULL)
1542 enabled = crtc;
1543 else
1544 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001545 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001546 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001547 if (planeb_wm > (long)wm_info->max_wm)
1548 planeb_wm = wm_info->max_wm;
1549 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001550
1551 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1552
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001553 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001554 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001555
Matt Roper59bea882015-02-27 10:12:01 -08001556 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001557
1558 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001559 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001560 enabled = NULL;
1561 }
1562
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001563 /*
1564 * Overlay gets an aggressive default since video jitter is bad.
1565 */
1566 cwm = 2;
1567
1568 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001569 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001570
1571 /* Calc sr entries for one plane configs */
1572 if (HAS_FW_BLC(dev) && enabled) {
1573 /* self-refresh has much higher latency */
1574 static const int sr_latency_ns = 6000;
Ville Syrjälä124abe02015-09-08 13:40:45 +03001575 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001576 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001577 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001578 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Ville Syrjäläac484962016-01-20 21:05:26 +02001579 int cpp = drm_format_plane_cpp(enabled->primary->state->fb->pixel_format, 0);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001580 unsigned long line_time_us;
1581 int entries;
1582
Ville Syrjälä922044c2014-02-14 14:18:57 +02001583 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001584
1585 /* Use ns/us then divide to preserve precision */
1586 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001587 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001588 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1589 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1590 srwm = wm_info->fifo_size - entries;
1591 if (srwm < 0)
1592 srwm = 1;
1593
1594 if (IS_I945G(dev) || IS_I945GM(dev))
1595 I915_WRITE(FW_BLC_SELF,
1596 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1597 else if (IS_I915GM(dev))
1598 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1599 }
1600
1601 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1602 planea_wm, planeb_wm, cwm, srwm);
1603
1604 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1605 fwater_hi = (cwm & 0x1f);
1606
1607 /* Set request length to 8 cachelines per fetch */
1608 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1609 fwater_hi = fwater_hi | (1 << 8);
1610
1611 I915_WRITE(FW_BLC, fwater_lo);
1612 I915_WRITE(FW_BLC2, fwater_hi);
1613
Imre Deak5209b1f2014-07-01 12:36:17 +03001614 if (enabled)
1615 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001616}
1617
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001618static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001619{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001620 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001621 struct drm_i915_private *dev_priv = dev->dev_private;
1622 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001623 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001624 uint32_t fwater_lo;
1625 int planea_wm;
1626
1627 crtc = single_enabled_crtc(dev);
1628 if (crtc == NULL)
1629 return;
1630
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001631 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001632 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001633 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001634 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001635 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001636 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1637 fwater_lo |= (3<<8) | planea_wm;
1638
1639 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1640
1641 I915_WRITE(FW_BLC, fwater_lo);
1642}
1643
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001644uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001645{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001646 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001647
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001648 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001649
1650 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1651 * adjust the pixel_rate here. */
1652
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001653 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001654 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001655 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001656
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001657 pipe_w = pipe_config->pipe_src_w;
1658 pipe_h = pipe_config->pipe_src_h;
1659
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001660 pfit_w = (pfit_size >> 16) & 0xFFFF;
1661 pfit_h = pfit_size & 0xFFFF;
1662 if (pipe_w < pfit_w)
1663 pipe_w = pfit_w;
1664 if (pipe_h < pfit_h)
1665 pipe_h = pfit_h;
1666
Matt Roper15126882015-12-03 11:37:40 -08001667 if (WARN_ON(!pfit_w || !pfit_h))
1668 return pixel_rate;
1669
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001670 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1671 pfit_w * pfit_h);
1672 }
1673
1674 return pixel_rate;
1675}
1676
Ville Syrjälä37126462013-08-01 16:18:55 +03001677/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001678static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001679{
1680 uint64_t ret;
1681
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001682 if (WARN(latency == 0, "Latency value missing\n"))
1683 return UINT_MAX;
1684
Ville Syrjäläac484962016-01-20 21:05:26 +02001685 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001686 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1687
1688 return ret;
1689}
1690
Ville Syrjälä37126462013-08-01 16:18:55 +03001691/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001692static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001693 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001694 uint32_t latency)
1695{
1696 uint32_t ret;
1697
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001698 if (WARN(latency == 0, "Latency value missing\n"))
1699 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001700 if (WARN_ON(!pipe_htotal))
1701 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001702
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001703 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001704 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001705 ret = DIV_ROUND_UP(ret, 64) + 2;
1706 return ret;
1707}
1708
Ville Syrjälä23297042013-07-05 11:57:17 +03001709static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001710 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001711{
Matt Roper15126882015-12-03 11:37:40 -08001712 /*
1713 * Neither of these should be possible since this function shouldn't be
1714 * called if the CRTC is off or the plane is invisible. But let's be
1715 * extra paranoid to avoid a potential divide-by-zero if we screw up
1716 * elsewhere in the driver.
1717 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001718 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001719 return 0;
1720 if (WARN_ON(!horiz_pixels))
1721 return 0;
1722
Ville Syrjäläac484962016-01-20 21:05:26 +02001723 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001724}
1725
Imre Deak820c1982013-12-17 14:46:36 +02001726struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001727 uint16_t pri;
1728 uint16_t spr;
1729 uint16_t cur;
1730 uint16_t fbc;
1731};
1732
Ville Syrjälä37126462013-08-01 16:18:55 +03001733/*
1734 * For both WM_PIPE and WM_LP.
1735 * mem_value must be in 0.1us units.
1736 */
Matt Roper7221fc32015-09-24 15:53:08 -07001737static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001738 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001739 uint32_t mem_value,
1740 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001741{
Ville Syrjäläac484962016-01-20 21:05:26 +02001742 int cpp = pstate->base.fb ?
1743 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001744 uint32_t method1, method2;
1745
Matt Roper7221fc32015-09-24 15:53:08 -07001746 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001747 return 0;
1748
Ville Syrjäläac484962016-01-20 21:05:26 +02001749 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001750
1751 if (!is_lp)
1752 return method1;
1753
Matt Roper7221fc32015-09-24 15:53:08 -07001754 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1755 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001756 drm_rect_width(&pstate->dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001757 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001758
1759 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001760}
1761
Ville Syrjälä37126462013-08-01 16:18:55 +03001762/*
1763 * For both WM_PIPE and WM_LP.
1764 * mem_value must be in 0.1us units.
1765 */
Matt Roper7221fc32015-09-24 15:53:08 -07001766static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001767 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001768 uint32_t mem_value)
1769{
Ville Syrjäläac484962016-01-20 21:05:26 +02001770 int cpp = pstate->base.fb ?
1771 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001772 uint32_t method1, method2;
1773
Matt Roper7221fc32015-09-24 15:53:08 -07001774 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001775 return 0;
1776
Ville Syrjäläac484962016-01-20 21:05:26 +02001777 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
Matt Roper7221fc32015-09-24 15:53:08 -07001778 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1779 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001780 drm_rect_width(&pstate->dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001781 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001782 return min(method1, method2);
1783}
1784
Ville Syrjälä37126462013-08-01 16:18:55 +03001785/*
1786 * For both WM_PIPE and WM_LP.
1787 * mem_value must be in 0.1us units.
1788 */
Matt Roper7221fc32015-09-24 15:53:08 -07001789static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001790 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001791 uint32_t mem_value)
1792{
Matt Roperb2435692016-02-02 22:06:51 -08001793 /*
1794 * We treat the cursor plane as always-on for the purposes of watermark
1795 * calculation. Until we have two-stage watermark programming merged,
1796 * this is necessary to avoid flickering.
1797 */
1798 int cpp = 4;
1799 int width = pstate->visible ? pstate->base.crtc_w : 64;
Matt Roper43d59ed2015-09-24 15:53:07 -07001800
Matt Roperb2435692016-02-02 22:06:51 -08001801 if (!cstate->base.active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001802 return 0;
1803
Matt Roper7221fc32015-09-24 15:53:08 -07001804 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1805 cstate->base.adjusted_mode.crtc_htotal,
Matt Roperb2435692016-02-02 22:06:51 -08001806 width, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001807}
1808
Paulo Zanonicca32e92013-05-31 11:45:06 -03001809/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07001810static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001811 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001812 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001813{
Ville Syrjäläac484962016-01-20 21:05:26 +02001814 int cpp = pstate->base.fb ?
1815 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
Matt Roper43d59ed2015-09-24 15:53:07 -07001816
Matt Roper7221fc32015-09-24 15:53:08 -07001817 if (!cstate->base.active || !pstate->visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001818 return 0;
1819
Ville Syrjäläac484962016-01-20 21:05:26 +02001820 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001821}
1822
Ville Syrjälä158ae642013-08-07 13:28:19 +03001823static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1824{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001825 if (INTEL_INFO(dev)->gen >= 8)
1826 return 3072;
1827 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001828 return 768;
1829 else
1830 return 512;
1831}
1832
Ville Syrjälä4e975082014-03-07 18:32:11 +02001833static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1834 int level, bool is_sprite)
1835{
1836 if (INTEL_INFO(dev)->gen >= 8)
1837 /* BDW primary/sprite plane watermarks */
1838 return level == 0 ? 255 : 2047;
1839 else if (INTEL_INFO(dev)->gen >= 7)
1840 /* IVB/HSW primary/sprite plane watermarks */
1841 return level == 0 ? 127 : 1023;
1842 else if (!is_sprite)
1843 /* ILK/SNB primary plane watermarks */
1844 return level == 0 ? 127 : 511;
1845 else
1846 /* ILK/SNB sprite plane watermarks */
1847 return level == 0 ? 63 : 255;
1848}
1849
1850static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1851 int level)
1852{
1853 if (INTEL_INFO(dev)->gen >= 7)
1854 return level == 0 ? 63 : 255;
1855 else
1856 return level == 0 ? 31 : 63;
1857}
1858
1859static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1860{
1861 if (INTEL_INFO(dev)->gen >= 8)
1862 return 31;
1863 else
1864 return 15;
1865}
1866
Ville Syrjälä158ae642013-08-07 13:28:19 +03001867/* Calculate the maximum primary/sprite plane watermark */
1868static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1869 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001870 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001871 enum intel_ddb_partitioning ddb_partitioning,
1872 bool is_sprite)
1873{
1874 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001875
1876 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001877 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001878 return 0;
1879
1880 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001881 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001882 fifo_size /= INTEL_INFO(dev)->num_pipes;
1883
1884 /*
1885 * For some reason the non self refresh
1886 * FIFO size is only half of the self
1887 * refresh FIFO size on ILK/SNB.
1888 */
1889 if (INTEL_INFO(dev)->gen <= 6)
1890 fifo_size /= 2;
1891 }
1892
Ville Syrjälä240264f2013-08-07 13:29:12 +03001893 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001894 /* level 0 is always calculated with 1:1 split */
1895 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1896 if (is_sprite)
1897 fifo_size *= 5;
1898 fifo_size /= 6;
1899 } else {
1900 fifo_size /= 2;
1901 }
1902 }
1903
1904 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001905 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001906}
1907
1908/* Calculate the maximum cursor plane watermark */
1909static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001910 int level,
1911 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001912{
1913 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001914 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001915 return 64;
1916
1917 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001918 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001919}
1920
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001921static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001922 int level,
1923 const struct intel_wm_config *config,
1924 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001925 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001926{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001927 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1928 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1929 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001930 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001931}
1932
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001933static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1934 int level,
1935 struct ilk_wm_maximums *max)
1936{
1937 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1938 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1939 max->cur = ilk_cursor_wm_reg_max(dev, level);
1940 max->fbc = ilk_fbc_wm_reg_max(dev);
1941}
1942
Ville Syrjäläd9395652013-10-09 19:18:10 +03001943static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001944 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001945 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001946{
1947 bool ret;
1948
1949 /* already determined to be invalid? */
1950 if (!result->enable)
1951 return false;
1952
1953 result->enable = result->pri_val <= max->pri &&
1954 result->spr_val <= max->spr &&
1955 result->cur_val <= max->cur;
1956
1957 ret = result->enable;
1958
1959 /*
1960 * HACK until we can pre-compute everything,
1961 * and thus fail gracefully if LP0 watermarks
1962 * are exceeded...
1963 */
1964 if (level == 0 && !result->enable) {
1965 if (result->pri_val > max->pri)
1966 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1967 level, result->pri_val, max->pri);
1968 if (result->spr_val > max->spr)
1969 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1970 level, result->spr_val, max->spr);
1971 if (result->cur_val > max->cur)
1972 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1973 level, result->cur_val, max->cur);
1974
1975 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1976 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1977 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1978 result->enable = true;
1979 }
1980
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001981 return ret;
1982}
1983
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001984static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07001985 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001986 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07001987 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07001988 struct intel_plane_state *pristate,
1989 struct intel_plane_state *sprstate,
1990 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001991 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001992{
1993 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1994 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1995 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1996
1997 /* WM1+ latency values stored in 0.5us units */
1998 if (level > 0) {
1999 pri_latency *= 5;
2000 spr_latency *= 5;
2001 cur_latency *= 5;
2002 }
2003
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002004 if (pristate) {
2005 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2006 pri_latency, level);
2007 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2008 }
2009
2010 if (sprstate)
2011 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2012
2013 if (curstate)
2014 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2015
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002016 result->enable = true;
2017}
2018
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002019static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002020hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002021{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002022 const struct intel_atomic_state *intel_state =
2023 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002024 const struct drm_display_mode *adjusted_mode =
2025 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002026 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002027
Matt Roperee91a152015-12-03 11:37:39 -08002028 if (!cstate->base.active)
2029 return 0;
2030 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2031 return 0;
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002032 if (WARN_ON(intel_state->cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002033 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002034
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002035 /* The WM are computed with base on how long it takes to fill a single
2036 * row at the given clock rate, multiplied by 8.
2037 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002038 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2039 adjusted_mode->crtc_clock);
2040 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002041 intel_state->cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002042
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002043 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2044 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002045}
2046
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002047static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002048{
2049 struct drm_i915_private *dev_priv = dev->dev_private;
2050
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002051 if (IS_GEN9(dev)) {
2052 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002053 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00002054 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002055
2056 /* read the first set of memory latencies[0:3] */
2057 val = 0; /* data0 to be programmed to 0 for first set */
2058 mutex_lock(&dev_priv->rps.hw_lock);
2059 ret = sandybridge_pcode_read(dev_priv,
2060 GEN9_PCODE_READ_MEM_LATENCY,
2061 &val);
2062 mutex_unlock(&dev_priv->rps.hw_lock);
2063
2064 if (ret) {
2065 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2066 return;
2067 }
2068
2069 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2070 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2071 GEN9_MEM_LATENCY_LEVEL_MASK;
2072 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2073 GEN9_MEM_LATENCY_LEVEL_MASK;
2074 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2075 GEN9_MEM_LATENCY_LEVEL_MASK;
2076
2077 /* read the second set of memory latencies[4:7] */
2078 val = 1; /* data0 to be programmed to 1 for second set */
2079 mutex_lock(&dev_priv->rps.hw_lock);
2080 ret = sandybridge_pcode_read(dev_priv,
2081 GEN9_PCODE_READ_MEM_LATENCY,
2082 &val);
2083 mutex_unlock(&dev_priv->rps.hw_lock);
2084 if (ret) {
2085 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2086 return;
2087 }
2088
2089 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2090 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2091 GEN9_MEM_LATENCY_LEVEL_MASK;
2092 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2093 GEN9_MEM_LATENCY_LEVEL_MASK;
2094 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2095 GEN9_MEM_LATENCY_LEVEL_MASK;
2096
Vandana Kannan367294b2014-11-04 17:06:46 +00002097 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002098 * WaWmMemoryReadLatency:skl
2099 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002100 * punit doesn't take into account the read latency so we need
2101 * to add 2us to the various latency levels we retrieve from
2102 * the punit.
2103 * - W0 is a bit special in that it's the only level that
2104 * can't be disabled if we want to have display working, so
2105 * we always add 2us there.
2106 * - For levels >=1, punit returns 0us latency when they are
2107 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00002108 *
2109 * Additionally, if a level n (n > 1) has a 0us latency, all
2110 * levels m (m >= n) need to be disabled. We make sure to
2111 * sanitize the values out of the punit to satisfy this
2112 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00002113 */
2114 wm[0] += 2;
2115 for (level = 1; level <= max_level; level++)
2116 if (wm[level] != 0)
2117 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002118 else {
2119 for (i = level + 1; i <= max_level; i++)
2120 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00002121
Vandana Kannan4f947382014-11-04 17:06:47 +00002122 break;
2123 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002124 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002125 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2126
2127 wm[0] = (sskpd >> 56) & 0xFF;
2128 if (wm[0] == 0)
2129 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002130 wm[1] = (sskpd >> 4) & 0xFF;
2131 wm[2] = (sskpd >> 12) & 0xFF;
2132 wm[3] = (sskpd >> 20) & 0x1FF;
2133 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002134 } else if (INTEL_INFO(dev)->gen >= 6) {
2135 uint32_t sskpd = I915_READ(MCH_SSKPD);
2136
2137 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2138 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2139 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2140 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002141 } else if (INTEL_INFO(dev)->gen >= 5) {
2142 uint32_t mltr = I915_READ(MLTR_ILK);
2143
2144 /* ILK primary LP0 latency is 700 ns */
2145 wm[0] = 7;
2146 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2147 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002148 }
2149}
2150
Ville Syrjälä53615a52013-08-01 16:18:50 +03002151static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2152{
2153 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002154 if (IS_GEN5(dev))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002155 wm[0] = 13;
2156}
2157
2158static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2159{
2160 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulin7e22dbb2016-05-10 10:57:06 +01002161 if (IS_GEN5(dev))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002162 wm[0] = 13;
2163
2164 /* WaDoubleCursorLP3Latency:ivb */
2165 if (IS_IVYBRIDGE(dev))
2166 wm[3] *= 2;
2167}
2168
Damien Lespiau546c81f2014-05-13 15:30:26 +01002169int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002170{
2171 /* how many WM levels are we expecting */
Damien Lespiaub6e742f2015-05-09 02:05:55 +01002172 if (INTEL_INFO(dev)->gen >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002173 return 7;
2174 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002175 return 4;
2176 else if (INTEL_INFO(dev)->gen >= 6)
2177 return 3;
2178 else
2179 return 2;
2180}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002181
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002182static void intel_print_wm_latency(struct drm_device *dev,
2183 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002184 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002185{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002186 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002187
2188 for (level = 0; level <= max_level; level++) {
2189 unsigned int latency = wm[level];
2190
2191 if (latency == 0) {
2192 DRM_ERROR("%s WM%d latency not provided\n",
2193 name, level);
2194 continue;
2195 }
2196
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002197 /*
2198 * - latencies are in us on gen9.
2199 * - before then, WM1+ latency values are in 0.5us units
2200 */
2201 if (IS_GEN9(dev))
2202 latency *= 10;
2203 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002204 latency *= 5;
2205
2206 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2207 name, level, wm[level],
2208 latency / 10, latency % 10);
2209 }
2210}
2211
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002212static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2213 uint16_t wm[5], uint16_t min)
2214{
2215 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2216
2217 if (wm[0] >= min)
2218 return false;
2219
2220 wm[0] = max(wm[0], min);
2221 for (level = 1; level <= max_level; level++)
2222 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2223
2224 return true;
2225}
2226
2227static void snb_wm_latency_quirk(struct drm_device *dev)
2228{
2229 struct drm_i915_private *dev_priv = dev->dev_private;
2230 bool changed;
2231
2232 /*
2233 * The BIOS provided WM memory latency values are often
2234 * inadequate for high resolution displays. Adjust them.
2235 */
2236 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2237 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2238 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2239
2240 if (!changed)
2241 return;
2242
2243 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2244 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2245 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2246 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2247}
2248
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002249static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002250{
2251 struct drm_i915_private *dev_priv = dev->dev_private;
2252
2253 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2254
2255 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2256 sizeof(dev_priv->wm.pri_latency));
2257 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2258 sizeof(dev_priv->wm.pri_latency));
2259
2260 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2261 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002262
2263 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2264 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2265 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002266
2267 if (IS_GEN6(dev))
2268 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002269}
2270
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002271static void skl_setup_wm_latency(struct drm_device *dev)
2272{
2273 struct drm_i915_private *dev_priv = dev->dev_private;
2274
2275 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2276 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2277}
2278
Matt Ropered4a6a72016-02-23 17:20:13 -08002279static bool ilk_validate_pipe_wm(struct drm_device *dev,
2280 struct intel_pipe_wm *pipe_wm)
2281{
2282 /* LP0 watermark maximums depend on this pipe alone */
2283 const struct intel_wm_config config = {
2284 .num_pipes_active = 1,
2285 .sprites_enabled = pipe_wm->sprites_enabled,
2286 .sprites_scaled = pipe_wm->sprites_scaled,
2287 };
2288 struct ilk_wm_maximums max;
2289
2290 /* LP0 watermarks always use 1/2 DDB partitioning */
2291 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2292
2293 /* At least LP0 must be valid */
2294 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2295 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2296 return false;
2297 }
2298
2299 return true;
2300}
2301
Matt Roper261a27d2015-10-08 15:28:25 -07002302/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002303static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002304{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002305 struct drm_atomic_state *state = cstate->base.state;
2306 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002307 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002308 struct drm_device *dev = state->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002309 const struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper43d59ed2015-09-24 15:53:07 -07002310 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002311 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002312 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002313 struct intel_plane_state *curstate = NULL;
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002314 int level, max_level = ilk_wm_max_level(dev), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002315 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002316
Matt Ropere8f1f022016-05-12 07:05:55 -07002317 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002318
Matt Roper43d59ed2015-09-24 15:53:07 -07002319 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002320 struct intel_plane_state *ps;
2321
2322 ps = intel_atomic_get_existing_plane_state(state,
2323 intel_plane);
2324 if (!ps)
2325 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002326
2327 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002328 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002329 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002330 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002331 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002332 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002333 }
2334
Matt Ropered4a6a72016-02-23 17:20:13 -08002335 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002336 if (sprstate) {
2337 pipe_wm->sprites_enabled = sprstate->visible;
2338 pipe_wm->sprites_scaled = sprstate->visible &&
2339 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2340 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2341 }
2342
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002343 usable_level = max_level;
2344
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002345 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002346 if (INTEL_INFO(dev)->gen <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002347 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002348
2349 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002350 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002351 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002352
Matt Roper86c8bbb2015-09-24 15:53:16 -07002353 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002354 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2355
2356 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2357 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002358
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002359 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002360 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002361
Matt Ropered4a6a72016-02-23 17:20:13 -08002362 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002363 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002364
2365 ilk_compute_wm_reg_maximums(dev, 1, &max);
2366
2367 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002368 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002369
Matt Roper86c8bbb2015-09-24 15:53:16 -07002370 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002371 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002372
2373 /*
2374 * Disable any watermark level that exceeds the
2375 * register maximums since such watermarks are
2376 * always invalid.
2377 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002378 if (level > usable_level)
2379 continue;
2380
2381 if (ilk_validate_wm_level(level, &max, wm))
2382 pipe_wm->wm[level] = *wm;
2383 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002384 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002385 }
2386
Matt Roper86c8bbb2015-09-24 15:53:16 -07002387 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002388}
2389
2390/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002391 * Build a set of 'intermediate' watermark values that satisfy both the old
2392 * state and the new state. These can be programmed to the hardware
2393 * immediately.
2394 */
2395static int ilk_compute_intermediate_wm(struct drm_device *dev,
2396 struct intel_crtc *intel_crtc,
2397 struct intel_crtc_state *newstate)
2398{
Matt Ropere8f1f022016-05-12 07:05:55 -07002399 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002400 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
2401 int level, max_level = ilk_wm_max_level(dev);
2402
2403 /*
2404 * Start with the final, target watermarks, then combine with the
2405 * currently active watermarks to get values that are safe both before
2406 * and after the vblank.
2407 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002408 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002409 a->pipe_enabled |= b->pipe_enabled;
2410 a->sprites_enabled |= b->sprites_enabled;
2411 a->sprites_scaled |= b->sprites_scaled;
2412
2413 for (level = 0; level <= max_level; level++) {
2414 struct intel_wm_level *a_wm = &a->wm[level];
2415 const struct intel_wm_level *b_wm = &b->wm[level];
2416
2417 a_wm->enable &= b_wm->enable;
2418 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2419 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2420 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2421 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2422 }
2423
2424 /*
2425 * We need to make sure that these merged watermark values are
2426 * actually a valid configuration themselves. If they're not,
2427 * there's no safe way to transition from the old state to
2428 * the new state, so we need to fail the atomic transaction.
2429 */
2430 if (!ilk_validate_pipe_wm(dev, a))
2431 return -EINVAL;
2432
2433 /*
2434 * If our intermediate WM are identical to the final WM, then we can
2435 * omit the post-vblank programming; only update if it's different.
2436 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002437 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) == 0)
Matt Ropered4a6a72016-02-23 17:20:13 -08002438 newstate->wm.need_postvbl_update = false;
2439
2440 return 0;
2441}
2442
2443/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002444 * Merge the watermarks from all active pipes for a specific level.
2445 */
2446static void ilk_merge_wm_level(struct drm_device *dev,
2447 int level,
2448 struct intel_wm_level *ret_wm)
2449{
2450 const struct intel_crtc *intel_crtc;
2451
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002452 ret_wm->enable = true;
2453
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002454 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002455 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002456 const struct intel_wm_level *wm = &active->wm[level];
2457
2458 if (!active->pipe_enabled)
2459 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002460
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002461 /*
2462 * The watermark values may have been used in the past,
2463 * so we must maintain them in the registers for some
2464 * time even if the level is now disabled.
2465 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002466 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002467 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002468
2469 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2470 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2471 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2472 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2473 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002474}
2475
2476/*
2477 * Merge all low power watermarks for all active pipes.
2478 */
2479static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002480 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002481 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002482 struct intel_pipe_wm *merged)
2483{
Paulo Zanoni7733b492015-07-07 15:26:04 -03002484 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002485 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002486 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002487
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002488 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2489 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2490 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002491 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002492
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002493 /* ILK: FBC WM must be disabled always */
2494 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002495
2496 /* merge each WM1+ level */
2497 for (level = 1; level <= max_level; level++) {
2498 struct intel_wm_level *wm = &merged->wm[level];
2499
2500 ilk_merge_wm_level(dev, level, wm);
2501
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002502 if (level > last_enabled_level)
2503 wm->enable = false;
2504 else if (!ilk_validate_wm_level(level, max, wm))
2505 /* make sure all following levels get disabled */
2506 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002507
2508 /*
2509 * The spec says it is preferred to disable
2510 * FBC WMs instead of disabling a WM level.
2511 */
2512 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002513 if (wm->enable)
2514 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002515 wm->fbc_val = 0;
2516 }
2517 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002518
2519 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2520 /*
2521 * FIXME this is racy. FBC might get enabled later.
2522 * What we should check here is whether FBC can be
2523 * enabled sometime later.
2524 */
Paulo Zanoni7733b492015-07-07 15:26:04 -03002525 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002526 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002527 for (level = 2; level <= max_level; level++) {
2528 struct intel_wm_level *wm = &merged->wm[level];
2529
2530 wm->enable = false;
2531 }
2532 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002533}
2534
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002535static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2536{
2537 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2538 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2539}
2540
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002541/* The value we need to program into the WM_LPx latency field */
2542static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2543{
2544 struct drm_i915_private *dev_priv = dev->dev_private;
2545
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002546 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002547 return 2 * level;
2548 else
2549 return dev_priv->wm.pri_latency[level];
2550}
2551
Imre Deak820c1982013-12-17 14:46:36 +02002552static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002553 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002554 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002555 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002556{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002557 struct intel_crtc *intel_crtc;
2558 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002559
Ville Syrjälä0362c782013-10-09 19:17:57 +03002560 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002561 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002562
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002563 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002564 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002565 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002566
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002567 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002568
Ville Syrjälä0362c782013-10-09 19:17:57 +03002569 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002570
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002571 /*
2572 * Maintain the watermark values even if the level is
2573 * disabled. Doing otherwise could cause underruns.
2574 */
2575 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002576 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002577 (r->pri_val << WM1_LP_SR_SHIFT) |
2578 r->cur_val;
2579
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002580 if (r->enable)
2581 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2582
Ville Syrjälä416f4722013-11-02 21:07:46 -07002583 if (INTEL_INFO(dev)->gen >= 8)
2584 results->wm_lp[wm_lp - 1] |=
2585 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2586 else
2587 results->wm_lp[wm_lp - 1] |=
2588 r->fbc_val << WM1_LP_FBC_SHIFT;
2589
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002590 /*
2591 * Always set WM1S_LP_EN when spr_val != 0, even if the
2592 * level is disabled. Doing otherwise could cause underruns.
2593 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002594 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2595 WARN_ON(wm_lp != 1);
2596 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2597 } else
2598 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002599 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002600
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002601 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002602 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002603 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002604 const struct intel_wm_level *r =
2605 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002606
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002607 if (WARN_ON(!r->enable))
2608 continue;
2609
Matt Ropered4a6a72016-02-23 17:20:13 -08002610 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002611
2612 results->wm_pipe[pipe] =
2613 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2614 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2615 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002616 }
2617}
2618
Paulo Zanoni861f3382013-05-31 10:19:21 -03002619/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2620 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002621static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002622 struct intel_pipe_wm *r1,
2623 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002624{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002625 int level, max_level = ilk_wm_max_level(dev);
2626 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002627
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002628 for (level = 1; level <= max_level; level++) {
2629 if (r1->wm[level].enable)
2630 level1 = level;
2631 if (r2->wm[level].enable)
2632 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002633 }
2634
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002635 if (level1 == level2) {
2636 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002637 return r2;
2638 else
2639 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002640 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002641 return r1;
2642 } else {
2643 return r2;
2644 }
2645}
2646
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002647/* dirty bits used to track which watermarks need changes */
2648#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2649#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2650#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2651#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2652#define WM_DIRTY_FBC (1 << 24)
2653#define WM_DIRTY_DDB (1 << 25)
2654
Damien Lespiau055e3932014-08-18 13:49:10 +01002655static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002656 const struct ilk_wm_values *old,
2657 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002658{
2659 unsigned int dirty = 0;
2660 enum pipe pipe;
2661 int wm_lp;
2662
Damien Lespiau055e3932014-08-18 13:49:10 +01002663 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002664 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2665 dirty |= WM_DIRTY_LINETIME(pipe);
2666 /* Must disable LP1+ watermarks too */
2667 dirty |= WM_DIRTY_LP_ALL;
2668 }
2669
2670 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2671 dirty |= WM_DIRTY_PIPE(pipe);
2672 /* Must disable LP1+ watermarks too */
2673 dirty |= WM_DIRTY_LP_ALL;
2674 }
2675 }
2676
2677 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2678 dirty |= WM_DIRTY_FBC;
2679 /* Must disable LP1+ watermarks too */
2680 dirty |= WM_DIRTY_LP_ALL;
2681 }
2682
2683 if (old->partitioning != new->partitioning) {
2684 dirty |= WM_DIRTY_DDB;
2685 /* Must disable LP1+ watermarks too */
2686 dirty |= WM_DIRTY_LP_ALL;
2687 }
2688
2689 /* LP1+ watermarks already deemed dirty, no need to continue */
2690 if (dirty & WM_DIRTY_LP_ALL)
2691 return dirty;
2692
2693 /* Find the lowest numbered LP1+ watermark in need of an update... */
2694 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2695 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2696 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2697 break;
2698 }
2699
2700 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2701 for (; wm_lp <= 3; wm_lp++)
2702 dirty |= WM_DIRTY_LP(wm_lp);
2703
2704 return dirty;
2705}
2706
Ville Syrjälä8553c182013-12-05 15:51:39 +02002707static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2708 unsigned int dirty)
2709{
Imre Deak820c1982013-12-17 14:46:36 +02002710 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002711 bool changed = false;
2712
2713 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2714 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2715 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2716 changed = true;
2717 }
2718 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2719 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2720 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2721 changed = true;
2722 }
2723 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2724 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2725 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2726 changed = true;
2727 }
2728
2729 /*
2730 * Don't touch WM1S_LP_EN here.
2731 * Doing so could cause underruns.
2732 */
2733
2734 return changed;
2735}
2736
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002737/*
2738 * The spec says we shouldn't write when we don't need, because every write
2739 * causes WMs to be re-evaluated, expending some power.
2740 */
Imre Deak820c1982013-12-17 14:46:36 +02002741static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2742 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002743{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002744 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002745 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002746 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002747 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002748
Damien Lespiau055e3932014-08-18 13:49:10 +01002749 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002750 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002751 return;
2752
Ville Syrjälä8553c182013-12-05 15:51:39 +02002753 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002754
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002755 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002756 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002757 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002758 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002759 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002760 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2761
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002762 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002763 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002764 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002765 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002766 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002767 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2768
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002769 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002770 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002771 val = I915_READ(WM_MISC);
2772 if (results->partitioning == INTEL_DDB_PART_1_2)
2773 val &= ~WM_MISC_DATA_PARTITION_5_6;
2774 else
2775 val |= WM_MISC_DATA_PARTITION_5_6;
2776 I915_WRITE(WM_MISC, val);
2777 } else {
2778 val = I915_READ(DISP_ARB_CTL2);
2779 if (results->partitioning == INTEL_DDB_PART_1_2)
2780 val &= ~DISP_DATA_PARTITION_5_6;
2781 else
2782 val |= DISP_DATA_PARTITION_5_6;
2783 I915_WRITE(DISP_ARB_CTL2, val);
2784 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002785 }
2786
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002787 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002788 val = I915_READ(DISP_ARB_CTL);
2789 if (results->enable_fbc_wm)
2790 val &= ~DISP_FBC_WM_DIS;
2791 else
2792 val |= DISP_FBC_WM_DIS;
2793 I915_WRITE(DISP_ARB_CTL, val);
2794 }
2795
Imre Deak954911e2013-12-17 14:46:34 +02002796 if (dirty & WM_DIRTY_LP(1) &&
2797 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2798 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2799
2800 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002801 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2802 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2803 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2804 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2805 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002806
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002807 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002808 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002809 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002810 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002811 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002812 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002813
2814 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002815}
2816
Matt Ropered4a6a72016-02-23 17:20:13 -08002817bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02002818{
2819 struct drm_i915_private *dev_priv = dev->dev_private;
2820
2821 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2822}
2823
Damien Lespiaub9cec072014-11-04 17:06:43 +00002824/*
2825 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2826 * different active planes.
2827 */
2828
2829#define SKL_DDB_SIZE 896 /* in blocks */
Damien Lespiau43d735a2015-03-17 11:39:34 +02002830#define BXT_DDB_SIZE 512
Damien Lespiaub9cec072014-11-04 17:06:43 +00002831
Matt Roper024c9042015-09-24 15:53:11 -07002832/*
2833 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2834 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2835 * other universal planes are in indices 1..n. Note that this may leave unused
2836 * indices between the top "sprite" plane and the cursor.
2837 */
2838static int
2839skl_wm_plane_id(const struct intel_plane *plane)
2840{
2841 switch (plane->base.type) {
2842 case DRM_PLANE_TYPE_PRIMARY:
2843 return 0;
2844 case DRM_PLANE_TYPE_CURSOR:
2845 return PLANE_CURSOR;
2846 case DRM_PLANE_TYPE_OVERLAY:
2847 return plane->plane + 1;
2848 default:
2849 MISSING_CASE(plane->base.type);
2850 return plane->plane;
2851 }
2852}
2853
Damien Lespiaub9cec072014-11-04 17:06:43 +00002854static void
2855skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07002856 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07002857 struct skl_ddb_entry *alloc, /* out */
2858 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002859{
Matt Roperc107acf2016-05-12 07:06:01 -07002860 struct drm_atomic_state *state = cstate->base.state;
2861 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
2862 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07002863 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002864 unsigned int pipe_size, ddb_size;
2865 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07002866 int pipe = to_intel_crtc(for_crtc)->pipe;
2867
Matt Ropera6d3460e2016-05-12 07:06:04 -07002868 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00002869 alloc->start = 0;
2870 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07002871 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002872 return;
2873 }
2874
Matt Ropera6d3460e2016-05-12 07:06:04 -07002875 if (intel_state->active_pipe_changes)
2876 *num_active = hweight32(intel_state->active_crtcs);
2877 else
2878 *num_active = hweight32(dev_priv->active_crtcs);
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
Matt Roperc107acf2016-05-12 07:06:01 -07002887 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07002888 * If the state doesn't change the active CRTC's, then there's
2889 * no need to recalculate; the existing pipe allocation limits
2890 * should remain unchanged. Note that we're safe from racing
2891 * commits since any racing commit that changes the active CRTC
2892 * list would need to grab _all_ crtc locks, including the one
2893 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07002894 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07002895 if (!intel_state->active_pipe_changes) {
2896 *alloc = dev_priv->wm.skl_hw.ddb.pipe[pipe];
2897 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002898 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07002899
2900 nth_active_pipe = hweight32(intel_state->active_crtcs &
2901 (drm_crtc_mask(for_crtc) - 1));
2902 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
2903 alloc->start = nth_active_pipe * ddb_size / *num_active;
2904 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002905}
2906
Matt Roperc107acf2016-05-12 07:06:01 -07002907static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002908{
Matt Roperc107acf2016-05-12 07:06:01 -07002909 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002910 return 32;
2911
2912 return 8;
2913}
2914
Damien Lespiaua269c582014-11-04 17:06:49 +00002915static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2916{
2917 entry->start = reg & 0x3ff;
2918 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002919 if (entry->end)
2920 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002921}
2922
Damien Lespiau08db6652014-11-04 17:06:52 +00002923void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2924 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002925{
Damien Lespiaua269c582014-11-04 17:06:49 +00002926 enum pipe pipe;
2927 int plane;
2928 u32 val;
2929
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02002930 memset(ddb, 0, sizeof(*ddb));
2931
Damien Lespiaua269c582014-11-04 17:06:49 +00002932 for_each_pipe(dev_priv, pipe) {
Imre Deak4d800032016-02-17 16:31:29 +02002933 enum intel_display_power_domain power_domain;
2934
2935 power_domain = POWER_DOMAIN_PIPE(pipe);
2936 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02002937 continue;
2938
Damien Lespiaudd740782015-02-28 14:54:08 +00002939 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002940 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2941 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2942 val);
2943 }
2944
2945 val = I915_READ(CUR_BUF_CFG(pipe));
Matt Roper4969d332015-09-24 15:53:10 -07002946 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2947 val);
Imre Deak4d800032016-02-17 16:31:29 +02002948
2949 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00002950 }
2951}
2952
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07002953/*
2954 * Determines the downscale amount of a plane for the purposes of watermark calculations.
2955 * The bspec defines downscale amount as:
2956 *
2957 * """
2958 * Horizontal down scale amount = maximum[1, Horizontal source size /
2959 * Horizontal destination size]
2960 * Vertical down scale amount = maximum[1, Vertical source size /
2961 * Vertical destination size]
2962 * Total down scale amount = Horizontal down scale amount *
2963 * Vertical down scale amount
2964 * """
2965 *
2966 * Return value is provided in 16.16 fixed point form to retain fractional part.
2967 * Caller should take care of dividing & rounding off the value.
2968 */
2969static uint32_t
2970skl_plane_downscale_amount(const struct intel_plane_state *pstate)
2971{
2972 uint32_t downscale_h, downscale_w;
2973 uint32_t src_w, src_h, dst_w, dst_h;
2974
2975 if (WARN_ON(!pstate->visible))
2976 return DRM_PLANE_HELPER_NO_SCALING;
2977
2978 /* n.b., src is 16.16 fixed point, dst is whole integer */
2979 src_w = drm_rect_width(&pstate->src);
2980 src_h = drm_rect_height(&pstate->src);
2981 dst_w = drm_rect_width(&pstate->dst);
2982 dst_h = drm_rect_height(&pstate->dst);
2983 if (intel_rotation_90_or_270(pstate->base.rotation))
2984 swap(dst_w, dst_h);
2985
2986 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
2987 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
2988
2989 /* Provide result in 16.16 fixed point */
2990 return (uint64_t)downscale_w * downscale_h >> 16;
2991}
2992
Damien Lespiaub9cec072014-11-04 17:06:43 +00002993static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07002994skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
2995 const struct drm_plane_state *pstate,
2996 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002997{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07002998 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Matt Roper024c9042015-09-24 15:53:11 -07002999 struct drm_framebuffer *fb = pstate->fb;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003000 uint32_t width = 0, height = 0;
Matt Ropera1de91e2016-05-12 07:05:57 -07003001 unsigned format = fb ? fb->pixel_format : DRM_FORMAT_XRGB8888;
3002
3003 if (!intel_pstate->visible)
3004 return 0;
3005 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3006 return 0;
3007 if (y && format != DRM_FORMAT_NV12)
3008 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003009
3010 width = drm_rect_width(&intel_pstate->src) >> 16;
3011 height = drm_rect_height(&intel_pstate->src) >> 16;
3012
3013 if (intel_rotation_90_or_270(pstate->rotation))
3014 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003015
3016 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003017 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003018 if (y) /* y-plane data rate */
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003019 return width * height *
Matt Ropera1de91e2016-05-12 07:05:57 -07003020 drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003021 else /* uv-plane data rate */
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003022 return (width / 2) * (height / 2) *
Matt Ropera1de91e2016-05-12 07:05:57 -07003023 drm_format_plane_cpp(format, 1);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003024 }
3025
3026 /* for packed formats */
Matt Ropera1de91e2016-05-12 07:05:57 -07003027 return width * height * drm_format_plane_cpp(format, 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003028}
3029
3030/*
3031 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3032 * a 8192x4096@32bpp framebuffer:
3033 * 3 * 4096 * 8192 * 4 < 2^32
3034 */
3035static unsigned int
Matt Roper9c74d822016-05-12 07:05:58 -07003036skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003037{
Matt Roper9c74d822016-05-12 07:05:58 -07003038 struct drm_crtc_state *cstate = &intel_cstate->base;
3039 struct drm_atomic_state *state = cstate->state;
3040 struct drm_crtc *crtc = cstate->crtc;
3041 struct drm_device *dev = crtc->dev;
3042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003043 const struct drm_plane *plane;
Matt Roper024c9042015-09-24 15:53:11 -07003044 const struct intel_plane *intel_plane;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003045 struct drm_plane_state *pstate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003046 unsigned int rate, total_data_rate = 0;
Matt Roper9c74d822016-05-12 07:05:58 -07003047 int id;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003048 int i;
3049
3050 if (WARN_ON(!state))
3051 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003052
Matt Ropera1de91e2016-05-12 07:05:57 -07003053 /* Calculate and cache data rate for each plane */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003054 for_each_plane_in_state(state, plane, pstate, i) {
3055 id = skl_wm_plane_id(to_intel_plane(plane));
3056 intel_plane = to_intel_plane(plane);
Matt Roper024c9042015-09-24 15:53:11 -07003057
Matt Ropera6d3460e2016-05-12 07:06:04 -07003058 if (intel_plane->pipe != intel_crtc->pipe)
3059 continue;
Matt Roper024c9042015-09-24 15:53:11 -07003060
Matt Ropera6d3460e2016-05-12 07:06:04 -07003061 /* packed/uv */
3062 rate = skl_plane_relative_data_rate(intel_cstate,
3063 pstate, 0);
3064 intel_cstate->wm.skl.plane_data_rate[id] = rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003065
Matt Ropera6d3460e2016-05-12 07:06:04 -07003066 /* y-plane */
3067 rate = skl_plane_relative_data_rate(intel_cstate,
3068 pstate, 1);
3069 intel_cstate->wm.skl.plane_y_data_rate[id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003070 }
3071
3072 /* Calculate CRTC's total data rate from cached values */
3073 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3074 int id = skl_wm_plane_id(intel_plane);
3075
3076 /* packed/uv */
Matt Roper9c74d822016-05-12 07:05:58 -07003077 total_data_rate += intel_cstate->wm.skl.plane_data_rate[id];
3078 total_data_rate += intel_cstate->wm.skl.plane_y_data_rate[id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003079 }
3080
Matt Roper9c74d822016-05-12 07:05:58 -07003081 WARN_ON(cstate->plane_mask && total_data_rate == 0);
3082
Damien Lespiaub9cec072014-11-04 17:06:43 +00003083 return total_data_rate;
3084}
3085
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003086static uint16_t
3087skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3088 const int y)
3089{
3090 struct drm_framebuffer *fb = pstate->fb;
3091 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3092 uint32_t src_w, src_h;
3093 uint32_t min_scanlines = 8;
3094 uint8_t plane_bpp;
3095
3096 if (WARN_ON(!fb))
3097 return 0;
3098
3099 /* For packed formats, no y-plane, return 0 */
3100 if (y && fb->pixel_format != DRM_FORMAT_NV12)
3101 return 0;
3102
3103 /* For Non Y-tile return 8-blocks */
3104 if (fb->modifier[0] != I915_FORMAT_MOD_Y_TILED &&
3105 fb->modifier[0] != I915_FORMAT_MOD_Yf_TILED)
3106 return 8;
3107
3108 src_w = drm_rect_width(&intel_pstate->src) >> 16;
3109 src_h = drm_rect_height(&intel_pstate->src) >> 16;
3110
3111 if (intel_rotation_90_or_270(pstate->rotation))
3112 swap(src_w, src_h);
3113
3114 /* Halve UV plane width and height for NV12 */
3115 if (fb->pixel_format == DRM_FORMAT_NV12 && !y) {
3116 src_w /= 2;
3117 src_h /= 2;
3118 }
3119
3120 if (fb->pixel_format == DRM_FORMAT_NV12 && !y)
3121 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 1);
3122 else
3123 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 0);
3124
3125 if (intel_rotation_90_or_270(pstate->rotation)) {
3126 switch (plane_bpp) {
3127 case 1:
3128 min_scanlines = 32;
3129 break;
3130 case 2:
3131 min_scanlines = 16;
3132 break;
3133 case 4:
3134 min_scanlines = 8;
3135 break;
3136 case 8:
3137 min_scanlines = 4;
3138 break;
3139 default:
3140 WARN(1, "Unsupported pixel depth %u for rotation",
3141 plane_bpp);
3142 min_scanlines = 32;
3143 }
3144 }
3145
3146 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3147}
3148
Matt Roperc107acf2016-05-12 07:06:01 -07003149static int
Matt Roper024c9042015-09-24 15:53:11 -07003150skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003151 struct skl_ddb_allocation *ddb /* out */)
3152{
Matt Roperc107acf2016-05-12 07:06:01 -07003153 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003154 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003155 struct drm_device *dev = crtc->dev;
3156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003157 struct intel_plane *intel_plane;
Matt Roperc107acf2016-05-12 07:06:01 -07003158 struct drm_plane *plane;
3159 struct drm_plane_state *pstate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003160 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003161 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003162 uint16_t alloc_size, start, cursor_blocks;
Matt Roper86a2100a2016-05-12 07:05:59 -07003163 uint16_t *minimum = cstate->wm.skl.minimum_blocks;
3164 uint16_t *y_minimum = cstate->wm.skl.minimum_y_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003165 unsigned int total_data_rate;
Matt Roperc107acf2016-05-12 07:06:01 -07003166 int num_active;
3167 int id, i;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003168
Matt Ropera6d3460e2016-05-12 07:06:04 -07003169 if (WARN_ON(!state))
3170 return 0;
3171
Matt Roperc107acf2016-05-12 07:06:01 -07003172 if (!cstate->base.active) {
3173 ddb->pipe[pipe].start = ddb->pipe[pipe].end = 0;
3174 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3175 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3176 return 0;
3177 }
3178
Matt Ropera6d3460e2016-05-12 07:06:04 -07003179 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003180 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003181 if (alloc_size == 0) {
3182 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003183 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003184 }
3185
Matt Roperc107acf2016-05-12 07:06:01 -07003186 cursor_blocks = skl_cursor_allocation(num_active);
Matt Roper4969d332015-09-24 15:53:10 -07003187 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
3188 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003189
3190 alloc_size -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003191
Damien Lespiau80958152015-02-09 13:35:10 +00003192 /* 1. Allocate the mininum required blocks for each active plane */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003193 for_each_plane_in_state(state, plane, pstate, i) {
3194 intel_plane = to_intel_plane(plane);
3195 id = skl_wm_plane_id(intel_plane);
Damien Lespiau80958152015-02-09 13:35:10 +00003196
Matt Ropera6d3460e2016-05-12 07:06:04 -07003197 if (intel_plane->pipe != pipe)
3198 continue;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003199
Matt Ropera6d3460e2016-05-12 07:06:04 -07003200 if (!to_intel_plane_state(pstate)->visible) {
3201 minimum[id] = 0;
3202 y_minimum[id] = 0;
3203 continue;
Matt Roperc107acf2016-05-12 07:06:01 -07003204 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003205 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
3206 minimum[id] = 0;
3207 y_minimum[id] = 0;
3208 continue;
Matt Roperc107acf2016-05-12 07:06:01 -07003209 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003210
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003211 minimum[id] = skl_ddb_min_alloc(pstate, 0);
3212 y_minimum[id] = skl_ddb_min_alloc(pstate, 1);
Matt Roperc107acf2016-05-12 07:06:01 -07003213 }
3214
3215 for (i = 0; i < PLANE_CURSOR; i++) {
3216 alloc_size -= minimum[i];
3217 alloc_size -= y_minimum[i];
Damien Lespiau80958152015-02-09 13:35:10 +00003218 }
3219
Damien Lespiaub9cec072014-11-04 17:06:43 +00003220 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003221 * 2. Distribute the remaining space in proportion to the amount of
3222 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003223 *
3224 * FIXME: we may not allocate every single block here.
3225 */
Matt Roper024c9042015-09-24 15:53:11 -07003226 total_data_rate = skl_get_total_relative_data_rate(cstate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003227 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003228 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003229
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003230 start = alloc->start;
Matt Roper024c9042015-09-24 15:53:11 -07003231 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003232 unsigned int data_rate, y_data_rate;
3233 uint16_t plane_blocks, y_plane_blocks = 0;
Matt Roper024c9042015-09-24 15:53:11 -07003234 int id = skl_wm_plane_id(intel_plane);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003235
Matt Ropera1de91e2016-05-12 07:05:57 -07003236 data_rate = cstate->wm.skl.plane_data_rate[id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003237
3238 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003239 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003240 * promote the expression to 64 bits to avoid overflowing, the
3241 * result is < available as data_rate / total_data_rate < 1
3242 */
Matt Roper024c9042015-09-24 15:53:11 -07003243 plane_blocks = minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003244 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3245 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003246
Matt Roperc107acf2016-05-12 07:06:01 -07003247 /* Leave disabled planes at (0,0) */
3248 if (data_rate) {
3249 ddb->plane[pipe][id].start = start;
3250 ddb->plane[pipe][id].end = start + plane_blocks;
3251 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003252
3253 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003254
3255 /*
3256 * allocation for y_plane part of planar format:
3257 */
Matt Ropera1de91e2016-05-12 07:05:57 -07003258 y_data_rate = cstate->wm.skl.plane_y_data_rate[id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003259
Matt Ropera1de91e2016-05-12 07:05:57 -07003260 y_plane_blocks = y_minimum[id];
3261 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3262 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003263
Matt Roperc107acf2016-05-12 07:06:01 -07003264 if (y_data_rate) {
3265 ddb->y_plane[pipe][id].start = start;
3266 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
3267 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003268
Matt Ropera1de91e2016-05-12 07:05:57 -07003269 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003270 }
3271
Matt Roperc107acf2016-05-12 07:06:01 -07003272 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003273}
3274
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02003275static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003276{
3277 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02003278 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003279}
3280
3281/*
3282 * 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 +02003283 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003284 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3285 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3286*/
Ville Syrjäläac484962016-01-20 21:05:26 +02003287static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003288{
3289 uint32_t wm_intermediate_val, ret;
3290
3291 if (latency == 0)
3292 return UINT_MAX;
3293
Ville Syrjäläac484962016-01-20 21:05:26 +02003294 wm_intermediate_val = latency * pixel_rate * cpp / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003295 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3296
3297 return ret;
3298}
3299
3300static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02003301 uint32_t horiz_pixels, uint8_t cpp,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003302 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003303{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003304 uint32_t ret;
3305 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3306 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003307
3308 if (latency == 0)
3309 return UINT_MAX;
3310
Ville Syrjäläac484962016-01-20 21:05:26 +02003311 plane_bytes_per_line = horiz_pixels * cpp;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003312
3313 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3314 tiling == I915_FORMAT_MOD_Yf_TILED) {
3315 plane_bytes_per_line *= 4;
3316 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3317 plane_blocks_per_line /= 4;
3318 } else {
3319 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3320 }
3321
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003322 wm_intermediate_val = latency * pixel_rate;
3323 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003324 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003325
3326 return ret;
3327}
3328
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003329static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3330 struct intel_plane_state *pstate)
3331{
3332 uint64_t adjusted_pixel_rate;
3333 uint64_t downscale_amount;
3334 uint64_t pixel_rate;
3335
3336 /* Shouldn't reach here on disabled planes... */
3337 if (WARN_ON(!pstate->visible))
3338 return 0;
3339
3340 /*
3341 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3342 * with additional adjustments for plane-specific scaling.
3343 */
3344 adjusted_pixel_rate = skl_pipe_pixel_rate(cstate);
3345 downscale_amount = skl_plane_downscale_amount(pstate);
3346
3347 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3348 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3349
3350 return pixel_rate;
3351}
3352
Matt Roper55994c22016-05-12 07:06:08 -07003353static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3354 struct intel_crtc_state *cstate,
3355 struct intel_plane_state *intel_pstate,
3356 uint16_t ddb_allocation,
3357 int level,
3358 uint16_t *out_blocks, /* out */
3359 uint8_t *out_lines, /* out */
3360 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003361{
Matt Roper33815fa2016-05-12 07:06:05 -07003362 struct drm_plane_state *pstate = &intel_pstate->base;
3363 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003364 uint32_t latency = dev_priv->wm.skl_latency[level];
3365 uint32_t method1, method2;
3366 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3367 uint32_t res_blocks, res_lines;
3368 uint32_t selected_result;
Ville Syrjäläac484962016-01-20 21:05:26 +02003369 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003370 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003371 uint32_t plane_pixel_rate;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003372
Matt Roper55994c22016-05-12 07:06:08 -07003373 if (latency == 0 || !cstate->base.active || !intel_pstate->visible) {
3374 *enabled = false;
3375 return 0;
3376 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003377
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003378 width = drm_rect_width(&intel_pstate->src) >> 16;
3379 height = drm_rect_height(&intel_pstate->src) >> 16;
3380
Matt Roper33815fa2016-05-12 07:06:05 -07003381 if (intel_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003382 swap(width, height);
3383
Ville Syrjäläac484962016-01-20 21:05:26 +02003384 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003385 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3386
3387 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3388 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003389 cstate->base.adjusted_mode.crtc_htotal,
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003390 width,
3391 cpp,
3392 fb->modifier[0],
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003393 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003394
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003395 plane_bytes_per_line = width * cpp;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003396 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003397
Matt Roper024c9042015-09-24 15:53:11 -07003398 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3399 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003400 uint32_t min_scanlines = 4;
3401 uint32_t y_tile_minimum;
Matt Roper33815fa2016-05-12 07:06:05 -07003402 if (intel_rotation_90_or_270(pstate->rotation)) {
Ville Syrjäläac484962016-01-20 21:05:26 +02003403 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
Matt Roper024c9042015-09-24 15:53:11 -07003404 drm_format_plane_cpp(fb->pixel_format, 1) :
3405 drm_format_plane_cpp(fb->pixel_format, 0);
3406
Ville Syrjäläac484962016-01-20 21:05:26 +02003407 switch (cpp) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003408 case 1:
3409 min_scanlines = 16;
3410 break;
3411 case 2:
3412 min_scanlines = 8;
3413 break;
3414 case 8:
3415 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08003416 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003417 }
3418 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003419 selected_result = max(method2, y_tile_minimum);
3420 } else {
3421 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3422 selected_result = min(method1, method2);
3423 else
3424 selected_result = method1;
3425 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003426
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003427 res_blocks = selected_result + 1;
3428 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003429
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003430 if (level >= 1 && level <= 7) {
Matt Roper024c9042015-09-24 15:53:11 -07003431 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3432 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003433 res_lines += 4;
3434 else
3435 res_blocks++;
3436 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003437
Matt Roper55994c22016-05-12 07:06:08 -07003438 if (res_blocks >= ddb_allocation || res_lines > 31) {
3439 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003440
3441 /*
3442 * If there are no valid level 0 watermarks, then we can't
3443 * support this display configuration.
3444 */
3445 if (level) {
3446 return 0;
3447 } else {
3448 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3449 DRM_DEBUG_KMS("Plane %d.%d: blocks required = %u/%u, lines required = %u/31\n",
3450 to_intel_crtc(cstate->base.crtc)->pipe,
3451 skl_wm_plane_id(to_intel_plane(pstate->plane)),
3452 res_blocks, ddb_allocation, res_lines);
3453
3454 return -EINVAL;
3455 }
Matt Roper55994c22016-05-12 07:06:08 -07003456 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003457
3458 *out_blocks = res_blocks;
3459 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003460 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003461
Matt Roper55994c22016-05-12 07:06:08 -07003462 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003463}
3464
Matt Roperf4a96752016-05-12 07:06:06 -07003465static int
3466skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3467 struct skl_ddb_allocation *ddb,
3468 struct intel_crtc_state *cstate,
3469 int level,
3470 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003471{
Matt Roper024c9042015-09-24 15:53:11 -07003472 struct drm_device *dev = dev_priv->dev;
Matt Roperf4a96752016-05-12 07:06:06 -07003473 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003474 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roperf4a96752016-05-12 07:06:06 -07003475 struct drm_plane *plane;
Matt Roper024c9042015-09-24 15:53:11 -07003476 struct intel_plane *intel_plane;
Matt Roper33815fa2016-05-12 07:06:05 -07003477 struct intel_plane_state *intel_pstate;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003478 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003479 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003480 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003481
Matt Roperf4a96752016-05-12 07:06:06 -07003482 /*
3483 * We'll only calculate watermarks for planes that are actually
3484 * enabled, so make sure all other planes are set as disabled.
3485 */
3486 memset(result, 0, sizeof(*result));
3487
3488 for_each_intel_plane_mask(dev, intel_plane, cstate->base.plane_mask) {
Matt Roper024c9042015-09-24 15:53:11 -07003489 int i = skl_wm_plane_id(intel_plane);
3490
Matt Roperf4a96752016-05-12 07:06:06 -07003491 plane = &intel_plane->base;
3492 intel_pstate = NULL;
3493 if (state)
3494 intel_pstate =
3495 intel_atomic_get_existing_plane_state(state,
3496 intel_plane);
3497
3498 /*
3499 * Note: If we start supporting multiple pending atomic commits
3500 * against the same planes/CRTC's in the future, plane->state
3501 * will no longer be the correct pre-state to use for the
3502 * calculations here and we'll need to change where we get the
3503 * 'unchanged' plane data from.
3504 *
3505 * For now this is fine because we only allow one queued commit
3506 * against a CRTC. Even if the plane isn't modified by this
3507 * transaction and we don't have a plane lock, we still have
3508 * the CRTC's lock, so we know that no other transactions are
3509 * racing with us to update it.
3510 */
3511 if (!intel_pstate)
3512 intel_pstate = to_intel_plane_state(plane->state);
3513
3514 WARN_ON(!intel_pstate->base.fb);
3515
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003516 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3517
Matt Roper55994c22016-05-12 07:06:08 -07003518 ret = skl_compute_plane_wm(dev_priv,
3519 cstate,
3520 intel_pstate,
3521 ddb_blocks,
3522 level,
3523 &result->plane_res_b[i],
3524 &result->plane_res_l[i],
3525 &result->plane_en[i]);
3526 if (ret)
3527 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003528 }
Matt Roperf4a96752016-05-12 07:06:06 -07003529
3530 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003531}
3532
Damien Lespiau407b50f2014-11-04 17:06:57 +00003533static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003534skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003535{
Matt Roper024c9042015-09-24 15:53:11 -07003536 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003537 return 0;
3538
Matt Roper024c9042015-09-24 15:53:11 -07003539 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003540 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003541
Matt Roper024c9042015-09-24 15:53:11 -07003542 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3543 skl_pipe_pixel_rate(cstate));
Damien Lespiau407b50f2014-11-04 17:06:57 +00003544}
3545
Matt Roper024c9042015-09-24 15:53:11 -07003546static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003547 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003548{
Matt Roper024c9042015-09-24 15:53:11 -07003549 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiau9414f562014-11-04 17:06:58 +00003550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003551 struct intel_plane *intel_plane;
Damien Lespiau9414f562014-11-04 17:06:58 +00003552
Matt Roper024c9042015-09-24 15:53:11 -07003553 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003554 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003555
3556 /* Until we know more, just disable transition WMs */
Matt Roper024c9042015-09-24 15:53:11 -07003557 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3558 int i = skl_wm_plane_id(intel_plane);
3559
Damien Lespiau9414f562014-11-04 17:06:58 +00003560 trans_wm->plane_en[i] = false;
Matt Roper024c9042015-09-24 15:53:11 -07003561 }
Damien Lespiau407b50f2014-11-04 17:06:57 +00003562}
3563
Matt Roper55994c22016-05-12 07:06:08 -07003564static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3565 struct skl_ddb_allocation *ddb,
3566 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003567{
Matt Roper024c9042015-09-24 15:53:11 -07003568 struct drm_device *dev = cstate->base.crtc->dev;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003569 const struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003570 int level, max_level = ilk_wm_max_level(dev);
Matt Roper55994c22016-05-12 07:06:08 -07003571 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003572
3573 for (level = 0; level <= max_level; level++) {
Matt Roper55994c22016-05-12 07:06:08 -07003574 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3575 level, &pipe_wm->wm[level]);
3576 if (ret)
3577 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003578 }
Matt Roper024c9042015-09-24 15:53:11 -07003579 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003580
Matt Roper024c9042015-09-24 15:53:11 -07003581 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
Matt Roper55994c22016-05-12 07:06:08 -07003582
3583 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003584}
3585
3586static void skl_compute_wm_results(struct drm_device *dev,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003587 struct skl_pipe_wm *p_wm,
3588 struct skl_wm_values *r,
3589 struct intel_crtc *intel_crtc)
3590{
3591 int level, max_level = ilk_wm_max_level(dev);
3592 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003593 uint32_t temp;
3594 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003595
3596 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003597 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3598 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003599
3600 temp |= p_wm->wm[level].plane_res_l[i] <<
3601 PLANE_WM_LINES_SHIFT;
3602 temp |= p_wm->wm[level].plane_res_b[i];
3603 if (p_wm->wm[level].plane_en[i])
3604 temp |= PLANE_WM_EN;
3605
3606 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003607 }
3608
3609 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003610
Matt Roper4969d332015-09-24 15:53:10 -07003611 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3612 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003613
Matt Roper4969d332015-09-24 15:53:10 -07003614 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003615 temp |= PLANE_WM_EN;
3616
Matt Roper4969d332015-09-24 15:53:10 -07003617 r->plane[pipe][PLANE_CURSOR][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003618
3619 }
3620
Damien Lespiau9414f562014-11-04 17:06:58 +00003621 /* transition WMs */
3622 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3623 temp = 0;
3624 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3625 temp |= p_wm->trans_wm.plane_res_b[i];
3626 if (p_wm->trans_wm.plane_en[i])
3627 temp |= PLANE_WM_EN;
3628
3629 r->plane_trans[pipe][i] = temp;
3630 }
3631
3632 temp = 0;
Matt Roper4969d332015-09-24 15:53:10 -07003633 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3634 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3635 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
Damien Lespiau9414f562014-11-04 17:06:58 +00003636 temp |= PLANE_WM_EN;
3637
Matt Roper4969d332015-09-24 15:53:10 -07003638 r->plane_trans[pipe][PLANE_CURSOR] = temp;
Damien Lespiau9414f562014-11-04 17:06:58 +00003639
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003640 r->wm_linetime[pipe] = p_wm->linetime;
3641}
3642
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003643static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3644 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003645 const struct skl_ddb_entry *entry)
3646{
3647 if (entry->end)
3648 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3649 else
3650 I915_WRITE(reg, 0);
3651}
3652
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003653static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3654 const struct skl_wm_values *new)
3655{
3656 struct drm_device *dev = dev_priv->dev;
3657 struct intel_crtc *crtc;
3658
Jani Nikula19c80542015-12-16 12:48:16 +02003659 for_each_intel_crtc(dev, crtc) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003660 int i, level, max_level = ilk_wm_max_level(dev);
3661 enum pipe pipe = crtc->pipe;
3662
Matt Roper2b4b9f32016-05-12 07:06:07 -07003663 if ((new->dirty_pipes & drm_crtc_mask(&crtc->base)) == 0)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003664 continue;
Matt Roper734fa012016-05-12 15:11:40 -07003665 if (!crtc->active)
3666 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003667
Damien Lespiau5d374d92014-11-04 17:07:00 +00003668 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3669
3670 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003671 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003672 I915_WRITE(PLANE_WM(pipe, i, level),
3673 new->plane[pipe][i][level]);
3674 I915_WRITE(CUR_WM(pipe, level),
Matt Roper4969d332015-09-24 15:53:10 -07003675 new->plane[pipe][PLANE_CURSOR][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003676 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003677 for (i = 0; i < intel_num_planes(crtc); i++)
3678 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3679 new->plane_trans[pipe][i]);
Matt Roper4969d332015-09-24 15:53:10 -07003680 I915_WRITE(CUR_WM_TRANS(pipe),
3681 new->plane_trans[pipe][PLANE_CURSOR]);
Damien Lespiau5d374d92014-11-04 17:07:00 +00003682
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003683 for (i = 0; i < intel_num_planes(crtc); i++) {
Damien Lespiau5d374d92014-11-04 17:07:00 +00003684 skl_ddb_entry_write(dev_priv,
3685 PLANE_BUF_CFG(pipe, i),
3686 &new->ddb.plane[pipe][i]);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003687 skl_ddb_entry_write(dev_priv,
3688 PLANE_NV12_BUF_CFG(pipe, i),
3689 &new->ddb.y_plane[pipe][i]);
3690 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003691
3692 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
Matt Roper4969d332015-09-24 15:53:10 -07003693 &new->ddb.plane[pipe][PLANE_CURSOR]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003694 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003695}
3696
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003697/*
3698 * When setting up a new DDB allocation arrangement, we need to correctly
3699 * sequence the times at which the new allocations for the pipes are taken into
3700 * account or we'll have pipes fetching from space previously allocated to
3701 * another pipe.
3702 *
3703 * Roughly the sequence looks like:
3704 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3705 * overlapping with a previous light-up pipe (another way to put it is:
3706 * pipes with their new allocation strickly included into their old ones).
3707 * 2. re-allocate the other pipes that get their allocation reduced
3708 * 3. allocate the pipes having their allocation increased
3709 *
3710 * Steps 1. and 2. are here to take care of the following case:
3711 * - Initially DDB looks like this:
3712 * | B | C |
3713 * - enable pipe A.
3714 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3715 * allocation
3716 * | A | B | C |
3717 *
3718 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3719 */
3720
Damien Lespiaud21b7952014-11-04 17:07:03 +00003721static void
3722skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003723{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003724 int plane;
3725
Damien Lespiaud21b7952014-11-04 17:07:03 +00003726 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3727
Damien Lespiaudd740782015-02-28 14:54:08 +00003728 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003729 I915_WRITE(PLANE_SURF(pipe, plane),
3730 I915_READ(PLANE_SURF(pipe, plane)));
3731 }
3732 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3733}
3734
3735static bool
3736skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3737 const struct skl_ddb_allocation *new,
3738 enum pipe pipe)
3739{
3740 uint16_t old_size, new_size;
3741
3742 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3743 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3744
3745 return old_size != new_size &&
3746 new->pipe[pipe].start >= old->pipe[pipe].start &&
3747 new->pipe[pipe].end <= old->pipe[pipe].end;
3748}
3749
3750static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3751 struct skl_wm_values *new_values)
3752{
3753 struct drm_device *dev = dev_priv->dev;
3754 struct skl_ddb_allocation *cur_ddb, *new_ddb;
Ville Syrjäläc929cb42015-04-02 18:28:07 +03003755 bool reallocated[I915_MAX_PIPES] = {};
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003756 struct intel_crtc *crtc;
3757 enum pipe pipe;
3758
3759 new_ddb = &new_values->ddb;
3760 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3761
3762 /*
3763 * First pass: flush the pipes with the new allocation contained into
3764 * the old space.
3765 *
3766 * We'll wait for the vblank on those pipes to ensure we can safely
3767 * re-allocate the freed space without this pipe fetching from it.
3768 */
3769 for_each_intel_crtc(dev, crtc) {
3770 if (!crtc->active)
3771 continue;
3772
3773 pipe = crtc->pipe;
3774
3775 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3776 continue;
3777
Damien Lespiaud21b7952014-11-04 17:07:03 +00003778 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003779 intel_wait_for_vblank(dev, pipe);
3780
3781 reallocated[pipe] = true;
3782 }
3783
3784
3785 /*
3786 * Second pass: flush the pipes that are having their allocation
3787 * reduced, but overlapping with a previous allocation.
3788 *
3789 * Here as well we need to wait for the vblank to make sure the freed
3790 * space is not used anymore.
3791 */
3792 for_each_intel_crtc(dev, crtc) {
3793 if (!crtc->active)
3794 continue;
3795
3796 pipe = crtc->pipe;
3797
3798 if (reallocated[pipe])
3799 continue;
3800
3801 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3802 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003803 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003804 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303805 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003806 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003807 }
3808
3809 /*
3810 * Third pass: flush the pipes that got more space allocated.
3811 *
3812 * We don't need to actively wait for the update here, next vblank
3813 * will just get more DDB space with the correct WM values.
3814 */
3815 for_each_intel_crtc(dev, crtc) {
3816 if (!crtc->active)
3817 continue;
3818
3819 pipe = crtc->pipe;
3820
3821 /*
3822 * At this point, only the pipes more space than before are
3823 * left to re-allocate.
3824 */
3825 if (reallocated[pipe])
3826 continue;
3827
Damien Lespiaud21b7952014-11-04 17:07:03 +00003828 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003829 }
3830}
3831
Matt Roper55994c22016-05-12 07:06:08 -07003832static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
3833 struct skl_ddb_allocation *ddb, /* out */
3834 struct skl_pipe_wm *pipe_wm, /* out */
3835 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003836{
Matt Roperf4a96752016-05-12 07:06:06 -07003837 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->crtc);
3838 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07003839 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003840
Matt Roper55994c22016-05-12 07:06:08 -07003841 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
3842 if (ret)
3843 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003844
Matt Roper4e0963c2015-09-24 15:53:15 -07003845 if (!memcmp(&intel_crtc->wm.active.skl, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07003846 *changed = false;
3847 else
3848 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003849
Matt Roper55994c22016-05-12 07:06:08 -07003850 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003851}
3852
Matt Roper98d39492016-05-12 07:06:03 -07003853static int
3854skl_compute_ddb(struct drm_atomic_state *state)
3855{
3856 struct drm_device *dev = state->dev;
3857 struct drm_i915_private *dev_priv = to_i915(dev);
3858 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3859 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07003860 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper98d39492016-05-12 07:06:03 -07003861 unsigned realloc_pipes = dev_priv->active_crtcs;
3862 int ret;
3863
3864 /*
3865 * If this is our first atomic update following hardware readout,
3866 * we can't trust the DDB that the BIOS programmed for us. Let's
3867 * pretend that all pipes switched active status so that we'll
3868 * ensure a full DDB recompute.
3869 */
3870 if (dev_priv->wm.distrust_bios_wm)
3871 intel_state->active_pipe_changes = ~0;
3872
3873 /*
3874 * If the modeset changes which CRTC's are active, we need to
3875 * recompute the DDB allocation for *all* active pipes, even
3876 * those that weren't otherwise being modified in any way by this
3877 * atomic commit. Due to the shrinking of the per-pipe allocations
3878 * when new active CRTC's are added, it's possible for a pipe that
3879 * we were already using and aren't changing at all here to suddenly
3880 * become invalid if its DDB needs exceeds its new allocation.
3881 *
3882 * Note that if we wind up doing a full DDB recompute, we can't let
3883 * any other display updates race with this transaction, so we need
3884 * to grab the lock on *all* CRTC's.
3885 */
Matt Roper734fa012016-05-12 15:11:40 -07003886 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07003887 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07003888 intel_state->wm_results.dirty_pipes = ~0;
3889 }
Matt Roper98d39492016-05-12 07:06:03 -07003890
3891 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
3892 struct intel_crtc_state *cstate;
3893
3894 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
3895 if (IS_ERR(cstate))
3896 return PTR_ERR(cstate);
3897
Matt Roper734fa012016-05-12 15:11:40 -07003898 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07003899 if (ret)
3900 return ret;
3901 }
3902
3903 return 0;
3904}
3905
3906static int
3907skl_compute_wm(struct drm_atomic_state *state)
3908{
3909 struct drm_crtc *crtc;
3910 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07003911 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3912 struct skl_wm_values *results = &intel_state->wm_results;
3913 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07003914 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07003915 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07003916
3917 /*
3918 * If this transaction isn't actually touching any CRTC's, don't
3919 * bother with watermark calculation. Note that if we pass this
3920 * test, we're guaranteed to hold at least one CRTC state mutex,
3921 * which means we can safely use values like dev_priv->active_crtcs
3922 * since any racing commits that want to update them would need to
3923 * hold _all_ CRTC state mutexes.
3924 */
3925 for_each_crtc_in_state(state, crtc, cstate, i)
3926 changed = true;
3927 if (!changed)
3928 return 0;
3929
Matt Roper734fa012016-05-12 15:11:40 -07003930 /* Clear all dirty flags */
3931 results->dirty_pipes = 0;
3932
Matt Roper98d39492016-05-12 07:06:03 -07003933 ret = skl_compute_ddb(state);
3934 if (ret)
3935 return ret;
3936
Matt Roper734fa012016-05-12 15:11:40 -07003937 /*
3938 * Calculate WM's for all pipes that are part of this transaction.
3939 * Note that the DDB allocation above may have added more CRTC's that
3940 * weren't otherwise being modified (and set bits in dirty_pipes) if
3941 * pipe allocations had to change.
3942 *
3943 * FIXME: Now that we're doing this in the atomic check phase, we
3944 * should allow skl_update_pipe_wm() to return failure in cases where
3945 * no suitable watermark values can be found.
3946 */
3947 for_each_crtc_in_state(state, crtc, cstate, i) {
3948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3949 struct intel_crtc_state *intel_cstate =
3950 to_intel_crtc_state(cstate);
3951
3952 pipe_wm = &intel_cstate->wm.skl.optimal;
3953 ret = skl_update_pipe_wm(cstate, &results->ddb, pipe_wm,
3954 &changed);
3955 if (ret)
3956 return ret;
3957
3958 if (changed)
3959 results->dirty_pipes |= drm_crtc_mask(crtc);
3960
3961 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
3962 /* This pipe's WM's did not change */
3963 continue;
3964
3965 intel_cstate->update_wm_pre = true;
3966 skl_compute_wm_results(crtc->dev, pipe_wm, results, intel_crtc);
3967 }
3968
Matt Roper98d39492016-05-12 07:06:03 -07003969 return 0;
3970}
3971
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003972static void skl_update_wm(struct drm_crtc *crtc)
3973{
3974 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3975 struct drm_device *dev = crtc->dev;
3976 struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003977 struct skl_wm_values *results = &dev_priv->wm.skl_results;
Matt Roper4e0963c2015-09-24 15:53:15 -07003978 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07003979 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Bob Paauweadda50b2015-07-21 10:42:53 -07003980
Matt Roper734fa012016-05-12 15:11:40 -07003981 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003982 return;
3983
Matt Roper734fa012016-05-12 15:11:40 -07003984 intel_crtc->wm.active.skl = *pipe_wm;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003985
Matt Roper734fa012016-05-12 15:11:40 -07003986 mutex_lock(&dev_priv->wm.wm_mutex);
3987
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003988 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003989 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003990
3991 /* store the new configuration */
3992 dev_priv->wm.skl_hw = *results;
Matt Roper734fa012016-05-12 15:11:40 -07003993
3994 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003995}
3996
Ville Syrjäläd8905652016-01-14 14:53:35 +02003997static void ilk_compute_wm_config(struct drm_device *dev,
3998 struct intel_wm_config *config)
3999{
4000 struct intel_crtc *crtc;
4001
4002 /* Compute the currently _active_ config */
4003 for_each_intel_crtc(dev, crtc) {
4004 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4005
4006 if (!wm->pipe_enabled)
4007 continue;
4008
4009 config->sprites_enabled |= wm->sprites_enabled;
4010 config->sprites_scaled |= wm->sprites_scaled;
4011 config->num_pipes_active++;
4012 }
4013}
4014
Matt Ropered4a6a72016-02-23 17:20:13 -08004015static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004016{
Matt Ropered4a6a72016-02-23 17:20:13 -08004017 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004018 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004019 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004020 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004021 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004022 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004023
Ville Syrjäläd8905652016-01-14 14:53:35 +02004024 ilk_compute_wm_config(dev, &config);
4025
4026 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4027 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004028
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004029 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03004030 if (INTEL_INFO(dev)->gen >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004031 config.num_pipes_active == 1 && config.sprites_enabled) {
4032 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4033 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004034
Imre Deak820c1982013-12-17 14:46:36 +02004035 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004036 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004037 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004038 }
4039
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004040 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004041 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004042
Imre Deak820c1982013-12-17 14:46:36 +02004043 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004044
Imre Deak820c1982013-12-17 14:46:36 +02004045 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004046}
4047
Matt Ropered4a6a72016-02-23 17:20:13 -08004048static void ilk_initial_watermarks(struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004049{
Matt Ropered4a6a72016-02-23 17:20:13 -08004050 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4051 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004052
Matt Ropered4a6a72016-02-23 17:20:13 -08004053 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004054 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004055 ilk_program_watermarks(dev_priv);
4056 mutex_unlock(&dev_priv->wm.wm_mutex);
4057}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004058
Matt Ropered4a6a72016-02-23 17:20:13 -08004059static void ilk_optimize_watermarks(struct intel_crtc_state *cstate)
4060{
4061 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4062 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4063
4064 mutex_lock(&dev_priv->wm.wm_mutex);
4065 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004066 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004067 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004068 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004069 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004070}
4071
Pradeep Bhat30789992014-11-04 17:06:45 +00004072static void skl_pipe_wm_active_state(uint32_t val,
4073 struct skl_pipe_wm *active,
4074 bool is_transwm,
4075 bool is_cursor,
4076 int i,
4077 int level)
4078{
4079 bool is_enabled = (val & PLANE_WM_EN) != 0;
4080
4081 if (!is_transwm) {
4082 if (!is_cursor) {
4083 active->wm[level].plane_en[i] = is_enabled;
4084 active->wm[level].plane_res_b[i] =
4085 val & PLANE_WM_BLOCKS_MASK;
4086 active->wm[level].plane_res_l[i] =
4087 (val >> PLANE_WM_LINES_SHIFT) &
4088 PLANE_WM_LINES_MASK;
4089 } else {
Matt Roper4969d332015-09-24 15:53:10 -07004090 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
4091 active->wm[level].plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004092 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07004093 active->wm[level].plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004094 (val >> PLANE_WM_LINES_SHIFT) &
4095 PLANE_WM_LINES_MASK;
4096 }
4097 } else {
4098 if (!is_cursor) {
4099 active->trans_wm.plane_en[i] = is_enabled;
4100 active->trans_wm.plane_res_b[i] =
4101 val & PLANE_WM_BLOCKS_MASK;
4102 active->trans_wm.plane_res_l[i] =
4103 (val >> PLANE_WM_LINES_SHIFT) &
4104 PLANE_WM_LINES_MASK;
4105 } else {
Matt Roper4969d332015-09-24 15:53:10 -07004106 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
4107 active->trans_wm.plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004108 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07004109 active->trans_wm.plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004110 (val >> PLANE_WM_LINES_SHIFT) &
4111 PLANE_WM_LINES_MASK;
4112 }
4113 }
4114}
4115
4116static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4117{
4118 struct drm_device *dev = crtc->dev;
4119 struct drm_i915_private *dev_priv = dev->dev_private;
4120 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
4121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004122 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004123 struct skl_pipe_wm *active = &cstate->wm.skl.optimal;
Pradeep Bhat30789992014-11-04 17:06:45 +00004124 enum pipe pipe = intel_crtc->pipe;
4125 int level, i, max_level;
4126 uint32_t temp;
4127
4128 max_level = ilk_wm_max_level(dev);
4129
4130 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
4131
4132 for (level = 0; level <= max_level; level++) {
4133 for (i = 0; i < intel_num_planes(intel_crtc); i++)
4134 hw->plane[pipe][i][level] =
4135 I915_READ(PLANE_WM(pipe, i, level));
Matt Roper4969d332015-09-24 15:53:10 -07004136 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
Pradeep Bhat30789992014-11-04 17:06:45 +00004137 }
4138
4139 for (i = 0; i < intel_num_planes(intel_crtc); i++)
4140 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
Matt Roper4969d332015-09-24 15:53:10 -07004141 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004142
Matt Roper3ef00282015-03-09 10:19:24 -07004143 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004144 return;
4145
Matt Roper2b4b9f32016-05-12 07:06:07 -07004146 hw->dirty_pipes |= drm_crtc_mask(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004147
4148 active->linetime = hw->wm_linetime[pipe];
4149
4150 for (level = 0; level <= max_level; level++) {
4151 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
4152 temp = hw->plane[pipe][i][level];
4153 skl_pipe_wm_active_state(temp, active, false,
4154 false, i, level);
4155 }
Matt Roper4969d332015-09-24 15:53:10 -07004156 temp = hw->plane[pipe][PLANE_CURSOR][level];
Pradeep Bhat30789992014-11-04 17:06:45 +00004157 skl_pipe_wm_active_state(temp, active, false, true, i, level);
4158 }
4159
4160 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
4161 temp = hw->plane_trans[pipe][i];
4162 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
4163 }
4164
Matt Roper4969d332015-09-24 15:53:10 -07004165 temp = hw->plane_trans[pipe][PLANE_CURSOR];
Pradeep Bhat30789992014-11-04 17:06:45 +00004166 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
Matt Roper4e0963c2015-09-24 15:53:15 -07004167
4168 intel_crtc->wm.active.skl = *active;
Pradeep Bhat30789992014-11-04 17:06:45 +00004169}
4170
4171void skl_wm_get_hw_state(struct drm_device *dev)
4172{
Damien Lespiaua269c582014-11-04 17:06:49 +00004173 struct drm_i915_private *dev_priv = dev->dev_private;
4174 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004175 struct drm_crtc *crtc;
4176
Damien Lespiaua269c582014-11-04 17:06:49 +00004177 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00004178 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4179 skl_pipe_wm_get_hw_state(crtc);
Matt Ropera1de91e2016-05-12 07:05:57 -07004180
Matt Roper279e99d2016-05-12 07:06:02 -07004181 if (dev_priv->active_crtcs) {
4182 /* Fully recompute DDB on first atomic commit */
4183 dev_priv->wm.distrust_bios_wm = true;
4184 } else {
4185 /* Easy/common case; just sanitize DDB now if everything off */
4186 memset(ddb, 0, sizeof(*ddb));
4187 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004188}
4189
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004190static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4191{
4192 struct drm_device *dev = crtc->dev;
4193 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004194 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004196 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004197 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004198 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004199 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004200 [PIPE_A] = WM0_PIPEA_ILK,
4201 [PIPE_B] = WM0_PIPEB_ILK,
4202 [PIPE_C] = WM0_PIPEC_IVB,
4203 };
4204
4205 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004206 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004207 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004208
Ville Syrjälä15606532016-05-13 17:55:17 +03004209 memset(active, 0, sizeof(*active));
4210
Matt Roper3ef00282015-03-09 10:19:24 -07004211 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004212
4213 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004214 u32 tmp = hw->wm_pipe[pipe];
4215
4216 /*
4217 * For active pipes LP0 watermark is marked as
4218 * enabled, and LP1+ watermaks as disabled since
4219 * we can't really reverse compute them in case
4220 * multiple pipes are active.
4221 */
4222 active->wm[0].enable = true;
4223 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4224 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4225 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4226 active->linetime = hw->wm_linetime[pipe];
4227 } else {
4228 int level, max_level = ilk_wm_max_level(dev);
4229
4230 /*
4231 * For inactive pipes, all watermark levels
4232 * should be marked as enabled but zeroed,
4233 * which is what we'd compute them to.
4234 */
4235 for (level = 0; level <= max_level; level++)
4236 active->wm[level].enable = true;
4237 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004238
4239 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004240}
4241
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004242#define _FW_WM(value, plane) \
4243 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4244#define _FW_WM_VLV(value, plane) \
4245 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4246
4247static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4248 struct vlv_wm_values *wm)
4249{
4250 enum pipe pipe;
4251 uint32_t tmp;
4252
4253 for_each_pipe(dev_priv, pipe) {
4254 tmp = I915_READ(VLV_DDL(pipe));
4255
4256 wm->ddl[pipe].primary =
4257 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4258 wm->ddl[pipe].cursor =
4259 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4260 wm->ddl[pipe].sprite[0] =
4261 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4262 wm->ddl[pipe].sprite[1] =
4263 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4264 }
4265
4266 tmp = I915_READ(DSPFW1);
4267 wm->sr.plane = _FW_WM(tmp, SR);
4268 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
4269 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
4270 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
4271
4272 tmp = I915_READ(DSPFW2);
4273 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
4274 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
4275 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
4276
4277 tmp = I915_READ(DSPFW3);
4278 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4279
4280 if (IS_CHERRYVIEW(dev_priv)) {
4281 tmp = I915_READ(DSPFW7_CHV);
4282 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4283 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4284
4285 tmp = I915_READ(DSPFW8_CHV);
4286 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
4287 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
4288
4289 tmp = I915_READ(DSPFW9_CHV);
4290 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
4291 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
4292
4293 tmp = I915_READ(DSPHOWM);
4294 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4295 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4296 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4297 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4298 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4299 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4300 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4301 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4302 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4303 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4304 } else {
4305 tmp = I915_READ(DSPFW7);
4306 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4307 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4308
4309 tmp = I915_READ(DSPHOWM);
4310 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4311 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4312 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4313 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4314 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4315 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4316 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4317 }
4318}
4319
4320#undef _FW_WM
4321#undef _FW_WM_VLV
4322
4323void vlv_wm_get_hw_state(struct drm_device *dev)
4324{
4325 struct drm_i915_private *dev_priv = to_i915(dev);
4326 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4327 struct intel_plane *plane;
4328 enum pipe pipe;
4329 u32 val;
4330
4331 vlv_read_wm_values(dev_priv, wm);
4332
4333 for_each_intel_plane(dev, plane) {
4334 switch (plane->base.type) {
4335 int sprite;
4336 case DRM_PLANE_TYPE_CURSOR:
4337 plane->wm.fifo_size = 63;
4338 break;
4339 case DRM_PLANE_TYPE_PRIMARY:
4340 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4341 break;
4342 case DRM_PLANE_TYPE_OVERLAY:
4343 sprite = plane->plane;
4344 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4345 break;
4346 }
4347 }
4348
4349 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4350 wm->level = VLV_WM_LEVEL_PM2;
4351
4352 if (IS_CHERRYVIEW(dev_priv)) {
4353 mutex_lock(&dev_priv->rps.hw_lock);
4354
4355 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4356 if (val & DSP_MAXFIFO_PM5_ENABLE)
4357 wm->level = VLV_WM_LEVEL_PM5;
4358
Ville Syrjälä58590c12015-09-08 21:05:12 +03004359 /*
4360 * If DDR DVFS is disabled in the BIOS, Punit
4361 * will never ack the request. So if that happens
4362 * assume we don't have to enable/disable DDR DVFS
4363 * dynamically. To test that just set the REQ_ACK
4364 * bit to poke the Punit, but don't change the
4365 * HIGH/LOW bits so that we don't actually change
4366 * the current state.
4367 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004368 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004369 val |= FORCE_DDR_FREQ_REQ_ACK;
4370 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4371
4372 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4373 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4374 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4375 "assuming DDR DVFS is disabled\n");
4376 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4377 } else {
4378 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4379 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4380 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4381 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004382
4383 mutex_unlock(&dev_priv->rps.hw_lock);
4384 }
4385
4386 for_each_pipe(dev_priv, pipe)
4387 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4388 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4389 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4390
4391 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4392 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4393}
4394
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004395void ilk_wm_get_hw_state(struct drm_device *dev)
4396{
4397 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004398 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004399 struct drm_crtc *crtc;
4400
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004401 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004402 ilk_pipe_wm_get_hw_state(crtc);
4403
4404 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4405 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4406 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4407
4408 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004409 if (INTEL_INFO(dev)->gen >= 7) {
4410 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4411 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4412 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004413
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004414 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004415 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4416 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4417 else if (IS_IVYBRIDGE(dev))
4418 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4419 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004420
4421 hw->enable_fbc_wm =
4422 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4423}
4424
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004425/**
4426 * intel_update_watermarks - update FIFO watermark values based on current modes
4427 *
4428 * Calculate watermark values for the various WM regs based on current mode
4429 * and plane configuration.
4430 *
4431 * There are several cases to deal with here:
4432 * - normal (i.e. non-self-refresh)
4433 * - self-refresh (SR) mode
4434 * - lines are large relative to FIFO size (buffer can hold up to 2)
4435 * - lines are small relative to FIFO size (buffer can hold more than 2
4436 * lines), so need to account for TLB latency
4437 *
4438 * The normal calculation is:
4439 * watermark = dotclock * bytes per pixel * latency
4440 * where latency is platform & configuration dependent (we assume pessimal
4441 * values here).
4442 *
4443 * The SR calculation is:
4444 * watermark = (trunc(latency/line time)+1) * surface width *
4445 * bytes per pixel
4446 * where
4447 * line time = htotal / dotclock
4448 * surface width = hdisplay for normal plane and 64 for cursor
4449 * and latency is assumed to be high, as above.
4450 *
4451 * The final value programmed to the register should always be rounded up,
4452 * and include an extra 2 entries to account for clock crossings.
4453 *
4454 * We don't use the sprite, so we can ignore that. And on Crestline we have
4455 * to set the non-SR watermarks to 8.
4456 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004457void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004458{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004459 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004460
4461 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004462 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004463}
4464
Jani Nikulae2828912016-01-18 09:19:47 +02004465/*
Daniel Vetter92703882012-08-09 16:46:01 +02004466 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004467 */
4468DEFINE_SPINLOCK(mchdev_lock);
4469
4470/* Global for IPS driver to get at the current i915 device. Protected by
4471 * mchdev_lock. */
4472static struct drm_i915_private *i915_mch_dev;
4473
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004474bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004475{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004476 u16 rgvswctl;
4477
Daniel Vetter92703882012-08-09 16:46:01 +02004478 assert_spin_locked(&mchdev_lock);
4479
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004480 rgvswctl = I915_READ16(MEMSWCTL);
4481 if (rgvswctl & MEMCTL_CMD_STS) {
4482 DRM_DEBUG("gpu busy, RCS change rejected\n");
4483 return false; /* still busy with another command */
4484 }
4485
4486 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4487 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4488 I915_WRITE16(MEMSWCTL, rgvswctl);
4489 POSTING_READ16(MEMSWCTL);
4490
4491 rgvswctl |= MEMCTL_CMD_STS;
4492 I915_WRITE16(MEMSWCTL, rgvswctl);
4493
4494 return true;
4495}
4496
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004497static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004498{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004499 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004500 u8 fmax, fmin, fstart, vstart;
4501
Daniel Vetter92703882012-08-09 16:46:01 +02004502 spin_lock_irq(&mchdev_lock);
4503
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004504 rgvmodectl = I915_READ(MEMMODECTL);
4505
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004506 /* Enable temp reporting */
4507 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4508 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4509
4510 /* 100ms RC evaluation intervals */
4511 I915_WRITE(RCUPEI, 100000);
4512 I915_WRITE(RCDNEI, 100000);
4513
4514 /* Set max/min thresholds to 90ms and 80ms respectively */
4515 I915_WRITE(RCBMAXAVG, 90000);
4516 I915_WRITE(RCBMINAVG, 80000);
4517
4518 I915_WRITE(MEMIHYST, 1);
4519
4520 /* Set up min, max, and cur for interrupt handling */
4521 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4522 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4523 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4524 MEMMODE_FSTART_SHIFT;
4525
Ville Syrjälä616847e2015-09-18 20:03:19 +03004526 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004527 PXVFREQ_PX_SHIFT;
4528
Daniel Vetter20e4d402012-08-08 23:35:39 +02004529 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4530 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004531
Daniel Vetter20e4d402012-08-08 23:35:39 +02004532 dev_priv->ips.max_delay = fstart;
4533 dev_priv->ips.min_delay = fmin;
4534 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004535
4536 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4537 fmax, fmin, fstart);
4538
4539 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4540
4541 /*
4542 * Interrupts will be enabled in ironlake_irq_postinstall
4543 */
4544
4545 I915_WRITE(VIDSTART, vstart);
4546 POSTING_READ(VIDSTART);
4547
4548 rgvmodectl |= MEMMODE_SWMODE_EN;
4549 I915_WRITE(MEMMODECTL, rgvmodectl);
4550
Daniel Vetter92703882012-08-09 16:46:01 +02004551 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004552 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004553 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004554
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004555 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004556
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004557 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4558 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004559 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004560 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004561 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004562
4563 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004564}
4565
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004566static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004567{
Daniel Vetter92703882012-08-09 16:46:01 +02004568 u16 rgvswctl;
4569
4570 spin_lock_irq(&mchdev_lock);
4571
4572 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004573
4574 /* Ack interrupts, disable EFC interrupt */
4575 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4576 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4577 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4578 I915_WRITE(DEIIR, DE_PCU_EVENT);
4579 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4580
4581 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004582 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004583 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004584 rgvswctl |= MEMCTL_CMD_STS;
4585 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004586 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004587
Daniel Vetter92703882012-08-09 16:46:01 +02004588 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004589}
4590
Daniel Vetteracbe9472012-07-26 11:50:05 +02004591/* There's a funny hw issue where the hw returns all 0 when reading from
4592 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4593 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4594 * all limits and the gpu stuck at whatever frequency it is at atm).
4595 */
Akash Goel74ef1172015-03-06 11:07:19 +05304596static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004597{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004598 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004599
Daniel Vetter20b46e52012-07-26 11:16:14 +02004600 /* Only set the down limit when we've reached the lowest level to avoid
4601 * getting more interrupts, otherwise leave this clear. This prevents a
4602 * race in the hw when coming out of rc6: There's a tiny window where
4603 * the hw runs at the minimal clock before selecting the desired
4604 * frequency, if the down threshold expires in that window we will not
4605 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03004606 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05304607 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4608 if (val <= dev_priv->rps.min_freq_softlimit)
4609 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4610 } else {
4611 limits = dev_priv->rps.max_freq_softlimit << 24;
4612 if (val <= dev_priv->rps.min_freq_softlimit)
4613 limits |= dev_priv->rps.min_freq_softlimit << 16;
4614 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004615
4616 return limits;
4617}
4618
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004619static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4620{
4621 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304622 u32 threshold_up = 0, threshold_down = 0; /* in % */
4623 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004624
4625 new_power = dev_priv->rps.power;
4626 switch (dev_priv->rps.power) {
4627 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004628 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004629 new_power = BETWEEN;
4630 break;
4631
4632 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004633 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004634 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004635 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004636 new_power = HIGH_POWER;
4637 break;
4638
4639 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004640 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 +01004641 new_power = BETWEEN;
4642 break;
4643 }
4644 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004645 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004646 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004647 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004648 new_power = HIGH_POWER;
4649 if (new_power == dev_priv->rps.power)
4650 return;
4651
4652 /* Note the units here are not exactly 1us, but 1280ns. */
4653 switch (new_power) {
4654 case LOW_POWER:
4655 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304656 ei_up = 16000;
4657 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004658
4659 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304660 ei_down = 32000;
4661 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004662 break;
4663
4664 case BETWEEN:
4665 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304666 ei_up = 13000;
4667 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004668
4669 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304670 ei_down = 32000;
4671 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004672 break;
4673
4674 case HIGH_POWER:
4675 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304676 ei_up = 10000;
4677 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004678
4679 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304680 ei_down = 32000;
4681 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004682 break;
4683 }
4684
Akash Goel8a586432015-03-06 11:07:18 +05304685 I915_WRITE(GEN6_RP_UP_EI,
4686 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4687 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4688 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4689
4690 I915_WRITE(GEN6_RP_DOWN_EI,
4691 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4692 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4693 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4694
4695 I915_WRITE(GEN6_RP_CONTROL,
4696 GEN6_RP_MEDIA_TURBO |
4697 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4698 GEN6_RP_MEDIA_IS_GFX |
4699 GEN6_RP_ENABLE |
4700 GEN6_RP_UP_BUSY_AVG |
4701 GEN6_RP_DOWN_IDLE_AVG);
4702
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004703 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004704 dev_priv->rps.up_threshold = threshold_up;
4705 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004706 dev_priv->rps.last_adj = 0;
4707}
4708
Chris Wilson2876ce72014-03-28 08:03:34 +00004709static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4710{
4711 u32 mask = 0;
4712
4713 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004714 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004715 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004716 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004717
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004718 mask &= dev_priv->pm_rps_events;
4719
Imre Deak59d02a12014-12-19 19:33:26 +02004720 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004721}
4722
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004723/* gen6_set_rps is called to update the frequency request, but should also be
4724 * called when the range (min_delay and max_delay) is modified so that we can
4725 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilsondc979972016-05-10 14:10:04 +01004726static void gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004727{
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304728 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01004729 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304730 return;
4731
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004732 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004733 WARN_ON(val > dev_priv->rps.max_freq);
4734 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004735
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004736 /* min/max delay may still have been modified so be sure to
4737 * write the limits value.
4738 */
4739 if (val != dev_priv->rps.cur_freq) {
4740 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004741
Chris Wilsondc979972016-05-10 14:10:04 +01004742 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05304743 I915_WRITE(GEN6_RPNSWREQ,
4744 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01004745 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004746 I915_WRITE(GEN6_RPNSWREQ,
4747 HSW_FREQUENCY(val));
4748 else
4749 I915_WRITE(GEN6_RPNSWREQ,
4750 GEN6_FREQUENCY(val) |
4751 GEN6_OFFSET(0) |
4752 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004753 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004754
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004755 /* Make sure we continue to get interrupts
4756 * until we hit the minimum or maximum frequencies.
4757 */
Akash Goel74ef1172015-03-06 11:07:19 +05304758 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004759 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004760
Ben Widawskyd5570a72012-09-07 19:43:41 -07004761 POSTING_READ(GEN6_RPNSWREQ);
4762
Ben Widawskyb39fb292014-03-19 18:31:11 -07004763 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004764 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004765}
4766
Chris Wilsondc979972016-05-10 14:10:04 +01004767static void valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004768{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004769 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004770 WARN_ON(val > dev_priv->rps.max_freq);
4771 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004772
Chris Wilsondc979972016-05-10 14:10:04 +01004773 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004774 "Odd GPU freq value\n"))
4775 val &= ~1;
4776
Deepak Scd25dd52015-07-10 18:31:40 +05304777 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4778
Chris Wilson8fb55192015-04-07 16:20:28 +01004779 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004780 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01004781 if (!IS_CHERRYVIEW(dev_priv))
4782 gen6_set_rps_thresholds(dev_priv, val);
4783 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004784
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004785 dev_priv->rps.cur_freq = val;
4786 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4787}
4788
Deepak Sa7f6e232015-05-09 18:04:44 +05304789/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05304790 *
4791 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05304792 * 1. Forcewake Media well.
4793 * 2. Request idle freq.
4794 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05304795*/
4796static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4797{
Chris Wilsonaed242f2015-03-18 09:48:21 +00004798 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304799
Chris Wilsonaed242f2015-03-18 09:48:21 +00004800 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304801 return;
4802
Deepak Sa7f6e232015-05-09 18:04:44 +05304803 /* Wake up the media well, as that takes a lot less
4804 * power than the Render well. */
4805 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilsondc979972016-05-10 14:10:04 +01004806 valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05304807 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05304808}
4809
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004810void gen6_rps_busy(struct drm_i915_private *dev_priv)
4811{
4812 mutex_lock(&dev_priv->rps.hw_lock);
4813 if (dev_priv->rps.enabled) {
4814 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4815 gen6_rps_reset_ei(dev_priv);
4816 I915_WRITE(GEN6_PMINTRMSK,
4817 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4818 }
4819 mutex_unlock(&dev_priv->rps.hw_lock);
4820}
4821
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004822void gen6_rps_idle(struct drm_i915_private *dev_priv)
4823{
4824 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004825 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01004826 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05304827 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004828 else
Chris Wilsondc979972016-05-10 14:10:04 +01004829 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004830 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004831 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004832 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004833 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004834
Chris Wilson8d3afd72015-05-21 21:01:47 +01004835 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004836 while (!list_empty(&dev_priv->rps.clients))
4837 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004838 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004839}
4840
Chris Wilson1854d5c2015-04-07 16:20:32 +01004841void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01004842 struct intel_rps_client *rps,
4843 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004844{
Chris Wilson8d3afd72015-05-21 21:01:47 +01004845 /* This is intentionally racy! We peek at the state here, then
4846 * validate inside the RPS worker.
4847 */
4848 if (!(dev_priv->mm.busy &&
4849 dev_priv->rps.enabled &&
4850 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4851 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004852
Chris Wilsone61b9952015-04-27 13:41:24 +01004853 /* Force a RPS boost (and don't count it against the client) if
4854 * the GPU is severely congested.
4855 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004856 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01004857 rps = NULL;
4858
Chris Wilson8d3afd72015-05-21 21:01:47 +01004859 spin_lock(&dev_priv->rps.client_lock);
4860 if (rps == NULL || list_empty(&rps->link)) {
4861 spin_lock_irq(&dev_priv->irq_lock);
4862 if (dev_priv->rps.interrupts_enabled) {
4863 dev_priv->rps.client_boost = true;
4864 queue_work(dev_priv->wq, &dev_priv->rps.work);
4865 }
4866 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004867
Chris Wilson2e1b8732015-04-27 13:41:22 +01004868 if (rps != NULL) {
4869 list_add(&rps->link, &dev_priv->rps.clients);
4870 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01004871 } else
4872 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01004873 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004874 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004875}
4876
Chris Wilsondc979972016-05-10 14:10:04 +01004877void intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004878{
Chris Wilsondc979972016-05-10 14:10:04 +01004879 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4880 valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004881 else
Chris Wilsondc979972016-05-10 14:10:04 +01004882 gen6_set_rps(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004883}
4884
Chris Wilsondc979972016-05-10 14:10:04 +01004885static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00004886{
Zhe Wang20e49362014-11-04 17:07:05 +00004887 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004888 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004889}
4890
Chris Wilsondc979972016-05-10 14:10:04 +01004891static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05304892{
Akash Goel2030d682016-04-23 00:05:45 +05304893 I915_WRITE(GEN6_RP_CONTROL, 0);
4894}
4895
Chris Wilsondc979972016-05-10 14:10:04 +01004896static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004897{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004898 I915_WRITE(GEN6_RC_CONTROL, 0);
4899 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05304900 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004901}
4902
Chris Wilsondc979972016-05-10 14:10:04 +01004903static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05304904{
Deepak S38807742014-05-23 21:00:15 +05304905 I915_WRITE(GEN6_RC_CONTROL, 0);
4906}
4907
Chris Wilsondc979972016-05-10 14:10:04 +01004908static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004909{
Deepak S98a2e5f2014-08-18 10:35:27 -07004910 /* we're doing forcewake before Disabling RC6,
4911 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004912 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004913
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004914 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004915
Mika Kuoppala59bad942015-01-16 11:34:40 +02004916 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004917}
4918
Chris Wilsondc979972016-05-10 14:10:04 +01004919static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07004920{
Chris Wilsondc979972016-05-10 14:10:04 +01004921 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03004922 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4923 mode = GEN6_RC_CTL_RC6_ENABLE;
4924 else
4925 mode = 0;
4926 }
Chris Wilsondc979972016-05-10 14:10:04 +01004927 if (HAS_RC6p(dev_priv))
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004928 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004929 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
4930 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
4931 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004932
4933 else
4934 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004935 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07004936}
4937
Chris Wilsondc979972016-05-10 14:10:04 +01004938static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304939{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004940 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304941 bool enable_rc6 = true;
4942 unsigned long rc6_ctx_base;
4943
4944 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
4945 DRM_DEBUG_KMS("RC6 Base location not set properly.\n");
4946 enable_rc6 = false;
4947 }
4948
4949 /*
4950 * The exact context size is not known for BXT, so assume a page size
4951 * for this check.
4952 */
4953 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004954 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
4955 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
4956 ggtt->stolen_reserved_size))) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304957 DRM_DEBUG_KMS("RC6 Base address not as expected.\n");
4958 enable_rc6 = false;
4959 }
4960
4961 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
4962 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
4963 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
4964 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
4965 DRM_DEBUG_KMS("Engine Idle wait time not set properly.\n");
4966 enable_rc6 = false;
4967 }
4968
4969 if (!(I915_READ(GEN6_RC_CONTROL) & (GEN6_RC_CTL_RC6_ENABLE |
4970 GEN6_RC_CTL_HW_ENABLE)) &&
4971 ((I915_READ(GEN6_RC_CONTROL) & GEN6_RC_CTL_HW_ENABLE) ||
4972 !(I915_READ(GEN6_RC_STATE) & RC6_STATE))) {
4973 DRM_DEBUG_KMS("HW/SW RC6 is not enabled by BIOS.\n");
4974 enable_rc6 = false;
4975 }
4976
4977 return enable_rc6;
4978}
4979
Chris Wilsondc979972016-05-10 14:10:04 +01004980int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004981{
Daniel Vettere7d66d82015-06-15 23:23:54 +02004982 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01004983 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03004984 return 0;
4985
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304986 if (!enable_rc6)
4987 return 0;
4988
Chris Wilsondc979972016-05-10 14:10:04 +01004989 if (IS_BROXTON(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304990 DRM_INFO("RC6 disabled by BIOS\n");
4991 return 0;
4992 }
4993
Daniel Vetter456470e2012-08-08 23:35:40 +02004994 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004995 if (enable_rc6 >= 0) {
4996 int mask;
4997
Chris Wilsondc979972016-05-10 14:10:04 +01004998 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03004999 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5000 INTEL_RC6pp_ENABLE;
5001 else
5002 mask = INTEL_RC6_ENABLE;
5003
5004 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02005005 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
5006 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005007
5008 return enable_rc6 & mask;
5009 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005010
Chris Wilsondc979972016-05-10 14:10:04 +01005011 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005012 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005013
5014 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005015}
5016
Chris Wilsondc979972016-05-10 14:10:04 +01005017static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005018{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005019 uint32_t rp_state_cap;
5020 u32 ddcc_status = 0;
5021 int ret;
5022
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005023 /* All of these values are in units of 50MHz */
5024 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005025 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Chris Wilsondc979972016-05-10 14:10:04 +01005026 if (IS_BROXTON(dev_priv)) {
Bob Paauwe35040562015-06-25 14:54:07 -07005027 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
5028 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5029 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5030 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5031 } else {
5032 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
5033 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5034 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5035 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5036 }
5037
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005038 /* hw_max = RP0 until we check for overclocking */
5039 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
5040
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005041 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005042 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
5043 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005044 ret = sandybridge_pcode_read(dev_priv,
5045 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5046 &ddcc_status);
5047 if (0 == ret)
5048 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005049 clamp_t(u8,
5050 ((ddcc_status >> 8) & 0xff),
5051 dev_priv->rps.min_freq,
5052 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005053 }
5054
Chris Wilsondc979972016-05-10 14:10:04 +01005055 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305056 /* Store the frequency values in 16.66 MHZ units, which is
5057 the natural hardware unit for SKL */
5058 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5059 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5060 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5061 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5062 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5063 }
5064
Chris Wilsonaed242f2015-03-18 09:48:21 +00005065 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5066
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005067 /* Preserve min/max settings in case of re-init */
5068 if (dev_priv->rps.max_freq_softlimit == 0)
5069 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5070
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005071 if (dev_priv->rps.min_freq_softlimit == 0) {
Chris Wilsondc979972016-05-10 14:10:04 +01005072 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005073 dev_priv->rps.min_freq_softlimit =
Ville Syrjälä813b5e62015-03-25 19:27:16 +02005074 max_t(int, dev_priv->rps.efficient_freq,
5075 intel_freq_opcode(dev_priv, 450));
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005076 else
5077 dev_priv->rps.min_freq_softlimit =
5078 dev_priv->rps.min_freq;
5079 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005080}
5081
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005082/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005083static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005084{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005085 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5086
Chris Wilsondc979972016-05-10 14:10:04 +01005087 gen6_init_rps_frequencies(dev_priv);
Damien Lespiauba1c5542015-01-16 18:07:26 +00005088
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305089 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01005090 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Akash Goel2030d682016-04-23 00:05:45 +05305091 /*
5092 * BIOS could leave the Hw Turbo enabled, so need to explicitly
5093 * clear out the Control register just to avoid inconsitency
5094 * with debugfs interface, which will show Turbo as enabled
5095 * only and that is not expected by the User after adding the
5096 * WaGsvDisableTurbo. Apart from this there is no problem even
5097 * if the Turbo is left enabled in the Control register, as the
5098 * Up/Down interrupts would remain masked.
5099 */
Chris Wilsondc979972016-05-10 14:10:04 +01005100 gen9_disable_rps(dev_priv);
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305101 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5102 return;
5103 }
5104
Akash Goel0beb0592015-03-06 11:07:20 +05305105 /* Program defaults and thresholds for RPS*/
5106 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5107 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005108
Akash Goel0beb0592015-03-06 11:07:20 +05305109 /* 1 second timeout*/
5110 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5111 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5112
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005113 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005114
Akash Goel0beb0592015-03-06 11:07:20 +05305115 /* Leaning on the below call to gen6_set_rps to program/setup the
5116 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5117 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
5118 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsondc979972016-05-10 14:10:04 +01005119 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005120
5121 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5122}
5123
Chris Wilsondc979972016-05-10 14:10:04 +01005124static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005125{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005126 struct intel_engine_cs *engine;
Zhe Wang20e49362014-11-04 17:07:05 +00005127 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005128
5129 /* 1a: Software RC state - RC0 */
5130 I915_WRITE(GEN6_RC_STATE, 0);
5131
5132 /* 1b: Get forcewake during program sequence. Although the driver
5133 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005134 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005135
5136 /* 2a: Disable RC states. */
5137 I915_WRITE(GEN6_RC_CONTROL, 0);
5138
5139 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305140
5141 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005142 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305143 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5144 else
5145 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005146 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5147 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005148 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005149 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305150
Dave Gordon1a3d1892016-05-13 15:36:30 +01005151 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305152 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5153
Zhe Wang20e49362014-11-04 17:07:05 +00005154 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005155
Zhe Wang38c23522015-01-20 12:23:04 +00005156 /* 2c: Program Coarse Power Gating Policies. */
5157 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5158 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5159
Zhe Wang20e49362014-11-04 17:07:05 +00005160 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005161 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005162 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005163 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305164 /* WaRsUseTimeoutMode */
Chris Wilsondc979972016-05-10 14:10:04 +01005165 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_D0) ||
5166 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305167 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305168 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5169 GEN7_RC_CTL_TO_MODE |
5170 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305171 } else {
5172 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305173 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5174 GEN6_RC_CTL_EI_MODE(1) |
5175 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305176 }
Zhe Wang20e49362014-11-04 17:07:05 +00005177
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305178 /*
5179 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305180 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305181 */
Chris Wilsondc979972016-05-10 14:10:04 +01005182 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305183 I915_WRITE(GEN9_PG_ENABLE, 0);
5184 else
5185 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5186 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005187
Mika Kuoppala59bad942015-01-16 11:34:40 +02005188 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005189}
5190
Chris Wilsondc979972016-05-10 14:10:04 +01005191static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005192{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005193 struct intel_engine_cs *engine;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005194 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005195
5196 /* 1a: Software RC state - RC0 */
5197 I915_WRITE(GEN6_RC_STATE, 0);
5198
5199 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5200 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005201 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005202
5203 /* 2a: Disable RC states. */
5204 I915_WRITE(GEN6_RC_CONTROL, 0);
5205
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005206 /* Initialize rps frequencies */
Chris Wilsondc979972016-05-10 14:10:04 +01005207 gen6_init_rps_frequencies(dev_priv);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005208
5209 /* 2b: Program RC6 thresholds.*/
5210 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5211 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5212 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005213 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005214 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005215 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005216 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005217 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5218 else
5219 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005220
5221 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005222 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005223 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005224 intel_print_rc6_info(dev_priv, rc6_mask);
5225 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005226 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5227 GEN7_RC_CTL_TO_MODE |
5228 rc6_mask);
5229 else
5230 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5231 GEN6_RC_CTL_EI_MODE(1) |
5232 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005233
5234 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005235 I915_WRITE(GEN6_RPNSWREQ,
5236 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5237 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5238 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005239 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5240 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005241
Daniel Vetter7526ed72014-09-29 15:07:19 +02005242 /* Docs recommend 900MHz, and 300 MHz respectively */
5243 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5244 dev_priv->rps.max_freq_softlimit << 24 |
5245 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005246
Daniel Vetter7526ed72014-09-29 15:07:19 +02005247 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5248 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5249 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5250 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005251
Daniel Vetter7526ed72014-09-29 15:07:19 +02005252 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005253
5254 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005255 I915_WRITE(GEN6_RP_CONTROL,
5256 GEN6_RP_MEDIA_TURBO |
5257 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5258 GEN6_RP_MEDIA_IS_GFX |
5259 GEN6_RP_ENABLE |
5260 GEN6_RP_UP_BUSY_AVG |
5261 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005262
Daniel Vetter7526ed72014-09-29 15:07:19 +02005263 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005264
Tom O'Rourkec7f31532014-11-19 14:21:54 -08005265 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsondc979972016-05-10 14:10:04 +01005266 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005267
Mika Kuoppala59bad942015-01-16 11:34:40 +02005268 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005269}
5270
Chris Wilsondc979972016-05-10 14:10:04 +01005271static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005272{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005273 struct intel_engine_cs *engine;
Ben Widawskyd060c162014-03-19 18:31:08 -07005274 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005275 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005276 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005277 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005278
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005279 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005280
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005281 /* Here begins a magic sequence of register writes to enable
5282 * auto-downclocking.
5283 *
5284 * Perhaps there might be some value in exposing these to
5285 * userspace...
5286 */
5287 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005288
5289 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005290 gtfifodbg = I915_READ(GTFIFODBG);
5291 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005292 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5293 I915_WRITE(GTFIFODBG, gtfifodbg);
5294 }
5295
Mika Kuoppala59bad942015-01-16 11:34:40 +02005296 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005297
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005298 /* Initialize rps frequencies */
Chris Wilsondc979972016-05-10 14:10:04 +01005299 gen6_init_rps_frequencies(dev_priv);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06005300
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005301 /* disable the counters and set deterministic thresholds */
5302 I915_WRITE(GEN6_RC_CONTROL, 0);
5303
5304 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5305 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5306 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5307 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5308 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5309
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005310 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005311 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005312
5313 I915_WRITE(GEN6_RC_SLEEP, 0);
5314 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005315 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005316 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5317 else
5318 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005319 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005320 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5321
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005322 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005323 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005324 if (rc6_mode & INTEL_RC6_ENABLE)
5325 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5326
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005327 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005328 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005329 if (rc6_mode & INTEL_RC6p_ENABLE)
5330 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005331
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005332 if (rc6_mode & INTEL_RC6pp_ENABLE)
5333 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5334 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005335
Chris Wilsondc979972016-05-10 14:10:04 +01005336 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005337
5338 I915_WRITE(GEN6_RC_CONTROL,
5339 rc6_mask |
5340 GEN6_RC_CTL_EI_MODE(1) |
5341 GEN6_RC_CTL_HW_ENABLE);
5342
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005343 /* Power down if completely idle for over 50ms */
5344 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005345 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005346
Ben Widawsky42c05262012-09-26 10:34:00 -07005347 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07005348 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07005349 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07005350
5351 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5352 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5353 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07005354 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07005355 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07005356 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005357 }
5358
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005359 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsondc979972016-05-10 14:10:04 +01005360 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005361
Ben Widawsky31643d52012-09-26 10:34:01 -07005362 rc6vids = 0;
5363 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005364 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005365 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005366 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005367 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5368 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5369 rc6vids &= 0xffff00;
5370 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5371 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5372 if (ret)
5373 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5374 }
5375
Mika Kuoppala59bad942015-01-16 11:34:40 +02005376 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005377}
5378
Chris Wilsondc979972016-05-10 14:10:04 +01005379static void __gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005380{
5381 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005382 unsigned int gpu_freq;
5383 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305384 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005385 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005386 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005387
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005388 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005389
Ben Widawskyeda79642013-10-07 17:15:48 -03005390 policy = cpufreq_cpu_get(0);
5391 if (policy) {
5392 max_ia_freq = policy->cpuinfo.max_freq;
5393 cpufreq_cpu_put(policy);
5394 } else {
5395 /*
5396 * Default to measured freq if none found, PCU will ensure we
5397 * don't go over
5398 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005399 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005400 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005401
5402 /* Convert from kHz to MHz */
5403 max_ia_freq /= 1000;
5404
Ben Widawsky153b4b952013-10-22 22:05:09 -07005405 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005406 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5407 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005408
Chris Wilsondc979972016-05-10 14:10:04 +01005409 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305410 /* Convert GT frequency to 50 HZ units */
5411 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5412 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5413 } else {
5414 min_gpu_freq = dev_priv->rps.min_freq;
5415 max_gpu_freq = dev_priv->rps.max_freq;
5416 }
5417
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005418 /*
5419 * For each potential GPU frequency, load a ring frequency we'd like
5420 * to use for memory access. We do this by specifying the IA frequency
5421 * the PCU should use as a reference to determine the ring frequency.
5422 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305423 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5424 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005425 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005426
Chris Wilsondc979972016-05-10 14:10:04 +01005427 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305428 /*
5429 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5430 * No floor required for ring frequency on SKL.
5431 */
5432 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005433 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005434 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5435 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005436 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005437 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005438 ring_freq = max(min_ring_freq, ring_freq);
5439 /* leave ia_freq as the default, chosen by cpufreq */
5440 } else {
5441 /* On older processors, there is no separate ring
5442 * clock domain, so in order to boost the bandwidth
5443 * of the ring, we need to upclock the CPU (ia_freq).
5444 *
5445 * For GPU frequencies less than 750MHz,
5446 * just use the lowest ring freq.
5447 */
5448 if (gpu_freq < min_freq)
5449 ia_freq = 800;
5450 else
5451 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5452 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5453 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005454
Ben Widawsky42c05262012-09-26 10:34:00 -07005455 sandybridge_pcode_write(dev_priv,
5456 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005457 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5458 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5459 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005460 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005461}
5462
Chris Wilsondc979972016-05-10 14:10:04 +01005463void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005464{
Chris Wilsondc979972016-05-10 14:10:04 +01005465 if (!HAS_CORE_RING_FREQ(dev_priv))
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005466 return;
5467
5468 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsondc979972016-05-10 14:10:04 +01005469 __gen6_update_ring_freq(dev_priv);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005470 mutex_unlock(&dev_priv->rps.hw_lock);
5471}
5472
Ville Syrjälä03af2042014-06-28 02:03:53 +03005473static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305474{
5475 u32 val, rp0;
5476
Jani Nikula5b5929c2015-10-07 11:17:46 +03005477 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305478
Chris Wilsondc979972016-05-10 14:10:04 +01005479 switch (INTEL_INFO(dev_priv)->eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005480 case 8:
5481 /* (2 * 4) config */
5482 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5483 break;
5484 case 12:
5485 /* (2 * 6) config */
5486 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5487 break;
5488 case 16:
5489 /* (2 * 8) config */
5490 default:
5491 /* Setting (2 * 8) Min RP0 for any other combination */
5492 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5493 break;
Deepak S095acd52015-01-17 11:05:59 +05305494 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005495
5496 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5497
Deepak S2b6b3a02014-05-27 15:59:30 +05305498 return rp0;
5499}
5500
5501static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5502{
5503 u32 val, rpe;
5504
5505 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5506 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5507
5508 return rpe;
5509}
5510
Deepak S7707df42014-07-12 18:46:14 +05305511static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5512{
5513 u32 val, rp1;
5514
Jani Nikula5b5929c2015-10-07 11:17:46 +03005515 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5516 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5517
Deepak S7707df42014-07-12 18:46:14 +05305518 return rp1;
5519}
5520
Deepak Sf8f2b002014-07-10 13:16:21 +05305521static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5522{
5523 u32 val, rp1;
5524
5525 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5526
5527 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5528
5529 return rp1;
5530}
5531
Ville Syrjälä03af2042014-06-28 02:03:53 +03005532static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005533{
5534 u32 val, rp0;
5535
Jani Nikula64936252013-05-22 15:36:20 +03005536 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005537
5538 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5539 /* Clamp to max */
5540 rp0 = min_t(u32, rp0, 0xea);
5541
5542 return rp0;
5543}
5544
5545static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5546{
5547 u32 val, rpe;
5548
Jani Nikula64936252013-05-22 15:36:20 +03005549 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005550 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005551 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005552 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5553
5554 return rpe;
5555}
5556
Ville Syrjälä03af2042014-06-28 02:03:53 +03005557static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005558{
Imre Deak36146032014-12-04 18:39:35 +02005559 u32 val;
5560
5561 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5562 /*
5563 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5564 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5565 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5566 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5567 * to make sure it matches what Punit accepts.
5568 */
5569 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005570}
5571
Imre Deakae484342014-03-31 15:10:44 +03005572/* Check that the pctx buffer wasn't move under us. */
5573static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5574{
5575 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5576
5577 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5578 dev_priv->vlv_pctx->stolen->start);
5579}
5580
Deepak S38807742014-05-23 21:00:15 +05305581
5582/* Check that the pcbr address is not empty. */
5583static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5584{
5585 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5586
5587 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5588}
5589
Chris Wilsondc979972016-05-10 14:10:04 +01005590static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305591{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005592 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005593 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05305594 u32 pcbr;
5595 int pctx_size = 32*1024;
5596
Deepak S38807742014-05-23 21:00:15 +05305597 pcbr = I915_READ(VLV_PCBR);
5598 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005599 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305600 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005601 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05305602
5603 pctx_paddr = (paddr & (~4095));
5604 I915_WRITE(VLV_PCBR, pctx_paddr);
5605 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005606
5607 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305608}
5609
Chris Wilsondc979972016-05-10 14:10:04 +01005610static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005611{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005612 struct drm_i915_gem_object *pctx;
5613 unsigned long pctx_paddr;
5614 u32 pcbr;
5615 int pctx_size = 24*1024;
5616
Chris Wilsondc979972016-05-10 14:10:04 +01005617 mutex_lock(&dev_priv->dev->struct_mutex);
Imre Deak17b0c1f2014-02-11 21:39:06 +02005618
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005619 pcbr = I915_READ(VLV_PCBR);
5620 if (pcbr) {
5621 /* BIOS set it up already, grab the pre-alloc'd space */
5622 int pcbr_offset;
5623
5624 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5625 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5626 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005627 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005628 pctx_size);
5629 goto out;
5630 }
5631
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005632 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5633
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005634 /*
5635 * From the Gunit register HAS:
5636 * The Gfx driver is expected to program this register and ensure
5637 * proper allocation within Gfx stolen memory. For example, this
5638 * register should be programmed such than the PCBR range does not
5639 * overlap with other ranges, such as the frame buffer, protected
5640 * memory, or any other relevant ranges.
5641 */
Chris Wilsondc979972016-05-10 14:10:04 +01005642 pctx = i915_gem_object_create_stolen(dev_priv->dev, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005643 if (!pctx) {
5644 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005645 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005646 }
5647
5648 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5649 I915_WRITE(VLV_PCBR, pctx_paddr);
5650
5651out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005652 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005653 dev_priv->vlv_pctx = pctx;
Chris Wilsondc979972016-05-10 14:10:04 +01005654 mutex_unlock(&dev_priv->dev->struct_mutex);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005655}
5656
Chris Wilsondc979972016-05-10 14:10:04 +01005657static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03005658{
Imre Deakae484342014-03-31 15:10:44 +03005659 if (WARN_ON(!dev_priv->vlv_pctx))
5660 return;
5661
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005662 drm_gem_object_unreference_unlocked(&dev_priv->vlv_pctx->base);
Imre Deakae484342014-03-31 15:10:44 +03005663 dev_priv->vlv_pctx = NULL;
5664}
5665
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005666static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5667{
5668 dev_priv->rps.gpll_ref_freq =
5669 vlv_get_cck_clock(dev_priv, "GPLL ref",
5670 CCK_GPLL_CLOCK_CONTROL,
5671 dev_priv->czclk_freq);
5672
5673 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5674 dev_priv->rps.gpll_ref_freq);
5675}
5676
Chris Wilsondc979972016-05-10 14:10:04 +01005677static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005678{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005679 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005680
Chris Wilsondc979972016-05-10 14:10:04 +01005681 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005682
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005683 vlv_init_gpll_ref_freq(dev_priv);
5684
Imre Deak4e805192014-04-14 20:24:41 +03005685 mutex_lock(&dev_priv->rps.hw_lock);
5686
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005687 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5688 switch ((val >> 6) & 3) {
5689 case 0:
5690 case 1:
5691 dev_priv->mem_freq = 800;
5692 break;
5693 case 2:
5694 dev_priv->mem_freq = 1066;
5695 break;
5696 case 3:
5697 dev_priv->mem_freq = 1333;
5698 break;
5699 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005700 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005701
Imre Deak4e805192014-04-14 20:24:41 +03005702 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5703 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5704 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005705 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005706 dev_priv->rps.max_freq);
5707
5708 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5709 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005710 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005711 dev_priv->rps.efficient_freq);
5712
Deepak Sf8f2b002014-07-10 13:16:21 +05305713 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5714 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005715 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305716 dev_priv->rps.rp1_freq);
5717
Imre Deak4e805192014-04-14 20:24:41 +03005718 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5719 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005720 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005721 dev_priv->rps.min_freq);
5722
Chris Wilsonaed242f2015-03-18 09:48:21 +00005723 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5724
Imre Deak4e805192014-04-14 20:24:41 +03005725 /* Preserve min/max settings in case of re-init */
5726 if (dev_priv->rps.max_freq_softlimit == 0)
5727 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5728
5729 if (dev_priv->rps.min_freq_softlimit == 0)
5730 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5731
5732 mutex_unlock(&dev_priv->rps.hw_lock);
5733}
5734
Chris Wilsondc979972016-05-10 14:10:04 +01005735static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305736{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005737 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305738
Chris Wilsondc979972016-05-10 14:10:04 +01005739 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05305740
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005741 vlv_init_gpll_ref_freq(dev_priv);
5742
Deepak S2b6b3a02014-05-27 15:59:30 +05305743 mutex_lock(&dev_priv->rps.hw_lock);
5744
Ville Syrjäläa5805162015-05-26 20:42:30 +03005745 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005746 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005747 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005748
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005749 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005750 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005751 dev_priv->mem_freq = 2000;
5752 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005753 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005754 dev_priv->mem_freq = 1600;
5755 break;
5756 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005757 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005758
Deepak S2b6b3a02014-05-27 15:59:30 +05305759 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5760 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5761 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005762 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305763 dev_priv->rps.max_freq);
5764
5765 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5766 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005767 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305768 dev_priv->rps.efficient_freq);
5769
Deepak S7707df42014-07-12 18:46:14 +05305770 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5771 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005772 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305773 dev_priv->rps.rp1_freq);
5774
Deepak S5b7c91b2015-05-09 18:15:46 +05305775 /* PUnit validated range is only [RPe, RP0] */
5776 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305777 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005778 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305779 dev_priv->rps.min_freq);
5780
Ville Syrjälä1c147622014-08-18 14:42:43 +03005781 WARN_ONCE((dev_priv->rps.max_freq |
5782 dev_priv->rps.efficient_freq |
5783 dev_priv->rps.rp1_freq |
5784 dev_priv->rps.min_freq) & 1,
5785 "Odd GPU freq values\n");
5786
Chris Wilsonaed242f2015-03-18 09:48:21 +00005787 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5788
Deepak S2b6b3a02014-05-27 15:59:30 +05305789 /* Preserve min/max settings in case of re-init */
5790 if (dev_priv->rps.max_freq_softlimit == 0)
5791 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5792
5793 if (dev_priv->rps.min_freq_softlimit == 0)
5794 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5795
5796 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305797}
5798
Chris Wilsondc979972016-05-10 14:10:04 +01005799static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005800{
Chris Wilsondc979972016-05-10 14:10:04 +01005801 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005802}
5803
Chris Wilsondc979972016-05-10 14:10:04 +01005804static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305805{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005806 struct intel_engine_cs *engine;
Deepak S2b6b3a02014-05-27 15:59:30 +05305807 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305808
5809 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5810
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005811 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
5812 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05305813 if (gtfifodbg) {
5814 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5815 gtfifodbg);
5816 I915_WRITE(GTFIFODBG, gtfifodbg);
5817 }
5818
5819 cherryview_check_pctx(dev_priv);
5820
5821 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5822 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005823 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305824
Ville Syrjälä160614a2015-01-19 13:50:47 +02005825 /* Disable RC states. */
5826 I915_WRITE(GEN6_RC_CONTROL, 0);
5827
Deepak S38807742014-05-23 21:00:15 +05305828 /* 2a: Program RC6 thresholds.*/
5829 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5830 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5831 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5832
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005833 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005834 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05305835 I915_WRITE(GEN6_RC_SLEEP, 0);
5836
Deepak Sf4f71c72015-03-28 15:23:35 +05305837 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5838 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05305839
5840 /* allows RC6 residency counter to work */
5841 I915_WRITE(VLV_COUNTER_CONTROL,
5842 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5843 VLV_MEDIA_RC6_COUNT_EN |
5844 VLV_RENDER_RC6_COUNT_EN));
5845
5846 /* For now we assume BIOS is allocating and populating the PCBR */
5847 pcbr = I915_READ(VLV_PCBR);
5848
Deepak S38807742014-05-23 21:00:15 +05305849 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005850 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
5851 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005852 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305853
5854 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5855
Deepak S2b6b3a02014-05-27 15:59:30 +05305856 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005857 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305858 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5859 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5860 I915_WRITE(GEN6_RP_UP_EI, 66000);
5861 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5862
5863 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5864
5865 /* 5: Enable RPS */
5866 I915_WRITE(GEN6_RP_CONTROL,
5867 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005868 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305869 GEN6_RP_ENABLE |
5870 GEN6_RP_UP_BUSY_AVG |
5871 GEN6_RP_DOWN_IDLE_AVG);
5872
Deepak S3ef62342015-04-29 08:36:24 +05305873 /* Setting Fixed Bias */
5874 val = VLV_OVERRIDE_EN |
5875 VLV_SOC_TDP_EN |
5876 CHV_BIAS_CPU_50_SOC_50;
5877 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5878
Deepak S2b6b3a02014-05-27 15:59:30 +05305879 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5880
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005881 /* RPS code assumes GPLL is used */
5882 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5883
Jani Nikula742f4912015-09-03 11:16:09 +03005884 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05305885 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5886
5887 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5888 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005889 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305890 dev_priv->rps.cur_freq);
5891
5892 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005893 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5894 dev_priv->rps.idle_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05305895
Chris Wilsondc979972016-05-10 14:10:04 +01005896 valleyview_set_rps(dev_priv, dev_priv->rps.idle_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05305897
Mika Kuoppala59bad942015-01-16 11:34:40 +02005898 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305899}
5900
Chris Wilsondc979972016-05-10 14:10:04 +01005901static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005902{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005903 struct intel_engine_cs *engine;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005904 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005905
5906 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5907
Imre Deakae484342014-03-31 15:10:44 +03005908 valleyview_check_pctx(dev_priv);
5909
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005910 gtfifodbg = I915_READ(GTFIFODBG);
5911 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005912 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5913 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005914 I915_WRITE(GTFIFODBG, gtfifodbg);
5915 }
5916
Deepak Sc8d9a592013-11-23 14:55:42 +05305917 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005918 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005919
Ville Syrjälä160614a2015-01-19 13:50:47 +02005920 /* Disable RC states. */
5921 I915_WRITE(GEN6_RC_CONTROL, 0);
5922
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005923 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005924 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5925 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5926 I915_WRITE(GEN6_RP_UP_EI, 66000);
5927 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5928
5929 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5930
5931 I915_WRITE(GEN6_RP_CONTROL,
5932 GEN6_RP_MEDIA_TURBO |
5933 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5934 GEN6_RP_MEDIA_IS_GFX |
5935 GEN6_RP_ENABLE |
5936 GEN6_RP_UP_BUSY_AVG |
5937 GEN6_RP_DOWN_IDLE_CONT);
5938
5939 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5940 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5941 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5942
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005943 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005944 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005945
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005946 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005947
5948 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005949 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005950 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5951 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005952 VLV_MEDIA_RC6_COUNT_EN |
5953 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005954
Chris Wilsondc979972016-05-10 14:10:04 +01005955 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005956 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005957
Chris Wilsondc979972016-05-10 14:10:04 +01005958 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005959
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005960 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005961
Deepak S3ef62342015-04-29 08:36:24 +05305962 /* Setting Fixed Bias */
5963 val = VLV_OVERRIDE_EN |
5964 VLV_SOC_TDP_EN |
5965 VLV_BIAS_CPU_125_SOC_875;
5966 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5967
Jani Nikula64936252013-05-22 15:36:20 +03005968 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005969
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005970 /* RPS code assumes GPLL is used */
5971 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5972
Jani Nikula742f4912015-09-03 11:16:09 +03005973 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07005974 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5975
Ben Widawskyb39fb292014-03-19 18:31:11 -07005976 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005977 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005978 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005979 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005980
Ville Syrjälä73008b92013-06-25 19:21:01 +03005981 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005982 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5983 dev_priv->rps.idle_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005984
Chris Wilsondc979972016-05-10 14:10:04 +01005985 valleyview_set_rps(dev_priv, dev_priv->rps.idle_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005986
Mika Kuoppala59bad942015-01-16 11:34:40 +02005987 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005988}
5989
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005990static unsigned long intel_pxfreq(u32 vidfreq)
5991{
5992 unsigned long freq;
5993 int div = (vidfreq & 0x3f0000) >> 16;
5994 int post = (vidfreq & 0x3000) >> 12;
5995 int pre = (vidfreq & 0x7);
5996
5997 if (!pre)
5998 return 0;
5999
6000 freq = ((div * 133333) / ((1<<post) * pre));
6001
6002 return freq;
6003}
6004
Daniel Vettereb48eb02012-04-26 23:28:12 +02006005static const struct cparams {
6006 u16 i;
6007 u16 t;
6008 u16 m;
6009 u16 c;
6010} cparams[] = {
6011 { 1, 1333, 301, 28664 },
6012 { 1, 1066, 294, 24460 },
6013 { 1, 800, 294, 25192 },
6014 { 0, 1333, 276, 27605 },
6015 { 0, 1066, 276, 27605 },
6016 { 0, 800, 231, 23784 },
6017};
6018
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006019static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006020{
6021 u64 total_count, diff, ret;
6022 u32 count1, count2, count3, m = 0, c = 0;
6023 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6024 int i;
6025
Daniel Vetter02d71952012-08-09 16:44:54 +02006026 assert_spin_locked(&mchdev_lock);
6027
Daniel Vetter20e4d402012-08-08 23:35:39 +02006028 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006029
6030 /* Prevent division-by-zero if we are asking too fast.
6031 * Also, we don't get interesting results if we are polling
6032 * faster than once in 10ms, so just return the saved value
6033 * in such cases.
6034 */
6035 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006036 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006037
6038 count1 = I915_READ(DMIEC);
6039 count2 = I915_READ(DDREC);
6040 count3 = I915_READ(CSIEC);
6041
6042 total_count = count1 + count2 + count3;
6043
6044 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006045 if (total_count < dev_priv->ips.last_count1) {
6046 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006047 diff += total_count;
6048 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006049 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006050 }
6051
6052 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006053 if (cparams[i].i == dev_priv->ips.c_m &&
6054 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006055 m = cparams[i].m;
6056 c = cparams[i].c;
6057 break;
6058 }
6059 }
6060
6061 diff = div_u64(diff, diff1);
6062 ret = ((m * diff) + c);
6063 ret = div_u64(ret, 10);
6064
Daniel Vetter20e4d402012-08-08 23:35:39 +02006065 dev_priv->ips.last_count1 = total_count;
6066 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006067
Daniel Vetter20e4d402012-08-08 23:35:39 +02006068 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006069
6070 return ret;
6071}
6072
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006073unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6074{
6075 unsigned long val;
6076
Chris Wilsondc979972016-05-10 14:10:04 +01006077 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006078 return 0;
6079
6080 spin_lock_irq(&mchdev_lock);
6081
6082 val = __i915_chipset_val(dev_priv);
6083
6084 spin_unlock_irq(&mchdev_lock);
6085
6086 return val;
6087}
6088
Daniel Vettereb48eb02012-04-26 23:28:12 +02006089unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6090{
6091 unsigned long m, x, b;
6092 u32 tsfs;
6093
6094 tsfs = I915_READ(TSFS);
6095
6096 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6097 x = I915_READ8(TR1);
6098
6099 b = tsfs & TSFS_INTR_MASK;
6100
6101 return ((m * x) / 127) - b;
6102}
6103
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006104static int _pxvid_to_vd(u8 pxvid)
6105{
6106 if (pxvid == 0)
6107 return 0;
6108
6109 if (pxvid >= 8 && pxvid < 31)
6110 pxvid = 31;
6111
6112 return (pxvid + 2) * 125;
6113}
6114
6115static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006116{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006117 const int vd = _pxvid_to_vd(pxvid);
6118 const int vm = vd - 1125;
6119
Chris Wilsondc979972016-05-10 14:10:04 +01006120 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006121 return vm > 0 ? vm : 0;
6122
6123 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006124}
6125
Daniel Vetter02d71952012-08-09 16:44:54 +02006126static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006127{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006128 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006129 u32 count;
6130
Daniel Vetter02d71952012-08-09 16:44:54 +02006131 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006132
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006133 now = ktime_get_raw_ns();
6134 diffms = now - dev_priv->ips.last_time2;
6135 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006136
6137 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006138 if (!diffms)
6139 return;
6140
6141 count = I915_READ(GFXEC);
6142
Daniel Vetter20e4d402012-08-08 23:35:39 +02006143 if (count < dev_priv->ips.last_count2) {
6144 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006145 diff += count;
6146 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006147 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006148 }
6149
Daniel Vetter20e4d402012-08-08 23:35:39 +02006150 dev_priv->ips.last_count2 = count;
6151 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006152
6153 /* More magic constants... */
6154 diff = diff * 1181;
6155 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006156 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006157}
6158
Daniel Vetter02d71952012-08-09 16:44:54 +02006159void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6160{
Chris Wilsondc979972016-05-10 14:10:04 +01006161 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006162 return;
6163
Daniel Vetter92703882012-08-09 16:46:01 +02006164 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006165
6166 __i915_update_gfx_val(dev_priv);
6167
Daniel Vetter92703882012-08-09 16:46:01 +02006168 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006169}
6170
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006171static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006172{
6173 unsigned long t, corr, state1, corr2, state2;
6174 u32 pxvid, ext_v;
6175
Daniel Vetter02d71952012-08-09 16:44:54 +02006176 assert_spin_locked(&mchdev_lock);
6177
Ville Syrjälä616847e2015-09-18 20:03:19 +03006178 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006179 pxvid = (pxvid >> 24) & 0x7f;
6180 ext_v = pvid_to_extvid(dev_priv, pxvid);
6181
6182 state1 = ext_v;
6183
6184 t = i915_mch_val(dev_priv);
6185
6186 /* Revel in the empirically derived constants */
6187
6188 /* Correction factor in 1/100000 units */
6189 if (t > 80)
6190 corr = ((t * 2349) + 135940);
6191 else if (t >= 50)
6192 corr = ((t * 964) + 29317);
6193 else /* < 50 */
6194 corr = ((t * 301) + 1004);
6195
6196 corr = corr * ((150142 * state1) / 10000 - 78642);
6197 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006198 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006199
6200 state2 = (corr2 * state1) / 10000;
6201 state2 /= 100; /* convert to mW */
6202
Daniel Vetter02d71952012-08-09 16:44:54 +02006203 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006204
Daniel Vetter20e4d402012-08-08 23:35:39 +02006205 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006206}
6207
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006208unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6209{
6210 unsigned long val;
6211
Chris Wilsondc979972016-05-10 14:10:04 +01006212 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006213 return 0;
6214
6215 spin_lock_irq(&mchdev_lock);
6216
6217 val = __i915_gfx_val(dev_priv);
6218
6219 spin_unlock_irq(&mchdev_lock);
6220
6221 return val;
6222}
6223
Daniel Vettereb48eb02012-04-26 23:28:12 +02006224/**
6225 * i915_read_mch_val - return value for IPS use
6226 *
6227 * Calculate and return a value for the IPS driver to use when deciding whether
6228 * we have thermal and power headroom to increase CPU or GPU power budget.
6229 */
6230unsigned long i915_read_mch_val(void)
6231{
6232 struct drm_i915_private *dev_priv;
6233 unsigned long chipset_val, graphics_val, ret = 0;
6234
Daniel Vetter92703882012-08-09 16:46:01 +02006235 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006236 if (!i915_mch_dev)
6237 goto out_unlock;
6238 dev_priv = i915_mch_dev;
6239
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006240 chipset_val = __i915_chipset_val(dev_priv);
6241 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006242
6243 ret = chipset_val + graphics_val;
6244
6245out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006246 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006247
6248 return ret;
6249}
6250EXPORT_SYMBOL_GPL(i915_read_mch_val);
6251
6252/**
6253 * i915_gpu_raise - raise GPU frequency limit
6254 *
6255 * Raise the limit; IPS indicates we have thermal headroom.
6256 */
6257bool i915_gpu_raise(void)
6258{
6259 struct drm_i915_private *dev_priv;
6260 bool ret = true;
6261
Daniel Vetter92703882012-08-09 16:46:01 +02006262 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006263 if (!i915_mch_dev) {
6264 ret = false;
6265 goto out_unlock;
6266 }
6267 dev_priv = i915_mch_dev;
6268
Daniel Vetter20e4d402012-08-08 23:35:39 +02006269 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6270 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006271
6272out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006273 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006274
6275 return ret;
6276}
6277EXPORT_SYMBOL_GPL(i915_gpu_raise);
6278
6279/**
6280 * i915_gpu_lower - lower GPU frequency limit
6281 *
6282 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6283 * frequency maximum.
6284 */
6285bool i915_gpu_lower(void)
6286{
6287 struct drm_i915_private *dev_priv;
6288 bool ret = true;
6289
Daniel Vetter92703882012-08-09 16:46:01 +02006290 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006291 if (!i915_mch_dev) {
6292 ret = false;
6293 goto out_unlock;
6294 }
6295 dev_priv = i915_mch_dev;
6296
Daniel Vetter20e4d402012-08-08 23:35:39 +02006297 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6298 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006299
6300out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006301 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006302
6303 return ret;
6304}
6305EXPORT_SYMBOL_GPL(i915_gpu_lower);
6306
6307/**
6308 * i915_gpu_busy - indicate GPU business to IPS
6309 *
6310 * Tell the IPS driver whether or not the GPU is busy.
6311 */
6312bool i915_gpu_busy(void)
6313{
6314 struct drm_i915_private *dev_priv;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006315 struct intel_engine_cs *engine;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006316 bool ret = false;
6317
Daniel Vetter92703882012-08-09 16:46:01 +02006318 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006319 if (!i915_mch_dev)
6320 goto out_unlock;
6321 dev_priv = i915_mch_dev;
6322
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006323 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006324 ret |= !list_empty(&engine->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006325
6326out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006327 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006328
6329 return ret;
6330}
6331EXPORT_SYMBOL_GPL(i915_gpu_busy);
6332
6333/**
6334 * i915_gpu_turbo_disable - disable graphics turbo
6335 *
6336 * Disable graphics turbo by resetting the max frequency and setting the
6337 * current frequency to the default.
6338 */
6339bool i915_gpu_turbo_disable(void)
6340{
6341 struct drm_i915_private *dev_priv;
6342 bool ret = true;
6343
Daniel Vetter92703882012-08-09 16:46:01 +02006344 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006345 if (!i915_mch_dev) {
6346 ret = false;
6347 goto out_unlock;
6348 }
6349 dev_priv = i915_mch_dev;
6350
Daniel Vetter20e4d402012-08-08 23:35:39 +02006351 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006352
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006353 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006354 ret = false;
6355
6356out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006357 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006358
6359 return ret;
6360}
6361EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6362
6363/**
6364 * Tells the intel_ips driver that the i915 driver is now loaded, if
6365 * IPS got loaded first.
6366 *
6367 * This awkward dance is so that neither module has to depend on the
6368 * other in order for IPS to do the appropriate communication of
6369 * GPU turbo limits to i915.
6370 */
6371static void
6372ips_ping_for_i915_load(void)
6373{
6374 void (*link)(void);
6375
6376 link = symbol_get(ips_link_to_i915_driver);
6377 if (link) {
6378 link();
6379 symbol_put(ips_link_to_i915_driver);
6380 }
6381}
6382
6383void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6384{
Daniel Vetter02d71952012-08-09 16:44:54 +02006385 /* We only register the i915 ips part with intel-ips once everything is
6386 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006387 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006388 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006389 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006390
6391 ips_ping_for_i915_load();
6392}
6393
6394void intel_gpu_ips_teardown(void)
6395{
Daniel Vetter92703882012-08-09 16:46:01 +02006396 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006397 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006398 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006399}
Deepak S76c3552f2014-01-30 23:08:16 +05306400
Chris Wilsondc979972016-05-10 14:10:04 +01006401static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006402{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006403 u32 lcfuse;
6404 u8 pxw[16];
6405 int i;
6406
6407 /* Disable to program */
6408 I915_WRITE(ECR, 0);
6409 POSTING_READ(ECR);
6410
6411 /* Program energy weights for various events */
6412 I915_WRITE(SDEW, 0x15040d00);
6413 I915_WRITE(CSIEW0, 0x007f0000);
6414 I915_WRITE(CSIEW1, 0x1e220004);
6415 I915_WRITE(CSIEW2, 0x04000004);
6416
6417 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006418 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006419 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006420 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006421
6422 /* Program P-state weights to account for frequency power adjustment */
6423 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006424 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006425 unsigned long freq = intel_pxfreq(pxvidfreq);
6426 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6427 PXVFREQ_PX_SHIFT;
6428 unsigned long val;
6429
6430 val = vid * vid;
6431 val *= (freq / 1000);
6432 val *= 255;
6433 val /= (127*127*900);
6434 if (val > 0xff)
6435 DRM_ERROR("bad pxval: %ld\n", val);
6436 pxw[i] = val;
6437 }
6438 /* Render standby states get 0 weight */
6439 pxw[14] = 0;
6440 pxw[15] = 0;
6441
6442 for (i = 0; i < 4; i++) {
6443 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6444 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006445 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006446 }
6447
6448 /* Adjust magic regs to magic values (more experimental results) */
6449 I915_WRITE(OGW0, 0);
6450 I915_WRITE(OGW1, 0);
6451 I915_WRITE(EG0, 0x00007f00);
6452 I915_WRITE(EG1, 0x0000000e);
6453 I915_WRITE(EG2, 0x000e0000);
6454 I915_WRITE(EG3, 0x68000300);
6455 I915_WRITE(EG4, 0x42000000);
6456 I915_WRITE(EG5, 0x00140031);
6457 I915_WRITE(EG6, 0);
6458 I915_WRITE(EG7, 0);
6459
6460 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006461 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006462
6463 /* Enable PMON + select events */
6464 I915_WRITE(ECR, 0x80000019);
6465
6466 lcfuse = I915_READ(LCFUSE02);
6467
Daniel Vetter20e4d402012-08-08 23:35:39 +02006468 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006469}
6470
Chris Wilsondc979972016-05-10 14:10:04 +01006471void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006472{
Imre Deakb268c692015-12-15 20:10:31 +02006473 /*
6474 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6475 * requirement.
6476 */
6477 if (!i915.enable_rc6) {
6478 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6479 intel_runtime_pm_get(dev_priv);
6480 }
Imre Deake6069ca2014-04-18 16:01:02 +03006481
Chris Wilsondc979972016-05-10 14:10:04 +01006482 if (IS_CHERRYVIEW(dev_priv))
6483 cherryview_init_gt_powersave(dev_priv);
6484 else if (IS_VALLEYVIEW(dev_priv))
6485 valleyview_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006486}
6487
Chris Wilsondc979972016-05-10 14:10:04 +01006488void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006489{
Chris Wilsondc979972016-05-10 14:10:04 +01006490 if (IS_CHERRYVIEW(dev_priv))
Deepak S38807742014-05-23 21:00:15 +05306491 return;
Chris Wilsondc979972016-05-10 14:10:04 +01006492 else if (IS_VALLEYVIEW(dev_priv))
6493 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006494
6495 if (!i915.enable_rc6)
6496 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006497}
6498
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006499static void gen6_suspend_rps(struct drm_i915_private *dev_priv)
Imre Deakdbea3ce2014-12-15 18:59:28 +02006500{
Imre Deakdbea3ce2014-12-15 18:59:28 +02006501 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6502
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006503 gen6_disable_rps_interrupts(dev_priv);
Imre Deakdbea3ce2014-12-15 18:59:28 +02006504}
6505
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006506/**
6507 * intel_suspend_gt_powersave - suspend PM work and helper threads
Chris Wilsondc979972016-05-10 14:10:04 +01006508 * @dev_priv: i915 device
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006509 *
6510 * We don't want to disable RC6 or other features here, we just want
6511 * to make sure any work we've queued has finished and won't bother
6512 * us while we're suspended.
6513 */
Chris Wilsondc979972016-05-10 14:10:04 +01006514void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006515{
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006516 if (INTEL_GEN(dev_priv) < 6)
Imre Deakd4d70aa2014-11-19 15:30:04 +02006517 return;
6518
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006519 gen6_suspend_rps(dev_priv);
Deepak Sb47adc12014-06-20 20:03:02 +05306520
6521 /* Force GPU to min freq during suspend */
6522 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006523}
6524
Chris Wilsondc979972016-05-10 14:10:04 +01006525void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006526{
Chris Wilsondc979972016-05-10 14:10:04 +01006527 if (IS_IRONLAKE_M(dev_priv)) {
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006528 ironlake_disable_drps(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006529 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
6530 intel_suspend_gt_powersave(dev_priv);
Imre Deake4948372014-05-12 18:35:04 +03006531
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006532 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsondc979972016-05-10 14:10:04 +01006533 if (INTEL_INFO(dev_priv)->gen >= 9) {
6534 gen9_disable_rc6(dev_priv);
6535 gen9_disable_rps(dev_priv);
6536 } else if (IS_CHERRYVIEW(dev_priv))
6537 cherryview_disable_rps(dev_priv);
6538 else if (IS_VALLEYVIEW(dev_priv))
6539 valleyview_disable_rps(dev_priv);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006540 else
Chris Wilsondc979972016-05-10 14:10:04 +01006541 gen6_disable_rps(dev_priv);
Imre Deake5347702014-11-19 15:30:02 +02006542
Chris Wilsonc0951f02013-10-10 21:58:50 +01006543 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006544 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006545 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006546}
6547
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006548static void intel_gen6_powersave_work(struct work_struct *work)
6549{
6550 struct drm_i915_private *dev_priv =
6551 container_of(work, struct drm_i915_private,
6552 rps.delayed_resume_work.work);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006553
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006554 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006555
Chris Wilsondc979972016-05-10 14:10:04 +01006556 gen6_reset_rps_interrupts(dev_priv);
Imre Deak3cc134e2014-11-19 15:30:03 +02006557
Chris Wilsondc979972016-05-10 14:10:04 +01006558 if (IS_CHERRYVIEW(dev_priv)) {
6559 cherryview_enable_rps(dev_priv);
6560 } else if (IS_VALLEYVIEW(dev_priv)) {
6561 valleyview_enable_rps(dev_priv);
6562 } else if (INTEL_INFO(dev_priv)->gen >= 9) {
6563 gen9_enable_rc6(dev_priv);
6564 gen9_enable_rps(dev_priv);
6565 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
6566 __gen6_update_ring_freq(dev_priv);
6567 } else if (IS_BROADWELL(dev_priv)) {
6568 gen8_enable_rps(dev_priv);
6569 __gen6_update_ring_freq(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006570 } else {
Chris Wilsondc979972016-05-10 14:10:04 +01006571 gen6_enable_rps(dev_priv);
6572 __gen6_update_ring_freq(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006573 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006574
6575 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6576 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6577
6578 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6579 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6580
Chris Wilsonc0951f02013-10-10 21:58:50 +01006581 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02006582
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006583 gen6_enable_rps_interrupts(dev_priv);
Imre Deak3cc134e2014-11-19 15:30:03 +02006584
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006585 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03006586
6587 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006588}
6589
Chris Wilsondc979972016-05-10 14:10:04 +01006590void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006591{
Yu Zhangf61018b2015-02-10 19:05:52 +08006592 /* Powersaving is controlled by the host when inside a VM */
Chris Wilsonc0336662016-05-06 15:40:21 +01006593 if (intel_vgpu_active(dev_priv))
Yu Zhangf61018b2015-02-10 19:05:52 +08006594 return;
6595
Chris Wilsondc979972016-05-10 14:10:04 +01006596 if (IS_IRONLAKE_M(dev_priv)) {
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006597 ironlake_enable_drps(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006598 mutex_lock(&dev_priv->dev->struct_mutex);
6599 intel_init_emon(dev_priv);
6600 mutex_unlock(&dev_priv->dev->struct_mutex);
6601 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006602 /*
6603 * PCU communication is slow and this doesn't need to be
6604 * done at any specific time, so do this out of our fast path
6605 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03006606 *
6607 * We depend on the HW RC6 power context save/restore
6608 * mechanism when entering D3 through runtime PM suspend. So
6609 * disable RPM until RPS/RC6 is properly setup. We can only
6610 * get here via the driver load/system resume/runtime resume
6611 * paths, so the _noresume version is enough (and in case of
6612 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006613 */
Imre Deakc6df39b2014-04-14 20:24:29 +03006614 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6615 round_jiffies_up_relative(HZ)))
6616 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006617 }
6618}
6619
Chris Wilsondc979972016-05-10 14:10:04 +01006620void intel_reset_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakc6df39b2014-04-14 20:24:29 +03006621{
Chris Wilsondc979972016-05-10 14:10:04 +01006622 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deakdbea3ce2014-12-15 18:59:28 +02006623 return;
6624
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006625 gen6_suspend_rps(dev_priv);
Imre Deakc6df39b2014-04-14 20:24:29 +03006626 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03006627}
6628
Daniel Vetter3107bd42012-10-31 22:52:31 +01006629static void ibx_init_clock_gating(struct drm_device *dev)
6630{
6631 struct drm_i915_private *dev_priv = dev->dev_private;
6632
6633 /*
6634 * On Ibex Peak and Cougar Point, we need to disable clock
6635 * gating for the panel power sequencer or it will fail to
6636 * start up when no ports are active.
6637 */
6638 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6639}
6640
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006641static void g4x_disable_trickle_feed(struct drm_device *dev)
6642{
6643 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006644 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006645
Damien Lespiau055e3932014-08-18 13:49:10 +01006646 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006647 I915_WRITE(DSPCNTR(pipe),
6648 I915_READ(DSPCNTR(pipe)) |
6649 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006650
6651 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6652 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006653 }
6654}
6655
Ville Syrjälä017636c2013-12-05 15:51:37 +02006656static void ilk_init_lp_watermarks(struct drm_device *dev)
6657{
6658 struct drm_i915_private *dev_priv = dev->dev_private;
6659
6660 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6661 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6662 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6663
6664 /*
6665 * Don't touch WM1S_LP_EN here.
6666 * Doing so could cause underruns.
6667 */
6668}
6669
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006670static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006671{
6672 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006673 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006674
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006675 /*
6676 * Required for FBC
6677 * WaFbcDisableDpfcClockGating:ilk
6678 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006679 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6680 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6681 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006682
6683 I915_WRITE(PCH_3DCGDIS0,
6684 MARIUNIT_CLOCK_GATE_DISABLE |
6685 SVSMUNIT_CLOCK_GATE_DISABLE);
6686 I915_WRITE(PCH_3DCGDIS1,
6687 VFMUNIT_CLOCK_GATE_DISABLE);
6688
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006689 /*
6690 * According to the spec the following bits should be set in
6691 * order to enable memory self-refresh
6692 * The bit 22/21 of 0x42004
6693 * The bit 5 of 0x42020
6694 * The bit 15 of 0x45000
6695 */
6696 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6697 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6698 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006699 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006700 I915_WRITE(DISP_ARB_CTL,
6701 (I915_READ(DISP_ARB_CTL) |
6702 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006703
6704 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006705
6706 /*
6707 * Based on the document from hardware guys the following bits
6708 * should be set unconditionally in order to enable FBC.
6709 * The bit 22 of 0x42000
6710 * The bit 22 of 0x42004
6711 * The bit 7,8,9 of 0x42020.
6712 */
6713 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006714 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006715 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6716 I915_READ(ILK_DISPLAY_CHICKEN1) |
6717 ILK_FBCQ_DIS);
6718 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6719 I915_READ(ILK_DISPLAY_CHICKEN2) |
6720 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006721 }
6722
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006723 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6724
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006725 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6726 I915_READ(ILK_DISPLAY_CHICKEN2) |
6727 ILK_ELPIN_409_SELECT);
6728 I915_WRITE(_3D_CHICKEN2,
6729 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6730 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006731
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006732 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006733 I915_WRITE(CACHE_MODE_0,
6734 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006735
Akash Goel4e046322014-04-04 17:14:38 +05306736 /* WaDisable_RenderCache_OperationalFlush:ilk */
6737 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6738
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006739 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006740
Daniel Vetter3107bd42012-10-31 22:52:31 +01006741 ibx_init_clock_gating(dev);
6742}
6743
6744static void cpt_init_clock_gating(struct drm_device *dev)
6745{
6746 struct drm_i915_private *dev_priv = dev->dev_private;
6747 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006748 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006749
6750 /*
6751 * On Ibex Peak and Cougar Point, we need to disable clock
6752 * gating for the panel power sequencer or it will fail to
6753 * start up when no ports are active.
6754 */
Jesse Barnescd664072013-10-02 10:34:19 -07006755 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6756 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6757 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006758 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6759 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006760 /* The below fixes the weird display corruption, a few pixels shifted
6761 * downward, on (only) LVDS of some HP laptops with IVY.
6762 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006763 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006764 val = I915_READ(TRANS_CHICKEN2(pipe));
6765 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6766 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006767 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006768 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006769 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6770 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6771 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006772 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6773 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006774 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006775 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006776 I915_WRITE(TRANS_CHICKEN1(pipe),
6777 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6778 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006779}
6780
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006781static void gen6_check_mch_setup(struct drm_device *dev)
6782{
6783 struct drm_i915_private *dev_priv = dev->dev_private;
6784 uint32_t tmp;
6785
6786 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006787 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6788 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6789 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006790}
6791
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006792static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006793{
6794 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006795 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006796
Damien Lespiau231e54f2012-10-19 17:55:41 +01006797 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006798
6799 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6800 I915_READ(ILK_DISPLAY_CHICKEN2) |
6801 ILK_ELPIN_409_SELECT);
6802
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006803 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006804 I915_WRITE(_3D_CHICKEN,
6805 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6806
Akash Goel4e046322014-04-04 17:14:38 +05306807 /* WaDisable_RenderCache_OperationalFlush:snb */
6808 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6809
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006810 /*
6811 * BSpec recoomends 8x4 when MSAA is used,
6812 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006813 *
6814 * Note that PS/WM thread counts depend on the WIZ hashing
6815 * disable bit, which we don't touch here, but it's good
6816 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006817 */
6818 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006819 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006820
Ville Syrjälä017636c2013-12-05 15:51:37 +02006821 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006822
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006823 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006824 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006825
6826 I915_WRITE(GEN6_UCGCTL1,
6827 I915_READ(GEN6_UCGCTL1) |
6828 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6829 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6830
6831 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6832 * gating disable must be set. Failure to set it results in
6833 * flickering pixels due to Z write ordering failures after
6834 * some amount of runtime in the Mesa "fire" demo, and Unigine
6835 * Sanctuary and Tropics, and apparently anything else with
6836 * alpha test or pixel discard.
6837 *
6838 * According to the spec, bit 11 (RCCUNIT) must also be set,
6839 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006840 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006841 * WaDisableRCCUnitClockGating:snb
6842 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006843 */
6844 I915_WRITE(GEN6_UCGCTL2,
6845 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6846 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6847
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006848 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006849 I915_WRITE(_3D_CHICKEN3,
6850 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006851
6852 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006853 * Bspec says:
6854 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6855 * 3DSTATE_SF number of SF output attributes is more than 16."
6856 */
6857 I915_WRITE(_3D_CHICKEN3,
6858 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6859
6860 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006861 * According to the spec the following bits should be
6862 * set in order to enable memory self-refresh and fbc:
6863 * The bit21 and bit22 of 0x42000
6864 * The bit21 and bit22 of 0x42004
6865 * The bit5 and bit7 of 0x42020
6866 * The bit14 of 0x70180
6867 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006868 *
6869 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006870 */
6871 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6872 I915_READ(ILK_DISPLAY_CHICKEN1) |
6873 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6874 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6875 I915_READ(ILK_DISPLAY_CHICKEN2) |
6876 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006877 I915_WRITE(ILK_DSPCLK_GATE_D,
6878 I915_READ(ILK_DSPCLK_GATE_D) |
6879 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6880 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006881
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006882 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006883
Daniel Vetter3107bd42012-10-31 22:52:31 +01006884 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006885
6886 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006887}
6888
6889static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6890{
6891 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6892
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006893 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006894 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006895 *
6896 * This actually overrides the dispatch
6897 * mode for all thread types.
6898 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006899 reg &= ~GEN7_FF_SCHED_MASK;
6900 reg |= GEN7_FF_TS_SCHED_HW;
6901 reg |= GEN7_FF_VS_SCHED_HW;
6902 reg |= GEN7_FF_DS_SCHED_HW;
6903
6904 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6905}
6906
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006907static void lpt_init_clock_gating(struct drm_device *dev)
6908{
6909 struct drm_i915_private *dev_priv = dev->dev_private;
6910
6911 /*
6912 * TODO: this bit should only be enabled when really needed, then
6913 * disabled when not needed anymore in order to save power.
6914 */
Ville Syrjäläc2699522015-08-27 23:55:59 +03006915 if (HAS_PCH_LPT_LP(dev))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006916 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6917 I915_READ(SOUTH_DSPCLK_GATE_D) |
6918 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006919
6920 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03006921 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6922 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006923 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006924}
6925
Imre Deak7d708ee2013-04-17 14:04:50 +03006926static void lpt_suspend_hw(struct drm_device *dev)
6927{
6928 struct drm_i915_private *dev_priv = dev->dev_private;
6929
Ville Syrjäläc2699522015-08-27 23:55:59 +03006930 if (HAS_PCH_LPT_LP(dev)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03006931 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6932
6933 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6934 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6935 }
6936}
6937
Imre Deak450174f2016-05-03 15:54:21 +03006938static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
6939 int general_prio_credits,
6940 int high_prio_credits)
6941{
6942 u32 misccpctl;
6943
6944 /* WaTempDisableDOPClkGating:bdw */
6945 misccpctl = I915_READ(GEN7_MISCCPCTL);
6946 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6947
6948 I915_WRITE(GEN8_L3SQCREG1,
6949 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
6950 L3_HIGH_PRIO_CREDITS(high_prio_credits));
6951
6952 /*
6953 * Wait at least 100 clocks before re-enabling clock gating.
6954 * See the definition of L3SQCREG1 in BSpec.
6955 */
6956 POSTING_READ(GEN8_L3SQCREG1);
6957 udelay(1);
6958 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6959}
6960
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02006961static void skylake_init_clock_gating(struct drm_device *dev)
6962{
6963 struct drm_i915_private *dev_priv = dev->dev_private;
6964
6965 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,kbl */
6966 I915_WRITE(CHICKEN_PAR1_1,
6967 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
6968}
6969
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006970static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006971{
6972 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006973 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006974
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03006975 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006976
Ben Widawskyab57fff2013-12-12 15:28:04 -08006977 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006978 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006979
Ben Widawskyab57fff2013-12-12 15:28:04 -08006980 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006981 I915_WRITE(CHICKEN_PAR1_1,
6982 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6983
Ben Widawskyab57fff2013-12-12 15:28:04 -08006984 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006985 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006986 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006987 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006988 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006989 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006990
Ben Widawskyab57fff2013-12-12 15:28:04 -08006991 /* WaVSRefCountFullforceMissDisable:bdw */
6992 /* WaDSRefCountFullforceMissDisable:bdw */
6993 I915_WRITE(GEN7_FF_THREAD_MODE,
6994 I915_READ(GEN7_FF_THREAD_MODE) &
6995 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006996
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006997 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6998 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006999
7000 /* WaDisableSDEUnitClockGating:bdw */
7001 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7002 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007003
Imre Deak450174f2016-05-03 15:54:21 +03007004 /* WaProgramL3SqcReg1Default:bdw */
7005 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007006
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007007 /*
7008 * WaGttCachingOffByDefault:bdw
7009 * GTT cache may not work with big pages, so if those
7010 * are ever enabled GTT cache may need to be disabled.
7011 */
7012 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7013
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03007014 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007015}
7016
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007017static void haswell_init_clock_gating(struct drm_device *dev)
7018{
7019 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007020
Ville Syrjälä017636c2013-12-05 15:51:37 +02007021 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007022
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007023 /* L3 caching of data atomics doesn't work -- disable it. */
7024 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7025 I915_WRITE(HSW_ROW_CHICKEN3,
7026 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7027
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007028 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007029 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7030 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7031 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7032
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007033 /* WaVSRefCountFullforceMissDisable:hsw */
7034 I915_WRITE(GEN7_FF_THREAD_MODE,
7035 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007036
Akash Goel4e046322014-04-04 17:14:38 +05307037 /* WaDisable_RenderCache_OperationalFlush:hsw */
7038 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7039
Chia-I Wufe27c602014-01-28 13:29:33 +08007040 /* enable HiZ Raw Stall Optimization */
7041 I915_WRITE(CACHE_MODE_0_GEN7,
7042 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7043
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007044 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007045 I915_WRITE(CACHE_MODE_1,
7046 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007047
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007048 /*
7049 * BSpec recommends 8x4 when MSAA is used,
7050 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007051 *
7052 * Note that PS/WM thread counts depend on the WIZ hashing
7053 * disable bit, which we don't touch here, but it's good
7054 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007055 */
7056 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007057 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007058
Kenneth Graunke94411592014-12-31 16:23:00 -08007059 /* WaSampleCChickenBitEnable:hsw */
7060 I915_WRITE(HALF_SLICE_CHICKEN3,
7061 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7062
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007063 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007064 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7065
Paulo Zanoni90a88642013-05-03 17:23:45 -03007066 /* WaRsPkgCStateDisplayPMReq:hsw */
7067 I915_WRITE(CHICKEN_PAR1_1,
7068 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007069
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007070 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007071}
7072
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007073static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007074{
7075 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07007076 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007077
Ville Syrjälä017636c2013-12-05 15:51:37 +02007078 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007079
Damien Lespiau231e54f2012-10-19 17:55:41 +01007080 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007081
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007082 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007083 I915_WRITE(_3D_CHICKEN3,
7084 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7085
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007086 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007087 I915_WRITE(IVB_CHICKEN3,
7088 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7089 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7090
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007091 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07007092 if (IS_IVB_GT1(dev))
7093 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7094 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007095
Akash Goel4e046322014-04-04 17:14:38 +05307096 /* WaDisable_RenderCache_OperationalFlush:ivb */
7097 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7098
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007099 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007100 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7101 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7102
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007103 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007104 I915_WRITE(GEN7_L3CNTLREG1,
7105 GEN7_WA_FOR_GEN7_L3_CONTROL);
7106 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007107 GEN7_WA_L3_CHICKEN_MODE);
7108 if (IS_IVB_GT1(dev))
7109 I915_WRITE(GEN7_ROW_CHICKEN2,
7110 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007111 else {
7112 /* must write both registers */
7113 I915_WRITE(GEN7_ROW_CHICKEN2,
7114 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007115 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7116 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007117 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007118
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007119 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007120 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7121 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7122
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007123 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007124 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007125 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007126 */
7127 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007128 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007129
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007130 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007131 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7132 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7133 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7134
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007135 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007136
7137 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007138
Chris Wilson22721342014-03-04 09:41:43 +00007139 if (0) { /* causes HiZ corruption on ivb:gt1 */
7140 /* enable HiZ Raw Stall Optimization */
7141 I915_WRITE(CACHE_MODE_0_GEN7,
7142 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7143 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007144
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007145 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007146 I915_WRITE(CACHE_MODE_1,
7147 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007148
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007149 /*
7150 * BSpec recommends 8x4 when MSAA is used,
7151 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007152 *
7153 * Note that PS/WM thread counts depend on the WIZ hashing
7154 * disable bit, which we don't touch here, but it's good
7155 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007156 */
7157 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007158 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007159
Ben Widawsky20848222012-05-04 18:58:59 -07007160 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7161 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7162 snpcr |= GEN6_MBC_SNPCR_MED;
7163 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007164
Ben Widawskyab5c6082013-04-05 13:12:41 -07007165 if (!HAS_PCH_NOP(dev))
7166 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007167
7168 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007169}
7170
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007171static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007172{
7173 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007174
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007175 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007176 I915_WRITE(_3D_CHICKEN3,
7177 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7178
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007179 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007180 I915_WRITE(IVB_CHICKEN3,
7181 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7182 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7183
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007184 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007185 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007186 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007187 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7188 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007189
Akash Goel4e046322014-04-04 17:14:38 +05307190 /* WaDisable_RenderCache_OperationalFlush:vlv */
7191 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7192
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007193 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007194 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7195 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7196
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007197 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007198 I915_WRITE(GEN7_ROW_CHICKEN2,
7199 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7200
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007201 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007202 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7203 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7204 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7205
Ville Syrjälä46680e02014-01-22 21:33:01 +02007206 gen7_setup_fixed_func_scheduler(dev_priv);
7207
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007208 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007209 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007210 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007211 */
7212 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007213 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007214
Akash Goelc98f5062014-03-24 23:00:07 +05307215 /* WaDisableL3Bank2xClockGate:vlv
7216 * Disabling L3 clock gating- MMIO 940c[25] = 1
7217 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7218 I915_WRITE(GEN7_UCGCTL4,
7219 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007220
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007221 /*
7222 * BSpec says this must be set, even though
7223 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7224 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007225 I915_WRITE(CACHE_MODE_1,
7226 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007227
7228 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007229 * BSpec recommends 8x4 when MSAA is used,
7230 * however in practice 16x4 seems fastest.
7231 *
7232 * Note that PS/WM thread counts depend on the WIZ hashing
7233 * disable bit, which we don't touch here, but it's good
7234 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7235 */
7236 I915_WRITE(GEN7_GT_MODE,
7237 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7238
7239 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007240 * WaIncreaseL3CreditsForVLVB0:vlv
7241 * This is the hardware default actually.
7242 */
7243 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7244
7245 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007246 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007247 * Disable clock gating on th GCFG unit to prevent a delay
7248 * in the reporting of vblank events.
7249 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007250 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007251}
7252
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007253static void cherryview_init_clock_gating(struct drm_device *dev)
7254{
7255 struct drm_i915_private *dev_priv = dev->dev_private;
7256
Ville Syrjälä232ce332014-04-09 13:28:35 +03007257 /* WaVSRefCountFullforceMissDisable:chv */
7258 /* WaDSRefCountFullforceMissDisable:chv */
7259 I915_WRITE(GEN7_FF_THREAD_MODE,
7260 I915_READ(GEN7_FF_THREAD_MODE) &
7261 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007262
7263 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7264 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7265 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007266
7267 /* WaDisableCSUnitClockGating:chv */
7268 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7269 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007270
7271 /* WaDisableSDEUnitClockGating:chv */
7272 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7273 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007274
7275 /*
Imre Deak450174f2016-05-03 15:54:21 +03007276 * WaProgramL3SqcReg1Default:chv
7277 * See gfxspecs/Related Documents/Performance Guide/
7278 * LSQC Setting Recommendations.
7279 */
7280 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7281
7282 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007283 * GTT cache may not work with big pages, so if those
7284 * are ever enabled GTT cache may need to be disabled.
7285 */
7286 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007287}
7288
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007289static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007290{
7291 struct drm_i915_private *dev_priv = dev->dev_private;
7292 uint32_t dspclk_gate;
7293
7294 I915_WRITE(RENCLK_GATE_D1, 0);
7295 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7296 GS_UNIT_CLOCK_GATE_DISABLE |
7297 CL_UNIT_CLOCK_GATE_DISABLE);
7298 I915_WRITE(RAMCLK_GATE_D, 0);
7299 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7300 OVRUNIT_CLOCK_GATE_DISABLE |
7301 OVCUNIT_CLOCK_GATE_DISABLE;
7302 if (IS_GM45(dev))
7303 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7304 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007305
7306 /* WaDisableRenderCachePipelinedFlush */
7307 I915_WRITE(CACHE_MODE_0,
7308 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007309
Akash Goel4e046322014-04-04 17:14:38 +05307310 /* WaDisable_RenderCache_OperationalFlush:g4x */
7311 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7312
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007313 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007314}
7315
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007316static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007317{
7318 struct drm_i915_private *dev_priv = dev->dev_private;
7319
7320 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7321 I915_WRITE(RENCLK_GATE_D2, 0);
7322 I915_WRITE(DSPCLK_GATE_D, 0);
7323 I915_WRITE(RAMCLK_GATE_D, 0);
7324 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007325 I915_WRITE(MI_ARB_STATE,
7326 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307327
7328 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7329 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007330}
7331
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007332static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007333{
7334 struct drm_i915_private *dev_priv = dev->dev_private;
7335
7336 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7337 I965_RCC_CLOCK_GATE_DISABLE |
7338 I965_RCPB_CLOCK_GATE_DISABLE |
7339 I965_ISC_CLOCK_GATE_DISABLE |
7340 I965_FBC_CLOCK_GATE_DISABLE);
7341 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007342 I915_WRITE(MI_ARB_STATE,
7343 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307344
7345 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7346 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007347}
7348
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007349static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007350{
7351 struct drm_i915_private *dev_priv = dev->dev_private;
7352 u32 dstate = I915_READ(D_STATE);
7353
7354 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7355 DSTATE_DOT_CLOCK_GATING;
7356 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007357
7358 if (IS_PINEVIEW(dev))
7359 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007360
7361 /* IIR "flip pending" means done if this bit is set */
7362 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007363
7364 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007365 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007366
7367 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7368 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007369
7370 I915_WRITE(MI_ARB_STATE,
7371 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007372}
7373
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007374static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007375{
7376 struct drm_i915_private *dev_priv = dev->dev_private;
7377
7378 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007379
7380 /* interrupts should cause a wake up from C3 */
7381 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7382 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007383
7384 I915_WRITE(MEM_MODE,
7385 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007386}
7387
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007388static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007389{
7390 struct drm_i915_private *dev_priv = dev->dev_private;
7391
7392 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007393
7394 I915_WRITE(MEM_MODE,
7395 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7396 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007397}
7398
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007399void intel_init_clock_gating(struct drm_device *dev)
7400{
7401 struct drm_i915_private *dev_priv = dev->dev_private;
7402
Imre Deakbb400da2016-03-16 13:38:54 +02007403 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007404}
7405
Imre Deak7d708ee2013-04-17 14:04:50 +03007406void intel_suspend_hw(struct drm_device *dev)
7407{
7408 if (HAS_PCH_LPT(dev))
7409 lpt_suspend_hw(dev);
7410}
7411
Imre Deakbb400da2016-03-16 13:38:54 +02007412static void nop_init_clock_gating(struct drm_device *dev)
7413{
7414 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7415}
7416
7417/**
7418 * intel_init_clock_gating_hooks - setup the clock gating hooks
7419 * @dev_priv: device private
7420 *
7421 * Setup the hooks that configure which clocks of a given platform can be
7422 * gated and also apply various GT and display specific workarounds for these
7423 * platforms. Note that some GT specific workarounds are applied separately
7424 * when GPU contexts or batchbuffers start their execution.
7425 */
7426void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7427{
7428 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007429 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007430 else if (IS_KABYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007431 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007432 else if (IS_BROXTON(dev_priv))
7433 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7434 else if (IS_BROADWELL(dev_priv))
7435 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7436 else if (IS_CHERRYVIEW(dev_priv))
7437 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7438 else if (IS_HASWELL(dev_priv))
7439 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7440 else if (IS_IVYBRIDGE(dev_priv))
7441 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7442 else if (IS_VALLEYVIEW(dev_priv))
7443 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7444 else if (IS_GEN6(dev_priv))
7445 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7446 else if (IS_GEN5(dev_priv))
7447 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7448 else if (IS_G4X(dev_priv))
7449 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7450 else if (IS_CRESTLINE(dev_priv))
7451 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7452 else if (IS_BROADWATER(dev_priv))
7453 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7454 else if (IS_GEN3(dev_priv))
7455 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7456 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7457 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7458 else if (IS_GEN2(dev_priv))
7459 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7460 else {
7461 MISSING_CASE(INTEL_DEVID(dev_priv));
7462 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7463 }
7464}
7465
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007466/* Set up chip specific power management-related functions */
7467void intel_init_pm(struct drm_device *dev)
7468{
7469 struct drm_i915_private *dev_priv = dev->dev_private;
7470
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007471 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007472
Daniel Vetterc921aba2012-04-26 23:28:17 +02007473 /* For cxsr */
7474 if (IS_PINEVIEW(dev))
7475 i915_pineview_get_mem_freq(dev);
7476 else if (IS_GEN5(dev))
7477 i915_ironlake_get_mem_freq(dev);
7478
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007479 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007480 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007481 skl_setup_wm_latency(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007482 dev_priv->display.update_wm = skl_update_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007483 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05307484 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007485 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007486
Ville Syrjäläbd602542014-01-07 16:14:10 +02007487 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7488 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7489 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7490 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007491 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007492 dev_priv->display.compute_intermediate_wm =
7493 ilk_compute_intermediate_wm;
7494 dev_priv->display.initial_watermarks =
7495 ilk_initial_watermarks;
7496 dev_priv->display.optimize_watermarks =
7497 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007498 } else {
7499 DRM_DEBUG_KMS("Failed to read display plane latency. "
7500 "Disable CxSR\n");
7501 }
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007502 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007503 vlv_setup_wm_latency(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007504 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007505 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007506 vlv_setup_wm_latency(dev);
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007507 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007508 } else if (IS_PINEVIEW(dev)) {
7509 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7510 dev_priv->is_ddr3,
7511 dev_priv->fsb_freq,
7512 dev_priv->mem_freq)) {
7513 DRM_INFO("failed to find known CxSR latency "
7514 "(found ddr%s fsb freq %d, mem freq %d), "
7515 "disabling CxSR\n",
7516 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7517 dev_priv->fsb_freq, dev_priv->mem_freq);
7518 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007519 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007520 dev_priv->display.update_wm = NULL;
7521 } else
7522 dev_priv->display.update_wm = pineview_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007523 } else if (IS_G4X(dev)) {
7524 dev_priv->display.update_wm = g4x_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007525 } else if (IS_GEN4(dev)) {
7526 dev_priv->display.update_wm = i965_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007527 } else if (IS_GEN3(dev)) {
7528 dev_priv->display.update_wm = i9xx_update_wm;
7529 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007530 } else if (IS_GEN2(dev)) {
7531 if (INTEL_INFO(dev)->num_pipes == 1) {
7532 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007533 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007534 } else {
7535 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007536 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007537 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007538 } else {
7539 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007540 }
7541}
7542
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007543int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007544{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007545 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007546
7547 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7548 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7549 return -EAGAIN;
7550 }
7551
7552 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00007553 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007554 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7555
7556 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7557 500)) {
7558 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7559 return -ETIMEDOUT;
7560 }
7561
7562 *val = I915_READ(GEN6_PCODE_DATA);
7563 I915_WRITE(GEN6_PCODE_DATA, 0);
7564
7565 return 0;
7566}
7567
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007568int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007569{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007570 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007571
7572 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7573 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7574 return -EAGAIN;
7575 }
7576
7577 I915_WRITE(GEN6_PCODE_DATA, val);
7578 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7579
7580 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7581 500)) {
7582 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7583 return -ETIMEDOUT;
7584 }
7585
7586 I915_WRITE(GEN6_PCODE_DATA, 0);
7587
7588 return 0;
7589}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007590
Ville Syrjälädd06f882014-11-10 22:55:12 +02007591static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7592{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007593 /*
7594 * N = val - 0xb7
7595 * Slow = Fast = GPLL ref * N
7596 */
7597 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007598}
7599
Fengguang Wub55dd642014-07-12 11:21:39 +02007600static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007601{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007602 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007603}
7604
Fengguang Wub55dd642014-07-12 11:21:39 +02007605static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307606{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007607 /*
7608 * N = val / 2
7609 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7610 */
7611 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307612}
7613
Fengguang Wub55dd642014-07-12 11:21:39 +02007614static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307615{
Ville Syrjälä1c147622014-08-18 14:42:43 +03007616 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007617 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307618}
7619
Ville Syrjälä616bc822015-01-23 21:04:25 +02007620int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7621{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007622 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007623 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7624 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007625 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007626 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007627 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007628 return byt_gpu_freq(dev_priv, val);
7629 else
7630 return val * GT_FREQUENCY_MULTIPLIER;
7631}
7632
Ville Syrjälä616bc822015-01-23 21:04:25 +02007633int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7634{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007635 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007636 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7637 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007638 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007639 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007640 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007641 return byt_freq_opcode(dev_priv, val);
7642 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007643 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05307644}
7645
Chris Wilson6ad790c2015-04-07 16:20:31 +01007646struct request_boost {
7647 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02007648 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007649};
7650
7651static void __intel_rps_boost_work(struct work_struct *work)
7652{
7653 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01007654 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007655
Chris Wilsone61b9952015-04-27 13:41:24 +01007656 if (!i915_gem_request_completed(req, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01007657 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007658
Chris Wilson73db04c2016-04-28 09:56:55 +01007659 i915_gem_request_unreference(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007660 kfree(boost);
7661}
7662
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007663void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007664{
7665 struct request_boost *boost;
7666
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007667 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007668 return;
7669
Chris Wilsone61b9952015-04-27 13:41:24 +01007670 if (i915_gem_request_completed(req, true))
7671 return;
7672
Chris Wilson6ad790c2015-04-07 16:20:31 +01007673 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7674 if (boost == NULL)
7675 return;
7676
Daniel Vettereed29a52015-05-21 14:21:25 +02007677 i915_gem_request_reference(req);
7678 boost->req = req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007679
7680 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007681 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007682}
7683
Daniel Vetterf742a552013-12-06 10:17:53 +01007684void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007685{
7686 struct drm_i915_private *dev_priv = dev->dev_private;
7687
Daniel Vetterf742a552013-12-06 10:17:53 +01007688 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01007689 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01007690
Chris Wilson907b28c2013-07-19 20:36:52 +01007691 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7692 intel_gen6_powersave_work);
Chris Wilson1854d5c2015-04-07 16:20:32 +01007693 INIT_LIST_HEAD(&dev_priv->rps.clients);
Chris Wilson2e1b8732015-04-27 13:41:22 +01007694 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7695 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007696
Paulo Zanoni33688d92014-03-07 20:08:19 -03007697 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02007698 atomic_set(&dev_priv->pm.wakeref_count, 0);
Imre Deak2b19efe2015-12-15 20:10:37 +02007699 atomic_set(&dev_priv->pm.atomic_seq, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007700}