blob: be8a96743e865a0c3afc8063e2c0257fbb24f80c [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 Deakd965e7ac2015-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äfacd619b2013-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äfacd619b2013-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äfacd619b2013-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, Mahesh8d19d7d2016-05-19 15:03:01 -07003000 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003001 uint32_t width = 0, height = 0;
Matt Ropera1de91e2016-05-12 07:05:57 -07003002 unsigned format = fb ? fb->pixel_format : DRM_FORMAT_XRGB8888;
3003
3004 if (!intel_pstate->visible)
3005 return 0;
3006 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3007 return 0;
3008 if (y && format != DRM_FORMAT_NV12)
3009 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003010
3011 width = drm_rect_width(&intel_pstate->src) >> 16;
3012 height = drm_rect_height(&intel_pstate->src) >> 16;
3013
3014 if (intel_rotation_90_or_270(pstate->rotation))
3015 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003016
3017 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003018 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003019 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003020 data_rate = width * height *
Matt Ropera1de91e2016-05-12 07:05:57 -07003021 drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003022 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003023 data_rate = (width / 2) * (height / 2) *
Matt Ropera1de91e2016-05-12 07:05:57 -07003024 drm_format_plane_cpp(format, 1);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003025 } else {
3026 /* for packed formats */
3027 data_rate = width * height * drm_format_plane_cpp(format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003028 }
3029
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003030 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3031
3032 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003033}
3034
3035/*
3036 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3037 * a 8192x4096@32bpp framebuffer:
3038 * 3 * 4096 * 8192 * 4 < 2^32
3039 */
3040static unsigned int
Matt Roper9c74d822016-05-12 07:05:58 -07003041skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003042{
Matt Roper9c74d822016-05-12 07:05:58 -07003043 struct drm_crtc_state *cstate = &intel_cstate->base;
3044 struct drm_atomic_state *state = cstate->state;
3045 struct drm_crtc *crtc = cstate->crtc;
3046 struct drm_device *dev = crtc->dev;
3047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Ropera6d3460e2016-05-12 07:06:04 -07003048 const struct drm_plane *plane;
Matt Roper024c9042015-09-24 15:53:11 -07003049 const struct intel_plane *intel_plane;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003050 struct drm_plane_state *pstate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003051 unsigned int rate, total_data_rate = 0;
Matt Roper9c74d822016-05-12 07:05:58 -07003052 int id;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003053 int i;
3054
3055 if (WARN_ON(!state))
3056 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003057
Matt Ropera1de91e2016-05-12 07:05:57 -07003058 /* Calculate and cache data rate for each plane */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003059 for_each_plane_in_state(state, plane, pstate, i) {
3060 id = skl_wm_plane_id(to_intel_plane(plane));
3061 intel_plane = to_intel_plane(plane);
Matt Roper024c9042015-09-24 15:53:11 -07003062
Matt Ropera6d3460e2016-05-12 07:06:04 -07003063 if (intel_plane->pipe != intel_crtc->pipe)
3064 continue;
Matt Roper024c9042015-09-24 15:53:11 -07003065
Matt Ropera6d3460e2016-05-12 07:06:04 -07003066 /* packed/uv */
3067 rate = skl_plane_relative_data_rate(intel_cstate,
3068 pstate, 0);
3069 intel_cstate->wm.skl.plane_data_rate[id] = rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003070
Matt Ropera6d3460e2016-05-12 07:06:04 -07003071 /* y-plane */
3072 rate = skl_plane_relative_data_rate(intel_cstate,
3073 pstate, 1);
3074 intel_cstate->wm.skl.plane_y_data_rate[id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003075 }
3076
3077 /* Calculate CRTC's total data rate from cached values */
3078 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3079 int id = skl_wm_plane_id(intel_plane);
3080
3081 /* packed/uv */
Matt Roper9c74d822016-05-12 07:05:58 -07003082 total_data_rate += intel_cstate->wm.skl.plane_data_rate[id];
3083 total_data_rate += intel_cstate->wm.skl.plane_y_data_rate[id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003084 }
3085
Matt Roper9c74d822016-05-12 07:05:58 -07003086 WARN_ON(cstate->plane_mask && total_data_rate == 0);
3087
Damien Lespiaub9cec072014-11-04 17:06:43 +00003088 return total_data_rate;
3089}
3090
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003091static uint16_t
3092skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3093 const int y)
3094{
3095 struct drm_framebuffer *fb = pstate->fb;
3096 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3097 uint32_t src_w, src_h;
3098 uint32_t min_scanlines = 8;
3099 uint8_t plane_bpp;
3100
3101 if (WARN_ON(!fb))
3102 return 0;
3103
3104 /* For packed formats, no y-plane, return 0 */
3105 if (y && fb->pixel_format != DRM_FORMAT_NV12)
3106 return 0;
3107
3108 /* For Non Y-tile return 8-blocks */
3109 if (fb->modifier[0] != I915_FORMAT_MOD_Y_TILED &&
3110 fb->modifier[0] != I915_FORMAT_MOD_Yf_TILED)
3111 return 8;
3112
3113 src_w = drm_rect_width(&intel_pstate->src) >> 16;
3114 src_h = drm_rect_height(&intel_pstate->src) >> 16;
3115
3116 if (intel_rotation_90_or_270(pstate->rotation))
3117 swap(src_w, src_h);
3118
3119 /* Halve UV plane width and height for NV12 */
3120 if (fb->pixel_format == DRM_FORMAT_NV12 && !y) {
3121 src_w /= 2;
3122 src_h /= 2;
3123 }
3124
3125 if (fb->pixel_format == DRM_FORMAT_NV12 && !y)
3126 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 1);
3127 else
3128 plane_bpp = drm_format_plane_cpp(fb->pixel_format, 0);
3129
3130 if (intel_rotation_90_or_270(pstate->rotation)) {
3131 switch (plane_bpp) {
3132 case 1:
3133 min_scanlines = 32;
3134 break;
3135 case 2:
3136 min_scanlines = 16;
3137 break;
3138 case 4:
3139 min_scanlines = 8;
3140 break;
3141 case 8:
3142 min_scanlines = 4;
3143 break;
3144 default:
3145 WARN(1, "Unsupported pixel depth %u for rotation",
3146 plane_bpp);
3147 min_scanlines = 32;
3148 }
3149 }
3150
3151 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3152}
3153
Matt Roperc107acf2016-05-12 07:06:01 -07003154static int
Matt Roper024c9042015-09-24 15:53:11 -07003155skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003156 struct skl_ddb_allocation *ddb /* out */)
3157{
Matt Roperc107acf2016-05-12 07:06:01 -07003158 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003159 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003160 struct drm_device *dev = crtc->dev;
3161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003162 struct intel_plane *intel_plane;
Matt Roperc107acf2016-05-12 07:06:01 -07003163 struct drm_plane *plane;
3164 struct drm_plane_state *pstate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003165 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003166 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003167 uint16_t alloc_size, start, cursor_blocks;
Matt Roper86a2100a2016-05-12 07:05:59 -07003168 uint16_t *minimum = cstate->wm.skl.minimum_blocks;
3169 uint16_t *y_minimum = cstate->wm.skl.minimum_y_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003170 unsigned int total_data_rate;
Matt Roperc107acf2016-05-12 07:06:01 -07003171 int num_active;
3172 int id, i;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003173
Matt Ropera6d3460e2016-05-12 07:06:04 -07003174 if (WARN_ON(!state))
3175 return 0;
3176
Matt Roperc107acf2016-05-12 07:06:01 -07003177 if (!cstate->base.active) {
3178 ddb->pipe[pipe].start = ddb->pipe[pipe].end = 0;
3179 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3180 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3181 return 0;
3182 }
3183
Matt Ropera6d3460e2016-05-12 07:06:04 -07003184 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003185 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003186 if (alloc_size == 0) {
3187 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003188 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003189 }
3190
Matt Roperc107acf2016-05-12 07:06:01 -07003191 cursor_blocks = skl_cursor_allocation(num_active);
Matt Roper4969d332015-09-24 15:53:10 -07003192 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
3193 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003194
3195 alloc_size -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003196
Damien Lespiau80958152015-02-09 13:35:10 +00003197 /* 1. Allocate the mininum required blocks for each active plane */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003198 for_each_plane_in_state(state, plane, pstate, i) {
3199 intel_plane = to_intel_plane(plane);
3200 id = skl_wm_plane_id(intel_plane);
Damien Lespiau80958152015-02-09 13:35:10 +00003201
Matt Ropera6d3460e2016-05-12 07:06:04 -07003202 if (intel_plane->pipe != pipe)
3203 continue;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003204
Matt Ropera6d3460e2016-05-12 07:06:04 -07003205 if (!to_intel_plane_state(pstate)->visible) {
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 if (plane->type == DRM_PLANE_TYPE_CURSOR) {
3211 minimum[id] = 0;
3212 y_minimum[id] = 0;
3213 continue;
Matt Roperc107acf2016-05-12 07:06:01 -07003214 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003215
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003216 minimum[id] = skl_ddb_min_alloc(pstate, 0);
3217 y_minimum[id] = skl_ddb_min_alloc(pstate, 1);
Matt Roperc107acf2016-05-12 07:06:01 -07003218 }
3219
3220 for (i = 0; i < PLANE_CURSOR; i++) {
3221 alloc_size -= minimum[i];
3222 alloc_size -= y_minimum[i];
Damien Lespiau80958152015-02-09 13:35:10 +00003223 }
3224
Damien Lespiaub9cec072014-11-04 17:06:43 +00003225 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003226 * 2. Distribute the remaining space in proportion to the amount of
3227 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003228 *
3229 * FIXME: we may not allocate every single block here.
3230 */
Matt Roper024c9042015-09-24 15:53:11 -07003231 total_data_rate = skl_get_total_relative_data_rate(cstate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003232 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003233 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003234
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003235 start = alloc->start;
Matt Roper024c9042015-09-24 15:53:11 -07003236 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003237 unsigned int data_rate, y_data_rate;
3238 uint16_t plane_blocks, y_plane_blocks = 0;
Matt Roper024c9042015-09-24 15:53:11 -07003239 int id = skl_wm_plane_id(intel_plane);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003240
Matt Ropera1de91e2016-05-12 07:05:57 -07003241 data_rate = cstate->wm.skl.plane_data_rate[id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003242
3243 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003244 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003245 * promote the expression to 64 bits to avoid overflowing, the
3246 * result is < available as data_rate / total_data_rate < 1
3247 */
Matt Roper024c9042015-09-24 15:53:11 -07003248 plane_blocks = minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003249 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3250 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003251
Matt Roperc107acf2016-05-12 07:06:01 -07003252 /* Leave disabled planes at (0,0) */
3253 if (data_rate) {
3254 ddb->plane[pipe][id].start = start;
3255 ddb->plane[pipe][id].end = start + plane_blocks;
3256 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003257
3258 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003259
3260 /*
3261 * allocation for y_plane part of planar format:
3262 */
Matt Ropera1de91e2016-05-12 07:05:57 -07003263 y_data_rate = cstate->wm.skl.plane_y_data_rate[id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003264
Matt Ropera1de91e2016-05-12 07:05:57 -07003265 y_plane_blocks = y_minimum[id];
3266 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3267 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003268
Matt Roperc107acf2016-05-12 07:06:01 -07003269 if (y_data_rate) {
3270 ddb->y_plane[pipe][id].start = start;
3271 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
3272 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003273
Matt Ropera1de91e2016-05-12 07:05:57 -07003274 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003275 }
3276
Matt Roperc107acf2016-05-12 07:06:01 -07003277 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003278}
3279
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02003280static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003281{
3282 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02003283 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003284}
3285
3286/*
3287 * 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 +02003288 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003289 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3290 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3291*/
Ville Syrjäläac484962016-01-20 21:05:26 +02003292static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003293{
3294 uint32_t wm_intermediate_val, ret;
3295
3296 if (latency == 0)
3297 return UINT_MAX;
3298
Ville Syrjäläac484962016-01-20 21:05:26 +02003299 wm_intermediate_val = latency * pixel_rate * cpp / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003300 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3301
3302 return ret;
3303}
3304
3305static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02003306 uint32_t horiz_pixels, uint8_t cpp,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003307 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003308{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003309 uint32_t ret;
3310 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3311 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003312
3313 if (latency == 0)
3314 return UINT_MAX;
3315
Ville Syrjäläac484962016-01-20 21:05:26 +02003316 plane_bytes_per_line = horiz_pixels * cpp;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003317
3318 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3319 tiling == I915_FORMAT_MOD_Yf_TILED) {
3320 plane_bytes_per_line *= 4;
3321 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3322 plane_blocks_per_line /= 4;
3323 } else {
3324 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3325 }
3326
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003327 wm_intermediate_val = latency * pixel_rate;
3328 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003329 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003330
3331 return ret;
3332}
3333
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003334static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3335 struct intel_plane_state *pstate)
3336{
3337 uint64_t adjusted_pixel_rate;
3338 uint64_t downscale_amount;
3339 uint64_t pixel_rate;
3340
3341 /* Shouldn't reach here on disabled planes... */
3342 if (WARN_ON(!pstate->visible))
3343 return 0;
3344
3345 /*
3346 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3347 * with additional adjustments for plane-specific scaling.
3348 */
3349 adjusted_pixel_rate = skl_pipe_pixel_rate(cstate);
3350 downscale_amount = skl_plane_downscale_amount(pstate);
3351
3352 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3353 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3354
3355 return pixel_rate;
3356}
3357
Matt Roper55994c22016-05-12 07:06:08 -07003358static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3359 struct intel_crtc_state *cstate,
3360 struct intel_plane_state *intel_pstate,
3361 uint16_t ddb_allocation,
3362 int level,
3363 uint16_t *out_blocks, /* out */
3364 uint8_t *out_lines, /* out */
3365 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003366{
Matt Roper33815fa2016-05-12 07:06:05 -07003367 struct drm_plane_state *pstate = &intel_pstate->base;
3368 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003369 uint32_t latency = dev_priv->wm.skl_latency[level];
3370 uint32_t method1, method2;
3371 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3372 uint32_t res_blocks, res_lines;
3373 uint32_t selected_result;
Ville Syrjäläac484962016-01-20 21:05:26 +02003374 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003375 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003376 uint32_t plane_pixel_rate;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003377
Matt Roper55994c22016-05-12 07:06:08 -07003378 if (latency == 0 || !cstate->base.active || !intel_pstate->visible) {
3379 *enabled = false;
3380 return 0;
3381 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003382
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003383 width = drm_rect_width(&intel_pstate->src) >> 16;
3384 height = drm_rect_height(&intel_pstate->src) >> 16;
3385
Matt Roper33815fa2016-05-12 07:06:05 -07003386 if (intel_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003387 swap(width, height);
3388
Ville Syrjäläac484962016-01-20 21:05:26 +02003389 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003390 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3391
3392 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3393 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003394 cstate->base.adjusted_mode.crtc_htotal,
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003395 width,
3396 cpp,
3397 fb->modifier[0],
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003398 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003399
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003400 plane_bytes_per_line = width * cpp;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003401 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003402
Matt Roper024c9042015-09-24 15:53:11 -07003403 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3404 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003405 uint32_t min_scanlines = 4;
3406 uint32_t y_tile_minimum;
Matt Roper33815fa2016-05-12 07:06:05 -07003407 if (intel_rotation_90_or_270(pstate->rotation)) {
Ville Syrjäläac484962016-01-20 21:05:26 +02003408 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
Matt Roper024c9042015-09-24 15:53:11 -07003409 drm_format_plane_cpp(fb->pixel_format, 1) :
3410 drm_format_plane_cpp(fb->pixel_format, 0);
3411
Ville Syrjäläac484962016-01-20 21:05:26 +02003412 switch (cpp) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003413 case 1:
3414 min_scanlines = 16;
3415 break;
3416 case 2:
3417 min_scanlines = 8;
3418 break;
3419 case 8:
3420 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08003421 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003422 }
3423 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003424 selected_result = max(method2, y_tile_minimum);
3425 } else {
3426 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3427 selected_result = min(method1, method2);
3428 else
3429 selected_result = method1;
3430 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003431
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003432 res_blocks = selected_result + 1;
3433 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003434
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003435 if (level >= 1 && level <= 7) {
Matt Roper024c9042015-09-24 15:53:11 -07003436 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3437 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003438 res_lines += 4;
3439 else
3440 res_blocks++;
3441 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003442
Matt Roper55994c22016-05-12 07:06:08 -07003443 if (res_blocks >= ddb_allocation || res_lines > 31) {
3444 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003445
3446 /*
3447 * If there are no valid level 0 watermarks, then we can't
3448 * support this display configuration.
3449 */
3450 if (level) {
3451 return 0;
3452 } else {
3453 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3454 DRM_DEBUG_KMS("Plane %d.%d: blocks required = %u/%u, lines required = %u/31\n",
3455 to_intel_crtc(cstate->base.crtc)->pipe,
3456 skl_wm_plane_id(to_intel_plane(pstate->plane)),
3457 res_blocks, ddb_allocation, res_lines);
3458
3459 return -EINVAL;
3460 }
Matt Roper55994c22016-05-12 07:06:08 -07003461 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003462
3463 *out_blocks = res_blocks;
3464 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003465 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003466
Matt Roper55994c22016-05-12 07:06:08 -07003467 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003468}
3469
Matt Roperf4a96752016-05-12 07:06:06 -07003470static int
3471skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3472 struct skl_ddb_allocation *ddb,
3473 struct intel_crtc_state *cstate,
3474 int level,
3475 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003476{
Matt Roper024c9042015-09-24 15:53:11 -07003477 struct drm_device *dev = dev_priv->dev;
Matt Roperf4a96752016-05-12 07:06:06 -07003478 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003479 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roperf4a96752016-05-12 07:06:06 -07003480 struct drm_plane *plane;
Matt Roper024c9042015-09-24 15:53:11 -07003481 struct intel_plane *intel_plane;
Matt Roper33815fa2016-05-12 07:06:05 -07003482 struct intel_plane_state *intel_pstate;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003483 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003484 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003485 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003486
Matt Roperf4a96752016-05-12 07:06:06 -07003487 /*
3488 * We'll only calculate watermarks for planes that are actually
3489 * enabled, so make sure all other planes are set as disabled.
3490 */
3491 memset(result, 0, sizeof(*result));
3492
3493 for_each_intel_plane_mask(dev, intel_plane, cstate->base.plane_mask) {
Matt Roper024c9042015-09-24 15:53:11 -07003494 int i = skl_wm_plane_id(intel_plane);
3495
Matt Roperf4a96752016-05-12 07:06:06 -07003496 plane = &intel_plane->base;
3497 intel_pstate = NULL;
3498 if (state)
3499 intel_pstate =
3500 intel_atomic_get_existing_plane_state(state,
3501 intel_plane);
3502
3503 /*
3504 * Note: If we start supporting multiple pending atomic commits
3505 * against the same planes/CRTC's in the future, plane->state
3506 * will no longer be the correct pre-state to use for the
3507 * calculations here and we'll need to change where we get the
3508 * 'unchanged' plane data from.
3509 *
3510 * For now this is fine because we only allow one queued commit
3511 * against a CRTC. Even if the plane isn't modified by this
3512 * transaction and we don't have a plane lock, we still have
3513 * the CRTC's lock, so we know that no other transactions are
3514 * racing with us to update it.
3515 */
3516 if (!intel_pstate)
3517 intel_pstate = to_intel_plane_state(plane->state);
3518
3519 WARN_ON(!intel_pstate->base.fb);
3520
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003521 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3522
Matt Roper55994c22016-05-12 07:06:08 -07003523 ret = skl_compute_plane_wm(dev_priv,
3524 cstate,
3525 intel_pstate,
3526 ddb_blocks,
3527 level,
3528 &result->plane_res_b[i],
3529 &result->plane_res_l[i],
3530 &result->plane_en[i]);
3531 if (ret)
3532 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003533 }
Matt Roperf4a96752016-05-12 07:06:06 -07003534
3535 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003536}
3537
Damien Lespiau407b50f2014-11-04 17:06:57 +00003538static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003539skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003540{
Matt Roper024c9042015-09-24 15:53:11 -07003541 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003542 return 0;
3543
Matt Roper024c9042015-09-24 15:53:11 -07003544 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003545 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003546
Matt Roper024c9042015-09-24 15:53:11 -07003547 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3548 skl_pipe_pixel_rate(cstate));
Damien Lespiau407b50f2014-11-04 17:06:57 +00003549}
3550
Matt Roper024c9042015-09-24 15:53:11 -07003551static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003552 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003553{
Matt Roper024c9042015-09-24 15:53:11 -07003554 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiau9414f562014-11-04 17:06:58 +00003555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003556 struct intel_plane *intel_plane;
Damien Lespiau9414f562014-11-04 17:06:58 +00003557
Matt Roper024c9042015-09-24 15:53:11 -07003558 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003559 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003560
3561 /* Until we know more, just disable transition WMs */
Matt Roper024c9042015-09-24 15:53:11 -07003562 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3563 int i = skl_wm_plane_id(intel_plane);
3564
Damien Lespiau9414f562014-11-04 17:06:58 +00003565 trans_wm->plane_en[i] = false;
Matt Roper024c9042015-09-24 15:53:11 -07003566 }
Damien Lespiau407b50f2014-11-04 17:06:57 +00003567}
3568
Matt Roper55994c22016-05-12 07:06:08 -07003569static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3570 struct skl_ddb_allocation *ddb,
3571 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003572{
Matt Roper024c9042015-09-24 15:53:11 -07003573 struct drm_device *dev = cstate->base.crtc->dev;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003574 const struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003575 int level, max_level = ilk_wm_max_level(dev);
Matt Roper55994c22016-05-12 07:06:08 -07003576 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003577
3578 for (level = 0; level <= max_level; level++) {
Matt Roper55994c22016-05-12 07:06:08 -07003579 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3580 level, &pipe_wm->wm[level]);
3581 if (ret)
3582 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003583 }
Matt Roper024c9042015-09-24 15:53:11 -07003584 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003585
Matt Roper024c9042015-09-24 15:53:11 -07003586 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
Matt Roper55994c22016-05-12 07:06:08 -07003587
3588 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003589}
3590
3591static void skl_compute_wm_results(struct drm_device *dev,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003592 struct skl_pipe_wm *p_wm,
3593 struct skl_wm_values *r,
3594 struct intel_crtc *intel_crtc)
3595{
3596 int level, max_level = ilk_wm_max_level(dev);
3597 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003598 uint32_t temp;
3599 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003600
3601 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003602 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3603 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003604
3605 temp |= p_wm->wm[level].plane_res_l[i] <<
3606 PLANE_WM_LINES_SHIFT;
3607 temp |= p_wm->wm[level].plane_res_b[i];
3608 if (p_wm->wm[level].plane_en[i])
3609 temp |= PLANE_WM_EN;
3610
3611 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003612 }
3613
3614 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003615
Matt Roper4969d332015-09-24 15:53:10 -07003616 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3617 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003618
Matt Roper4969d332015-09-24 15:53:10 -07003619 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003620 temp |= PLANE_WM_EN;
3621
Matt Roper4969d332015-09-24 15:53:10 -07003622 r->plane[pipe][PLANE_CURSOR][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003623
3624 }
3625
Damien Lespiau9414f562014-11-04 17:06:58 +00003626 /* transition WMs */
3627 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3628 temp = 0;
3629 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3630 temp |= p_wm->trans_wm.plane_res_b[i];
3631 if (p_wm->trans_wm.plane_en[i])
3632 temp |= PLANE_WM_EN;
3633
3634 r->plane_trans[pipe][i] = temp;
3635 }
3636
3637 temp = 0;
Matt Roper4969d332015-09-24 15:53:10 -07003638 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3639 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3640 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
Damien Lespiau9414f562014-11-04 17:06:58 +00003641 temp |= PLANE_WM_EN;
3642
Matt Roper4969d332015-09-24 15:53:10 -07003643 r->plane_trans[pipe][PLANE_CURSOR] = temp;
Damien Lespiau9414f562014-11-04 17:06:58 +00003644
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003645 r->wm_linetime[pipe] = p_wm->linetime;
3646}
3647
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003648static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3649 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003650 const struct skl_ddb_entry *entry)
3651{
3652 if (entry->end)
3653 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3654 else
3655 I915_WRITE(reg, 0);
3656}
3657
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003658static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3659 const struct skl_wm_values *new)
3660{
3661 struct drm_device *dev = dev_priv->dev;
3662 struct intel_crtc *crtc;
3663
Jani Nikula19c80542015-12-16 12:48:16 +02003664 for_each_intel_crtc(dev, crtc) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003665 int i, level, max_level = ilk_wm_max_level(dev);
3666 enum pipe pipe = crtc->pipe;
3667
Matt Roper2b4b9f32016-05-12 07:06:07 -07003668 if ((new->dirty_pipes & drm_crtc_mask(&crtc->base)) == 0)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003669 continue;
Matt Roper734fa012016-05-12 15:11:40 -07003670 if (!crtc->active)
3671 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003672
Damien Lespiau5d374d92014-11-04 17:07:00 +00003673 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3674
3675 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003676 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003677 I915_WRITE(PLANE_WM(pipe, i, level),
3678 new->plane[pipe][i][level]);
3679 I915_WRITE(CUR_WM(pipe, level),
Matt Roper4969d332015-09-24 15:53:10 -07003680 new->plane[pipe][PLANE_CURSOR][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003681 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003682 for (i = 0; i < intel_num_planes(crtc); i++)
3683 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3684 new->plane_trans[pipe][i]);
Matt Roper4969d332015-09-24 15:53:10 -07003685 I915_WRITE(CUR_WM_TRANS(pipe),
3686 new->plane_trans[pipe][PLANE_CURSOR]);
Damien Lespiau5d374d92014-11-04 17:07:00 +00003687
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003688 for (i = 0; i < intel_num_planes(crtc); i++) {
Damien Lespiau5d374d92014-11-04 17:07:00 +00003689 skl_ddb_entry_write(dev_priv,
3690 PLANE_BUF_CFG(pipe, i),
3691 &new->ddb.plane[pipe][i]);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003692 skl_ddb_entry_write(dev_priv,
3693 PLANE_NV12_BUF_CFG(pipe, i),
3694 &new->ddb.y_plane[pipe][i]);
3695 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003696
3697 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
Matt Roper4969d332015-09-24 15:53:10 -07003698 &new->ddb.plane[pipe][PLANE_CURSOR]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003699 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003700}
3701
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003702/*
3703 * When setting up a new DDB allocation arrangement, we need to correctly
3704 * sequence the times at which the new allocations for the pipes are taken into
3705 * account or we'll have pipes fetching from space previously allocated to
3706 * another pipe.
3707 *
3708 * Roughly the sequence looks like:
3709 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3710 * overlapping with a previous light-up pipe (another way to put it is:
3711 * pipes with their new allocation strickly included into their old ones).
3712 * 2. re-allocate the other pipes that get their allocation reduced
3713 * 3. allocate the pipes having their allocation increased
3714 *
3715 * Steps 1. and 2. are here to take care of the following case:
3716 * - Initially DDB looks like this:
3717 * | B | C |
3718 * - enable pipe A.
3719 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3720 * allocation
3721 * | A | B | C |
3722 *
3723 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3724 */
3725
Damien Lespiaud21b7952014-11-04 17:07:03 +00003726static void
3727skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003728{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003729 int plane;
3730
Damien Lespiaud21b7952014-11-04 17:07:03 +00003731 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3732
Damien Lespiaudd740782015-02-28 14:54:08 +00003733 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003734 I915_WRITE(PLANE_SURF(pipe, plane),
3735 I915_READ(PLANE_SURF(pipe, plane)));
3736 }
3737 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3738}
3739
3740static bool
3741skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3742 const struct skl_ddb_allocation *new,
3743 enum pipe pipe)
3744{
3745 uint16_t old_size, new_size;
3746
3747 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3748 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3749
3750 return old_size != new_size &&
3751 new->pipe[pipe].start >= old->pipe[pipe].start &&
3752 new->pipe[pipe].end <= old->pipe[pipe].end;
3753}
3754
3755static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3756 struct skl_wm_values *new_values)
3757{
3758 struct drm_device *dev = dev_priv->dev;
3759 struct skl_ddb_allocation *cur_ddb, *new_ddb;
Ville Syrjäläc929cb42015-04-02 18:28:07 +03003760 bool reallocated[I915_MAX_PIPES] = {};
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003761 struct intel_crtc *crtc;
3762 enum pipe pipe;
3763
3764 new_ddb = &new_values->ddb;
3765 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3766
3767 /*
3768 * First pass: flush the pipes with the new allocation contained into
3769 * the old space.
3770 *
3771 * We'll wait for the vblank on those pipes to ensure we can safely
3772 * re-allocate the freed space without this pipe fetching from it.
3773 */
3774 for_each_intel_crtc(dev, crtc) {
3775 if (!crtc->active)
3776 continue;
3777
3778 pipe = crtc->pipe;
3779
3780 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3781 continue;
3782
Damien Lespiaud21b7952014-11-04 17:07:03 +00003783 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003784 intel_wait_for_vblank(dev, pipe);
3785
3786 reallocated[pipe] = true;
3787 }
3788
3789
3790 /*
3791 * Second pass: flush the pipes that are having their allocation
3792 * reduced, but overlapping with a previous allocation.
3793 *
3794 * Here as well we need to wait for the vblank to make sure the freed
3795 * space is not used anymore.
3796 */
3797 for_each_intel_crtc(dev, crtc) {
3798 if (!crtc->active)
3799 continue;
3800
3801 pipe = crtc->pipe;
3802
3803 if (reallocated[pipe])
3804 continue;
3805
3806 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3807 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003808 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003809 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303810 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003811 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003812 }
3813
3814 /*
3815 * Third pass: flush the pipes that got more space allocated.
3816 *
3817 * We don't need to actively wait for the update here, next vblank
3818 * will just get more DDB space with the correct WM values.
3819 */
3820 for_each_intel_crtc(dev, crtc) {
3821 if (!crtc->active)
3822 continue;
3823
3824 pipe = crtc->pipe;
3825
3826 /*
3827 * At this point, only the pipes more space than before are
3828 * left to re-allocate.
3829 */
3830 if (reallocated[pipe])
3831 continue;
3832
Damien Lespiaud21b7952014-11-04 17:07:03 +00003833 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003834 }
3835}
3836
Matt Roper55994c22016-05-12 07:06:08 -07003837static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
3838 struct skl_ddb_allocation *ddb, /* out */
3839 struct skl_pipe_wm *pipe_wm, /* out */
3840 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003841{
Matt Roperf4a96752016-05-12 07:06:06 -07003842 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->crtc);
3843 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07003844 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003845
Matt Roper55994c22016-05-12 07:06:08 -07003846 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
3847 if (ret)
3848 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003849
Matt Roper4e0963c2015-09-24 15:53:15 -07003850 if (!memcmp(&intel_crtc->wm.active.skl, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07003851 *changed = false;
3852 else
3853 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003854
Matt Roper55994c22016-05-12 07:06:08 -07003855 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003856}
3857
Matt Roper98d39492016-05-12 07:06:03 -07003858static int
3859skl_compute_ddb(struct drm_atomic_state *state)
3860{
3861 struct drm_device *dev = state->dev;
3862 struct drm_i915_private *dev_priv = to_i915(dev);
3863 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3864 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07003865 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper98d39492016-05-12 07:06:03 -07003866 unsigned realloc_pipes = dev_priv->active_crtcs;
3867 int ret;
3868
3869 /*
3870 * If this is our first atomic update following hardware readout,
3871 * we can't trust the DDB that the BIOS programmed for us. Let's
3872 * pretend that all pipes switched active status so that we'll
3873 * ensure a full DDB recompute.
3874 */
3875 if (dev_priv->wm.distrust_bios_wm)
3876 intel_state->active_pipe_changes = ~0;
3877
3878 /*
3879 * If the modeset changes which CRTC's are active, we need to
3880 * recompute the DDB allocation for *all* active pipes, even
3881 * those that weren't otherwise being modified in any way by this
3882 * atomic commit. Due to the shrinking of the per-pipe allocations
3883 * when new active CRTC's are added, it's possible for a pipe that
3884 * we were already using and aren't changing at all here to suddenly
3885 * become invalid if its DDB needs exceeds its new allocation.
3886 *
3887 * Note that if we wind up doing a full DDB recompute, we can't let
3888 * any other display updates race with this transaction, so we need
3889 * to grab the lock on *all* CRTC's.
3890 */
Matt Roper734fa012016-05-12 15:11:40 -07003891 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07003892 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07003893 intel_state->wm_results.dirty_pipes = ~0;
3894 }
Matt Roper98d39492016-05-12 07:06:03 -07003895
3896 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
3897 struct intel_crtc_state *cstate;
3898
3899 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
3900 if (IS_ERR(cstate))
3901 return PTR_ERR(cstate);
3902
Matt Roper734fa012016-05-12 15:11:40 -07003903 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07003904 if (ret)
3905 return ret;
3906 }
3907
3908 return 0;
3909}
3910
3911static int
3912skl_compute_wm(struct drm_atomic_state *state)
3913{
3914 struct drm_crtc *crtc;
3915 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07003916 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3917 struct skl_wm_values *results = &intel_state->wm_results;
3918 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07003919 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07003920 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07003921
3922 /*
3923 * If this transaction isn't actually touching any CRTC's, don't
3924 * bother with watermark calculation. Note that if we pass this
3925 * test, we're guaranteed to hold at least one CRTC state mutex,
3926 * which means we can safely use values like dev_priv->active_crtcs
3927 * since any racing commits that want to update them would need to
3928 * hold _all_ CRTC state mutexes.
3929 */
3930 for_each_crtc_in_state(state, crtc, cstate, i)
3931 changed = true;
3932 if (!changed)
3933 return 0;
3934
Matt Roper734fa012016-05-12 15:11:40 -07003935 /* Clear all dirty flags */
3936 results->dirty_pipes = 0;
3937
Matt Roper98d39492016-05-12 07:06:03 -07003938 ret = skl_compute_ddb(state);
3939 if (ret)
3940 return ret;
3941
Matt Roper734fa012016-05-12 15:11:40 -07003942 /*
3943 * Calculate WM's for all pipes that are part of this transaction.
3944 * Note that the DDB allocation above may have added more CRTC's that
3945 * weren't otherwise being modified (and set bits in dirty_pipes) if
3946 * pipe allocations had to change.
3947 *
3948 * FIXME: Now that we're doing this in the atomic check phase, we
3949 * should allow skl_update_pipe_wm() to return failure in cases where
3950 * no suitable watermark values can be found.
3951 */
3952 for_each_crtc_in_state(state, crtc, cstate, i) {
3953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3954 struct intel_crtc_state *intel_cstate =
3955 to_intel_crtc_state(cstate);
3956
3957 pipe_wm = &intel_cstate->wm.skl.optimal;
3958 ret = skl_update_pipe_wm(cstate, &results->ddb, pipe_wm,
3959 &changed);
3960 if (ret)
3961 return ret;
3962
3963 if (changed)
3964 results->dirty_pipes |= drm_crtc_mask(crtc);
3965
3966 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
3967 /* This pipe's WM's did not change */
3968 continue;
3969
3970 intel_cstate->update_wm_pre = true;
3971 skl_compute_wm_results(crtc->dev, pipe_wm, results, intel_crtc);
3972 }
3973
Matt Roper98d39492016-05-12 07:06:03 -07003974 return 0;
3975}
3976
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003977static void skl_update_wm(struct drm_crtc *crtc)
3978{
3979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3980 struct drm_device *dev = crtc->dev;
3981 struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003982 struct skl_wm_values *results = &dev_priv->wm.skl_results;
Matt Roper4e0963c2015-09-24 15:53:15 -07003983 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07003984 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Bob Paauweadda50b2015-07-21 10:42:53 -07003985
Matt Roper734fa012016-05-12 15:11:40 -07003986 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003987 return;
3988
Matt Roper734fa012016-05-12 15:11:40 -07003989 intel_crtc->wm.active.skl = *pipe_wm;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003990
Matt Roper734fa012016-05-12 15:11:40 -07003991 mutex_lock(&dev_priv->wm.wm_mutex);
3992
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003993 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003994 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003995
3996 /* store the new configuration */
3997 dev_priv->wm.skl_hw = *results;
Matt Roper734fa012016-05-12 15:11:40 -07003998
3999 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004000}
4001
Ville Syrjäläd8905652016-01-14 14:53:35 +02004002static void ilk_compute_wm_config(struct drm_device *dev,
4003 struct intel_wm_config *config)
4004{
4005 struct intel_crtc *crtc;
4006
4007 /* Compute the currently _active_ config */
4008 for_each_intel_crtc(dev, crtc) {
4009 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4010
4011 if (!wm->pipe_enabled)
4012 continue;
4013
4014 config->sprites_enabled |= wm->sprites_enabled;
4015 config->sprites_scaled |= wm->sprites_scaled;
4016 config->num_pipes_active++;
4017 }
4018}
4019
Matt Ropered4a6a72016-02-23 17:20:13 -08004020static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004021{
Matt Ropered4a6a72016-02-23 17:20:13 -08004022 struct drm_device *dev = dev_priv->dev;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004023 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004024 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004025 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004026 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004027 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004028
Ville Syrjäläd8905652016-01-14 14:53:35 +02004029 ilk_compute_wm_config(dev, &config);
4030
4031 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4032 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004033
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004034 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03004035 if (INTEL_INFO(dev)->gen >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004036 config.num_pipes_active == 1 && config.sprites_enabled) {
4037 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4038 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004039
Imre Deak820c1982013-12-17 14:46:36 +02004040 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004041 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004042 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004043 }
4044
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004045 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004046 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004047
Imre Deak820c1982013-12-17 14:46:36 +02004048 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004049
Imre Deak820c1982013-12-17 14:46:36 +02004050 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004051}
4052
Matt Ropered4a6a72016-02-23 17:20:13 -08004053static void ilk_initial_watermarks(struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004054{
Matt Ropered4a6a72016-02-23 17:20:13 -08004055 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4056 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004057
Matt Ropered4a6a72016-02-23 17:20:13 -08004058 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004059 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004060 ilk_program_watermarks(dev_priv);
4061 mutex_unlock(&dev_priv->wm.wm_mutex);
4062}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004063
Matt Ropered4a6a72016-02-23 17:20:13 -08004064static void ilk_optimize_watermarks(struct intel_crtc_state *cstate)
4065{
4066 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4067 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4068
4069 mutex_lock(&dev_priv->wm.wm_mutex);
4070 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004071 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004072 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004073 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004074 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004075}
4076
Pradeep Bhat30789992014-11-04 17:06:45 +00004077static void skl_pipe_wm_active_state(uint32_t val,
4078 struct skl_pipe_wm *active,
4079 bool is_transwm,
4080 bool is_cursor,
4081 int i,
4082 int level)
4083{
4084 bool is_enabled = (val & PLANE_WM_EN) != 0;
4085
4086 if (!is_transwm) {
4087 if (!is_cursor) {
4088 active->wm[level].plane_en[i] = is_enabled;
4089 active->wm[level].plane_res_b[i] =
4090 val & PLANE_WM_BLOCKS_MASK;
4091 active->wm[level].plane_res_l[i] =
4092 (val >> PLANE_WM_LINES_SHIFT) &
4093 PLANE_WM_LINES_MASK;
4094 } else {
Matt Roper4969d332015-09-24 15:53:10 -07004095 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
4096 active->wm[level].plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004097 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07004098 active->wm[level].plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004099 (val >> PLANE_WM_LINES_SHIFT) &
4100 PLANE_WM_LINES_MASK;
4101 }
4102 } else {
4103 if (!is_cursor) {
4104 active->trans_wm.plane_en[i] = is_enabled;
4105 active->trans_wm.plane_res_b[i] =
4106 val & PLANE_WM_BLOCKS_MASK;
4107 active->trans_wm.plane_res_l[i] =
4108 (val >> PLANE_WM_LINES_SHIFT) &
4109 PLANE_WM_LINES_MASK;
4110 } else {
Matt Roper4969d332015-09-24 15:53:10 -07004111 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
4112 active->trans_wm.plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004113 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07004114 active->trans_wm.plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00004115 (val >> PLANE_WM_LINES_SHIFT) &
4116 PLANE_WM_LINES_MASK;
4117 }
4118 }
4119}
4120
4121static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4122{
4123 struct drm_device *dev = crtc->dev;
4124 struct drm_i915_private *dev_priv = dev->dev_private;
4125 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
4126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004127 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004128 struct skl_pipe_wm *active = &cstate->wm.skl.optimal;
Pradeep Bhat30789992014-11-04 17:06:45 +00004129 enum pipe pipe = intel_crtc->pipe;
4130 int level, i, max_level;
4131 uint32_t temp;
4132
4133 max_level = ilk_wm_max_level(dev);
4134
4135 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
4136
4137 for (level = 0; level <= max_level; level++) {
4138 for (i = 0; i < intel_num_planes(intel_crtc); i++)
4139 hw->plane[pipe][i][level] =
4140 I915_READ(PLANE_WM(pipe, i, level));
Matt Roper4969d332015-09-24 15:53:10 -07004141 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
Pradeep Bhat30789992014-11-04 17:06:45 +00004142 }
4143
4144 for (i = 0; i < intel_num_planes(intel_crtc); i++)
4145 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
Matt Roper4969d332015-09-24 15:53:10 -07004146 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004147
Matt Roper3ef00282015-03-09 10:19:24 -07004148 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004149 return;
4150
Matt Roper2b4b9f32016-05-12 07:06:07 -07004151 hw->dirty_pipes |= drm_crtc_mask(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004152
4153 active->linetime = hw->wm_linetime[pipe];
4154
4155 for (level = 0; level <= max_level; level++) {
4156 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
4157 temp = hw->plane[pipe][i][level];
4158 skl_pipe_wm_active_state(temp, active, false,
4159 false, i, level);
4160 }
Matt Roper4969d332015-09-24 15:53:10 -07004161 temp = hw->plane[pipe][PLANE_CURSOR][level];
Pradeep Bhat30789992014-11-04 17:06:45 +00004162 skl_pipe_wm_active_state(temp, active, false, true, i, level);
4163 }
4164
4165 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
4166 temp = hw->plane_trans[pipe][i];
4167 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
4168 }
4169
Matt Roper4969d332015-09-24 15:53:10 -07004170 temp = hw->plane_trans[pipe][PLANE_CURSOR];
Pradeep Bhat30789992014-11-04 17:06:45 +00004171 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
Matt Roper4e0963c2015-09-24 15:53:15 -07004172
4173 intel_crtc->wm.active.skl = *active;
Pradeep Bhat30789992014-11-04 17:06:45 +00004174}
4175
4176void skl_wm_get_hw_state(struct drm_device *dev)
4177{
Damien Lespiaua269c582014-11-04 17:06:49 +00004178 struct drm_i915_private *dev_priv = dev->dev_private;
4179 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004180 struct drm_crtc *crtc;
4181
Damien Lespiaua269c582014-11-04 17:06:49 +00004182 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00004183 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
4184 skl_pipe_wm_get_hw_state(crtc);
Matt Ropera1de91e2016-05-12 07:05:57 -07004185
Matt Roper279e99d2016-05-12 07:06:02 -07004186 if (dev_priv->active_crtcs) {
4187 /* Fully recompute DDB on first atomic commit */
4188 dev_priv->wm.distrust_bios_wm = true;
4189 } else {
4190 /* Easy/common case; just sanitize DDB now if everything off */
4191 memset(ddb, 0, sizeof(*ddb));
4192 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004193}
4194
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004195static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4196{
4197 struct drm_device *dev = crtc->dev;
4198 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004199 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004201 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004202 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004203 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004204 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004205 [PIPE_A] = WM0_PIPEA_ILK,
4206 [PIPE_B] = WM0_PIPEB_ILK,
4207 [PIPE_C] = WM0_PIPEC_IVB,
4208 };
4209
4210 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004211 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004212 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004213
Ville Syrjälä15606532016-05-13 17:55:17 +03004214 memset(active, 0, sizeof(*active));
4215
Matt Roper3ef00282015-03-09 10:19:24 -07004216 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004217
4218 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004219 u32 tmp = hw->wm_pipe[pipe];
4220
4221 /*
4222 * For active pipes LP0 watermark is marked as
4223 * enabled, and LP1+ watermaks as disabled since
4224 * we can't really reverse compute them in case
4225 * multiple pipes are active.
4226 */
4227 active->wm[0].enable = true;
4228 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4229 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4230 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4231 active->linetime = hw->wm_linetime[pipe];
4232 } else {
4233 int level, max_level = ilk_wm_max_level(dev);
4234
4235 /*
4236 * For inactive pipes, all watermark levels
4237 * should be marked as enabled but zeroed,
4238 * which is what we'd compute them to.
4239 */
4240 for (level = 0; level <= max_level; level++)
4241 active->wm[level].enable = true;
4242 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004243
4244 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004245}
4246
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004247#define _FW_WM(value, plane) \
4248 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4249#define _FW_WM_VLV(value, plane) \
4250 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4251
4252static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4253 struct vlv_wm_values *wm)
4254{
4255 enum pipe pipe;
4256 uint32_t tmp;
4257
4258 for_each_pipe(dev_priv, pipe) {
4259 tmp = I915_READ(VLV_DDL(pipe));
4260
4261 wm->ddl[pipe].primary =
4262 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4263 wm->ddl[pipe].cursor =
4264 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4265 wm->ddl[pipe].sprite[0] =
4266 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4267 wm->ddl[pipe].sprite[1] =
4268 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4269 }
4270
4271 tmp = I915_READ(DSPFW1);
4272 wm->sr.plane = _FW_WM(tmp, SR);
4273 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
4274 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
4275 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
4276
4277 tmp = I915_READ(DSPFW2);
4278 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
4279 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
4280 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
4281
4282 tmp = I915_READ(DSPFW3);
4283 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4284
4285 if (IS_CHERRYVIEW(dev_priv)) {
4286 tmp = I915_READ(DSPFW7_CHV);
4287 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4288 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4289
4290 tmp = I915_READ(DSPFW8_CHV);
4291 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
4292 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
4293
4294 tmp = I915_READ(DSPFW9_CHV);
4295 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
4296 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
4297
4298 tmp = I915_READ(DSPHOWM);
4299 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4300 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4301 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4302 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
4303 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4304 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4305 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4306 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4307 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4308 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4309 } else {
4310 tmp = I915_READ(DSPFW7);
4311 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4312 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4313
4314 tmp = I915_READ(DSPHOWM);
4315 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4316 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4317 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4318 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4319 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4320 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4321 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4322 }
4323}
4324
4325#undef _FW_WM
4326#undef _FW_WM_VLV
4327
4328void vlv_wm_get_hw_state(struct drm_device *dev)
4329{
4330 struct drm_i915_private *dev_priv = to_i915(dev);
4331 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4332 struct intel_plane *plane;
4333 enum pipe pipe;
4334 u32 val;
4335
4336 vlv_read_wm_values(dev_priv, wm);
4337
4338 for_each_intel_plane(dev, plane) {
4339 switch (plane->base.type) {
4340 int sprite;
4341 case DRM_PLANE_TYPE_CURSOR:
4342 plane->wm.fifo_size = 63;
4343 break;
4344 case DRM_PLANE_TYPE_PRIMARY:
4345 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4346 break;
4347 case DRM_PLANE_TYPE_OVERLAY:
4348 sprite = plane->plane;
4349 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4350 break;
4351 }
4352 }
4353
4354 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4355 wm->level = VLV_WM_LEVEL_PM2;
4356
4357 if (IS_CHERRYVIEW(dev_priv)) {
4358 mutex_lock(&dev_priv->rps.hw_lock);
4359
4360 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4361 if (val & DSP_MAXFIFO_PM5_ENABLE)
4362 wm->level = VLV_WM_LEVEL_PM5;
4363
Ville Syrjälä58590c12015-09-08 21:05:12 +03004364 /*
4365 * If DDR DVFS is disabled in the BIOS, Punit
4366 * will never ack the request. So if that happens
4367 * assume we don't have to enable/disable DDR DVFS
4368 * dynamically. To test that just set the REQ_ACK
4369 * bit to poke the Punit, but don't change the
4370 * HIGH/LOW bits so that we don't actually change
4371 * the current state.
4372 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004373 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004374 val |= FORCE_DDR_FREQ_REQ_ACK;
4375 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4376
4377 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4378 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4379 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4380 "assuming DDR DVFS is disabled\n");
4381 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4382 } else {
4383 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4384 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4385 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4386 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004387
4388 mutex_unlock(&dev_priv->rps.hw_lock);
4389 }
4390
4391 for_each_pipe(dev_priv, pipe)
4392 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4393 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4394 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4395
4396 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4397 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4398}
4399
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004400void ilk_wm_get_hw_state(struct drm_device *dev)
4401{
4402 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004403 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004404 struct drm_crtc *crtc;
4405
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004406 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004407 ilk_pipe_wm_get_hw_state(crtc);
4408
4409 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4410 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4411 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4412
4413 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004414 if (INTEL_INFO(dev)->gen >= 7) {
4415 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4416 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4417 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004418
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004419 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004420 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4421 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4422 else if (IS_IVYBRIDGE(dev))
4423 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4424 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004425
4426 hw->enable_fbc_wm =
4427 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4428}
4429
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004430/**
4431 * intel_update_watermarks - update FIFO watermark values based on current modes
4432 *
4433 * Calculate watermark values for the various WM regs based on current mode
4434 * and plane configuration.
4435 *
4436 * There are several cases to deal with here:
4437 * - normal (i.e. non-self-refresh)
4438 * - self-refresh (SR) mode
4439 * - lines are large relative to FIFO size (buffer can hold up to 2)
4440 * - lines are small relative to FIFO size (buffer can hold more than 2
4441 * lines), so need to account for TLB latency
4442 *
4443 * The normal calculation is:
4444 * watermark = dotclock * bytes per pixel * latency
4445 * where latency is platform & configuration dependent (we assume pessimal
4446 * values here).
4447 *
4448 * The SR calculation is:
4449 * watermark = (trunc(latency/line time)+1) * surface width *
4450 * bytes per pixel
4451 * where
4452 * line time = htotal / dotclock
4453 * surface width = hdisplay for normal plane and 64 for cursor
4454 * and latency is assumed to be high, as above.
4455 *
4456 * The final value programmed to the register should always be rounded up,
4457 * and include an extra 2 entries to account for clock crossings.
4458 *
4459 * We don't use the sprite, so we can ignore that. And on Crestline we have
4460 * to set the non-SR watermarks to 8.
4461 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004462void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004463{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004464 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004465
4466 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004467 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004468}
4469
Jani Nikulae2828912016-01-18 09:19:47 +02004470/*
Daniel Vetter92703882012-08-09 16:46:01 +02004471 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004472 */
4473DEFINE_SPINLOCK(mchdev_lock);
4474
4475/* Global for IPS driver to get at the current i915 device. Protected by
4476 * mchdev_lock. */
4477static struct drm_i915_private *i915_mch_dev;
4478
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004479bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004480{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004481 u16 rgvswctl;
4482
Daniel Vetter92703882012-08-09 16:46:01 +02004483 assert_spin_locked(&mchdev_lock);
4484
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004485 rgvswctl = I915_READ16(MEMSWCTL);
4486 if (rgvswctl & MEMCTL_CMD_STS) {
4487 DRM_DEBUG("gpu busy, RCS change rejected\n");
4488 return false; /* still busy with another command */
4489 }
4490
4491 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4492 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4493 I915_WRITE16(MEMSWCTL, rgvswctl);
4494 POSTING_READ16(MEMSWCTL);
4495
4496 rgvswctl |= MEMCTL_CMD_STS;
4497 I915_WRITE16(MEMSWCTL, rgvswctl);
4498
4499 return true;
4500}
4501
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004502static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004503{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004504 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004505 u8 fmax, fmin, fstart, vstart;
4506
Daniel Vetter92703882012-08-09 16:46:01 +02004507 spin_lock_irq(&mchdev_lock);
4508
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004509 rgvmodectl = I915_READ(MEMMODECTL);
4510
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004511 /* Enable temp reporting */
4512 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4513 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4514
4515 /* 100ms RC evaluation intervals */
4516 I915_WRITE(RCUPEI, 100000);
4517 I915_WRITE(RCDNEI, 100000);
4518
4519 /* Set max/min thresholds to 90ms and 80ms respectively */
4520 I915_WRITE(RCBMAXAVG, 90000);
4521 I915_WRITE(RCBMINAVG, 80000);
4522
4523 I915_WRITE(MEMIHYST, 1);
4524
4525 /* Set up min, max, and cur for interrupt handling */
4526 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4527 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4528 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4529 MEMMODE_FSTART_SHIFT;
4530
Ville Syrjälä616847e2015-09-18 20:03:19 +03004531 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004532 PXVFREQ_PX_SHIFT;
4533
Daniel Vetter20e4d402012-08-08 23:35:39 +02004534 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4535 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004536
Daniel Vetter20e4d402012-08-08 23:35:39 +02004537 dev_priv->ips.max_delay = fstart;
4538 dev_priv->ips.min_delay = fmin;
4539 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004540
4541 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4542 fmax, fmin, fstart);
4543
4544 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4545
4546 /*
4547 * Interrupts will be enabled in ironlake_irq_postinstall
4548 */
4549
4550 I915_WRITE(VIDSTART, vstart);
4551 POSTING_READ(VIDSTART);
4552
4553 rgvmodectl |= MEMMODE_SWMODE_EN;
4554 I915_WRITE(MEMMODECTL, rgvmodectl);
4555
Daniel Vetter92703882012-08-09 16:46:01 +02004556 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004557 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004558 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004559
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004560 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004561
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004562 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4563 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004564 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004565 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004566 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004567
4568 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004569}
4570
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004571static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004572{
Daniel Vetter92703882012-08-09 16:46:01 +02004573 u16 rgvswctl;
4574
4575 spin_lock_irq(&mchdev_lock);
4576
4577 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004578
4579 /* Ack interrupts, disable EFC interrupt */
4580 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4581 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4582 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4583 I915_WRITE(DEIIR, DE_PCU_EVENT);
4584 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4585
4586 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004587 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004588 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004589 rgvswctl |= MEMCTL_CMD_STS;
4590 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004591 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004592
Daniel Vetter92703882012-08-09 16:46:01 +02004593 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004594}
4595
Daniel Vetteracbe9472012-07-26 11:50:05 +02004596/* There's a funny hw issue where the hw returns all 0 when reading from
4597 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4598 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4599 * all limits and the gpu stuck at whatever frequency it is at atm).
4600 */
Akash Goel74ef1172015-03-06 11:07:19 +05304601static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004602{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004603 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004604
Daniel Vetter20b46e52012-07-26 11:16:14 +02004605 /* Only set the down limit when we've reached the lowest level to avoid
4606 * getting more interrupts, otherwise leave this clear. This prevents a
4607 * race in the hw when coming out of rc6: There's a tiny window where
4608 * the hw runs at the minimal clock before selecting the desired
4609 * frequency, if the down threshold expires in that window we will not
4610 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03004611 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05304612 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4613 if (val <= dev_priv->rps.min_freq_softlimit)
4614 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4615 } else {
4616 limits = dev_priv->rps.max_freq_softlimit << 24;
4617 if (val <= dev_priv->rps.min_freq_softlimit)
4618 limits |= dev_priv->rps.min_freq_softlimit << 16;
4619 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004620
4621 return limits;
4622}
4623
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004624static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4625{
4626 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304627 u32 threshold_up = 0, threshold_down = 0; /* in % */
4628 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004629
4630 new_power = dev_priv->rps.power;
4631 switch (dev_priv->rps.power) {
4632 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004633 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004634 new_power = BETWEEN;
4635 break;
4636
4637 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004638 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004639 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004640 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004641 new_power = HIGH_POWER;
4642 break;
4643
4644 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004645 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 +01004646 new_power = BETWEEN;
4647 break;
4648 }
4649 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004650 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004651 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004652 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004653 new_power = HIGH_POWER;
4654 if (new_power == dev_priv->rps.power)
4655 return;
4656
4657 /* Note the units here are not exactly 1us, but 1280ns. */
4658 switch (new_power) {
4659 case LOW_POWER:
4660 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304661 ei_up = 16000;
4662 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004663
4664 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304665 ei_down = 32000;
4666 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004667 break;
4668
4669 case BETWEEN:
4670 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304671 ei_up = 13000;
4672 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004673
4674 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304675 ei_down = 32000;
4676 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004677 break;
4678
4679 case HIGH_POWER:
4680 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304681 ei_up = 10000;
4682 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004683
4684 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304685 ei_down = 32000;
4686 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004687 break;
4688 }
4689
Akash Goel8a586432015-03-06 11:07:18 +05304690 I915_WRITE(GEN6_RP_UP_EI,
4691 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4692 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4693 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4694
4695 I915_WRITE(GEN6_RP_DOWN_EI,
4696 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4697 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4698 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4699
4700 I915_WRITE(GEN6_RP_CONTROL,
4701 GEN6_RP_MEDIA_TURBO |
4702 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4703 GEN6_RP_MEDIA_IS_GFX |
4704 GEN6_RP_ENABLE |
4705 GEN6_RP_UP_BUSY_AVG |
4706 GEN6_RP_DOWN_IDLE_AVG);
4707
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004708 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004709 dev_priv->rps.up_threshold = threshold_up;
4710 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004711 dev_priv->rps.last_adj = 0;
4712}
4713
Chris Wilson2876ce72014-03-28 08:03:34 +00004714static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4715{
4716 u32 mask = 0;
4717
4718 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004719 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004720 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004721 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004722
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004723 mask &= dev_priv->pm_rps_events;
4724
Imre Deak59d02a12014-12-19 19:33:26 +02004725 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004726}
4727
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004728/* gen6_set_rps is called to update the frequency request, but should also be
4729 * called when the range (min_delay and max_delay) is modified so that we can
4730 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilsondc979972016-05-10 14:10:04 +01004731static void gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004732{
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304733 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01004734 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304735 return;
4736
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004737 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004738 WARN_ON(val > dev_priv->rps.max_freq);
4739 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004740
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004741 /* min/max delay may still have been modified so be sure to
4742 * write the limits value.
4743 */
4744 if (val != dev_priv->rps.cur_freq) {
4745 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004746
Chris Wilsondc979972016-05-10 14:10:04 +01004747 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05304748 I915_WRITE(GEN6_RPNSWREQ,
4749 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01004750 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004751 I915_WRITE(GEN6_RPNSWREQ,
4752 HSW_FREQUENCY(val));
4753 else
4754 I915_WRITE(GEN6_RPNSWREQ,
4755 GEN6_FREQUENCY(val) |
4756 GEN6_OFFSET(0) |
4757 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004758 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004759
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004760 /* Make sure we continue to get interrupts
4761 * until we hit the minimum or maximum frequencies.
4762 */
Akash Goel74ef1172015-03-06 11:07:19 +05304763 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004764 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004765
Ben Widawskyd5570a72012-09-07 19:43:41 -07004766 POSTING_READ(GEN6_RPNSWREQ);
4767
Ben Widawskyb39fb292014-03-19 18:31:11 -07004768 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004769 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004770}
4771
Chris Wilsondc979972016-05-10 14:10:04 +01004772static void valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004773{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004774 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004775 WARN_ON(val > dev_priv->rps.max_freq);
4776 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004777
Chris Wilsondc979972016-05-10 14:10:04 +01004778 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004779 "Odd GPU freq value\n"))
4780 val &= ~1;
4781
Deepak Scd25dd52015-07-10 18:31:40 +05304782 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4783
Chris Wilson8fb55192015-04-07 16:20:28 +01004784 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004785 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01004786 if (!IS_CHERRYVIEW(dev_priv))
4787 gen6_set_rps_thresholds(dev_priv, val);
4788 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004789
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004790 dev_priv->rps.cur_freq = val;
4791 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4792}
4793
Deepak Sa7f6e232015-05-09 18:04:44 +05304794/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05304795 *
4796 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05304797 * 1. Forcewake Media well.
4798 * 2. Request idle freq.
4799 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05304800*/
4801static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4802{
Chris Wilsonaed242f2015-03-18 09:48:21 +00004803 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304804
Chris Wilsonaed242f2015-03-18 09:48:21 +00004805 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304806 return;
4807
Deepak Sa7f6e232015-05-09 18:04:44 +05304808 /* Wake up the media well, as that takes a lot less
4809 * power than the Render well. */
4810 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilsondc979972016-05-10 14:10:04 +01004811 valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05304812 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05304813}
4814
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004815void gen6_rps_busy(struct drm_i915_private *dev_priv)
4816{
4817 mutex_lock(&dev_priv->rps.hw_lock);
4818 if (dev_priv->rps.enabled) {
4819 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4820 gen6_rps_reset_ei(dev_priv);
4821 I915_WRITE(GEN6_PMINTRMSK,
4822 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4823 }
4824 mutex_unlock(&dev_priv->rps.hw_lock);
4825}
4826
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004827void gen6_rps_idle(struct drm_i915_private *dev_priv)
4828{
4829 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004830 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01004831 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05304832 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004833 else
Chris Wilsondc979972016-05-10 14:10:04 +01004834 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004835 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004836 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004837 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004838 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004839
Chris Wilson8d3afd72015-05-21 21:01:47 +01004840 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004841 while (!list_empty(&dev_priv->rps.clients))
4842 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004843 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004844}
4845
Chris Wilson1854d5c2015-04-07 16:20:32 +01004846void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01004847 struct intel_rps_client *rps,
4848 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004849{
Chris Wilson8d3afd72015-05-21 21:01:47 +01004850 /* This is intentionally racy! We peek at the state here, then
4851 * validate inside the RPS worker.
4852 */
4853 if (!(dev_priv->mm.busy &&
4854 dev_priv->rps.enabled &&
4855 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4856 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004857
Chris Wilsone61b9952015-04-27 13:41:24 +01004858 /* Force a RPS boost (and don't count it against the client) if
4859 * the GPU is severely congested.
4860 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004861 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01004862 rps = NULL;
4863
Chris Wilson8d3afd72015-05-21 21:01:47 +01004864 spin_lock(&dev_priv->rps.client_lock);
4865 if (rps == NULL || list_empty(&rps->link)) {
4866 spin_lock_irq(&dev_priv->irq_lock);
4867 if (dev_priv->rps.interrupts_enabled) {
4868 dev_priv->rps.client_boost = true;
4869 queue_work(dev_priv->wq, &dev_priv->rps.work);
4870 }
4871 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004872
Chris Wilson2e1b8732015-04-27 13:41:22 +01004873 if (rps != NULL) {
4874 list_add(&rps->link, &dev_priv->rps.clients);
4875 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01004876 } else
4877 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01004878 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004879 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004880}
4881
Chris Wilsondc979972016-05-10 14:10:04 +01004882void intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004883{
Chris Wilsondc979972016-05-10 14:10:04 +01004884 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4885 valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004886 else
Chris Wilsondc979972016-05-10 14:10:04 +01004887 gen6_set_rps(dev_priv, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004888}
4889
Chris Wilsondc979972016-05-10 14:10:04 +01004890static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00004891{
Zhe Wang20e49362014-11-04 17:07:05 +00004892 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004893 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004894}
4895
Chris Wilsondc979972016-05-10 14:10:04 +01004896static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05304897{
Akash Goel2030d682016-04-23 00:05:45 +05304898 I915_WRITE(GEN6_RP_CONTROL, 0);
4899}
4900
Chris Wilsondc979972016-05-10 14:10:04 +01004901static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004902{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004903 I915_WRITE(GEN6_RC_CONTROL, 0);
4904 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05304905 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004906}
4907
Chris Wilsondc979972016-05-10 14:10:04 +01004908static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05304909{
Deepak S38807742014-05-23 21:00:15 +05304910 I915_WRITE(GEN6_RC_CONTROL, 0);
4911}
4912
Chris Wilsondc979972016-05-10 14:10:04 +01004913static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004914{
Deepak S98a2e5f2014-08-18 10:35:27 -07004915 /* we're doing forcewake before Disabling RC6,
4916 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004917 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004918
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004919 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004920
Mika Kuoppala59bad942015-01-16 11:34:40 +02004921 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004922}
4923
Chris Wilsondc979972016-05-10 14:10:04 +01004924static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07004925{
Chris Wilsondc979972016-05-10 14:10:04 +01004926 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03004927 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4928 mode = GEN6_RC_CTL_RC6_ENABLE;
4929 else
4930 mode = 0;
4931 }
Chris Wilsondc979972016-05-10 14:10:04 +01004932 if (HAS_RC6p(dev_priv))
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004933 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004934 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
4935 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
4936 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004937
4938 else
4939 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004940 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07004941}
4942
Chris Wilsondc979972016-05-10 14:10:04 +01004943static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304944{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004945 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304946 bool enable_rc6 = true;
4947 unsigned long rc6_ctx_base;
4948
4949 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
4950 DRM_DEBUG_KMS("RC6 Base location not set properly.\n");
4951 enable_rc6 = false;
4952 }
4953
4954 /*
4955 * The exact context size is not known for BXT, so assume a page size
4956 * for this check.
4957 */
4958 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03004959 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
4960 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
4961 ggtt->stolen_reserved_size))) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304962 DRM_DEBUG_KMS("RC6 Base address not as expected.\n");
4963 enable_rc6 = false;
4964 }
4965
4966 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
4967 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
4968 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
4969 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
4970 DRM_DEBUG_KMS("Engine Idle wait time not set properly.\n");
4971 enable_rc6 = false;
4972 }
4973
4974 if (!(I915_READ(GEN6_RC_CONTROL) & (GEN6_RC_CTL_RC6_ENABLE |
4975 GEN6_RC_CTL_HW_ENABLE)) &&
4976 ((I915_READ(GEN6_RC_CONTROL) & GEN6_RC_CTL_HW_ENABLE) ||
4977 !(I915_READ(GEN6_RC_STATE) & RC6_STATE))) {
4978 DRM_DEBUG_KMS("HW/SW RC6 is not enabled by BIOS.\n");
4979 enable_rc6 = false;
4980 }
4981
4982 return enable_rc6;
4983}
4984
Chris Wilsondc979972016-05-10 14:10:04 +01004985int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004986{
Daniel Vettere7d66d82015-06-15 23:23:54 +02004987 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01004988 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03004989 return 0;
4990
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304991 if (!enable_rc6)
4992 return 0;
4993
Chris Wilsondc979972016-05-10 14:10:04 +01004994 if (IS_BROXTON(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05304995 DRM_INFO("RC6 disabled by BIOS\n");
4996 return 0;
4997 }
4998
Daniel Vetter456470e2012-08-08 23:35:40 +02004999 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005000 if (enable_rc6 >= 0) {
5001 int mask;
5002
Chris Wilsondc979972016-05-10 14:10:04 +01005003 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005004 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5005 INTEL_RC6pp_ENABLE;
5006 else
5007 mask = INTEL_RC6_ENABLE;
5008
5009 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02005010 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
5011 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005012
5013 return enable_rc6 & mask;
5014 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005015
Chris Wilsondc979972016-05-10 14:10:04 +01005016 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005017 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005018
5019 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005020}
5021
Chris Wilsondc979972016-05-10 14:10:04 +01005022static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005023{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005024 uint32_t rp_state_cap;
5025 u32 ddcc_status = 0;
5026 int ret;
5027
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005028 /* All of these values are in units of 50MHz */
5029 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005030 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Chris Wilsondc979972016-05-10 14:10:04 +01005031 if (IS_BROXTON(dev_priv)) {
Bob Paauwe35040562015-06-25 14:54:07 -07005032 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
5033 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5034 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5035 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5036 } else {
5037 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
5038 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5039 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5040 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5041 }
5042
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005043 /* hw_max = RP0 until we check for overclocking */
5044 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
5045
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005046 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005047 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
5048 IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005049 ret = sandybridge_pcode_read(dev_priv,
5050 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5051 &ddcc_status);
5052 if (0 == ret)
5053 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005054 clamp_t(u8,
5055 ((ddcc_status >> 8) & 0xff),
5056 dev_priv->rps.min_freq,
5057 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005058 }
5059
Chris Wilsondc979972016-05-10 14:10:04 +01005060 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305061 /* Store the frequency values in 16.66 MHZ units, which is
5062 the natural hardware unit for SKL */
5063 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5064 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5065 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5066 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5067 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5068 }
5069
Chris Wilsonaed242f2015-03-18 09:48:21 +00005070 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5071
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005072 /* Preserve min/max settings in case of re-init */
5073 if (dev_priv->rps.max_freq_softlimit == 0)
5074 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5075
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005076 if (dev_priv->rps.min_freq_softlimit == 0) {
Chris Wilsondc979972016-05-10 14:10:04 +01005077 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005078 dev_priv->rps.min_freq_softlimit =
Ville Syrjälä813b5e62015-03-25 19:27:16 +02005079 max_t(int, dev_priv->rps.efficient_freq,
5080 intel_freq_opcode(dev_priv, 450));
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005081 else
5082 dev_priv->rps.min_freq_softlimit =
5083 dev_priv->rps.min_freq;
5084 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005085}
5086
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005087/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005088static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005089{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005090 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5091
Chris Wilsondc979972016-05-10 14:10:04 +01005092 gen6_init_rps_frequencies(dev_priv);
Damien Lespiauba1c5542015-01-16 18:07:26 +00005093
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305094 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Chris Wilsondc979972016-05-10 14:10:04 +01005095 if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Akash Goel2030d682016-04-23 00:05:45 +05305096 /*
5097 * BIOS could leave the Hw Turbo enabled, so need to explicitly
5098 * clear out the Control register just to avoid inconsitency
5099 * with debugfs interface, which will show Turbo as enabled
5100 * only and that is not expected by the User after adding the
5101 * WaGsvDisableTurbo. Apart from this there is no problem even
5102 * if the Turbo is left enabled in the Control register, as the
5103 * Up/Down interrupts would remain masked.
5104 */
Chris Wilsondc979972016-05-10 14:10:04 +01005105 gen9_disable_rps(dev_priv);
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05305106 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5107 return;
5108 }
5109
Akash Goel0beb0592015-03-06 11:07:20 +05305110 /* Program defaults and thresholds for RPS*/
5111 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5112 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005113
Akash Goel0beb0592015-03-06 11:07:20 +05305114 /* 1 second timeout*/
5115 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5116 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5117
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005118 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005119
Akash Goel0beb0592015-03-06 11:07:20 +05305120 /* Leaning on the below call to gen6_set_rps to program/setup the
5121 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5122 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
5123 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsondc979972016-05-10 14:10:04 +01005124 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005125
5126 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5127}
5128
Chris Wilsondc979972016-05-10 14:10:04 +01005129static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005130{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005131 struct intel_engine_cs *engine;
Zhe Wang20e49362014-11-04 17:07:05 +00005132 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005133
5134 /* 1a: Software RC state - RC0 */
5135 I915_WRITE(GEN6_RC_STATE, 0);
5136
5137 /* 1b: Get forcewake during program sequence. Although the driver
5138 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005139 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005140
5141 /* 2a: Disable RC states. */
5142 I915_WRITE(GEN6_RC_CONTROL, 0);
5143
5144 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305145
5146 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005147 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305148 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5149 else
5150 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005151 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5152 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005153 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005154 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305155
Dave Gordon1a3d1892016-05-13 15:36:30 +01005156 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305157 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5158
Zhe Wang20e49362014-11-04 17:07:05 +00005159 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005160
Zhe Wang38c23522015-01-20 12:23:04 +00005161 /* 2c: Program Coarse Power Gating Policies. */
5162 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5163 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5164
Zhe Wang20e49362014-11-04 17:07:05 +00005165 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005166 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005167 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005168 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305169 /* WaRsUseTimeoutMode */
Chris Wilsondc979972016-05-10 14:10:04 +01005170 if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_D0) ||
5171 IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305172 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305173 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5174 GEN7_RC_CTL_TO_MODE |
5175 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305176 } else {
5177 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05305178 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5179 GEN6_RC_CTL_EI_MODE(1) |
5180 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05305181 }
Zhe Wang20e49362014-11-04 17:07:05 +00005182
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305183 /*
5184 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305185 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305186 */
Chris Wilsondc979972016-05-10 14:10:04 +01005187 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305188 I915_WRITE(GEN9_PG_ENABLE, 0);
5189 else
5190 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5191 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005192
Mika Kuoppala59bad942015-01-16 11:34:40 +02005193 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005194}
5195
Chris Wilsondc979972016-05-10 14:10:04 +01005196static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005197{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005198 struct intel_engine_cs *engine;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005199 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005200
5201 /* 1a: Software RC state - RC0 */
5202 I915_WRITE(GEN6_RC_STATE, 0);
5203
5204 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5205 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005206 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005207
5208 /* 2a: Disable RC states. */
5209 I915_WRITE(GEN6_RC_CONTROL, 0);
5210
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005211 /* Initialize rps frequencies */
Chris Wilsondc979972016-05-10 14:10:04 +01005212 gen6_init_rps_frequencies(dev_priv);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005213
5214 /* 2b: Program RC6 thresholds.*/
5215 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5216 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5217 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005218 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005219 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005220 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005221 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005222 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5223 else
5224 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005225
5226 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005227 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005228 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005229 intel_print_rc6_info(dev_priv, rc6_mask);
5230 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005231 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5232 GEN7_RC_CTL_TO_MODE |
5233 rc6_mask);
5234 else
5235 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5236 GEN6_RC_CTL_EI_MODE(1) |
5237 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005238
5239 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005240 I915_WRITE(GEN6_RPNSWREQ,
5241 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5242 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5243 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005244 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5245 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005246
Daniel Vetter7526ed72014-09-29 15:07:19 +02005247 /* Docs recommend 900MHz, and 300 MHz respectively */
5248 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5249 dev_priv->rps.max_freq_softlimit << 24 |
5250 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005251
Daniel Vetter7526ed72014-09-29 15:07:19 +02005252 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5253 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5254 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5255 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005256
Daniel Vetter7526ed72014-09-29 15:07:19 +02005257 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005258
5259 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005260 I915_WRITE(GEN6_RP_CONTROL,
5261 GEN6_RP_MEDIA_TURBO |
5262 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5263 GEN6_RP_MEDIA_IS_GFX |
5264 GEN6_RP_ENABLE |
5265 GEN6_RP_UP_BUSY_AVG |
5266 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005267
Daniel Vetter7526ed72014-09-29 15:07:19 +02005268 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005269
Tom O'Rourkec7f31532014-11-19 14:21:54 -08005270 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsondc979972016-05-10 14:10:04 +01005271 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005272
Mika Kuoppala59bad942015-01-16 11:34:40 +02005273 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005274}
5275
Chris Wilsondc979972016-05-10 14:10:04 +01005276static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005277{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005278 struct intel_engine_cs *engine;
Ben Widawskyd060c162014-03-19 18:31:08 -07005279 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005280 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005281 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005282 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005283
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005284 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005285
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005286 /* Here begins a magic sequence of register writes to enable
5287 * auto-downclocking.
5288 *
5289 * Perhaps there might be some value in exposing these to
5290 * userspace...
5291 */
5292 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005293
5294 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005295 gtfifodbg = I915_READ(GTFIFODBG);
5296 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005297 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5298 I915_WRITE(GTFIFODBG, gtfifodbg);
5299 }
5300
Mika Kuoppala59bad942015-01-16 11:34:40 +02005301 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005302
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005303 /* Initialize rps frequencies */
Chris Wilsondc979972016-05-10 14:10:04 +01005304 gen6_init_rps_frequencies(dev_priv);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06005305
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005306 /* disable the counters and set deterministic thresholds */
5307 I915_WRITE(GEN6_RC_CONTROL, 0);
5308
5309 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5310 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5311 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5312 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5313 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5314
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005315 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005316 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005317
5318 I915_WRITE(GEN6_RC_SLEEP, 0);
5319 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005320 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005321 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5322 else
5323 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005324 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005325 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5326
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005327 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005328 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005329 if (rc6_mode & INTEL_RC6_ENABLE)
5330 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5331
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005332 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005333 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005334 if (rc6_mode & INTEL_RC6p_ENABLE)
5335 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005336
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005337 if (rc6_mode & INTEL_RC6pp_ENABLE)
5338 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5339 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005340
Chris Wilsondc979972016-05-10 14:10:04 +01005341 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005342
5343 I915_WRITE(GEN6_RC_CONTROL,
5344 rc6_mask |
5345 GEN6_RC_CTL_EI_MODE(1) |
5346 GEN6_RC_CTL_HW_ENABLE);
5347
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005348 /* Power down if completely idle for over 50ms */
5349 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005350 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005351
Ben Widawsky42c05262012-09-26 10:34:00 -07005352 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07005353 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07005354 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07005355
5356 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5357 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5358 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07005359 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07005360 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07005361 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005362 }
5363
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005364 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsondc979972016-05-10 14:10:04 +01005365 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005366
Ben Widawsky31643d52012-09-26 10:34:01 -07005367 rc6vids = 0;
5368 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005369 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005370 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005371 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005372 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5373 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5374 rc6vids &= 0xffff00;
5375 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5376 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5377 if (ret)
5378 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5379 }
5380
Mika Kuoppala59bad942015-01-16 11:34:40 +02005381 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005382}
5383
Chris Wilsondc979972016-05-10 14:10:04 +01005384static void __gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005385{
5386 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005387 unsigned int gpu_freq;
5388 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305389 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005390 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005391 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005392
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005393 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005394
Ben Widawskyeda79642013-10-07 17:15:48 -03005395 policy = cpufreq_cpu_get(0);
5396 if (policy) {
5397 max_ia_freq = policy->cpuinfo.max_freq;
5398 cpufreq_cpu_put(policy);
5399 } else {
5400 /*
5401 * Default to measured freq if none found, PCU will ensure we
5402 * don't go over
5403 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005404 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005405 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005406
5407 /* Convert from kHz to MHz */
5408 max_ia_freq /= 1000;
5409
Ben Widawsky153b4b952013-10-22 22:05:09 -07005410 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005411 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5412 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005413
Chris Wilsondc979972016-05-10 14:10:04 +01005414 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305415 /* Convert GT frequency to 50 HZ units */
5416 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5417 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5418 } else {
5419 min_gpu_freq = dev_priv->rps.min_freq;
5420 max_gpu_freq = dev_priv->rps.max_freq;
5421 }
5422
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005423 /*
5424 * For each potential GPU frequency, load a ring frequency we'd like
5425 * to use for memory access. We do this by specifying the IA frequency
5426 * the PCU should use as a reference to determine the ring frequency.
5427 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305428 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5429 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005430 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005431
Chris Wilsondc979972016-05-10 14:10:04 +01005432 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305433 /*
5434 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5435 * No floor required for ring frequency on SKL.
5436 */
5437 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005438 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005439 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5440 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005441 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005442 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005443 ring_freq = max(min_ring_freq, ring_freq);
5444 /* leave ia_freq as the default, chosen by cpufreq */
5445 } else {
5446 /* On older processors, there is no separate ring
5447 * clock domain, so in order to boost the bandwidth
5448 * of the ring, we need to upclock the CPU (ia_freq).
5449 *
5450 * For GPU frequencies less than 750MHz,
5451 * just use the lowest ring freq.
5452 */
5453 if (gpu_freq < min_freq)
5454 ia_freq = 800;
5455 else
5456 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5457 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5458 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005459
Ben Widawsky42c05262012-09-26 10:34:00 -07005460 sandybridge_pcode_write(dev_priv,
5461 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005462 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5463 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5464 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005465 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005466}
5467
Chris Wilsondc979972016-05-10 14:10:04 +01005468void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005469{
Chris Wilsondc979972016-05-10 14:10:04 +01005470 if (!HAS_CORE_RING_FREQ(dev_priv))
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005471 return;
5472
5473 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsondc979972016-05-10 14:10:04 +01005474 __gen6_update_ring_freq(dev_priv);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005475 mutex_unlock(&dev_priv->rps.hw_lock);
5476}
5477
Ville Syrjälä03af2042014-06-28 02:03:53 +03005478static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305479{
5480 u32 val, rp0;
5481
Jani Nikula5b5929c2015-10-07 11:17:46 +03005482 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305483
Chris Wilsondc979972016-05-10 14:10:04 +01005484 switch (INTEL_INFO(dev_priv)->eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005485 case 8:
5486 /* (2 * 4) config */
5487 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5488 break;
5489 case 12:
5490 /* (2 * 6) config */
5491 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5492 break;
5493 case 16:
5494 /* (2 * 8) config */
5495 default:
5496 /* Setting (2 * 8) Min RP0 for any other combination */
5497 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5498 break;
Deepak S095acd52015-01-17 11:05:59 +05305499 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005500
5501 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5502
Deepak S2b6b3a02014-05-27 15:59:30 +05305503 return rp0;
5504}
5505
5506static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5507{
5508 u32 val, rpe;
5509
5510 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5511 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5512
5513 return rpe;
5514}
5515
Deepak S7707df42014-07-12 18:46:14 +05305516static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5517{
5518 u32 val, rp1;
5519
Jani Nikula5b5929c2015-10-07 11:17:46 +03005520 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5521 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5522
Deepak S7707df42014-07-12 18:46:14 +05305523 return rp1;
5524}
5525
Deepak Sf8f2b002014-07-10 13:16:21 +05305526static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5527{
5528 u32 val, rp1;
5529
5530 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5531
5532 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5533
5534 return rp1;
5535}
5536
Ville Syrjälä03af2042014-06-28 02:03:53 +03005537static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005538{
5539 u32 val, rp0;
5540
Jani Nikula64936252013-05-22 15:36:20 +03005541 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005542
5543 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5544 /* Clamp to max */
5545 rp0 = min_t(u32, rp0, 0xea);
5546
5547 return rp0;
5548}
5549
5550static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5551{
5552 u32 val, rpe;
5553
Jani Nikula64936252013-05-22 15:36:20 +03005554 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005555 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005556 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005557 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5558
5559 return rpe;
5560}
5561
Ville Syrjälä03af2042014-06-28 02:03:53 +03005562static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005563{
Imre Deak36146032014-12-04 18:39:35 +02005564 u32 val;
5565
5566 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5567 /*
5568 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5569 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5570 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5571 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5572 * to make sure it matches what Punit accepts.
5573 */
5574 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005575}
5576
Imre Deakae484342014-03-31 15:10:44 +03005577/* Check that the pctx buffer wasn't move under us. */
5578static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5579{
5580 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5581
5582 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5583 dev_priv->vlv_pctx->stolen->start);
5584}
5585
Deepak S38807742014-05-23 21:00:15 +05305586
5587/* Check that the pcbr address is not empty. */
5588static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5589{
5590 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5591
5592 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5593}
5594
Chris Wilsondc979972016-05-10 14:10:04 +01005595static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305596{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005597 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005598 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05305599 u32 pcbr;
5600 int pctx_size = 32*1024;
5601
Deepak S38807742014-05-23 21:00:15 +05305602 pcbr = I915_READ(VLV_PCBR);
5603 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005604 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305605 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02005606 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05305607
5608 pctx_paddr = (paddr & (~4095));
5609 I915_WRITE(VLV_PCBR, pctx_paddr);
5610 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005611
5612 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305613}
5614
Chris Wilsondc979972016-05-10 14:10:04 +01005615static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005616{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005617 struct drm_i915_gem_object *pctx;
5618 unsigned long pctx_paddr;
5619 u32 pcbr;
5620 int pctx_size = 24*1024;
5621
Chris Wilsondc979972016-05-10 14:10:04 +01005622 mutex_lock(&dev_priv->dev->struct_mutex);
Imre Deak17b0c1f2014-02-11 21:39:06 +02005623
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005624 pcbr = I915_READ(VLV_PCBR);
5625 if (pcbr) {
5626 /* BIOS set it up already, grab the pre-alloc'd space */
5627 int pcbr_offset;
5628
5629 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5630 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5631 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005632 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005633 pctx_size);
5634 goto out;
5635 }
5636
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005637 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5638
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005639 /*
5640 * From the Gunit register HAS:
5641 * The Gfx driver is expected to program this register and ensure
5642 * proper allocation within Gfx stolen memory. For example, this
5643 * register should be programmed such than the PCBR range does not
5644 * overlap with other ranges, such as the frame buffer, protected
5645 * memory, or any other relevant ranges.
5646 */
Chris Wilsondc979972016-05-10 14:10:04 +01005647 pctx = i915_gem_object_create_stolen(dev_priv->dev, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005648 if (!pctx) {
5649 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005650 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005651 }
5652
5653 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5654 I915_WRITE(VLV_PCBR, pctx_paddr);
5655
5656out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005657 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005658 dev_priv->vlv_pctx = pctx;
Chris Wilsondc979972016-05-10 14:10:04 +01005659 mutex_unlock(&dev_priv->dev->struct_mutex);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005660}
5661
Chris Wilsondc979972016-05-10 14:10:04 +01005662static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03005663{
Imre Deakae484342014-03-31 15:10:44 +03005664 if (WARN_ON(!dev_priv->vlv_pctx))
5665 return;
5666
Tvrtko Ursulinee504892016-02-11 10:27:30 +00005667 drm_gem_object_unreference_unlocked(&dev_priv->vlv_pctx->base);
Imre Deakae484342014-03-31 15:10:44 +03005668 dev_priv->vlv_pctx = NULL;
5669}
5670
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005671static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5672{
5673 dev_priv->rps.gpll_ref_freq =
5674 vlv_get_cck_clock(dev_priv, "GPLL ref",
5675 CCK_GPLL_CLOCK_CONTROL,
5676 dev_priv->czclk_freq);
5677
5678 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5679 dev_priv->rps.gpll_ref_freq);
5680}
5681
Chris Wilsondc979972016-05-10 14:10:04 +01005682static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005683{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005684 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005685
Chris Wilsondc979972016-05-10 14:10:04 +01005686 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005687
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005688 vlv_init_gpll_ref_freq(dev_priv);
5689
Imre Deak4e805192014-04-14 20:24:41 +03005690 mutex_lock(&dev_priv->rps.hw_lock);
5691
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005692 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5693 switch ((val >> 6) & 3) {
5694 case 0:
5695 case 1:
5696 dev_priv->mem_freq = 800;
5697 break;
5698 case 2:
5699 dev_priv->mem_freq = 1066;
5700 break;
5701 case 3:
5702 dev_priv->mem_freq = 1333;
5703 break;
5704 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005705 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005706
Imre Deak4e805192014-04-14 20:24:41 +03005707 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5708 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5709 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005710 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005711 dev_priv->rps.max_freq);
5712
5713 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5714 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005715 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005716 dev_priv->rps.efficient_freq);
5717
Deepak Sf8f2b002014-07-10 13:16:21 +05305718 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5719 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005720 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305721 dev_priv->rps.rp1_freq);
5722
Imre Deak4e805192014-04-14 20:24:41 +03005723 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5724 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005725 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005726 dev_priv->rps.min_freq);
5727
Chris Wilsonaed242f2015-03-18 09:48:21 +00005728 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5729
Imre Deak4e805192014-04-14 20:24:41 +03005730 /* Preserve min/max settings in case of re-init */
5731 if (dev_priv->rps.max_freq_softlimit == 0)
5732 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5733
5734 if (dev_priv->rps.min_freq_softlimit == 0)
5735 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5736
5737 mutex_unlock(&dev_priv->rps.hw_lock);
5738}
5739
Chris Wilsondc979972016-05-10 14:10:04 +01005740static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305741{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005742 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305743
Chris Wilsondc979972016-05-10 14:10:04 +01005744 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05305745
Ville Syrjäläc30fec62016-03-04 21:43:02 +02005746 vlv_init_gpll_ref_freq(dev_priv);
5747
Deepak S2b6b3a02014-05-27 15:59:30 +05305748 mutex_lock(&dev_priv->rps.hw_lock);
5749
Ville Syrjäläa5805162015-05-26 20:42:30 +03005750 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005751 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005752 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005753
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005754 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005755 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005756 dev_priv->mem_freq = 2000;
5757 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005758 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005759 dev_priv->mem_freq = 1600;
5760 break;
5761 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005762 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005763
Deepak S2b6b3a02014-05-27 15:59:30 +05305764 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5765 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5766 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005767 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305768 dev_priv->rps.max_freq);
5769
5770 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5771 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005772 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305773 dev_priv->rps.efficient_freq);
5774
Deepak S7707df42014-07-12 18:46:14 +05305775 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5776 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005777 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305778 dev_priv->rps.rp1_freq);
5779
Deepak S5b7c91b2015-05-09 18:15:46 +05305780 /* PUnit validated range is only [RPe, RP0] */
5781 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305782 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005783 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305784 dev_priv->rps.min_freq);
5785
Ville Syrjälä1c147622014-08-18 14:42:43 +03005786 WARN_ONCE((dev_priv->rps.max_freq |
5787 dev_priv->rps.efficient_freq |
5788 dev_priv->rps.rp1_freq |
5789 dev_priv->rps.min_freq) & 1,
5790 "Odd GPU freq values\n");
5791
Chris Wilsonaed242f2015-03-18 09:48:21 +00005792 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5793
Deepak S2b6b3a02014-05-27 15:59:30 +05305794 /* Preserve min/max settings in case of re-init */
5795 if (dev_priv->rps.max_freq_softlimit == 0)
5796 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5797
5798 if (dev_priv->rps.min_freq_softlimit == 0)
5799 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5800
5801 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305802}
5803
Chris Wilsondc979972016-05-10 14:10:04 +01005804static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03005805{
Chris Wilsondc979972016-05-10 14:10:04 +01005806 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03005807}
5808
Chris Wilsondc979972016-05-10 14:10:04 +01005809static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305810{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005811 struct intel_engine_cs *engine;
Deepak S2b6b3a02014-05-27 15:59:30 +05305812 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305813
5814 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5815
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005816 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
5817 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05305818 if (gtfifodbg) {
5819 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5820 gtfifodbg);
5821 I915_WRITE(GTFIFODBG, gtfifodbg);
5822 }
5823
5824 cherryview_check_pctx(dev_priv);
5825
5826 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5827 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005828 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305829
Ville Syrjälä160614a2015-01-19 13:50:47 +02005830 /* Disable RC states. */
5831 I915_WRITE(GEN6_RC_CONTROL, 0);
5832
Deepak S38807742014-05-23 21:00:15 +05305833 /* 2a: Program RC6 thresholds.*/
5834 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5835 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5836 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5837
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005838 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005839 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05305840 I915_WRITE(GEN6_RC_SLEEP, 0);
5841
Deepak Sf4f71c72015-03-28 15:23:35 +05305842 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5843 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05305844
5845 /* allows RC6 residency counter to work */
5846 I915_WRITE(VLV_COUNTER_CONTROL,
5847 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5848 VLV_MEDIA_RC6_COUNT_EN |
5849 VLV_RENDER_RC6_COUNT_EN));
5850
5851 /* For now we assume BIOS is allocating and populating the PCBR */
5852 pcbr = I915_READ(VLV_PCBR);
5853
Deepak S38807742014-05-23 21:00:15 +05305854 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005855 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
5856 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005857 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305858
5859 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5860
Deepak S2b6b3a02014-05-27 15:59:30 +05305861 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005862 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305863 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5864 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5865 I915_WRITE(GEN6_RP_UP_EI, 66000);
5866 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5867
5868 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5869
5870 /* 5: Enable RPS */
5871 I915_WRITE(GEN6_RP_CONTROL,
5872 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005873 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305874 GEN6_RP_ENABLE |
5875 GEN6_RP_UP_BUSY_AVG |
5876 GEN6_RP_DOWN_IDLE_AVG);
5877
Deepak S3ef62342015-04-29 08:36:24 +05305878 /* Setting Fixed Bias */
5879 val = VLV_OVERRIDE_EN |
5880 VLV_SOC_TDP_EN |
5881 CHV_BIAS_CPU_50_SOC_50;
5882 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5883
Deepak S2b6b3a02014-05-27 15:59:30 +05305884 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5885
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005886 /* RPS code assumes GPLL is used */
5887 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5888
Jani Nikula742f4912015-09-03 11:16:09 +03005889 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05305890 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5891
5892 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5893 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005894 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305895 dev_priv->rps.cur_freq);
5896
5897 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005898 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5899 dev_priv->rps.idle_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05305900
Chris Wilsondc979972016-05-10 14:10:04 +01005901 valleyview_set_rps(dev_priv, dev_priv->rps.idle_freq);
Deepak S2b6b3a02014-05-27 15:59:30 +05305902
Mika Kuoppala59bad942015-01-16 11:34:40 +02005903 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305904}
5905
Chris Wilsondc979972016-05-10 14:10:04 +01005906static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005907{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005908 struct intel_engine_cs *engine;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005909 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005910
5911 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5912
Imre Deakae484342014-03-31 15:10:44 +03005913 valleyview_check_pctx(dev_priv);
5914
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005915 gtfifodbg = I915_READ(GTFIFODBG);
5916 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005917 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5918 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005919 I915_WRITE(GTFIFODBG, gtfifodbg);
5920 }
5921
Deepak Sc8d9a592013-11-23 14:55:42 +05305922 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005923 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005924
Ville Syrjälä160614a2015-01-19 13:50:47 +02005925 /* Disable RC states. */
5926 I915_WRITE(GEN6_RC_CONTROL, 0);
5927
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005928 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005929 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5930 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5931 I915_WRITE(GEN6_RP_UP_EI, 66000);
5932 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5933
5934 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5935
5936 I915_WRITE(GEN6_RP_CONTROL,
5937 GEN6_RP_MEDIA_TURBO |
5938 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5939 GEN6_RP_MEDIA_IS_GFX |
5940 GEN6_RP_ENABLE |
5941 GEN6_RP_UP_BUSY_AVG |
5942 GEN6_RP_DOWN_IDLE_CONT);
5943
5944 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5945 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5946 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5947
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005948 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005949 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005950
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08005951 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005952
5953 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005954 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005955 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5956 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005957 VLV_MEDIA_RC6_COUNT_EN |
5958 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005959
Chris Wilsondc979972016-05-10 14:10:04 +01005960 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005961 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005962
Chris Wilsondc979972016-05-10 14:10:04 +01005963 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005964
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005965 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005966
Deepak S3ef62342015-04-29 08:36:24 +05305967 /* Setting Fixed Bias */
5968 val = VLV_OVERRIDE_EN |
5969 VLV_SOC_TDP_EN |
5970 VLV_BIAS_CPU_125_SOC_875;
5971 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5972
Jani Nikula64936252013-05-22 15:36:20 +03005973 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005974
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005975 /* RPS code assumes GPLL is used */
5976 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5977
Jani Nikula742f4912015-09-03 11:16:09 +03005978 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07005979 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5980
Ben Widawskyb39fb292014-03-19 18:31:11 -07005981 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005982 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005983 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005984 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005985
Ville Syrjälä73008b92013-06-25 19:21:01 +03005986 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä5fd9f522016-03-04 21:43:03 +02005987 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5988 dev_priv->rps.idle_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005989
Chris Wilsondc979972016-05-10 14:10:04 +01005990 valleyview_set_rps(dev_priv, dev_priv->rps.idle_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005991
Mika Kuoppala59bad942015-01-16 11:34:40 +02005992 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005993}
5994
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005995static unsigned long intel_pxfreq(u32 vidfreq)
5996{
5997 unsigned long freq;
5998 int div = (vidfreq & 0x3f0000) >> 16;
5999 int post = (vidfreq & 0x3000) >> 12;
6000 int pre = (vidfreq & 0x7);
6001
6002 if (!pre)
6003 return 0;
6004
6005 freq = ((div * 133333) / ((1<<post) * pre));
6006
6007 return freq;
6008}
6009
Daniel Vettereb48eb02012-04-26 23:28:12 +02006010static const struct cparams {
6011 u16 i;
6012 u16 t;
6013 u16 m;
6014 u16 c;
6015} cparams[] = {
6016 { 1, 1333, 301, 28664 },
6017 { 1, 1066, 294, 24460 },
6018 { 1, 800, 294, 25192 },
6019 { 0, 1333, 276, 27605 },
6020 { 0, 1066, 276, 27605 },
6021 { 0, 800, 231, 23784 },
6022};
6023
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006024static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006025{
6026 u64 total_count, diff, ret;
6027 u32 count1, count2, count3, m = 0, c = 0;
6028 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6029 int i;
6030
Daniel Vetter02d71952012-08-09 16:44:54 +02006031 assert_spin_locked(&mchdev_lock);
6032
Daniel Vetter20e4d402012-08-08 23:35:39 +02006033 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006034
6035 /* Prevent division-by-zero if we are asking too fast.
6036 * Also, we don't get interesting results if we are polling
6037 * faster than once in 10ms, so just return the saved value
6038 * in such cases.
6039 */
6040 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006041 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006042
6043 count1 = I915_READ(DMIEC);
6044 count2 = I915_READ(DDREC);
6045 count3 = I915_READ(CSIEC);
6046
6047 total_count = count1 + count2 + count3;
6048
6049 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006050 if (total_count < dev_priv->ips.last_count1) {
6051 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006052 diff += total_count;
6053 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006054 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006055 }
6056
6057 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006058 if (cparams[i].i == dev_priv->ips.c_m &&
6059 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006060 m = cparams[i].m;
6061 c = cparams[i].c;
6062 break;
6063 }
6064 }
6065
6066 diff = div_u64(diff, diff1);
6067 ret = ((m * diff) + c);
6068 ret = div_u64(ret, 10);
6069
Daniel Vetter20e4d402012-08-08 23:35:39 +02006070 dev_priv->ips.last_count1 = total_count;
6071 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006072
Daniel Vetter20e4d402012-08-08 23:35:39 +02006073 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006074
6075 return ret;
6076}
6077
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006078unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6079{
6080 unsigned long val;
6081
Chris Wilsondc979972016-05-10 14:10:04 +01006082 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006083 return 0;
6084
6085 spin_lock_irq(&mchdev_lock);
6086
6087 val = __i915_chipset_val(dev_priv);
6088
6089 spin_unlock_irq(&mchdev_lock);
6090
6091 return val;
6092}
6093
Daniel Vettereb48eb02012-04-26 23:28:12 +02006094unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6095{
6096 unsigned long m, x, b;
6097 u32 tsfs;
6098
6099 tsfs = I915_READ(TSFS);
6100
6101 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6102 x = I915_READ8(TR1);
6103
6104 b = tsfs & TSFS_INTR_MASK;
6105
6106 return ((m * x) / 127) - b;
6107}
6108
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006109static int _pxvid_to_vd(u8 pxvid)
6110{
6111 if (pxvid == 0)
6112 return 0;
6113
6114 if (pxvid >= 8 && pxvid < 31)
6115 pxvid = 31;
6116
6117 return (pxvid + 2) * 125;
6118}
6119
6120static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006121{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006122 const int vd = _pxvid_to_vd(pxvid);
6123 const int vm = vd - 1125;
6124
Chris Wilsondc979972016-05-10 14:10:04 +01006125 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006126 return vm > 0 ? vm : 0;
6127
6128 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006129}
6130
Daniel Vetter02d71952012-08-09 16:44:54 +02006131static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006132{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006133 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006134 u32 count;
6135
Daniel Vetter02d71952012-08-09 16:44:54 +02006136 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006137
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006138 now = ktime_get_raw_ns();
6139 diffms = now - dev_priv->ips.last_time2;
6140 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006141
6142 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006143 if (!diffms)
6144 return;
6145
6146 count = I915_READ(GFXEC);
6147
Daniel Vetter20e4d402012-08-08 23:35:39 +02006148 if (count < dev_priv->ips.last_count2) {
6149 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006150 diff += count;
6151 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006152 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006153 }
6154
Daniel Vetter20e4d402012-08-08 23:35:39 +02006155 dev_priv->ips.last_count2 = count;
6156 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006157
6158 /* More magic constants... */
6159 diff = diff * 1181;
6160 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006161 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006162}
6163
Daniel Vetter02d71952012-08-09 16:44:54 +02006164void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6165{
Chris Wilsondc979972016-05-10 14:10:04 +01006166 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006167 return;
6168
Daniel Vetter92703882012-08-09 16:46:01 +02006169 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006170
6171 __i915_update_gfx_val(dev_priv);
6172
Daniel Vetter92703882012-08-09 16:46:01 +02006173 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006174}
6175
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006176static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006177{
6178 unsigned long t, corr, state1, corr2, state2;
6179 u32 pxvid, ext_v;
6180
Daniel Vetter02d71952012-08-09 16:44:54 +02006181 assert_spin_locked(&mchdev_lock);
6182
Ville Syrjälä616847e2015-09-18 20:03:19 +03006183 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006184 pxvid = (pxvid >> 24) & 0x7f;
6185 ext_v = pvid_to_extvid(dev_priv, pxvid);
6186
6187 state1 = ext_v;
6188
6189 t = i915_mch_val(dev_priv);
6190
6191 /* Revel in the empirically derived constants */
6192
6193 /* Correction factor in 1/100000 units */
6194 if (t > 80)
6195 corr = ((t * 2349) + 135940);
6196 else if (t >= 50)
6197 corr = ((t * 964) + 29317);
6198 else /* < 50 */
6199 corr = ((t * 301) + 1004);
6200
6201 corr = corr * ((150142 * state1) / 10000 - 78642);
6202 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006203 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006204
6205 state2 = (corr2 * state1) / 10000;
6206 state2 /= 100; /* convert to mW */
6207
Daniel Vetter02d71952012-08-09 16:44:54 +02006208 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006209
Daniel Vetter20e4d402012-08-08 23:35:39 +02006210 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006211}
6212
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006213unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6214{
6215 unsigned long val;
6216
Chris Wilsondc979972016-05-10 14:10:04 +01006217 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006218 return 0;
6219
6220 spin_lock_irq(&mchdev_lock);
6221
6222 val = __i915_gfx_val(dev_priv);
6223
6224 spin_unlock_irq(&mchdev_lock);
6225
6226 return val;
6227}
6228
Daniel Vettereb48eb02012-04-26 23:28:12 +02006229/**
6230 * i915_read_mch_val - return value for IPS use
6231 *
6232 * Calculate and return a value for the IPS driver to use when deciding whether
6233 * we have thermal and power headroom to increase CPU or GPU power budget.
6234 */
6235unsigned long i915_read_mch_val(void)
6236{
6237 struct drm_i915_private *dev_priv;
6238 unsigned long chipset_val, graphics_val, ret = 0;
6239
Daniel Vetter92703882012-08-09 16:46:01 +02006240 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006241 if (!i915_mch_dev)
6242 goto out_unlock;
6243 dev_priv = i915_mch_dev;
6244
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006245 chipset_val = __i915_chipset_val(dev_priv);
6246 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006247
6248 ret = chipset_val + graphics_val;
6249
6250out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006251 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006252
6253 return ret;
6254}
6255EXPORT_SYMBOL_GPL(i915_read_mch_val);
6256
6257/**
6258 * i915_gpu_raise - raise GPU frequency limit
6259 *
6260 * Raise the limit; IPS indicates we have thermal headroom.
6261 */
6262bool i915_gpu_raise(void)
6263{
6264 struct drm_i915_private *dev_priv;
6265 bool ret = true;
6266
Daniel Vetter92703882012-08-09 16:46:01 +02006267 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006268 if (!i915_mch_dev) {
6269 ret = false;
6270 goto out_unlock;
6271 }
6272 dev_priv = i915_mch_dev;
6273
Daniel Vetter20e4d402012-08-08 23:35:39 +02006274 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6275 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006276
6277out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006278 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006279
6280 return ret;
6281}
6282EXPORT_SYMBOL_GPL(i915_gpu_raise);
6283
6284/**
6285 * i915_gpu_lower - lower GPU frequency limit
6286 *
6287 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6288 * frequency maximum.
6289 */
6290bool i915_gpu_lower(void)
6291{
6292 struct drm_i915_private *dev_priv;
6293 bool ret = true;
6294
Daniel Vetter92703882012-08-09 16:46:01 +02006295 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006296 if (!i915_mch_dev) {
6297 ret = false;
6298 goto out_unlock;
6299 }
6300 dev_priv = i915_mch_dev;
6301
Daniel Vetter20e4d402012-08-08 23:35:39 +02006302 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6303 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006304
6305out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006306 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006307
6308 return ret;
6309}
6310EXPORT_SYMBOL_GPL(i915_gpu_lower);
6311
6312/**
6313 * i915_gpu_busy - indicate GPU business to IPS
6314 *
6315 * Tell the IPS driver whether or not the GPU is busy.
6316 */
6317bool i915_gpu_busy(void)
6318{
6319 struct drm_i915_private *dev_priv;
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006320 struct intel_engine_cs *engine;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006321 bool ret = false;
6322
Daniel Vetter92703882012-08-09 16:46:01 +02006323 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006324 if (!i915_mch_dev)
6325 goto out_unlock;
6326 dev_priv = i915_mch_dev;
6327
Dave Gordonb4ac5af2016-03-24 11:20:38 +00006328 for_each_engine(engine, dev_priv)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006329 ret |= !list_empty(&engine->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006330
6331out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006332 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006333
6334 return ret;
6335}
6336EXPORT_SYMBOL_GPL(i915_gpu_busy);
6337
6338/**
6339 * i915_gpu_turbo_disable - disable graphics turbo
6340 *
6341 * Disable graphics turbo by resetting the max frequency and setting the
6342 * current frequency to the default.
6343 */
6344bool i915_gpu_turbo_disable(void)
6345{
6346 struct drm_i915_private *dev_priv;
6347 bool ret = true;
6348
Daniel Vetter92703882012-08-09 16:46:01 +02006349 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006350 if (!i915_mch_dev) {
6351 ret = false;
6352 goto out_unlock;
6353 }
6354 dev_priv = i915_mch_dev;
6355
Daniel Vetter20e4d402012-08-08 23:35:39 +02006356 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006357
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006358 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006359 ret = false;
6360
6361out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006362 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006363
6364 return ret;
6365}
6366EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6367
6368/**
6369 * Tells the intel_ips driver that the i915 driver is now loaded, if
6370 * IPS got loaded first.
6371 *
6372 * This awkward dance is so that neither module has to depend on the
6373 * other in order for IPS to do the appropriate communication of
6374 * GPU turbo limits to i915.
6375 */
6376static void
6377ips_ping_for_i915_load(void)
6378{
6379 void (*link)(void);
6380
6381 link = symbol_get(ips_link_to_i915_driver);
6382 if (link) {
6383 link();
6384 symbol_put(ips_link_to_i915_driver);
6385 }
6386}
6387
6388void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6389{
Daniel Vetter02d71952012-08-09 16:44:54 +02006390 /* We only register the i915 ips part with intel-ips once everything is
6391 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006392 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006393 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006394 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006395
6396 ips_ping_for_i915_load();
6397}
6398
6399void intel_gpu_ips_teardown(void)
6400{
Daniel Vetter92703882012-08-09 16:46:01 +02006401 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006402 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006403 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006404}
Deepak S76c3552f2014-01-30 23:08:16 +05306405
Chris Wilsondc979972016-05-10 14:10:04 +01006406static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006407{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006408 u32 lcfuse;
6409 u8 pxw[16];
6410 int i;
6411
6412 /* Disable to program */
6413 I915_WRITE(ECR, 0);
6414 POSTING_READ(ECR);
6415
6416 /* Program energy weights for various events */
6417 I915_WRITE(SDEW, 0x15040d00);
6418 I915_WRITE(CSIEW0, 0x007f0000);
6419 I915_WRITE(CSIEW1, 0x1e220004);
6420 I915_WRITE(CSIEW2, 0x04000004);
6421
6422 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006423 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006424 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006425 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006426
6427 /* Program P-state weights to account for frequency power adjustment */
6428 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006429 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006430 unsigned long freq = intel_pxfreq(pxvidfreq);
6431 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6432 PXVFREQ_PX_SHIFT;
6433 unsigned long val;
6434
6435 val = vid * vid;
6436 val *= (freq / 1000);
6437 val *= 255;
6438 val /= (127*127*900);
6439 if (val > 0xff)
6440 DRM_ERROR("bad pxval: %ld\n", val);
6441 pxw[i] = val;
6442 }
6443 /* Render standby states get 0 weight */
6444 pxw[14] = 0;
6445 pxw[15] = 0;
6446
6447 for (i = 0; i < 4; i++) {
6448 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6449 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006450 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006451 }
6452
6453 /* Adjust magic regs to magic values (more experimental results) */
6454 I915_WRITE(OGW0, 0);
6455 I915_WRITE(OGW1, 0);
6456 I915_WRITE(EG0, 0x00007f00);
6457 I915_WRITE(EG1, 0x0000000e);
6458 I915_WRITE(EG2, 0x000e0000);
6459 I915_WRITE(EG3, 0x68000300);
6460 I915_WRITE(EG4, 0x42000000);
6461 I915_WRITE(EG5, 0x00140031);
6462 I915_WRITE(EG6, 0);
6463 I915_WRITE(EG7, 0);
6464
6465 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006466 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006467
6468 /* Enable PMON + select events */
6469 I915_WRITE(ECR, 0x80000019);
6470
6471 lcfuse = I915_READ(LCFUSE02);
6472
Daniel Vetter20e4d402012-08-08 23:35:39 +02006473 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006474}
6475
Chris Wilsondc979972016-05-10 14:10:04 +01006476void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006477{
Imre Deakb268c692015-12-15 20:10:31 +02006478 /*
6479 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6480 * requirement.
6481 */
6482 if (!i915.enable_rc6) {
6483 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6484 intel_runtime_pm_get(dev_priv);
6485 }
Imre Deake6069ca2014-04-18 16:01:02 +03006486
Chris Wilsondc979972016-05-10 14:10:04 +01006487 if (IS_CHERRYVIEW(dev_priv))
6488 cherryview_init_gt_powersave(dev_priv);
6489 else if (IS_VALLEYVIEW(dev_priv))
6490 valleyview_init_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006491}
6492
Chris Wilsondc979972016-05-10 14:10:04 +01006493void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006494{
Chris Wilsondc979972016-05-10 14:10:04 +01006495 if (IS_CHERRYVIEW(dev_priv))
Deepak S38807742014-05-23 21:00:15 +05306496 return;
Chris Wilsondc979972016-05-10 14:10:04 +01006497 else if (IS_VALLEYVIEW(dev_priv))
6498 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006499
6500 if (!i915.enable_rc6)
6501 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006502}
6503
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006504static void gen6_suspend_rps(struct drm_i915_private *dev_priv)
Imre Deakdbea3ce2014-12-15 18:59:28 +02006505{
Imre Deakdbea3ce2014-12-15 18:59:28 +02006506 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6507
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006508 gen6_disable_rps_interrupts(dev_priv);
Imre Deakdbea3ce2014-12-15 18:59:28 +02006509}
6510
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006511/**
6512 * intel_suspend_gt_powersave - suspend PM work and helper threads
Chris Wilsondc979972016-05-10 14:10:04 +01006513 * @dev_priv: i915 device
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006514 *
6515 * We don't want to disable RC6 or other features here, we just want
6516 * to make sure any work we've queued has finished and won't bother
6517 * us while we're suspended.
6518 */
Chris Wilsondc979972016-05-10 14:10:04 +01006519void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006520{
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006521 if (INTEL_GEN(dev_priv) < 6)
Imre Deakd4d70aa2014-11-19 15:30:04 +02006522 return;
6523
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006524 gen6_suspend_rps(dev_priv);
Deepak Sb47adc12014-06-20 20:03:02 +05306525
6526 /* Force GPU to min freq during suspend */
6527 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006528}
6529
Chris Wilsondc979972016-05-10 14:10:04 +01006530void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006531{
Chris Wilsondc979972016-05-10 14:10:04 +01006532 if (IS_IRONLAKE_M(dev_priv)) {
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006533 ironlake_disable_drps(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006534 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
6535 intel_suspend_gt_powersave(dev_priv);
Imre Deake4948372014-05-12 18:35:04 +03006536
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006537 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsondc979972016-05-10 14:10:04 +01006538 if (INTEL_INFO(dev_priv)->gen >= 9) {
6539 gen9_disable_rc6(dev_priv);
6540 gen9_disable_rps(dev_priv);
6541 } else if (IS_CHERRYVIEW(dev_priv))
6542 cherryview_disable_rps(dev_priv);
6543 else if (IS_VALLEYVIEW(dev_priv))
6544 valleyview_disable_rps(dev_priv);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006545 else
Chris Wilsondc979972016-05-10 14:10:04 +01006546 gen6_disable_rps(dev_priv);
Imre Deake5347702014-11-19 15:30:02 +02006547
Chris Wilsonc0951f02013-10-10 21:58:50 +01006548 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006549 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006550 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006551}
6552
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006553static void intel_gen6_powersave_work(struct work_struct *work)
6554{
6555 struct drm_i915_private *dev_priv =
6556 container_of(work, struct drm_i915_private,
6557 rps.delayed_resume_work.work);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006558
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006559 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006560
Chris Wilsondc979972016-05-10 14:10:04 +01006561 gen6_reset_rps_interrupts(dev_priv);
Imre Deak3cc134e2014-11-19 15:30:03 +02006562
Chris Wilsondc979972016-05-10 14:10:04 +01006563 if (IS_CHERRYVIEW(dev_priv)) {
6564 cherryview_enable_rps(dev_priv);
6565 } else if (IS_VALLEYVIEW(dev_priv)) {
6566 valleyview_enable_rps(dev_priv);
6567 } else if (INTEL_INFO(dev_priv)->gen >= 9) {
6568 gen9_enable_rc6(dev_priv);
6569 gen9_enable_rps(dev_priv);
6570 if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
6571 __gen6_update_ring_freq(dev_priv);
6572 } else if (IS_BROADWELL(dev_priv)) {
6573 gen8_enable_rps(dev_priv);
6574 __gen6_update_ring_freq(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006575 } else {
Chris Wilsondc979972016-05-10 14:10:04 +01006576 gen6_enable_rps(dev_priv);
6577 __gen6_update_ring_freq(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006578 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006579
6580 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6581 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6582
6583 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6584 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6585
Chris Wilsonc0951f02013-10-10 21:58:50 +01006586 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02006587
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006588 gen6_enable_rps_interrupts(dev_priv);
Imre Deak3cc134e2014-11-19 15:30:03 +02006589
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006590 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03006591
6592 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006593}
6594
Chris Wilsondc979972016-05-10 14:10:04 +01006595void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006596{
Yu Zhangf61018b2015-02-10 19:05:52 +08006597 /* Powersaving is controlled by the host when inside a VM */
Chris Wilsonc0336662016-05-06 15:40:21 +01006598 if (intel_vgpu_active(dev_priv))
Yu Zhangf61018b2015-02-10 19:05:52 +08006599 return;
6600
Chris Wilsondc979972016-05-10 14:10:04 +01006601 if (IS_IRONLAKE_M(dev_priv)) {
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006602 ironlake_enable_drps(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01006603 mutex_lock(&dev_priv->dev->struct_mutex);
6604 intel_init_emon(dev_priv);
6605 mutex_unlock(&dev_priv->dev->struct_mutex);
6606 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006607 /*
6608 * PCU communication is slow and this doesn't need to be
6609 * done at any specific time, so do this out of our fast path
6610 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03006611 *
6612 * We depend on the HW RC6 power context save/restore
6613 * mechanism when entering D3 through runtime PM suspend. So
6614 * disable RPM until RPS/RC6 is properly setup. We can only
6615 * get here via the driver load/system resume/runtime resume
6616 * paths, so the _noresume version is enough (and in case of
6617 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006618 */
Imre Deakc6df39b2014-04-14 20:24:29 +03006619 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6620 round_jiffies_up_relative(HZ)))
6621 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006622 }
6623}
6624
Chris Wilsondc979972016-05-10 14:10:04 +01006625void intel_reset_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakc6df39b2014-04-14 20:24:29 +03006626{
Chris Wilsondc979972016-05-10 14:10:04 +01006627 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deakdbea3ce2014-12-15 18:59:28 +02006628 return;
6629
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006630 gen6_suspend_rps(dev_priv);
Imre Deakc6df39b2014-04-14 20:24:29 +03006631 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03006632}
6633
Daniel Vetter3107bd42012-10-31 22:52:31 +01006634static void ibx_init_clock_gating(struct drm_device *dev)
6635{
6636 struct drm_i915_private *dev_priv = dev->dev_private;
6637
6638 /*
6639 * On Ibex Peak and Cougar Point, we need to disable clock
6640 * gating for the panel power sequencer or it will fail to
6641 * start up when no ports are active.
6642 */
6643 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6644}
6645
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006646static void g4x_disable_trickle_feed(struct drm_device *dev)
6647{
6648 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006649 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006650
Damien Lespiau055e3932014-08-18 13:49:10 +01006651 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006652 I915_WRITE(DSPCNTR(pipe),
6653 I915_READ(DSPCNTR(pipe)) |
6654 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006655
6656 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6657 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006658 }
6659}
6660
Ville Syrjälä017636c2013-12-05 15:51:37 +02006661static void ilk_init_lp_watermarks(struct drm_device *dev)
6662{
6663 struct drm_i915_private *dev_priv = dev->dev_private;
6664
6665 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6666 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6667 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6668
6669 /*
6670 * Don't touch WM1S_LP_EN here.
6671 * Doing so could cause underruns.
6672 */
6673}
6674
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006675static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006676{
6677 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006678 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006679
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006680 /*
6681 * Required for FBC
6682 * WaFbcDisableDpfcClockGating:ilk
6683 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006684 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6685 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6686 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006687
6688 I915_WRITE(PCH_3DCGDIS0,
6689 MARIUNIT_CLOCK_GATE_DISABLE |
6690 SVSMUNIT_CLOCK_GATE_DISABLE);
6691 I915_WRITE(PCH_3DCGDIS1,
6692 VFMUNIT_CLOCK_GATE_DISABLE);
6693
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006694 /*
6695 * According to the spec the following bits should be set in
6696 * order to enable memory self-refresh
6697 * The bit 22/21 of 0x42004
6698 * The bit 5 of 0x42020
6699 * The bit 15 of 0x45000
6700 */
6701 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6702 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6703 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006704 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006705 I915_WRITE(DISP_ARB_CTL,
6706 (I915_READ(DISP_ARB_CTL) |
6707 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006708
6709 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006710
6711 /*
6712 * Based on the document from hardware guys the following bits
6713 * should be set unconditionally in order to enable FBC.
6714 * The bit 22 of 0x42000
6715 * The bit 22 of 0x42004
6716 * The bit 7,8,9 of 0x42020.
6717 */
6718 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006719 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006720 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6721 I915_READ(ILK_DISPLAY_CHICKEN1) |
6722 ILK_FBCQ_DIS);
6723 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6724 I915_READ(ILK_DISPLAY_CHICKEN2) |
6725 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006726 }
6727
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006728 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6729
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006730 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6731 I915_READ(ILK_DISPLAY_CHICKEN2) |
6732 ILK_ELPIN_409_SELECT);
6733 I915_WRITE(_3D_CHICKEN2,
6734 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6735 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006736
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006737 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006738 I915_WRITE(CACHE_MODE_0,
6739 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006740
Akash Goel4e046322014-04-04 17:14:38 +05306741 /* WaDisable_RenderCache_OperationalFlush:ilk */
6742 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6743
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006744 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006745
Daniel Vetter3107bd42012-10-31 22:52:31 +01006746 ibx_init_clock_gating(dev);
6747}
6748
6749static void cpt_init_clock_gating(struct drm_device *dev)
6750{
6751 struct drm_i915_private *dev_priv = dev->dev_private;
6752 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006753 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006754
6755 /*
6756 * On Ibex Peak and Cougar Point, we need to disable clock
6757 * gating for the panel power sequencer or it will fail to
6758 * start up when no ports are active.
6759 */
Jesse Barnescd664072013-10-02 10:34:19 -07006760 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6761 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6762 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006763 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6764 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006765 /* The below fixes the weird display corruption, a few pixels shifted
6766 * downward, on (only) LVDS of some HP laptops with IVY.
6767 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006768 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006769 val = I915_READ(TRANS_CHICKEN2(pipe));
6770 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6771 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006772 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006773 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006774 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6775 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6776 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006777 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6778 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006779 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006780 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006781 I915_WRITE(TRANS_CHICKEN1(pipe),
6782 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6783 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006784}
6785
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006786static void gen6_check_mch_setup(struct drm_device *dev)
6787{
6788 struct drm_i915_private *dev_priv = dev->dev_private;
6789 uint32_t tmp;
6790
6791 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006792 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6793 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6794 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006795}
6796
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006797static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006798{
6799 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006800 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006801
Damien Lespiau231e54f2012-10-19 17:55:41 +01006802 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006803
6804 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6805 I915_READ(ILK_DISPLAY_CHICKEN2) |
6806 ILK_ELPIN_409_SELECT);
6807
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006808 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006809 I915_WRITE(_3D_CHICKEN,
6810 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6811
Akash Goel4e046322014-04-04 17:14:38 +05306812 /* WaDisable_RenderCache_OperationalFlush:snb */
6813 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6814
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006815 /*
6816 * BSpec recoomends 8x4 when MSAA is used,
6817 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006818 *
6819 * Note that PS/WM thread counts depend on the WIZ hashing
6820 * disable bit, which we don't touch here, but it's good
6821 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006822 */
6823 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006824 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006825
Ville Syrjälä017636c2013-12-05 15:51:37 +02006826 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006827
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006828 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006829 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006830
6831 I915_WRITE(GEN6_UCGCTL1,
6832 I915_READ(GEN6_UCGCTL1) |
6833 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6834 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6835
6836 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6837 * gating disable must be set. Failure to set it results in
6838 * flickering pixels due to Z write ordering failures after
6839 * some amount of runtime in the Mesa "fire" demo, and Unigine
6840 * Sanctuary and Tropics, and apparently anything else with
6841 * alpha test or pixel discard.
6842 *
6843 * According to the spec, bit 11 (RCCUNIT) must also be set,
6844 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006845 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006846 * WaDisableRCCUnitClockGating:snb
6847 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006848 */
6849 I915_WRITE(GEN6_UCGCTL2,
6850 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6851 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6852
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006853 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006854 I915_WRITE(_3D_CHICKEN3,
6855 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006856
6857 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006858 * Bspec says:
6859 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6860 * 3DSTATE_SF number of SF output attributes is more than 16."
6861 */
6862 I915_WRITE(_3D_CHICKEN3,
6863 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6864
6865 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006866 * According to the spec the following bits should be
6867 * set in order to enable memory self-refresh and fbc:
6868 * The bit21 and bit22 of 0x42000
6869 * The bit21 and bit22 of 0x42004
6870 * The bit5 and bit7 of 0x42020
6871 * The bit14 of 0x70180
6872 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006873 *
6874 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006875 */
6876 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6877 I915_READ(ILK_DISPLAY_CHICKEN1) |
6878 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6879 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6880 I915_READ(ILK_DISPLAY_CHICKEN2) |
6881 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006882 I915_WRITE(ILK_DSPCLK_GATE_D,
6883 I915_READ(ILK_DSPCLK_GATE_D) |
6884 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6885 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006886
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006887 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006888
Daniel Vetter3107bd42012-10-31 22:52:31 +01006889 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006890
6891 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006892}
6893
6894static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6895{
6896 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6897
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006898 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006899 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006900 *
6901 * This actually overrides the dispatch
6902 * mode for all thread types.
6903 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006904 reg &= ~GEN7_FF_SCHED_MASK;
6905 reg |= GEN7_FF_TS_SCHED_HW;
6906 reg |= GEN7_FF_VS_SCHED_HW;
6907 reg |= GEN7_FF_DS_SCHED_HW;
6908
6909 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6910}
6911
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006912static void lpt_init_clock_gating(struct drm_device *dev)
6913{
6914 struct drm_i915_private *dev_priv = dev->dev_private;
6915
6916 /*
6917 * TODO: this bit should only be enabled when really needed, then
6918 * disabled when not needed anymore in order to save power.
6919 */
Ville Syrjäläc2699522015-08-27 23:55:59 +03006920 if (HAS_PCH_LPT_LP(dev))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006921 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6922 I915_READ(SOUTH_DSPCLK_GATE_D) |
6923 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006924
6925 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03006926 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6927 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006928 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006929}
6930
Imre Deak7d708ee2013-04-17 14:04:50 +03006931static void lpt_suspend_hw(struct drm_device *dev)
6932{
6933 struct drm_i915_private *dev_priv = dev->dev_private;
6934
Ville Syrjäläc2699522015-08-27 23:55:59 +03006935 if (HAS_PCH_LPT_LP(dev)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03006936 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6937
6938 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6939 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6940 }
6941}
6942
Imre Deak450174f2016-05-03 15:54:21 +03006943static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
6944 int general_prio_credits,
6945 int high_prio_credits)
6946{
6947 u32 misccpctl;
6948
6949 /* WaTempDisableDOPClkGating:bdw */
6950 misccpctl = I915_READ(GEN7_MISCCPCTL);
6951 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6952
6953 I915_WRITE(GEN8_L3SQCREG1,
6954 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
6955 L3_HIGH_PRIO_CREDITS(high_prio_credits));
6956
6957 /*
6958 * Wait at least 100 clocks before re-enabling clock gating.
6959 * See the definition of L3SQCREG1 in BSpec.
6960 */
6961 POSTING_READ(GEN8_L3SQCREG1);
6962 udelay(1);
6963 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6964}
6965
Mika Kuoppala9498dba2016-06-07 17:19:01 +03006966static void kabylake_init_clock_gating(struct drm_device *dev)
6967{
6968 struct drm_i915_private *dev_priv = dev->dev_private;
6969
6970 /* See Bspec note for PSR2_CTL bit 31, Wa#828:kbl */
6971 I915_WRITE(CHICKEN_PAR1_1,
6972 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
6973
6974 /* WaDisableSDEUnitClockGating:kbl */
6975 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
6976 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6977 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6978}
6979
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02006980static void skylake_init_clock_gating(struct drm_device *dev)
6981{
6982 struct drm_i915_private *dev_priv = dev->dev_private;
6983
Mika Kuoppala9498dba2016-06-07 17:19:01 +03006984 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl */
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02006985 I915_WRITE(CHICKEN_PAR1_1,
6986 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
6987}
6988
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006989static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006990{
6991 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006992 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006993
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03006994 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006995
Ben Widawskyab57fff2013-12-12 15:28:04 -08006996 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006997 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006998
Ben Widawskyab57fff2013-12-12 15:28:04 -08006999 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007000 I915_WRITE(CHICKEN_PAR1_1,
7001 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7002
Ben Widawskyab57fff2013-12-12 15:28:04 -08007003 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007004 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007005 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007006 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007007 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007008 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007009
Ben Widawskyab57fff2013-12-12 15:28:04 -08007010 /* WaVSRefCountFullforceMissDisable:bdw */
7011 /* WaDSRefCountFullforceMissDisable:bdw */
7012 I915_WRITE(GEN7_FF_THREAD_MODE,
7013 I915_READ(GEN7_FF_THREAD_MODE) &
7014 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007015
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007016 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7017 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007018
7019 /* WaDisableSDEUnitClockGating:bdw */
7020 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7021 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007022
Imre Deak450174f2016-05-03 15:54:21 +03007023 /* WaProgramL3SqcReg1Default:bdw */
7024 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007025
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007026 /*
7027 * WaGttCachingOffByDefault:bdw
7028 * GTT cache may not work with big pages, so if those
7029 * are ever enabled GTT cache may need to be disabled.
7030 */
7031 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7032
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03007033 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007034}
7035
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007036static void haswell_init_clock_gating(struct drm_device *dev)
7037{
7038 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007039
Ville Syrjälä017636c2013-12-05 15:51:37 +02007040 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007041
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007042 /* L3 caching of data atomics doesn't work -- disable it. */
7043 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7044 I915_WRITE(HSW_ROW_CHICKEN3,
7045 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7046
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007047 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007048 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7049 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7050 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7051
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007052 /* WaVSRefCountFullforceMissDisable:hsw */
7053 I915_WRITE(GEN7_FF_THREAD_MODE,
7054 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007055
Akash Goel4e046322014-04-04 17:14:38 +05307056 /* WaDisable_RenderCache_OperationalFlush:hsw */
7057 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7058
Chia-I Wufe27c602014-01-28 13:29:33 +08007059 /* enable HiZ Raw Stall Optimization */
7060 I915_WRITE(CACHE_MODE_0_GEN7,
7061 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7062
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007063 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007064 I915_WRITE(CACHE_MODE_1,
7065 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007066
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007067 /*
7068 * BSpec recommends 8x4 when MSAA is used,
7069 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007070 *
7071 * Note that PS/WM thread counts depend on the WIZ hashing
7072 * disable bit, which we don't touch here, but it's good
7073 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007074 */
7075 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007076 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007077
Kenneth Graunke94411592014-12-31 16:23:00 -08007078 /* WaSampleCChickenBitEnable:hsw */
7079 I915_WRITE(HALF_SLICE_CHICKEN3,
7080 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7081
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007082 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007083 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7084
Paulo Zanoni90a88642013-05-03 17:23:45 -03007085 /* WaRsPkgCStateDisplayPMReq:hsw */
7086 I915_WRITE(CHICKEN_PAR1_1,
7087 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007088
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007089 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007090}
7091
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007092static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007093{
7094 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07007095 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007096
Ville Syrjälä017636c2013-12-05 15:51:37 +02007097 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007098
Damien Lespiau231e54f2012-10-19 17:55:41 +01007099 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007100
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007101 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007102 I915_WRITE(_3D_CHICKEN3,
7103 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7104
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007105 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007106 I915_WRITE(IVB_CHICKEN3,
7107 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7108 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7109
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007110 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07007111 if (IS_IVB_GT1(dev))
7112 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7113 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007114
Akash Goel4e046322014-04-04 17:14:38 +05307115 /* WaDisable_RenderCache_OperationalFlush:ivb */
7116 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7117
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007118 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007119 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7120 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7121
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007122 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007123 I915_WRITE(GEN7_L3CNTLREG1,
7124 GEN7_WA_FOR_GEN7_L3_CONTROL);
7125 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007126 GEN7_WA_L3_CHICKEN_MODE);
7127 if (IS_IVB_GT1(dev))
7128 I915_WRITE(GEN7_ROW_CHICKEN2,
7129 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007130 else {
7131 /* must write both registers */
7132 I915_WRITE(GEN7_ROW_CHICKEN2,
7133 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007134 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7135 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007136 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007137
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007138 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007139 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7140 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7141
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007142 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007143 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007144 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007145 */
7146 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007147 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007148
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007149 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007150 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7151 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7152 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7153
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007154 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007155
7156 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007157
Chris Wilson22721342014-03-04 09:41:43 +00007158 if (0) { /* causes HiZ corruption on ivb:gt1 */
7159 /* enable HiZ Raw Stall Optimization */
7160 I915_WRITE(CACHE_MODE_0_GEN7,
7161 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7162 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007163
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007164 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007165 I915_WRITE(CACHE_MODE_1,
7166 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007167
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007168 /*
7169 * BSpec recommends 8x4 when MSAA is used,
7170 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007171 *
7172 * Note that PS/WM thread counts depend on the WIZ hashing
7173 * disable bit, which we don't touch here, but it's good
7174 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007175 */
7176 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007177 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007178
Ben Widawsky20848222012-05-04 18:58:59 -07007179 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7180 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7181 snpcr |= GEN6_MBC_SNPCR_MED;
7182 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007183
Ben Widawskyab5c6082013-04-05 13:12:41 -07007184 if (!HAS_PCH_NOP(dev))
7185 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007186
7187 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007188}
7189
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007190static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007191{
7192 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007193
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007194 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007195 I915_WRITE(_3D_CHICKEN3,
7196 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7197
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007198 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007199 I915_WRITE(IVB_CHICKEN3,
7200 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7201 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7202
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007203 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007204 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007205 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007206 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7207 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007208
Akash Goel4e046322014-04-04 17:14:38 +05307209 /* WaDisable_RenderCache_OperationalFlush:vlv */
7210 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7211
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007212 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007213 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7214 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7215
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007216 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007217 I915_WRITE(GEN7_ROW_CHICKEN2,
7218 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7219
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007220 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007221 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7222 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7223 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7224
Ville Syrjälä46680e02014-01-22 21:33:01 +02007225 gen7_setup_fixed_func_scheduler(dev_priv);
7226
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007227 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007228 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007229 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007230 */
7231 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007232 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007233
Akash Goelc98f5062014-03-24 23:00:07 +05307234 /* WaDisableL3Bank2xClockGate:vlv
7235 * Disabling L3 clock gating- MMIO 940c[25] = 1
7236 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7237 I915_WRITE(GEN7_UCGCTL4,
7238 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007239
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007240 /*
7241 * BSpec says this must be set, even though
7242 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7243 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007244 I915_WRITE(CACHE_MODE_1,
7245 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007246
7247 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007248 * BSpec recommends 8x4 when MSAA is used,
7249 * however in practice 16x4 seems fastest.
7250 *
7251 * Note that PS/WM thread counts depend on the WIZ hashing
7252 * disable bit, which we don't touch here, but it's good
7253 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7254 */
7255 I915_WRITE(GEN7_GT_MODE,
7256 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7257
7258 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007259 * WaIncreaseL3CreditsForVLVB0:vlv
7260 * This is the hardware default actually.
7261 */
7262 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7263
7264 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007265 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007266 * Disable clock gating on th GCFG unit to prevent a delay
7267 * in the reporting of vblank events.
7268 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007269 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007270}
7271
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007272static void cherryview_init_clock_gating(struct drm_device *dev)
7273{
7274 struct drm_i915_private *dev_priv = dev->dev_private;
7275
Ville Syrjälä232ce332014-04-09 13:28:35 +03007276 /* WaVSRefCountFullforceMissDisable:chv */
7277 /* WaDSRefCountFullforceMissDisable:chv */
7278 I915_WRITE(GEN7_FF_THREAD_MODE,
7279 I915_READ(GEN7_FF_THREAD_MODE) &
7280 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007281
7282 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7283 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7284 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007285
7286 /* WaDisableCSUnitClockGating:chv */
7287 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7288 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007289
7290 /* WaDisableSDEUnitClockGating:chv */
7291 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7292 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007293
7294 /*
Imre Deak450174f2016-05-03 15:54:21 +03007295 * WaProgramL3SqcReg1Default:chv
7296 * See gfxspecs/Related Documents/Performance Guide/
7297 * LSQC Setting Recommendations.
7298 */
7299 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7300
7301 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007302 * GTT cache may not work with big pages, so if those
7303 * are ever enabled GTT cache may need to be disabled.
7304 */
7305 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007306}
7307
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007308static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007309{
7310 struct drm_i915_private *dev_priv = dev->dev_private;
7311 uint32_t dspclk_gate;
7312
7313 I915_WRITE(RENCLK_GATE_D1, 0);
7314 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7315 GS_UNIT_CLOCK_GATE_DISABLE |
7316 CL_UNIT_CLOCK_GATE_DISABLE);
7317 I915_WRITE(RAMCLK_GATE_D, 0);
7318 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7319 OVRUNIT_CLOCK_GATE_DISABLE |
7320 OVCUNIT_CLOCK_GATE_DISABLE;
7321 if (IS_GM45(dev))
7322 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7323 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007324
7325 /* WaDisableRenderCachePipelinedFlush */
7326 I915_WRITE(CACHE_MODE_0,
7327 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007328
Akash Goel4e046322014-04-04 17:14:38 +05307329 /* WaDisable_RenderCache_OperationalFlush:g4x */
7330 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7331
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007332 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007333}
7334
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007335static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007336{
7337 struct drm_i915_private *dev_priv = dev->dev_private;
7338
7339 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7340 I915_WRITE(RENCLK_GATE_D2, 0);
7341 I915_WRITE(DSPCLK_GATE_D, 0);
7342 I915_WRITE(RAMCLK_GATE_D, 0);
7343 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007344 I915_WRITE(MI_ARB_STATE,
7345 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307346
7347 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7348 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007349}
7350
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007351static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007352{
7353 struct drm_i915_private *dev_priv = dev->dev_private;
7354
7355 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7356 I965_RCC_CLOCK_GATE_DISABLE |
7357 I965_RCPB_CLOCK_GATE_DISABLE |
7358 I965_ISC_CLOCK_GATE_DISABLE |
7359 I965_FBC_CLOCK_GATE_DISABLE);
7360 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007361 I915_WRITE(MI_ARB_STATE,
7362 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307363
7364 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7365 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007366}
7367
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007368static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007369{
7370 struct drm_i915_private *dev_priv = dev->dev_private;
7371 u32 dstate = I915_READ(D_STATE);
7372
7373 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7374 DSTATE_DOT_CLOCK_GATING;
7375 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007376
7377 if (IS_PINEVIEW(dev))
7378 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007379
7380 /* IIR "flip pending" means done if this bit is set */
7381 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007382
7383 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007384 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007385
7386 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7387 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007388
7389 I915_WRITE(MI_ARB_STATE,
7390 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007391}
7392
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007393static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007394{
7395 struct drm_i915_private *dev_priv = dev->dev_private;
7396
7397 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007398
7399 /* interrupts should cause a wake up from C3 */
7400 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7401 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007402
7403 I915_WRITE(MEM_MODE,
7404 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007405}
7406
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007407static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007408{
7409 struct drm_i915_private *dev_priv = dev->dev_private;
7410
7411 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03007412
7413 I915_WRITE(MEM_MODE,
7414 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7415 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007416}
7417
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007418void intel_init_clock_gating(struct drm_device *dev)
7419{
7420 struct drm_i915_private *dev_priv = dev->dev_private;
7421
Imre Deakbb400da2016-03-16 13:38:54 +02007422 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007423}
7424
Imre Deak7d708ee2013-04-17 14:04:50 +03007425void intel_suspend_hw(struct drm_device *dev)
7426{
7427 if (HAS_PCH_LPT(dev))
7428 lpt_suspend_hw(dev);
7429}
7430
Imre Deakbb400da2016-03-16 13:38:54 +02007431static void nop_init_clock_gating(struct drm_device *dev)
7432{
7433 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7434}
7435
7436/**
7437 * intel_init_clock_gating_hooks - setup the clock gating hooks
7438 * @dev_priv: device private
7439 *
7440 * Setup the hooks that configure which clocks of a given platform can be
7441 * gated and also apply various GT and display specific workarounds for these
7442 * platforms. Note that some GT specific workarounds are applied separately
7443 * when GPU contexts or batchbuffers start their execution.
7444 */
7445void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7446{
7447 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007448 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007449 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007450 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007451 else if (IS_BROXTON(dev_priv))
7452 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7453 else if (IS_BROADWELL(dev_priv))
7454 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7455 else if (IS_CHERRYVIEW(dev_priv))
7456 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7457 else if (IS_HASWELL(dev_priv))
7458 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7459 else if (IS_IVYBRIDGE(dev_priv))
7460 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7461 else if (IS_VALLEYVIEW(dev_priv))
7462 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7463 else if (IS_GEN6(dev_priv))
7464 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7465 else if (IS_GEN5(dev_priv))
7466 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7467 else if (IS_G4X(dev_priv))
7468 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7469 else if (IS_CRESTLINE(dev_priv))
7470 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7471 else if (IS_BROADWATER(dev_priv))
7472 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7473 else if (IS_GEN3(dev_priv))
7474 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7475 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7476 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7477 else if (IS_GEN2(dev_priv))
7478 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7479 else {
7480 MISSING_CASE(INTEL_DEVID(dev_priv));
7481 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7482 }
7483}
7484
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007485/* Set up chip specific power management-related functions */
7486void intel_init_pm(struct drm_device *dev)
7487{
7488 struct drm_i915_private *dev_priv = dev->dev_private;
7489
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007490 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007491
Daniel Vetterc921aba2012-04-26 23:28:17 +02007492 /* For cxsr */
7493 if (IS_PINEVIEW(dev))
7494 i915_pineview_get_mem_freq(dev);
7495 else if (IS_GEN5(dev))
7496 i915_ironlake_get_mem_freq(dev);
7497
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007498 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007499 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007500 skl_setup_wm_latency(dev);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007501 dev_priv->display.update_wm = skl_update_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007502 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05307503 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007504 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007505
Ville Syrjäläbd602542014-01-07 16:14:10 +02007506 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7507 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7508 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7509 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007510 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007511 dev_priv->display.compute_intermediate_wm =
7512 ilk_compute_intermediate_wm;
7513 dev_priv->display.initial_watermarks =
7514 ilk_initial_watermarks;
7515 dev_priv->display.optimize_watermarks =
7516 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007517 } else {
7518 DRM_DEBUG_KMS("Failed to read display plane latency. "
7519 "Disable CxSR\n");
7520 }
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007521 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007522 vlv_setup_wm_latency(dev);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007523 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007524 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007525 vlv_setup_wm_latency(dev);
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007526 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007527 } else if (IS_PINEVIEW(dev)) {
7528 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7529 dev_priv->is_ddr3,
7530 dev_priv->fsb_freq,
7531 dev_priv->mem_freq)) {
7532 DRM_INFO("failed to find known CxSR latency "
7533 "(found ddr%s fsb freq %d, mem freq %d), "
7534 "disabling CxSR\n",
7535 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7536 dev_priv->fsb_freq, dev_priv->mem_freq);
7537 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007538 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007539 dev_priv->display.update_wm = NULL;
7540 } else
7541 dev_priv->display.update_wm = pineview_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007542 } else if (IS_G4X(dev)) {
7543 dev_priv->display.update_wm = g4x_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007544 } else if (IS_GEN4(dev)) {
7545 dev_priv->display.update_wm = i965_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007546 } else if (IS_GEN3(dev)) {
7547 dev_priv->display.update_wm = i9xx_update_wm;
7548 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007549 } else if (IS_GEN2(dev)) {
7550 if (INTEL_INFO(dev)->num_pipes == 1) {
7551 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007552 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007553 } else {
7554 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007555 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007556 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007557 } else {
7558 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007559 }
7560}
7561
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007562int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007563{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007564 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007565
7566 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7567 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7568 return -EAGAIN;
7569 }
7570
7571 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00007572 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007573 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7574
7575 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7576 500)) {
7577 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7578 return -ETIMEDOUT;
7579 }
7580
7581 *val = I915_READ(GEN6_PCODE_DATA);
7582 I915_WRITE(GEN6_PCODE_DATA, 0);
7583
7584 return 0;
7585}
7586
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007587int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007588{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007589 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007590
7591 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7592 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7593 return -EAGAIN;
7594 }
7595
7596 I915_WRITE(GEN6_PCODE_DATA, val);
7597 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7598
7599 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7600 500)) {
7601 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7602 return -ETIMEDOUT;
7603 }
7604
7605 I915_WRITE(GEN6_PCODE_DATA, 0);
7606
7607 return 0;
7608}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007609
Ville Syrjälädd06f882014-11-10 22:55:12 +02007610static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7611{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007612 /*
7613 * N = val - 0xb7
7614 * Slow = Fast = GPLL ref * N
7615 */
7616 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007617}
7618
Fengguang Wub55dd642014-07-12 11:21:39 +02007619static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007620{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007621 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007622}
7623
Fengguang Wub55dd642014-07-12 11:21:39 +02007624static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307625{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007626 /*
7627 * N = val / 2
7628 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7629 */
7630 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307631}
7632
Fengguang Wub55dd642014-07-12 11:21:39 +02007633static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307634{
Ville Syrjälä1c147622014-08-18 14:42:43 +03007635 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02007636 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307637}
7638
Ville Syrjälä616bc822015-01-23 21:04:25 +02007639int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7640{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007641 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007642 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7643 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007644 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007645 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007646 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007647 return byt_gpu_freq(dev_priv, val);
7648 else
7649 return val * GT_FREQUENCY_MULTIPLIER;
7650}
7651
Ville Syrjälä616bc822015-01-23 21:04:25 +02007652int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7653{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007654 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007655 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7656 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007657 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007658 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03007659 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007660 return byt_freq_opcode(dev_priv, val);
7661 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007662 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05307663}
7664
Chris Wilson6ad790c2015-04-07 16:20:31 +01007665struct request_boost {
7666 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02007667 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007668};
7669
7670static void __intel_rps_boost_work(struct work_struct *work)
7671{
7672 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01007673 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007674
Chris Wilsone61b9952015-04-27 13:41:24 +01007675 if (!i915_gem_request_completed(req, true))
Chris Wilsonc0336662016-05-06 15:40:21 +01007676 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007677
Chris Wilson73db04c2016-04-28 09:56:55 +01007678 i915_gem_request_unreference(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007679 kfree(boost);
7680}
7681
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007682void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007683{
7684 struct request_boost *boost;
7685
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007686 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007687 return;
7688
Chris Wilsone61b9952015-04-27 13:41:24 +01007689 if (i915_gem_request_completed(req, true))
7690 return;
7691
Chris Wilson6ad790c2015-04-07 16:20:31 +01007692 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7693 if (boost == NULL)
7694 return;
7695
Daniel Vettereed29a52015-05-21 14:21:25 +02007696 i915_gem_request_reference(req);
7697 boost->req = req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007698
7699 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01007700 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007701}
7702
Daniel Vetterf742a552013-12-06 10:17:53 +01007703void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007704{
7705 struct drm_i915_private *dev_priv = dev->dev_private;
7706
Daniel Vetterf742a552013-12-06 10:17:53 +01007707 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01007708 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01007709
Chris Wilson907b28c2013-07-19 20:36:52 +01007710 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7711 intel_gen6_powersave_work);
Chris Wilson1854d5c2015-04-07 16:20:32 +01007712 INIT_LIST_HEAD(&dev_priv->rps.clients);
Chris Wilson2e1b8732015-04-27 13:41:22 +01007713 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7714 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007715
Paulo Zanoni33688d92014-03-07 20:08:19 -03007716 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02007717 atomic_set(&dev_priv->pm.wakeref_count, 0);
Imre Deak2b19efe2015-12-15 20:10:37 +02007718 atomic_set(&dev_priv->pm.atomic_seq, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007719}