blob: 20bf854eae8cf1da857c4c9a77fee36ec24d7841 [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawskydc39fff2013-10-18 12:32:07 -070034/**
Jani Nikula18afd442016-01-18 09:19:48 +020035 * DOC: RC6
36 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070037 * RC6 is a special power stage which allows the GPU to enter an very
38 * low-voltage mode when idle, using down to 0V while at this stage. This
39 * stage is entered automatically when the GPU is idle when RC6 support is
40 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
41 *
42 * There are different RC6 modes available in Intel GPU, which differentiate
43 * among each other with the latency required to enter and leave RC6 and
44 * voltage consumed by the GPU in different states.
45 *
46 * The combination of the following flags define which states GPU is allowed
47 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
48 * RC6pp is deepest RC6. Their support by hardware varies according to the
49 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
50 * which brings the most power savings; deeper states save more power, but
51 * require higher latency to switch to and wake up.
52 */
53#define INTEL_RC6_ENABLE (1<<0)
54#define INTEL_RC6p_ENABLE (1<<1)
55#define INTEL_RC6pp_ENABLE (1<<2)
56
Imre Deaka82abe42015-03-27 14:00:04 +020057static void bxt_init_clock_gating(struct drm_device *dev)
58{
Imre Deak32608ca2015-03-11 11:10:27 +020059 struct drm_i915_private *dev_priv = dev->dev_private;
60
Nick Hoatha7546152015-06-29 14:07:32 +010061 /* WaDisableSDEUnitClockGating:bxt */
62 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
63 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
64
Imre Deak32608ca2015-03-11 11:10:27 +020065 /*
66 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020067 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020068 */
Imre Deak32608ca2015-03-11 11:10:27 +020069 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020070 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7a2015-12-01 10:23:52 +020071
72 /*
73 * Wa: Backlight PWM may stop in the asserted state, causing backlight
74 * to stay fully on.
75 */
76 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
77 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
78 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +020079}
80
Daniel Vetterc921aba2012-04-26 23:28:17 +020081static void i915_pineview_get_mem_freq(struct drm_device *dev)
82{
Jani Nikula50227e12014-03-31 14:27:21 +030083 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +020084 u32 tmp;
85
86 tmp = I915_READ(CLKCFG);
87
88 switch (tmp & CLKCFG_FSB_MASK) {
89 case CLKCFG_FSB_533:
90 dev_priv->fsb_freq = 533; /* 133*4 */
91 break;
92 case CLKCFG_FSB_800:
93 dev_priv->fsb_freq = 800; /* 200*4 */
94 break;
95 case CLKCFG_FSB_667:
96 dev_priv->fsb_freq = 667; /* 167*4 */
97 break;
98 case CLKCFG_FSB_400:
99 dev_priv->fsb_freq = 400; /* 100*4 */
100 break;
101 }
102
103 switch (tmp & CLKCFG_MEM_MASK) {
104 case CLKCFG_MEM_533:
105 dev_priv->mem_freq = 533;
106 break;
107 case CLKCFG_MEM_667:
108 dev_priv->mem_freq = 667;
109 break;
110 case CLKCFG_MEM_800:
111 dev_priv->mem_freq = 800;
112 break;
113 }
114
115 /* detect pineview DDR3 setting */
116 tmp = I915_READ(CSHRDDR3CTL);
117 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
118}
119
120static void i915_ironlake_get_mem_freq(struct drm_device *dev)
121{
Jani Nikula50227e12014-03-31 14:27:21 +0300122 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200123 u16 ddrpll, csipll;
124
125 ddrpll = I915_READ16(DDRMPLL1);
126 csipll = I915_READ16(CSIPLL0);
127
128 switch (ddrpll & 0xff) {
129 case 0xc:
130 dev_priv->mem_freq = 800;
131 break;
132 case 0x10:
133 dev_priv->mem_freq = 1066;
134 break;
135 case 0x14:
136 dev_priv->mem_freq = 1333;
137 break;
138 case 0x18:
139 dev_priv->mem_freq = 1600;
140 break;
141 default:
142 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
143 ddrpll & 0xff);
144 dev_priv->mem_freq = 0;
145 break;
146 }
147
Daniel Vetter20e4d402012-08-08 23:35:39 +0200148 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200149
150 switch (csipll & 0x3ff) {
151 case 0x00c:
152 dev_priv->fsb_freq = 3200;
153 break;
154 case 0x00e:
155 dev_priv->fsb_freq = 3733;
156 break;
157 case 0x010:
158 dev_priv->fsb_freq = 4266;
159 break;
160 case 0x012:
161 dev_priv->fsb_freq = 4800;
162 break;
163 case 0x014:
164 dev_priv->fsb_freq = 5333;
165 break;
166 case 0x016:
167 dev_priv->fsb_freq = 5866;
168 break;
169 case 0x018:
170 dev_priv->fsb_freq = 6400;
171 break;
172 default:
173 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
174 csipll & 0x3ff);
175 dev_priv->fsb_freq = 0;
176 break;
177 }
178
179 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200180 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200181 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200182 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200183 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200184 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200185 }
186}
187
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300188static const struct cxsr_latency cxsr_latency_table[] = {
189 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
190 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
191 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
192 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
193 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
194
195 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
196 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
197 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
198 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
199 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
200
201 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
202 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
203 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
204 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
205 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
206
207 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
208 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
209 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
210 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
211 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
212
213 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
214 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
215 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
216 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
217 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
218
219 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
220 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
221 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
222 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
223 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
224};
225
Daniel Vetter63c62272012-04-21 23:17:55 +0200226static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300227 int is_ddr3,
228 int fsb,
229 int mem)
230{
231 const struct cxsr_latency *latency;
232 int i;
233
234 if (fsb == 0 || mem == 0)
235 return NULL;
236
237 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
238 latency = &cxsr_latency_table[i];
239 if (is_desktop == latency->is_desktop &&
240 is_ddr3 == latency->is_ddr3 &&
241 fsb == latency->fsb_freq && mem == latency->mem_freq)
242 return latency;
243 }
244
245 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
246
247 return NULL;
248}
249
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200250static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
251{
252 u32 val;
253
254 mutex_lock(&dev_priv->rps.hw_lock);
255
256 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
257 if (enable)
258 val &= ~FORCE_DDR_HIGH_FREQ;
259 else
260 val |= FORCE_DDR_HIGH_FREQ;
261 val &= ~FORCE_DDR_LOW_FREQ;
262 val |= FORCE_DDR_FREQ_REQ_ACK;
263 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
264
265 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
266 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
267 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
268
269 mutex_unlock(&dev_priv->rps.hw_lock);
270}
271
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200272static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
273{
274 u32 val;
275
276 mutex_lock(&dev_priv->rps.hw_lock);
277
278 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
279 if (enable)
280 val |= DSP_MAXFIFO_PM5_ENABLE;
281 else
282 val &= ~DSP_MAXFIFO_PM5_ENABLE;
283 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
284
285 mutex_unlock(&dev_priv->rps.hw_lock);
286}
287
Ville Syrjäläf4998962015-03-10 17:02:21 +0200288#define FW_WM(value, plane) \
289 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
290
Imre Deak5209b1f2014-07-01 12:36:17 +0300291void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300292{
Imre Deak5209b1f2014-07-01 12:36:17 +0300293 struct drm_device *dev = dev_priv->dev;
294 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300295
Wayne Boyer666a4532015-12-09 12:29:35 -0800296 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Imre Deak5209b1f2014-07-01 12:36:17 +0300297 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300298 POSTING_READ(FW_BLC_SELF_VLV);
Ville Syrjälä852eb002015-06-24 22:00:07 +0300299 dev_priv->wm.vlv.cxsr = enable;
Imre Deak5209b1f2014-07-01 12:36:17 +0300300 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
301 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300302 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300303 } else if (IS_PINEVIEW(dev)) {
304 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
305 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
306 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300307 POSTING_READ(DSPFW3);
Imre Deak5209b1f2014-07-01 12:36:17 +0300308 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
309 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
310 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
311 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300312 POSTING_READ(FW_BLC_SELF);
Imre Deak5209b1f2014-07-01 12:36:17 +0300313 } else if (IS_I915GM(dev)) {
314 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
315 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
316 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300317 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300318 } else {
319 return;
320 }
321
322 DRM_DEBUG_KMS("memory self-refresh is %s\n",
323 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300324}
325
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200326
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300327/*
328 * Latency for FIFO fetches is dependent on several factors:
329 * - memory configuration (speed, channels)
330 * - chipset
331 * - current MCH state
332 * It can be fairly high in some situations, so here we assume a fairly
333 * pessimal value. It's a tradeoff between extra memory fetches (if we
334 * set this value too high, the FIFO will fetch frequently to stay full)
335 * and power consumption (set it too low to save power and we might see
336 * FIFO underruns and display "flicker").
337 *
338 * A value of 5us seems to be a good balance; safe for very low end
339 * platforms but not overly aggressive on lower latency configs.
340 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100341static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300342
Ville Syrjäläb5004722015-03-05 21:19:47 +0200343#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
344 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
345
346static int vlv_get_fifo_size(struct drm_device *dev,
347 enum pipe pipe, int plane)
348{
349 struct drm_i915_private *dev_priv = dev->dev_private;
350 int sprite0_start, sprite1_start, size;
351
352 switch (pipe) {
353 uint32_t dsparb, dsparb2, dsparb3;
354 case PIPE_A:
355 dsparb = I915_READ(DSPARB);
356 dsparb2 = I915_READ(DSPARB2);
357 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
358 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
359 break;
360 case PIPE_B:
361 dsparb = I915_READ(DSPARB);
362 dsparb2 = I915_READ(DSPARB2);
363 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
364 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
365 break;
366 case PIPE_C:
367 dsparb2 = I915_READ(DSPARB2);
368 dsparb3 = I915_READ(DSPARB3);
369 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
370 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
371 break;
372 default:
373 return 0;
374 }
375
376 switch (plane) {
377 case 0:
378 size = sprite0_start;
379 break;
380 case 1:
381 size = sprite1_start - sprite0_start;
382 break;
383 case 2:
384 size = 512 - 1 - sprite1_start;
385 break;
386 default:
387 return 0;
388 }
389
390 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
391 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
392 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
393 size);
394
395 return size;
396}
397
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300398static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300399{
400 struct drm_i915_private *dev_priv = dev->dev_private;
401 uint32_t dsparb = I915_READ(DSPARB);
402 int size;
403
404 size = dsparb & 0x7f;
405 if (plane)
406 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
407
408 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
409 plane ? "B" : "A", size);
410
411 return size;
412}
413
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200414static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300415{
416 struct drm_i915_private *dev_priv = dev->dev_private;
417 uint32_t dsparb = I915_READ(DSPARB);
418 int size;
419
420 size = dsparb & 0x1ff;
421 if (plane)
422 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
423 size >>= 1; /* Convert to cachelines */
424
425 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
426 plane ? "B" : "A", size);
427
428 return size;
429}
430
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300431static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300432{
433 struct drm_i915_private *dev_priv = dev->dev_private;
434 uint32_t dsparb = I915_READ(DSPARB);
435 int size;
436
437 size = dsparb & 0x7f;
438 size >>= 2; /* Convert to cachelines */
439
440 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
441 plane ? "B" : "A",
442 size);
443
444 return size;
445}
446
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300447/* Pineview has different values for various configs */
448static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300449 .fifo_size = PINEVIEW_DISPLAY_FIFO,
450 .max_wm = PINEVIEW_MAX_WM,
451 .default_wm = PINEVIEW_DFT_WM,
452 .guard_size = PINEVIEW_GUARD_WM,
453 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300454};
455static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300456 .fifo_size = PINEVIEW_DISPLAY_FIFO,
457 .max_wm = PINEVIEW_MAX_WM,
458 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
459 .guard_size = PINEVIEW_GUARD_WM,
460 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300461};
462static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300463 .fifo_size = PINEVIEW_CURSOR_FIFO,
464 .max_wm = PINEVIEW_CURSOR_MAX_WM,
465 .default_wm = PINEVIEW_CURSOR_DFT_WM,
466 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
467 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300468};
469static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300470 .fifo_size = PINEVIEW_CURSOR_FIFO,
471 .max_wm = PINEVIEW_CURSOR_MAX_WM,
472 .default_wm = PINEVIEW_CURSOR_DFT_WM,
473 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
474 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300475};
476static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300477 .fifo_size = G4X_FIFO_SIZE,
478 .max_wm = G4X_MAX_WM,
479 .default_wm = G4X_MAX_WM,
480 .guard_size = 2,
481 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482};
483static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300484 .fifo_size = I965_CURSOR_FIFO,
485 .max_wm = I965_CURSOR_MAX_WM,
486 .default_wm = I965_CURSOR_DFT_WM,
487 .guard_size = 2,
488 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300489};
490static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300491 .fifo_size = VALLEYVIEW_FIFO_SIZE,
492 .max_wm = VALLEYVIEW_MAX_WM,
493 .default_wm = VALLEYVIEW_MAX_WM,
494 .guard_size = 2,
495 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300496};
497static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300498 .fifo_size = I965_CURSOR_FIFO,
499 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
500 .default_wm = I965_CURSOR_DFT_WM,
501 .guard_size = 2,
502 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300503};
504static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300505 .fifo_size = I965_CURSOR_FIFO,
506 .max_wm = I965_CURSOR_MAX_WM,
507 .default_wm = I965_CURSOR_DFT_WM,
508 .guard_size = 2,
509 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300510};
511static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300512 .fifo_size = I945_FIFO_SIZE,
513 .max_wm = I915_MAX_WM,
514 .default_wm = 1,
515 .guard_size = 2,
516 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517};
518static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300519 .fifo_size = I915_FIFO_SIZE,
520 .max_wm = I915_MAX_WM,
521 .default_wm = 1,
522 .guard_size = 2,
523 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300524};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300525static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300526 .fifo_size = I855GM_FIFO_SIZE,
527 .max_wm = I915_MAX_WM,
528 .default_wm = 1,
529 .guard_size = 2,
530 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300531};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300532static const struct intel_watermark_params i830_bc_wm_info = {
533 .fifo_size = I855GM_FIFO_SIZE,
534 .max_wm = I915_MAX_WM/2,
535 .default_wm = 1,
536 .guard_size = 2,
537 .cacheline_size = I830_FIFO_LINE_SIZE,
538};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200539static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300540 .fifo_size = I830_FIFO_SIZE,
541 .max_wm = I915_MAX_WM,
542 .default_wm = 1,
543 .guard_size = 2,
544 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300545};
546
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300547/**
548 * intel_calculate_wm - calculate watermark level
549 * @clock_in_khz: pixel clock
550 * @wm: chip FIFO params
551 * @pixel_size: display pixel size
552 * @latency_ns: memory latency for the platform
553 *
554 * Calculate the watermark level (the level at which the display plane will
555 * start fetching from memory again). Each chip has a different display
556 * FIFO size and allocation, so the caller needs to figure that out and pass
557 * in the correct intel_watermark_params structure.
558 *
559 * As the pixel clock runs, the FIFO will be drained at a rate that depends
560 * on the pixel size. When it reaches the watermark level, it'll start
561 * fetching FIFO line sized based chunks from memory until the FIFO fills
562 * past the watermark point. If the FIFO drains completely, a FIFO underrun
563 * will occur, and a display engine hang could result.
564 */
565static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
566 const struct intel_watermark_params *wm,
567 int fifo_size,
568 int pixel_size,
569 unsigned long latency_ns)
570{
571 long entries_required, wm_size;
572
573 /*
574 * Note: we need to make sure we don't overflow for various clock &
575 * latency values.
576 * clocks go from a few thousand to several hundred thousand.
577 * latency is usually a few thousand
578 */
579 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
580 1000;
581 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
582
583 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
584
585 wm_size = fifo_size - (entries_required + wm->guard_size);
586
587 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
588
589 /* Don't promote wm_size to unsigned... */
590 if (wm_size > (long)wm->max_wm)
591 wm_size = wm->max_wm;
592 if (wm_size <= 0)
593 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300594
595 /*
596 * Bspec seems to indicate that the value shouldn't be lower than
597 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
598 * Lets go for 8 which is the burst size since certain platforms
599 * already use a hardcoded 8 (which is what the spec says should be
600 * done).
601 */
602 if (wm_size <= 8)
603 wm_size = 8;
604
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605 return wm_size;
606}
607
608static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
609{
610 struct drm_crtc *crtc, *enabled = NULL;
611
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100612 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000613 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300614 if (enabled)
615 return NULL;
616 enabled = crtc;
617 }
618 }
619
620 return enabled;
621}
622
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300623static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300625 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300626 struct drm_i915_private *dev_priv = dev->dev_private;
627 struct drm_crtc *crtc;
628 const struct cxsr_latency *latency;
629 u32 reg;
630 unsigned long wm;
631
632 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
633 dev_priv->fsb_freq, dev_priv->mem_freq);
634 if (!latency) {
635 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300636 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300637 return;
638 }
639
640 crtc = single_enabled_crtc(dev);
641 if (crtc) {
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300642 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800643 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300644 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300645
646 /* Display SR */
647 wm = intel_calculate_wm(clock, &pineview_display_wm,
648 pineview_display_wm.fifo_size,
649 pixel_size, latency->display_sr);
650 reg = I915_READ(DSPFW1);
651 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200652 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300653 I915_WRITE(DSPFW1, reg);
654 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
655
656 /* cursor SR */
657 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
658 pineview_display_wm.fifo_size,
659 pixel_size, latency->cursor_sr);
660 reg = I915_READ(DSPFW3);
661 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200662 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300663 I915_WRITE(DSPFW3, reg);
664
665 /* Display HPLL off SR */
666 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
667 pineview_display_hplloff_wm.fifo_size,
668 pixel_size, latency->display_hpll_disable);
669 reg = I915_READ(DSPFW3);
670 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200671 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300672 I915_WRITE(DSPFW3, reg);
673
674 /* cursor HPLL off SR */
675 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
676 pineview_display_hplloff_wm.fifo_size,
677 pixel_size, latency->cursor_hpll_disable);
678 reg = I915_READ(DSPFW3);
679 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200680 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300681 I915_WRITE(DSPFW3, reg);
682 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
683
Imre Deak5209b1f2014-07-01 12:36:17 +0300684 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300685 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300686 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300687 }
688}
689
690static bool g4x_compute_wm0(struct drm_device *dev,
691 int plane,
692 const struct intel_watermark_params *display,
693 int display_latency_ns,
694 const struct intel_watermark_params *cursor,
695 int cursor_latency_ns,
696 int *plane_wm,
697 int *cursor_wm)
698{
699 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300700 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300701 int htotal, hdisplay, clock, pixel_size;
702 int line_time_us, line_count;
703 int entries, tlb_miss;
704
705 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000706 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300707 *cursor_wm = cursor->guard_size;
708 *plane_wm = display->guard_size;
709 return false;
710 }
711
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200712 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100713 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800714 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200715 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800716 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300717
718 /* Use the small buffer method to calculate plane watermark */
719 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
720 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
721 if (tlb_miss > 0)
722 entries += tlb_miss;
723 entries = DIV_ROUND_UP(entries, display->cacheline_size);
724 *plane_wm = entries + display->guard_size;
725 if (*plane_wm > (int)display->max_wm)
726 *plane_wm = display->max_wm;
727
728 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200729 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300730 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800731 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
733 if (tlb_miss > 0)
734 entries += tlb_miss;
735 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
736 *cursor_wm = entries + cursor->guard_size;
737 if (*cursor_wm > (int)cursor->max_wm)
738 *cursor_wm = (int)cursor->max_wm;
739
740 return true;
741}
742
743/*
744 * Check the wm result.
745 *
746 * If any calculated watermark values is larger than the maximum value that
747 * can be programmed into the associated watermark register, that watermark
748 * must be disabled.
749 */
750static bool g4x_check_srwm(struct drm_device *dev,
751 int display_wm, int cursor_wm,
752 const struct intel_watermark_params *display,
753 const struct intel_watermark_params *cursor)
754{
755 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
756 display_wm, cursor_wm);
757
758 if (display_wm > display->max_wm) {
759 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
760 display_wm, display->max_wm);
761 return false;
762 }
763
764 if (cursor_wm > cursor->max_wm) {
765 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
766 cursor_wm, cursor->max_wm);
767 return false;
768 }
769
770 if (!(display_wm || cursor_wm)) {
771 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
772 return false;
773 }
774
775 return true;
776}
777
778static bool g4x_compute_srwm(struct drm_device *dev,
779 int plane,
780 int latency_ns,
781 const struct intel_watermark_params *display,
782 const struct intel_watermark_params *cursor,
783 int *display_wm, int *cursor_wm)
784{
785 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300786 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300787 int hdisplay, htotal, pixel_size, clock;
788 unsigned long line_time_us;
789 int line_count, line_size;
790 int small, large;
791 int entries;
792
793 if (!latency_ns) {
794 *display_wm = *cursor_wm = 0;
795 return false;
796 }
797
798 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200799 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100800 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800801 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200802 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800803 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300804
Ville Syrjälä922044c2014-02-14 14:18:57 +0200805 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300806 line_count = (latency_ns / line_time_us + 1000) / 1000;
807 line_size = hdisplay * pixel_size;
808
809 /* Use the minimum of the small and large buffer method for primary */
810 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
811 large = line_count * line_size;
812
813 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
814 *display_wm = entries + display->guard_size;
815
816 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800817 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300818 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
819 *cursor_wm = entries + cursor->guard_size;
820
821 return g4x_check_srwm(dev,
822 *display_wm, *cursor_wm,
823 display, cursor);
824}
825
Ville Syrjälä15665972015-03-10 16:16:28 +0200826#define FW_WM_VLV(value, plane) \
827 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
828
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200829static void vlv_write_wm_values(struct intel_crtc *crtc,
830 const struct vlv_wm_values *wm)
831{
832 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
833 enum pipe pipe = crtc->pipe;
834
835 I915_WRITE(VLV_DDL(pipe),
836 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
837 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
838 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
839 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
840
Ville Syrjäläae801522015-03-05 21:19:49 +0200841 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200842 FW_WM(wm->sr.plane, SR) |
843 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
844 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
845 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200846 I915_WRITE(DSPFW2,
Ville Syrjälä15665972015-03-10 16:16:28 +0200847 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
848 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
849 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200850 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200851 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200852
853 if (IS_CHERRYVIEW(dev_priv)) {
854 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200855 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
856 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200857 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200858 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
859 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200860 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä15665972015-03-10 16:16:28 +0200861 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
862 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200863 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200864 FW_WM(wm->sr.plane >> 9, SR_HI) |
865 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
866 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
867 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
868 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
869 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
870 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
871 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
872 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
873 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200874 } else {
875 I915_WRITE(DSPFW7,
Ville Syrjälä15665972015-03-10 16:16:28 +0200876 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
877 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200878 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200879 FW_WM(wm->sr.plane >> 9, SR_HI) |
880 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
881 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
882 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
883 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
884 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
885 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200886 }
887
Ville Syrjälä2cb389b2015-06-24 22:00:10 +0300888 /* zero (unused) WM1 watermarks */
889 I915_WRITE(DSPFW4, 0);
890 I915_WRITE(DSPFW5, 0);
891 I915_WRITE(DSPFW6, 0);
892 I915_WRITE(DSPHOWM1, 0);
893
Ville Syrjäläae801522015-03-05 21:19:49 +0200894 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200895}
896
Ville Syrjälä15665972015-03-10 16:16:28 +0200897#undef FW_WM_VLV
898
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300899enum vlv_wm_level {
900 VLV_WM_LEVEL_PM2,
901 VLV_WM_LEVEL_PM5,
902 VLV_WM_LEVEL_DDR_DVFS,
Ville Syrjälä6eb1a682015-06-24 22:00:03 +0300903};
904
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300905/* latency must be in 0.1us units. */
906static unsigned int vlv_wm_method2(unsigned int pixel_rate,
907 unsigned int pipe_htotal,
908 unsigned int horiz_pixels,
909 unsigned int bytes_per_pixel,
910 unsigned int latency)
911{
912 unsigned int ret;
913
914 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
915 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
916 ret = DIV_ROUND_UP(ret, 64);
917
918 return ret;
919}
920
921static void vlv_setup_wm_latency(struct drm_device *dev)
922{
923 struct drm_i915_private *dev_priv = dev->dev_private;
924
925 /* all latencies in usec */
926 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
927
Ville Syrjälä58590c12015-09-08 21:05:12 +0300928 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
929
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300930 if (IS_CHERRYVIEW(dev_priv)) {
931 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
932 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300933
934 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300935 }
936}
937
938static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
939 struct intel_crtc *crtc,
940 const struct intel_plane_state *state,
941 int level)
942{
943 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
944 int clock, htotal, pixel_size, width, wm;
945
946 if (dev_priv->wm.pri_latency[level] == 0)
947 return USHRT_MAX;
948
949 if (!state->visible)
950 return 0;
951
952 pixel_size = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
953 clock = crtc->config->base.adjusted_mode.crtc_clock;
954 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
955 width = crtc->config->pipe_src_w;
956 if (WARN_ON(htotal == 0))
957 htotal = 1;
958
959 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
960 /*
961 * FIXME the formula gives values that are
962 * too big for the cursor FIFO, and hence we
963 * would never be able to use cursors. For
964 * now just hardcode the watermark.
965 */
966 wm = 63;
967 } else {
968 wm = vlv_wm_method2(clock, htotal, width, pixel_size,
969 dev_priv->wm.pri_latency[level] * 10);
970 }
971
972 return min_t(int, wm, USHRT_MAX);
973}
974
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +0300975static void vlv_compute_fifo(struct intel_crtc *crtc)
976{
977 struct drm_device *dev = crtc->base.dev;
978 struct vlv_wm_state *wm_state = &crtc->wm_state;
979 struct intel_plane *plane;
980 unsigned int total_rate = 0;
981 const int fifo_size = 512 - 1;
982 int fifo_extra, fifo_left = fifo_size;
983
984 for_each_intel_plane_on_crtc(dev, crtc, plane) {
985 struct intel_plane_state *state =
986 to_intel_plane_state(plane->base.state);
987
988 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
989 continue;
990
991 if (state->visible) {
992 wm_state->num_active_planes++;
993 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
994 }
995 }
996
997 for_each_intel_plane_on_crtc(dev, crtc, plane) {
998 struct intel_plane_state *state =
999 to_intel_plane_state(plane->base.state);
1000 unsigned int rate;
1001
1002 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1003 plane->wm.fifo_size = 63;
1004 continue;
1005 }
1006
1007 if (!state->visible) {
1008 plane->wm.fifo_size = 0;
1009 continue;
1010 }
1011
1012 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1013 plane->wm.fifo_size = fifo_size * rate / total_rate;
1014 fifo_left -= plane->wm.fifo_size;
1015 }
1016
1017 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1018
1019 /* spread the remainder evenly */
1020 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1021 int plane_extra;
1022
1023 if (fifo_left == 0)
1024 break;
1025
1026 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1027 continue;
1028
1029 /* give it all to the first plane if none are active */
1030 if (plane->wm.fifo_size == 0 &&
1031 wm_state->num_active_planes)
1032 continue;
1033
1034 plane_extra = min(fifo_extra, fifo_left);
1035 plane->wm.fifo_size += plane_extra;
1036 fifo_left -= plane_extra;
1037 }
1038
1039 WARN_ON(fifo_left != 0);
1040}
1041
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001042static void vlv_invert_wms(struct intel_crtc *crtc)
1043{
1044 struct vlv_wm_state *wm_state = &crtc->wm_state;
1045 int level;
1046
1047 for (level = 0; level < wm_state->num_levels; level++) {
1048 struct drm_device *dev = crtc->base.dev;
1049 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1050 struct intel_plane *plane;
1051
1052 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1053 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1054
1055 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1056 switch (plane->base.type) {
1057 int sprite;
1058 case DRM_PLANE_TYPE_CURSOR:
1059 wm_state->wm[level].cursor = plane->wm.fifo_size -
1060 wm_state->wm[level].cursor;
1061 break;
1062 case DRM_PLANE_TYPE_PRIMARY:
1063 wm_state->wm[level].primary = plane->wm.fifo_size -
1064 wm_state->wm[level].primary;
1065 break;
1066 case DRM_PLANE_TYPE_OVERLAY:
1067 sprite = plane->plane;
1068 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1069 wm_state->wm[level].sprite[sprite];
1070 break;
1071 }
1072 }
1073 }
1074}
1075
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001076static void vlv_compute_wm(struct intel_crtc *crtc)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001077{
1078 struct drm_device *dev = crtc->base.dev;
1079 struct vlv_wm_state *wm_state = &crtc->wm_state;
1080 struct intel_plane *plane;
1081 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1082 int level;
1083
1084 memset(wm_state, 0, sizeof(*wm_state));
1085
Ville Syrjälä852eb002015-06-24 22:00:07 +03001086 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001087 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001088
1089 wm_state->num_active_planes = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001090
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001091 vlv_compute_fifo(crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001092
1093 if (wm_state->num_active_planes != 1)
1094 wm_state->cxsr = false;
1095
1096 if (wm_state->cxsr) {
1097 for (level = 0; level < wm_state->num_levels; level++) {
1098 wm_state->sr[level].plane = sr_fifo_size;
1099 wm_state->sr[level].cursor = 63;
1100 }
1101 }
1102
1103 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1104 struct intel_plane_state *state =
1105 to_intel_plane_state(plane->base.state);
1106
1107 if (!state->visible)
1108 continue;
1109
1110 /* normal watermarks */
1111 for (level = 0; level < wm_state->num_levels; level++) {
1112 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1113 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1114
1115 /* hack */
1116 if (WARN_ON(level == 0 && wm > max_wm))
1117 wm = max_wm;
1118
1119 if (wm > plane->wm.fifo_size)
1120 break;
1121
1122 switch (plane->base.type) {
1123 int sprite;
1124 case DRM_PLANE_TYPE_CURSOR:
1125 wm_state->wm[level].cursor = wm;
1126 break;
1127 case DRM_PLANE_TYPE_PRIMARY:
1128 wm_state->wm[level].primary = wm;
1129 break;
1130 case DRM_PLANE_TYPE_OVERLAY:
1131 sprite = plane->plane;
1132 wm_state->wm[level].sprite[sprite] = wm;
1133 break;
1134 }
1135 }
1136
1137 wm_state->num_levels = level;
1138
1139 if (!wm_state->cxsr)
1140 continue;
1141
1142 /* maxfifo watermarks */
1143 switch (plane->base.type) {
1144 int sprite, level;
1145 case DRM_PLANE_TYPE_CURSOR:
1146 for (level = 0; level < wm_state->num_levels; level++)
1147 wm_state->sr[level].cursor =
Thomas Daniel5a37ed02015-10-23 14:55:38 +01001148 wm_state->wm[level].cursor;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001149 break;
1150 case DRM_PLANE_TYPE_PRIMARY:
1151 for (level = 0; level < wm_state->num_levels; level++)
1152 wm_state->sr[level].plane =
1153 min(wm_state->sr[level].plane,
1154 wm_state->wm[level].primary);
1155 break;
1156 case DRM_PLANE_TYPE_OVERLAY:
1157 sprite = plane->plane;
1158 for (level = 0; level < wm_state->num_levels; level++)
1159 wm_state->sr[level].plane =
1160 min(wm_state->sr[level].plane,
1161 wm_state->wm[level].sprite[sprite]);
1162 break;
1163 }
1164 }
1165
1166 /* clear any (partially) filled invalid levels */
Ville Syrjälä58590c12015-09-08 21:05:12 +03001167 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001168 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1169 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1170 }
1171
1172 vlv_invert_wms(crtc);
1173}
1174
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001175#define VLV_FIFO(plane, value) \
1176 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1177
1178static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1179{
1180 struct drm_device *dev = crtc->base.dev;
1181 struct drm_i915_private *dev_priv = to_i915(dev);
1182 struct intel_plane *plane;
1183 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1184
1185 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1186 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1187 WARN_ON(plane->wm.fifo_size != 63);
1188 continue;
1189 }
1190
1191 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1192 sprite0_start = plane->wm.fifo_size;
1193 else if (plane->plane == 0)
1194 sprite1_start = sprite0_start + plane->wm.fifo_size;
1195 else
1196 fifo_size = sprite1_start + plane->wm.fifo_size;
1197 }
1198
1199 WARN_ON(fifo_size != 512 - 1);
1200
1201 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1202 pipe_name(crtc->pipe), sprite0_start,
1203 sprite1_start, fifo_size);
1204
1205 switch (crtc->pipe) {
1206 uint32_t dsparb, dsparb2, dsparb3;
1207 case PIPE_A:
1208 dsparb = I915_READ(DSPARB);
1209 dsparb2 = I915_READ(DSPARB2);
1210
1211 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1212 VLV_FIFO(SPRITEB, 0xff));
1213 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1214 VLV_FIFO(SPRITEB, sprite1_start));
1215
1216 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1217 VLV_FIFO(SPRITEB_HI, 0x1));
1218 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1219 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1220
1221 I915_WRITE(DSPARB, dsparb);
1222 I915_WRITE(DSPARB2, dsparb2);
1223 break;
1224 case PIPE_B:
1225 dsparb = I915_READ(DSPARB);
1226 dsparb2 = I915_READ(DSPARB2);
1227
1228 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1229 VLV_FIFO(SPRITED, 0xff));
1230 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1231 VLV_FIFO(SPRITED, sprite1_start));
1232
1233 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1234 VLV_FIFO(SPRITED_HI, 0xff));
1235 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1236 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1237
1238 I915_WRITE(DSPARB, dsparb);
1239 I915_WRITE(DSPARB2, dsparb2);
1240 break;
1241 case PIPE_C:
1242 dsparb3 = I915_READ(DSPARB3);
1243 dsparb2 = I915_READ(DSPARB2);
1244
1245 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1246 VLV_FIFO(SPRITEF, 0xff));
1247 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1248 VLV_FIFO(SPRITEF, sprite1_start));
1249
1250 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1251 VLV_FIFO(SPRITEF_HI, 0xff));
1252 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1253 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1254
1255 I915_WRITE(DSPARB3, dsparb3);
1256 I915_WRITE(DSPARB2, dsparb2);
1257 break;
1258 default:
1259 break;
1260 }
1261}
1262
1263#undef VLV_FIFO
1264
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001265static void vlv_merge_wm(struct drm_device *dev,
1266 struct vlv_wm_values *wm)
1267{
1268 struct intel_crtc *crtc;
1269 int num_active_crtcs = 0;
1270
Ville Syrjälä58590c12015-09-08 21:05:12 +03001271 wm->level = to_i915(dev)->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001272 wm->cxsr = true;
1273
1274 for_each_intel_crtc(dev, crtc) {
1275 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1276
1277 if (!crtc->active)
1278 continue;
1279
1280 if (!wm_state->cxsr)
1281 wm->cxsr = false;
1282
1283 num_active_crtcs++;
1284 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1285 }
1286
1287 if (num_active_crtcs != 1)
1288 wm->cxsr = false;
1289
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001290 if (num_active_crtcs > 1)
1291 wm->level = VLV_WM_LEVEL_PM2;
1292
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001293 for_each_intel_crtc(dev, crtc) {
1294 struct vlv_wm_state *wm_state = &crtc->wm_state;
1295 enum pipe pipe = crtc->pipe;
1296
1297 if (!crtc->active)
1298 continue;
1299
1300 wm->pipe[pipe] = wm_state->wm[wm->level];
1301 if (wm->cxsr)
1302 wm->sr = wm_state->sr[wm->level];
1303
1304 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1305 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1306 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1307 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1308 }
1309}
1310
1311static void vlv_update_wm(struct drm_crtc *crtc)
1312{
1313 struct drm_device *dev = crtc->dev;
1314 struct drm_i915_private *dev_priv = dev->dev_private;
1315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1316 enum pipe pipe = intel_crtc->pipe;
1317 struct vlv_wm_values wm = {};
1318
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03001319 vlv_compute_wm(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001320 vlv_merge_wm(dev, &wm);
1321
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001322 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1323 /* FIXME should be part of crtc atomic commit */
1324 vlv_pipe_set_fifo_size(intel_crtc);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001325 return;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001326 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001327
1328 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1329 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1330 chv_set_memory_dvfs(dev_priv, false);
1331
1332 if (wm.level < VLV_WM_LEVEL_PM5 &&
1333 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1334 chv_set_memory_pm5(dev_priv, false);
1335
Ville Syrjälä852eb002015-06-24 22:00:07 +03001336 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001337 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001338
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001339 /* FIXME should be part of crtc atomic commit */
1340 vlv_pipe_set_fifo_size(intel_crtc);
1341
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001342 vlv_write_wm_values(intel_crtc, &wm);
1343
1344 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1345 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1346 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1347 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1348 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1349
Ville Syrjälä852eb002015-06-24 22:00:07 +03001350 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001351 intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001352
1353 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1354 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1355 chv_set_memory_pm5(dev_priv, true);
1356
1357 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1358 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1359 chv_set_memory_dvfs(dev_priv, true);
1360
1361 dev_priv->wm.vlv = wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001362}
1363
Ville Syrjäläae801522015-03-05 21:19:49 +02001364#define single_plane_enabled(mask) is_power_of_2(mask)
1365
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001366static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001367{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001368 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001369 static const int sr_latency_ns = 12000;
1370 struct drm_i915_private *dev_priv = dev->dev_private;
1371 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1372 int plane_sr, cursor_sr;
1373 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001374 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001375
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001376 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001377 &g4x_wm_info, pessimal_latency_ns,
1378 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001379 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001380 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001381
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001382 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001383 &g4x_wm_info, pessimal_latency_ns,
1384 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001385 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001386 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001387
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001388 if (single_plane_enabled(enabled) &&
1389 g4x_compute_srwm(dev, ffs(enabled) - 1,
1390 sr_latency_ns,
1391 &g4x_wm_info,
1392 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001393 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001394 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001395 } else {
Imre Deak98584252014-06-13 14:54:20 +03001396 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001397 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001398 plane_sr = cursor_sr = 0;
1399 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001400
Ville Syrjäläa5043452014-06-28 02:04:18 +03001401 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1402 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001403 planea_wm, cursora_wm,
1404 planeb_wm, cursorb_wm,
1405 plane_sr, cursor_sr);
1406
1407 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001408 FW_WM(plane_sr, SR) |
1409 FW_WM(cursorb_wm, CURSORB) |
1410 FW_WM(planeb_wm, PLANEB) |
1411 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001412 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001413 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001414 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001415 /* HPLL off in SR has some issues on G4x... disable it */
1416 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001417 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001418 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001419
1420 if (cxsr_enabled)
1421 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001422}
1423
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001424static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001425{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001426 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001427 struct drm_i915_private *dev_priv = dev->dev_private;
1428 struct drm_crtc *crtc;
1429 int srwm = 1;
1430 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001431 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001432
1433 /* Calc sr entries for one plane configs */
1434 crtc = single_enabled_crtc(dev);
1435 if (crtc) {
1436 /* self-refresh has much higher latency */
1437 static const int sr_latency_ns = 12000;
Ville Syrjälä124abe02015-09-08 13:40:45 +03001438 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001439 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001440 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001441 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001442 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001443 unsigned long line_time_us;
1444 int entries;
1445
Ville Syrjälä922044c2014-02-14 14:18:57 +02001446 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001447
1448 /* Use ns/us then divide to preserve precision */
1449 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1450 pixel_size * hdisplay;
1451 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1452 srwm = I965_FIFO_SIZE - entries;
1453 if (srwm < 0)
1454 srwm = 1;
1455 srwm &= 0x1ff;
1456 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1457 entries, srwm);
1458
1459 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001460 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001461 entries = DIV_ROUND_UP(entries,
1462 i965_cursor_wm_info.cacheline_size);
1463 cursor_sr = i965_cursor_wm_info.fifo_size -
1464 (entries + i965_cursor_wm_info.guard_size);
1465
1466 if (cursor_sr > i965_cursor_wm_info.max_wm)
1467 cursor_sr = i965_cursor_wm_info.max_wm;
1468
1469 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1470 "cursor %d\n", srwm, cursor_sr);
1471
Imre Deak98584252014-06-13 14:54:20 +03001472 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001473 } else {
Imre Deak98584252014-06-13 14:54:20 +03001474 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001475 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001476 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001477 }
1478
1479 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1480 srwm);
1481
1482 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001483 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1484 FW_WM(8, CURSORB) |
1485 FW_WM(8, PLANEB) |
1486 FW_WM(8, PLANEA));
1487 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1488 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001489 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001490 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001491
1492 if (cxsr_enabled)
1493 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001494}
1495
Ville Syrjäläf4998962015-03-10 17:02:21 +02001496#undef FW_WM
1497
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001498static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001499{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001500 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001501 struct drm_i915_private *dev_priv = dev->dev_private;
1502 const struct intel_watermark_params *wm_info;
1503 uint32_t fwater_lo;
1504 uint32_t fwater_hi;
1505 int cwm, srwm = 1;
1506 int fifo_size;
1507 int planea_wm, planeb_wm;
1508 struct drm_crtc *crtc, *enabled = NULL;
1509
1510 if (IS_I945GM(dev))
1511 wm_info = &i945_wm_info;
1512 else if (!IS_GEN2(dev))
1513 wm_info = &i915_wm_info;
1514 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001515 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001516
1517 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1518 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001519 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001520 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001521 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001522 if (IS_GEN2(dev))
1523 cpp = 4;
1524
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001525 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001526 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001527 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001528 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001529 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001530 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001531 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001532 if (planea_wm > (long)wm_info->max_wm)
1533 planea_wm = wm_info->max_wm;
1534 }
1535
1536 if (IS_GEN2(dev))
1537 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001538
1539 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1540 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001541 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001542 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001543 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001544 if (IS_GEN2(dev))
1545 cpp = 4;
1546
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001547 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001548 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001549 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001550 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001551 if (enabled == NULL)
1552 enabled = crtc;
1553 else
1554 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001555 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001556 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001557 if (planeb_wm > (long)wm_info->max_wm)
1558 planeb_wm = wm_info->max_wm;
1559 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001560
1561 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1562
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001563 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001564 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001565
Matt Roper59bea882015-02-27 10:12:01 -08001566 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001567
1568 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001569 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001570 enabled = NULL;
1571 }
1572
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001573 /*
1574 * Overlay gets an aggressive default since video jitter is bad.
1575 */
1576 cwm = 2;
1577
1578 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001579 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001580
1581 /* Calc sr entries for one plane configs */
1582 if (HAS_FW_BLC(dev) && enabled) {
1583 /* self-refresh has much higher latency */
1584 static const int sr_latency_ns = 6000;
Ville Syrjälä124abe02015-09-08 13:40:45 +03001585 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001586 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001587 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001588 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001589 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001590 unsigned long line_time_us;
1591 int entries;
1592
Ville Syrjälä922044c2014-02-14 14:18:57 +02001593 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001594
1595 /* Use ns/us then divide to preserve precision */
1596 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1597 pixel_size * hdisplay;
1598 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1599 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1600 srwm = wm_info->fifo_size - entries;
1601 if (srwm < 0)
1602 srwm = 1;
1603
1604 if (IS_I945G(dev) || IS_I945GM(dev))
1605 I915_WRITE(FW_BLC_SELF,
1606 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1607 else if (IS_I915GM(dev))
1608 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1609 }
1610
1611 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1612 planea_wm, planeb_wm, cwm, srwm);
1613
1614 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1615 fwater_hi = (cwm & 0x1f);
1616
1617 /* Set request length to 8 cachelines per fetch */
1618 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1619 fwater_hi = fwater_hi | (1 << 8);
1620
1621 I915_WRITE(FW_BLC, fwater_lo);
1622 I915_WRITE(FW_BLC2, fwater_hi);
1623
Imre Deak5209b1f2014-07-01 12:36:17 +03001624 if (enabled)
1625 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001626}
1627
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001628static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001629{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001630 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001631 struct drm_i915_private *dev_priv = dev->dev_private;
1632 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001633 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001634 uint32_t fwater_lo;
1635 int planea_wm;
1636
1637 crtc = single_enabled_crtc(dev);
1638 if (crtc == NULL)
1639 return;
1640
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001641 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001642 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001643 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001644 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001645 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001646 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1647 fwater_lo |= (3<<8) | planea_wm;
1648
1649 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1650
1651 I915_WRITE(FW_BLC, fwater_lo);
1652}
1653
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001654uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001655{
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001656 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001657
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001658 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001659
1660 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1661 * adjust the pixel_rate here. */
1662
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001663 if (pipe_config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001664 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001665 uint32_t pfit_size = pipe_config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001666
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03001667 pipe_w = pipe_config->pipe_src_w;
1668 pipe_h = pipe_config->pipe_src_h;
1669
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001670 pfit_w = (pfit_size >> 16) & 0xFFFF;
1671 pfit_h = pfit_size & 0xFFFF;
1672 if (pipe_w < pfit_w)
1673 pipe_w = pfit_w;
1674 if (pipe_h < pfit_h)
1675 pipe_h = pfit_h;
1676
Matt Roper15126882015-12-03 11:37:40 -08001677 if (WARN_ON(!pfit_w || !pfit_h))
1678 return pixel_rate;
1679
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001680 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1681 pfit_w * pfit_h);
1682 }
1683
1684 return pixel_rate;
1685}
1686
Ville Syrjälä37126462013-08-01 16:18:55 +03001687/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001688static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001689 uint32_t latency)
1690{
1691 uint64_t ret;
1692
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001693 if (WARN(latency == 0, "Latency value missing\n"))
1694 return UINT_MAX;
1695
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001696 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1697 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1698
1699 return ret;
1700}
1701
Ville Syrjälä37126462013-08-01 16:18:55 +03001702/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001703static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001704 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1705 uint32_t latency)
1706{
1707 uint32_t ret;
1708
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001709 if (WARN(latency == 0, "Latency value missing\n"))
1710 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001711 if (WARN_ON(!pipe_htotal))
1712 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001713
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001714 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1715 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1716 ret = DIV_ROUND_UP(ret, 64) + 2;
1717 return ret;
1718}
1719
Ville Syrjälä23297042013-07-05 11:57:17 +03001720static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001721 uint8_t bytes_per_pixel)
1722{
Matt Roper15126882015-12-03 11:37:40 -08001723 /*
1724 * Neither of these should be possible since this function shouldn't be
1725 * called if the CRTC is off or the plane is invisible. But let's be
1726 * extra paranoid to avoid a potential divide-by-zero if we screw up
1727 * elsewhere in the driver.
1728 */
1729 if (WARN_ON(!bytes_per_pixel))
1730 return 0;
1731 if (WARN_ON(!horiz_pixels))
1732 return 0;
1733
Paulo Zanonicca32e92013-05-31 11:45:06 -03001734 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1735}
1736
Imre Deak820c1982013-12-17 14:46:36 +02001737struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001738 uint16_t pri;
1739 uint16_t spr;
1740 uint16_t cur;
1741 uint16_t fbc;
1742};
1743
Ville Syrjälä37126462013-08-01 16:18:55 +03001744/*
1745 * For both WM_PIPE and WM_LP.
1746 * mem_value must be in 0.1us units.
1747 */
Matt Roper7221fc32015-09-24 15:53:08 -07001748static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001749 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001750 uint32_t mem_value,
1751 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001752{
Matt Roper43d59ed2015-09-24 15:53:07 -07001753 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001754 uint32_t method1, method2;
1755
Matt Roper7221fc32015-09-24 15:53:08 -07001756 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001757 return 0;
1758
Matt Roper7221fc32015-09-24 15:53:08 -07001759 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001760
1761 if (!is_lp)
1762 return method1;
1763
Matt Roper7221fc32015-09-24 15:53:08 -07001764 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1765 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001766 drm_rect_width(&pstate->dst),
1767 bpp,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001768 mem_value);
1769
1770 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001771}
1772
Ville Syrjälä37126462013-08-01 16:18:55 +03001773/*
1774 * For both WM_PIPE and WM_LP.
1775 * mem_value must be in 0.1us units.
1776 */
Matt Roper7221fc32015-09-24 15:53:08 -07001777static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001778 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001779 uint32_t mem_value)
1780{
Matt Roper43d59ed2015-09-24 15:53:07 -07001781 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001782 uint32_t method1, method2;
1783
Matt Roper7221fc32015-09-24 15:53:08 -07001784 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001785 return 0;
1786
Matt Roper7221fc32015-09-24 15:53:08 -07001787 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), bpp, mem_value);
1788 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1789 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001790 drm_rect_width(&pstate->dst),
1791 bpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001792 mem_value);
1793 return min(method1, method2);
1794}
1795
Ville Syrjälä37126462013-08-01 16:18:55 +03001796/*
1797 * For both WM_PIPE and WM_LP.
1798 * mem_value must be in 0.1us units.
1799 */
Matt Roper7221fc32015-09-24 15:53:08 -07001800static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001801 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001802 uint32_t mem_value)
1803{
Matt Roper43d59ed2015-09-24 15:53:07 -07001804 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1805
Matt Roper7221fc32015-09-24 15:53:08 -07001806 if (!cstate->base.active || !pstate->visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001807 return 0;
1808
Matt Roper7221fc32015-09-24 15:53:08 -07001809 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1810 cstate->base.adjusted_mode.crtc_htotal,
Matt Roper43d59ed2015-09-24 15:53:07 -07001811 drm_rect_width(&pstate->dst),
1812 bpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001813 mem_value);
1814}
1815
Paulo Zanonicca32e92013-05-31 11:45:06 -03001816/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07001817static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001818 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001819 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001820{
Matt Roper43d59ed2015-09-24 15:53:07 -07001821 int bpp = pstate->base.fb ? pstate->base.fb->bits_per_pixel / 8 : 0;
1822
Matt Roper7221fc32015-09-24 15:53:08 -07001823 if (!cstate->base.active || !pstate->visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001824 return 0;
1825
Matt Roper43d59ed2015-09-24 15:53:07 -07001826 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), bpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001827}
1828
Ville Syrjälä158ae642013-08-07 13:28:19 +03001829static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1830{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001831 if (INTEL_INFO(dev)->gen >= 8)
1832 return 3072;
1833 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001834 return 768;
1835 else
1836 return 512;
1837}
1838
Ville Syrjälä4e975082014-03-07 18:32:11 +02001839static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1840 int level, bool is_sprite)
1841{
1842 if (INTEL_INFO(dev)->gen >= 8)
1843 /* BDW primary/sprite plane watermarks */
1844 return level == 0 ? 255 : 2047;
1845 else if (INTEL_INFO(dev)->gen >= 7)
1846 /* IVB/HSW primary/sprite plane watermarks */
1847 return level == 0 ? 127 : 1023;
1848 else if (!is_sprite)
1849 /* ILK/SNB primary plane watermarks */
1850 return level == 0 ? 127 : 511;
1851 else
1852 /* ILK/SNB sprite plane watermarks */
1853 return level == 0 ? 63 : 255;
1854}
1855
1856static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1857 int level)
1858{
1859 if (INTEL_INFO(dev)->gen >= 7)
1860 return level == 0 ? 63 : 255;
1861 else
1862 return level == 0 ? 31 : 63;
1863}
1864
1865static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1866{
1867 if (INTEL_INFO(dev)->gen >= 8)
1868 return 31;
1869 else
1870 return 15;
1871}
1872
Ville Syrjälä158ae642013-08-07 13:28:19 +03001873/* Calculate the maximum primary/sprite plane watermark */
1874static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1875 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001876 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001877 enum intel_ddb_partitioning ddb_partitioning,
1878 bool is_sprite)
1879{
1880 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001881
1882 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001883 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001884 return 0;
1885
1886 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001887 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001888 fifo_size /= INTEL_INFO(dev)->num_pipes;
1889
1890 /*
1891 * For some reason the non self refresh
1892 * FIFO size is only half of the self
1893 * refresh FIFO size on ILK/SNB.
1894 */
1895 if (INTEL_INFO(dev)->gen <= 6)
1896 fifo_size /= 2;
1897 }
1898
Ville Syrjälä240264f2013-08-07 13:29:12 +03001899 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001900 /* level 0 is always calculated with 1:1 split */
1901 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1902 if (is_sprite)
1903 fifo_size *= 5;
1904 fifo_size /= 6;
1905 } else {
1906 fifo_size /= 2;
1907 }
1908 }
1909
1910 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001911 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001912}
1913
1914/* Calculate the maximum cursor plane watermark */
1915static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001916 int level,
1917 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001918{
1919 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001920 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001921 return 64;
1922
1923 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001924 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001925}
1926
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001927static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001928 int level,
1929 const struct intel_wm_config *config,
1930 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001931 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001932{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001933 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1934 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1935 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001936 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001937}
1938
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001939static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1940 int level,
1941 struct ilk_wm_maximums *max)
1942{
1943 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1944 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1945 max->cur = ilk_cursor_wm_reg_max(dev, level);
1946 max->fbc = ilk_fbc_wm_reg_max(dev);
1947}
1948
Ville Syrjäläd9395652013-10-09 19:18:10 +03001949static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001950 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001951 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001952{
1953 bool ret;
1954
1955 /* already determined to be invalid? */
1956 if (!result->enable)
1957 return false;
1958
1959 result->enable = result->pri_val <= max->pri &&
1960 result->spr_val <= max->spr &&
1961 result->cur_val <= max->cur;
1962
1963 ret = result->enable;
1964
1965 /*
1966 * HACK until we can pre-compute everything,
1967 * and thus fail gracefully if LP0 watermarks
1968 * are exceeded...
1969 */
1970 if (level == 0 && !result->enable) {
1971 if (result->pri_val > max->pri)
1972 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1973 level, result->pri_val, max->pri);
1974 if (result->spr_val > max->spr)
1975 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1976 level, result->spr_val, max->spr);
1977 if (result->cur_val > max->cur)
1978 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1979 level, result->cur_val, max->cur);
1980
1981 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1982 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1983 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1984 result->enable = true;
1985 }
1986
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001987 return ret;
1988}
1989
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001990static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07001991 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001992 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07001993 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07001994 struct intel_plane_state *pristate,
1995 struct intel_plane_state *sprstate,
1996 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001997 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001998{
1999 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2000 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2001 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2002
2003 /* WM1+ latency values stored in 0.5us units */
2004 if (level > 0) {
2005 pri_latency *= 5;
2006 spr_latency *= 5;
2007 cur_latency *= 5;
2008 }
2009
Matt Roper86c8bbb2015-09-24 15:53:16 -07002010 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2011 pri_latency, level);
2012 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2013 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2014 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002015 result->enable = true;
2016}
2017
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002018static uint32_t
Matt Roperee91a152015-12-03 11:37:39 -08002019hsw_compute_linetime_wm(struct drm_device *dev,
2020 struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002021{
2022 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperee91a152015-12-03 11:37:39 -08002023 const struct drm_display_mode *adjusted_mode =
2024 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002025 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002026
Matt Roperee91a152015-12-03 11:37:39 -08002027 if (!cstate->base.active)
2028 return 0;
2029 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2030 return 0;
2031 if (WARN_ON(dev_priv->cdclk_freq == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002032 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002033
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002034 /* The WM are computed with base on how long it takes to fill a single
2035 * row at the given clock rate, multiplied by 8.
2036 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002037 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2038 adjusted_mode->crtc_clock);
2039 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjälä05024da2015-06-03 15:45:08 +03002040 dev_priv->cdclk_freq);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002041
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002042 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2043 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002044}
2045
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002046static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002047{
2048 struct drm_i915_private *dev_priv = dev->dev_private;
2049
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002050 if (IS_GEN9(dev)) {
2051 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002052 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00002053 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002054
2055 /* read the first set of memory latencies[0:3] */
2056 val = 0; /* data0 to be programmed to 0 for first set */
2057 mutex_lock(&dev_priv->rps.hw_lock);
2058 ret = sandybridge_pcode_read(dev_priv,
2059 GEN9_PCODE_READ_MEM_LATENCY,
2060 &val);
2061 mutex_unlock(&dev_priv->rps.hw_lock);
2062
2063 if (ret) {
2064 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2065 return;
2066 }
2067
2068 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2069 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2070 GEN9_MEM_LATENCY_LEVEL_MASK;
2071 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2072 GEN9_MEM_LATENCY_LEVEL_MASK;
2073 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2074 GEN9_MEM_LATENCY_LEVEL_MASK;
2075
2076 /* read the second set of memory latencies[4:7] */
2077 val = 1; /* data0 to be programmed to 1 for second set */
2078 mutex_lock(&dev_priv->rps.hw_lock);
2079 ret = sandybridge_pcode_read(dev_priv,
2080 GEN9_PCODE_READ_MEM_LATENCY,
2081 &val);
2082 mutex_unlock(&dev_priv->rps.hw_lock);
2083 if (ret) {
2084 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2085 return;
2086 }
2087
2088 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2089 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2090 GEN9_MEM_LATENCY_LEVEL_MASK;
2091 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2092 GEN9_MEM_LATENCY_LEVEL_MASK;
2093 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2094 GEN9_MEM_LATENCY_LEVEL_MASK;
2095
Vandana Kannan367294b2014-11-04 17:06:46 +00002096 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00002097 * WaWmMemoryReadLatency:skl
2098 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002099 * punit doesn't take into account the read latency so we need
2100 * to add 2us to the various latency levels we retrieve from
2101 * the punit.
2102 * - W0 is a bit special in that it's the only level that
2103 * can't be disabled if we want to have display working, so
2104 * we always add 2us there.
2105 * - For levels >=1, punit returns 0us latency when they are
2106 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00002107 *
2108 * Additionally, if a level n (n > 1) has a 0us latency, all
2109 * levels m (m >= n) need to be disabled. We make sure to
2110 * sanitize the values out of the punit to satisfy this
2111 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00002112 */
2113 wm[0] += 2;
2114 for (level = 1; level <= max_level; level++)
2115 if (wm[level] != 0)
2116 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002117 else {
2118 for (i = level + 1; i <= max_level; i++)
2119 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00002120
Vandana Kannan4f947382014-11-04 17:06:47 +00002121 break;
2122 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002123 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002124 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2125
2126 wm[0] = (sskpd >> 56) & 0xFF;
2127 if (wm[0] == 0)
2128 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002129 wm[1] = (sskpd >> 4) & 0xFF;
2130 wm[2] = (sskpd >> 12) & 0xFF;
2131 wm[3] = (sskpd >> 20) & 0x1FF;
2132 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002133 } else if (INTEL_INFO(dev)->gen >= 6) {
2134 uint32_t sskpd = I915_READ(MCH_SSKPD);
2135
2136 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2137 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2138 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2139 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002140 } else if (INTEL_INFO(dev)->gen >= 5) {
2141 uint32_t mltr = I915_READ(MLTR_ILK);
2142
2143 /* ILK primary LP0 latency is 700 ns */
2144 wm[0] = 7;
2145 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2146 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002147 }
2148}
2149
Ville Syrjälä53615a52013-08-01 16:18:50 +03002150static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2151{
2152 /* ILK sprite LP0 latency is 1300 ns */
2153 if (INTEL_INFO(dev)->gen == 5)
2154 wm[0] = 13;
2155}
2156
2157static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2158{
2159 /* ILK cursor LP0 latency is 1300 ns */
2160 if (INTEL_INFO(dev)->gen == 5)
2161 wm[0] = 13;
2162
2163 /* WaDoubleCursorLP3Latency:ivb */
2164 if (IS_IVYBRIDGE(dev))
2165 wm[3] *= 2;
2166}
2167
Damien Lespiau546c81f2014-05-13 15:30:26 +01002168int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002169{
2170 /* how many WM levels are we expecting */
Damien Lespiaub6e742f2015-05-09 02:05:55 +01002171 if (INTEL_INFO(dev)->gen >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002172 return 7;
2173 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002174 return 4;
2175 else if (INTEL_INFO(dev)->gen >= 6)
2176 return 3;
2177 else
2178 return 2;
2179}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002180
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002181static void intel_print_wm_latency(struct drm_device *dev,
2182 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002183 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002184{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002185 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002186
2187 for (level = 0; level <= max_level; level++) {
2188 unsigned int latency = wm[level];
2189
2190 if (latency == 0) {
2191 DRM_ERROR("%s WM%d latency not provided\n",
2192 name, level);
2193 continue;
2194 }
2195
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002196 /*
2197 * - latencies are in us on gen9.
2198 * - before then, WM1+ latency values are in 0.5us units
2199 */
2200 if (IS_GEN9(dev))
2201 latency *= 10;
2202 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002203 latency *= 5;
2204
2205 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2206 name, level, wm[level],
2207 latency / 10, latency % 10);
2208 }
2209}
2210
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002211static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2212 uint16_t wm[5], uint16_t min)
2213{
2214 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2215
2216 if (wm[0] >= min)
2217 return false;
2218
2219 wm[0] = max(wm[0], min);
2220 for (level = 1; level <= max_level; level++)
2221 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2222
2223 return true;
2224}
2225
2226static void snb_wm_latency_quirk(struct drm_device *dev)
2227{
2228 struct drm_i915_private *dev_priv = dev->dev_private;
2229 bool changed;
2230
2231 /*
2232 * The BIOS provided WM memory latency values are often
2233 * inadequate for high resolution displays. Adjust them.
2234 */
2235 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2236 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2237 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2238
2239 if (!changed)
2240 return;
2241
2242 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2243 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2244 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2245 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2246}
2247
Damien Lespiaufa50ad62014-03-17 18:01:16 +00002248static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002249{
2250 struct drm_i915_private *dev_priv = dev->dev_private;
2251
2252 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2253
2254 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2255 sizeof(dev_priv->wm.pri_latency));
2256 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2257 sizeof(dev_priv->wm.pri_latency));
2258
2259 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2260 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002261
2262 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2263 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2264 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002265
2266 if (IS_GEN6(dev))
2267 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002268}
2269
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002270static void skl_setup_wm_latency(struct drm_device *dev)
2271{
2272 struct drm_i915_private *dev_priv = dev->dev_private;
2273
2274 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2275 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2276}
2277
Matt Roper261a27d2015-10-08 15:28:25 -07002278/* Compute new watermarks for the pipe */
Matt Roper86c8bbb2015-09-24 15:53:16 -07002279static int ilk_compute_pipe_wm(struct intel_crtc *intel_crtc,
2280 struct drm_atomic_state *state)
Matt Roper261a27d2015-10-08 15:28:25 -07002281{
Matt Roper86c8bbb2015-09-24 15:53:16 -07002282 struct intel_pipe_wm *pipe_wm;
2283 struct drm_device *dev = intel_crtc->base.dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002284 const struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002285 struct intel_crtc_state *cstate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002286 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002287 struct drm_plane_state *ps;
2288 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002289 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002290 struct intel_plane_state *curstate = NULL;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002291 int level, max_level = ilk_wm_max_level(dev);
Matt Roperbf220452016-01-19 11:43:04 -08002292 /* LP0 watermark maximums depend on this pipe alone */
2293 struct intel_wm_config config = {
2294 .num_pipes_active = 1,
2295 };
Imre Deak820c1982013-12-17 14:46:36 +02002296 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002297
Matt Roper86c8bbb2015-09-24 15:53:16 -07002298 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
2299 if (IS_ERR(cstate))
2300 return PTR_ERR(cstate);
2301
2302 pipe_wm = &cstate->wm.optimal.ilk;
Ville Syrjäläf1ecaf82016-01-14 14:53:34 +02002303 memset(pipe_wm, 0, sizeof(*pipe_wm));
Matt Roper86c8bbb2015-09-24 15:53:16 -07002304
Matt Roper43d59ed2015-09-24 15:53:07 -07002305 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07002306 ps = drm_atomic_get_plane_state(state,
2307 &intel_plane->base);
2308 if (IS_ERR(ps))
2309 return PTR_ERR(ps);
2310
2311 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
2312 pristate = to_intel_plane_state(ps);
2313 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
2314 sprstate = to_intel_plane_state(ps);
2315 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
2316 curstate = to_intel_plane_state(ps);
Matt Roper43d59ed2015-09-24 15:53:07 -07002317 }
2318
Matt Roperbf220452016-01-19 11:43:04 -08002319 config.sprites_enabled = sprstate->visible;
2320 config.sprites_scaled = sprstate->visible &&
Matt Roper43d59ed2015-09-24 15:53:07 -07002321 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2322 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2323
Matt Roperbf220452016-01-19 11:43:04 -08002324 pipe_wm->pipe_enabled = cstate->base.active;
2325 pipe_wm->sprites_enabled = config.sprites_enabled;
2326 pipe_wm->sprites_scaled = config.sprites_scaled;
2327
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002328 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Matt Roper43d59ed2015-09-24 15:53:07 -07002329 if (INTEL_INFO(dev)->gen <= 6 && sprstate->visible)
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002330 max_level = 1;
2331
2332 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Roperbf220452016-01-19 11:43:04 -08002333 if (config.sprites_scaled)
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002334 max_level = 0;
2335
Matt Roper86c8bbb2015-09-24 15:53:16 -07002336 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
2337 pristate, sprstate, curstate, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002338
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002339 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Matt Roperee91a152015-12-03 11:37:39 -08002340 pipe_wm->linetime = hsw_compute_linetime_wm(dev, cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002341
Matt Roperbf220452016-01-19 11:43:04 -08002342 /* LP0 watermarks always use 1/2 DDB partitioning */
2343 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2344
2345 /* At least LP0 must be valid */
2346 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2347 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002348
2349 ilk_compute_wm_reg_maximums(dev, 1, &max);
2350
2351 for (level = 1; level <= max_level; level++) {
2352 struct intel_wm_level wm = {};
2353
Matt Roper86c8bbb2015-09-24 15:53:16 -07002354 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
2355 pristate, sprstate, curstate, &wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002356
2357 /*
2358 * Disable any watermark level that exceeds the
2359 * register maximums since such watermarks are
2360 * always invalid.
2361 */
2362 if (!ilk_validate_wm_level(level, &max, &wm))
2363 break;
2364
2365 pipe_wm->wm[level] = wm;
2366 }
2367
Matt Roper86c8bbb2015-09-24 15:53:16 -07002368 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002369}
2370
2371/*
2372 * Merge the watermarks from all active pipes for a specific level.
2373 */
2374static void ilk_merge_wm_level(struct drm_device *dev,
2375 int level,
2376 struct intel_wm_level *ret_wm)
2377{
2378 const struct intel_crtc *intel_crtc;
2379
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002380 ret_wm->enable = true;
2381
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002382 for_each_intel_crtc(dev, intel_crtc) {
Matt Roperbf220452016-01-19 11:43:04 -08002383 const struct intel_crtc_state *cstate =
2384 to_intel_crtc_state(intel_crtc->base.state);
2385 const struct intel_pipe_wm *active = &cstate->wm.optimal.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002386 const struct intel_wm_level *wm = &active->wm[level];
2387
2388 if (!active->pipe_enabled)
2389 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002390
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002391 /*
2392 * The watermark values may have been used in the past,
2393 * so we must maintain them in the registers for some
2394 * time even if the level is now disabled.
2395 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002396 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002397 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002398
2399 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2400 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2401 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2402 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2403 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002404}
2405
2406/*
2407 * Merge all low power watermarks for all active pipes.
2408 */
2409static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002410 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002411 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002412 struct intel_pipe_wm *merged)
2413{
Paulo Zanoni7733b492015-07-07 15:26:04 -03002414 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002415 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002416 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002417
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002418 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2419 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2420 config->num_pipes_active > 1)
2421 return;
2422
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002423 /* ILK: FBC WM must be disabled always */
2424 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002425
2426 /* merge each WM1+ level */
2427 for (level = 1; level <= max_level; level++) {
2428 struct intel_wm_level *wm = &merged->wm[level];
2429
2430 ilk_merge_wm_level(dev, level, wm);
2431
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002432 if (level > last_enabled_level)
2433 wm->enable = false;
2434 else if (!ilk_validate_wm_level(level, max, wm))
2435 /* make sure all following levels get disabled */
2436 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002437
2438 /*
2439 * The spec says it is preferred to disable
2440 * FBC WMs instead of disabling a WM level.
2441 */
2442 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002443 if (wm->enable)
2444 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002445 wm->fbc_val = 0;
2446 }
2447 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002448
2449 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2450 /*
2451 * FIXME this is racy. FBC might get enabled later.
2452 * What we should check here is whether FBC can be
2453 * enabled sometime later.
2454 */
Paulo Zanoni7733b492015-07-07 15:26:04 -03002455 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002456 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002457 for (level = 2; level <= max_level; level++) {
2458 struct intel_wm_level *wm = &merged->wm[level];
2459
2460 wm->enable = false;
2461 }
2462 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002463}
2464
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002465static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2466{
2467 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2468 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2469}
2470
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002471/* The value we need to program into the WM_LPx latency field */
2472static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2473{
2474 struct drm_i915_private *dev_priv = dev->dev_private;
2475
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002476 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002477 return 2 * level;
2478 else
2479 return dev_priv->wm.pri_latency[level];
2480}
2481
Imre Deak820c1982013-12-17 14:46:36 +02002482static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002483 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002484 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002485 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002486{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002487 struct intel_crtc *intel_crtc;
2488 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002489
Ville Syrjälä0362c782013-10-09 19:17:57 +03002490 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002491 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002492
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002493 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002494 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002495 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002496
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002497 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002498
Ville Syrjälä0362c782013-10-09 19:17:57 +03002499 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002500
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002501 /*
2502 * Maintain the watermark values even if the level is
2503 * disabled. Doing otherwise could cause underruns.
2504 */
2505 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002506 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002507 (r->pri_val << WM1_LP_SR_SHIFT) |
2508 r->cur_val;
2509
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002510 if (r->enable)
2511 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2512
Ville Syrjälä416f4722013-11-02 21:07:46 -07002513 if (INTEL_INFO(dev)->gen >= 8)
2514 results->wm_lp[wm_lp - 1] |=
2515 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2516 else
2517 results->wm_lp[wm_lp - 1] |=
2518 r->fbc_val << WM1_LP_FBC_SHIFT;
2519
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002520 /*
2521 * Always set WM1S_LP_EN when spr_val != 0, even if the
2522 * level is disabled. Doing otherwise could cause underruns.
2523 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002524 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2525 WARN_ON(wm_lp != 1);
2526 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2527 } else
2528 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002529 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002530
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002531 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002532 for_each_intel_crtc(dev, intel_crtc) {
Matt Roperbf220452016-01-19 11:43:04 -08002533 const struct intel_crtc_state *cstate =
2534 to_intel_crtc_state(intel_crtc->base.state);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002535 enum pipe pipe = intel_crtc->pipe;
Matt Roperbf220452016-01-19 11:43:04 -08002536 const struct intel_wm_level *r = &cstate->wm.optimal.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002537
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002538 if (WARN_ON(!r->enable))
2539 continue;
2540
Matt Roperbf220452016-01-19 11:43:04 -08002541 results->wm_linetime[pipe] = cstate->wm.optimal.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002542
2543 results->wm_pipe[pipe] =
2544 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2545 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2546 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002547 }
2548}
2549
Paulo Zanoni861f3382013-05-31 10:19:21 -03002550/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2551 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002552static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002553 struct intel_pipe_wm *r1,
2554 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002555{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002556 int level, max_level = ilk_wm_max_level(dev);
2557 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002558
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002559 for (level = 1; level <= max_level; level++) {
2560 if (r1->wm[level].enable)
2561 level1 = level;
2562 if (r2->wm[level].enable)
2563 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002564 }
2565
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002566 if (level1 == level2) {
2567 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002568 return r2;
2569 else
2570 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002571 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002572 return r1;
2573 } else {
2574 return r2;
2575 }
2576}
2577
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002578/* dirty bits used to track which watermarks need changes */
2579#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2580#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2581#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2582#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2583#define WM_DIRTY_FBC (1 << 24)
2584#define WM_DIRTY_DDB (1 << 25)
2585
Damien Lespiau055e3932014-08-18 13:49:10 +01002586static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002587 const struct ilk_wm_values *old,
2588 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002589{
2590 unsigned int dirty = 0;
2591 enum pipe pipe;
2592 int wm_lp;
2593
Damien Lespiau055e3932014-08-18 13:49:10 +01002594 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002595 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2596 dirty |= WM_DIRTY_LINETIME(pipe);
2597 /* Must disable LP1+ watermarks too */
2598 dirty |= WM_DIRTY_LP_ALL;
2599 }
2600
2601 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2602 dirty |= WM_DIRTY_PIPE(pipe);
2603 /* Must disable LP1+ watermarks too */
2604 dirty |= WM_DIRTY_LP_ALL;
2605 }
2606 }
2607
2608 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2609 dirty |= WM_DIRTY_FBC;
2610 /* Must disable LP1+ watermarks too */
2611 dirty |= WM_DIRTY_LP_ALL;
2612 }
2613
2614 if (old->partitioning != new->partitioning) {
2615 dirty |= WM_DIRTY_DDB;
2616 /* Must disable LP1+ watermarks too */
2617 dirty |= WM_DIRTY_LP_ALL;
2618 }
2619
2620 /* LP1+ watermarks already deemed dirty, no need to continue */
2621 if (dirty & WM_DIRTY_LP_ALL)
2622 return dirty;
2623
2624 /* Find the lowest numbered LP1+ watermark in need of an update... */
2625 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2626 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2627 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2628 break;
2629 }
2630
2631 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2632 for (; wm_lp <= 3; wm_lp++)
2633 dirty |= WM_DIRTY_LP(wm_lp);
2634
2635 return dirty;
2636}
2637
Ville Syrjälä8553c182013-12-05 15:51:39 +02002638static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2639 unsigned int dirty)
2640{
Imre Deak820c1982013-12-17 14:46:36 +02002641 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002642 bool changed = false;
2643
2644 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2645 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2646 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2647 changed = true;
2648 }
2649 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2650 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2651 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2652 changed = true;
2653 }
2654 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2655 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2656 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2657 changed = true;
2658 }
2659
2660 /*
2661 * Don't touch WM1S_LP_EN here.
2662 * Doing so could cause underruns.
2663 */
2664
2665 return changed;
2666}
2667
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002668/*
2669 * The spec says we shouldn't write when we don't need, because every write
2670 * causes WMs to be re-evaluated, expending some power.
2671 */
Imre Deak820c1982013-12-17 14:46:36 +02002672static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2673 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002674{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002675 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002676 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002677 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002678 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002679
Damien Lespiau055e3932014-08-18 13:49:10 +01002680 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002681 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002682 return;
2683
Ville Syrjälä8553c182013-12-05 15:51:39 +02002684 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002685
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002686 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002687 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002688 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002689 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002690 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002691 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2692
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002693 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002694 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002695 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002696 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002697 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002698 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2699
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002700 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002701 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002702 val = I915_READ(WM_MISC);
2703 if (results->partitioning == INTEL_DDB_PART_1_2)
2704 val &= ~WM_MISC_DATA_PARTITION_5_6;
2705 else
2706 val |= WM_MISC_DATA_PARTITION_5_6;
2707 I915_WRITE(WM_MISC, val);
2708 } else {
2709 val = I915_READ(DISP_ARB_CTL2);
2710 if (results->partitioning == INTEL_DDB_PART_1_2)
2711 val &= ~DISP_DATA_PARTITION_5_6;
2712 else
2713 val |= DISP_DATA_PARTITION_5_6;
2714 I915_WRITE(DISP_ARB_CTL2, val);
2715 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002716 }
2717
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002718 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002719 val = I915_READ(DISP_ARB_CTL);
2720 if (results->enable_fbc_wm)
2721 val &= ~DISP_FBC_WM_DIS;
2722 else
2723 val |= DISP_FBC_WM_DIS;
2724 I915_WRITE(DISP_ARB_CTL, val);
2725 }
2726
Imre Deak954911e2013-12-17 14:46:34 +02002727 if (dirty & WM_DIRTY_LP(1) &&
2728 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2729 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2730
2731 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002732 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2733 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2734 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2735 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2736 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002737
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002738 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002739 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002740 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002741 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002742 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002743 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002744
2745 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002746}
2747
Matt Roperbf220452016-01-19 11:43:04 -08002748static bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02002749{
2750 struct drm_i915_private *dev_priv = dev->dev_private;
2751
2752 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2753}
2754
Damien Lespiaub9cec072014-11-04 17:06:43 +00002755/*
2756 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2757 * different active planes.
2758 */
2759
2760#define SKL_DDB_SIZE 896 /* in blocks */
Damien Lespiau43d735a2015-03-17 11:39:34 +02002761#define BXT_DDB_SIZE 512
Damien Lespiaub9cec072014-11-04 17:06:43 +00002762
Matt Roper024c9042015-09-24 15:53:11 -07002763/*
2764 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2765 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2766 * other universal planes are in indices 1..n. Note that this may leave unused
2767 * indices between the top "sprite" plane and the cursor.
2768 */
2769static int
2770skl_wm_plane_id(const struct intel_plane *plane)
2771{
2772 switch (plane->base.type) {
2773 case DRM_PLANE_TYPE_PRIMARY:
2774 return 0;
2775 case DRM_PLANE_TYPE_CURSOR:
2776 return PLANE_CURSOR;
2777 case DRM_PLANE_TYPE_OVERLAY:
2778 return plane->plane + 1;
2779 default:
2780 MISSING_CASE(plane->base.type);
2781 return plane->plane;
2782 }
2783}
2784
Damien Lespiaub9cec072014-11-04 17:06:43 +00002785static void
2786skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07002787 const struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00002788 const struct intel_wm_config *config,
Damien Lespiaub9cec072014-11-04 17:06:43 +00002789 struct skl_ddb_entry *alloc /* out */)
2790{
Matt Roper024c9042015-09-24 15:53:11 -07002791 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002792 struct drm_crtc *crtc;
2793 unsigned int pipe_size, ddb_size;
2794 int nth_active_pipe;
2795
Matt Roper024c9042015-09-24 15:53:11 -07002796 if (!cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00002797 alloc->start = 0;
2798 alloc->end = 0;
2799 return;
2800 }
2801
Damien Lespiau43d735a2015-03-17 11:39:34 +02002802 if (IS_BROXTON(dev))
2803 ddb_size = BXT_DDB_SIZE;
2804 else
2805 ddb_size = SKL_DDB_SIZE;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002806
2807 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2808
2809 nth_active_pipe = 0;
2810 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002811 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002812 continue;
2813
2814 if (crtc == for_crtc)
2815 break;
2816
2817 nth_active_pipe++;
2818 }
2819
2820 pipe_size = ddb_size / config->num_pipes_active;
2821 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002822 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002823}
2824
2825static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2826{
2827 if (config->num_pipes_active == 1)
2828 return 32;
2829
2830 return 8;
2831}
2832
Damien Lespiaua269c582014-11-04 17:06:49 +00002833static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2834{
2835 entry->start = reg & 0x3ff;
2836 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002837 if (entry->end)
2838 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002839}
2840
Damien Lespiau08db6652014-11-04 17:06:52 +00002841void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2842 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002843{
Damien Lespiaua269c582014-11-04 17:06:49 +00002844 enum pipe pipe;
2845 int plane;
2846 u32 val;
2847
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02002848 memset(ddb, 0, sizeof(*ddb));
2849
Damien Lespiaua269c582014-11-04 17:06:49 +00002850 for_each_pipe(dev_priv, pipe) {
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02002851 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PIPE(pipe)))
2852 continue;
2853
Damien Lespiaudd740782015-02-28 14:54:08 +00002854 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002855 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2856 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2857 val);
2858 }
2859
2860 val = I915_READ(CUR_BUF_CFG(pipe));
Matt Roper4969d332015-09-24 15:53:10 -07002861 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2862 val);
Damien Lespiaua269c582014-11-04 17:06:49 +00002863 }
2864}
2865
Damien Lespiaub9cec072014-11-04 17:06:43 +00002866static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07002867skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
2868 const struct drm_plane_state *pstate,
2869 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002870{
Matt Roper024c9042015-09-24 15:53:11 -07002871 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2872 struct drm_framebuffer *fb = pstate->fb;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002873
2874 /* for planar format */
Matt Roper024c9042015-09-24 15:53:11 -07002875 if (fb->pixel_format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002876 if (y) /* y-plane data rate */
Matt Roper024c9042015-09-24 15:53:11 -07002877 return intel_crtc->config->pipe_src_w *
2878 intel_crtc->config->pipe_src_h *
2879 drm_format_plane_cpp(fb->pixel_format, 0);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002880 else /* uv-plane data rate */
Matt Roper024c9042015-09-24 15:53:11 -07002881 return (intel_crtc->config->pipe_src_w/2) *
2882 (intel_crtc->config->pipe_src_h/2) *
2883 drm_format_plane_cpp(fb->pixel_format, 1);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002884 }
2885
2886 /* for packed formats */
Matt Roper024c9042015-09-24 15:53:11 -07002887 return intel_crtc->config->pipe_src_w *
2888 intel_crtc->config->pipe_src_h *
2889 drm_format_plane_cpp(fb->pixel_format, 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002890}
2891
2892/*
2893 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2894 * a 8192x4096@32bpp framebuffer:
2895 * 3 * 4096 * 8192 * 4 < 2^32
2896 */
2897static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07002898skl_get_total_relative_data_rate(const struct intel_crtc_state *cstate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002899{
Matt Roper024c9042015-09-24 15:53:11 -07002900 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2901 struct drm_device *dev = intel_crtc->base.dev;
2902 const struct intel_plane *intel_plane;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002903 unsigned int total_data_rate = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002904
Matt Roper024c9042015-09-24 15:53:11 -07002905 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2906 const struct drm_plane_state *pstate = intel_plane->base.state;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002907
Matt Roper024c9042015-09-24 15:53:11 -07002908 if (pstate->fb == NULL)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002909 continue;
2910
Matt Roper024c9042015-09-24 15:53:11 -07002911 if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
2912 continue;
2913
2914 /* packed/uv */
2915 total_data_rate += skl_plane_relative_data_rate(cstate,
2916 pstate,
2917 0);
2918
2919 if (pstate->fb->pixel_format == DRM_FORMAT_NV12)
2920 /* y-plane */
2921 total_data_rate += skl_plane_relative_data_rate(cstate,
2922 pstate,
2923 1);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002924 }
2925
2926 return total_data_rate;
2927}
2928
2929static void
Matt Roper024c9042015-09-24 15:53:11 -07002930skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00002931 struct skl_ddb_allocation *ddb /* out */)
2932{
Matt Roper024c9042015-09-24 15:53:11 -07002933 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002934 struct drm_device *dev = crtc->dev;
Matt Roperaa363132015-09-24 15:53:18 -07002935 struct drm_i915_private *dev_priv = to_i915(dev);
2936 struct intel_wm_config *config = &dev_priv->wm.config;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07002938 struct intel_plane *intel_plane;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002939 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002940 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002941 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002942 uint16_t minimum[I915_MAX_PLANES];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002943 uint16_t y_minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002944 unsigned int total_data_rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002945
Matt Roper024c9042015-09-24 15:53:11 -07002946 skl_ddb_get_pipe_allocation_limits(dev, cstate, config, alloc);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002947 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002948 if (alloc_size == 0) {
2949 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roper4969d332015-09-24 15:53:10 -07002950 memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
2951 sizeof(ddb->plane[pipe][PLANE_CURSOR]));
Damien Lespiaub9cec072014-11-04 17:06:43 +00002952 return;
2953 }
2954
2955 cursor_blocks = skl_cursor_allocation(config);
Matt Roper4969d332015-09-24 15:53:10 -07002956 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
2957 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002958
2959 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002960 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002961
Damien Lespiau80958152015-02-09 13:35:10 +00002962 /* 1. Allocate the mininum required blocks for each active plane */
Matt Roper024c9042015-09-24 15:53:11 -07002963 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2964 struct drm_plane *plane = &intel_plane->base;
2965 struct drm_framebuffer *fb = plane->state->fb;
2966 int id = skl_wm_plane_id(intel_plane);
Damien Lespiau80958152015-02-09 13:35:10 +00002967
Matt Roper024c9042015-09-24 15:53:11 -07002968 if (fb == NULL)
2969 continue;
2970 if (plane->type == DRM_PLANE_TYPE_CURSOR)
Damien Lespiau80958152015-02-09 13:35:10 +00002971 continue;
2972
Matt Roper024c9042015-09-24 15:53:11 -07002973 minimum[id] = 8;
2974 alloc_size -= minimum[id];
2975 y_minimum[id] = (fb->pixel_format == DRM_FORMAT_NV12) ? 8 : 0;
2976 alloc_size -= y_minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00002977 }
2978
Damien Lespiaub9cec072014-11-04 17:06:43 +00002979 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002980 * 2. Distribute the remaining space in proportion to the amount of
2981 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002982 *
2983 * FIXME: we may not allocate every single block here.
2984 */
Matt Roper024c9042015-09-24 15:53:11 -07002985 total_data_rate = skl_get_total_relative_data_rate(cstate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002986
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002987 start = alloc->start;
Matt Roper024c9042015-09-24 15:53:11 -07002988 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2989 struct drm_plane *plane = &intel_plane->base;
2990 struct drm_plane_state *pstate = intel_plane->base.state;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07002991 unsigned int data_rate, y_data_rate;
2992 uint16_t plane_blocks, y_plane_blocks = 0;
Matt Roper024c9042015-09-24 15:53:11 -07002993 int id = skl_wm_plane_id(intel_plane);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002994
Matt Roper024c9042015-09-24 15:53:11 -07002995 if (pstate->fb == NULL)
2996 continue;
2997 if (plane->type == DRM_PLANE_TYPE_CURSOR)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002998 continue;
2999
Matt Roper024c9042015-09-24 15:53:11 -07003000 data_rate = skl_plane_relative_data_rate(cstate, pstate, 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003001
3002 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003003 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003004 * promote the expression to 64 bits to avoid overflowing, the
3005 * result is < available as data_rate / total_data_rate < 1
3006 */
Matt Roper024c9042015-09-24 15:53:11 -07003007 plane_blocks = minimum[id];
Damien Lespiau80958152015-02-09 13:35:10 +00003008 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3009 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003010
Matt Roper024c9042015-09-24 15:53:11 -07003011 ddb->plane[pipe][id].start = start;
3012 ddb->plane[pipe][id].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003013
3014 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003015
3016 /*
3017 * allocation for y_plane part of planar format:
3018 */
Matt Roper024c9042015-09-24 15:53:11 -07003019 if (pstate->fb->pixel_format == DRM_FORMAT_NV12) {
3020 y_data_rate = skl_plane_relative_data_rate(cstate,
3021 pstate,
3022 1);
3023 y_plane_blocks = y_minimum[id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003024 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3025 total_data_rate);
3026
Matt Roper024c9042015-09-24 15:53:11 -07003027 ddb->y_plane[pipe][id].start = start;
3028 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003029
3030 start += y_plane_blocks;
3031 }
3032
Damien Lespiaub9cec072014-11-04 17:06:43 +00003033 }
3034
3035}
3036
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02003037static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003038{
3039 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02003040 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003041}
3042
3043/*
3044 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3045 * for the read latency) and bytes_per_pixel should always be <= 8, so that
3046 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3047 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3048*/
3049static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
3050 uint32_t latency)
3051{
3052 uint32_t wm_intermediate_val, ret;
3053
3054 if (latency == 0)
3055 return UINT_MAX;
3056
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003057 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003058 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3059
3060 return ret;
3061}
3062
3063static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3064 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003065 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003066{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003067 uint32_t ret;
3068 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3069 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003070
3071 if (latency == 0)
3072 return UINT_MAX;
3073
3074 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003075
3076 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3077 tiling == I915_FORMAT_MOD_Yf_TILED) {
3078 plane_bytes_per_line *= 4;
3079 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3080 plane_blocks_per_line /= 4;
3081 } else {
3082 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3083 }
3084
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003085 wm_intermediate_val = latency * pixel_rate;
3086 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003087 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003088
3089 return ret;
3090}
3091
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003092static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3093 const struct intel_crtc *intel_crtc)
3094{
3095 struct drm_device *dev = intel_crtc->base.dev;
3096 struct drm_i915_private *dev_priv = dev->dev_private;
3097 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003098
Kumar, Maheshe6d90022015-10-23 09:41:34 -07003099 /*
3100 * If ddb allocation of pipes changed, it may require recalculation of
3101 * watermarks
3102 */
3103 if (memcmp(new_ddb->pipe, cur_ddb->pipe, sizeof(new_ddb->pipe)))
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003104 return true;
3105
3106 return false;
3107}
3108
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003109static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
Matt Roper024c9042015-09-24 15:53:11 -07003110 struct intel_crtc_state *cstate,
3111 struct intel_plane *intel_plane,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003112 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003113 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00003114 uint16_t *out_blocks, /* out */
3115 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003116{
Matt Roper024c9042015-09-24 15:53:11 -07003117 struct drm_plane *plane = &intel_plane->base;
3118 struct drm_framebuffer *fb = plane->state->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003119 uint32_t latency = dev_priv->wm.skl_latency[level];
3120 uint32_t method1, method2;
3121 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3122 uint32_t res_blocks, res_lines;
3123 uint32_t selected_result;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003124 uint8_t bytes_per_pixel;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003125
Matt Roper024c9042015-09-24 15:53:11 -07003126 if (latency == 0 || !cstate->base.active || !fb)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003127 return false;
3128
Matt Roper024c9042015-09-24 15:53:11 -07003129 bytes_per_pixel = drm_format_plane_cpp(fb->pixel_format, 0);
3130 method1 = skl_wm_method1(skl_pipe_pixel_rate(cstate),
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003131 bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003132 latency);
Matt Roper024c9042015-09-24 15:53:11 -07003133 method2 = skl_wm_method2(skl_pipe_pixel_rate(cstate),
3134 cstate->base.adjusted_mode.crtc_htotal,
3135 cstate->pipe_src_w,
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003136 bytes_per_pixel,
Matt Roper024c9042015-09-24 15:53:11 -07003137 fb->modifier[0],
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003138 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003139
Matt Roper024c9042015-09-24 15:53:11 -07003140 plane_bytes_per_line = cstate->pipe_src_w * bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003141 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003142
Matt Roper024c9042015-09-24 15:53:11 -07003143 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3144 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003145 uint32_t min_scanlines = 4;
3146 uint32_t y_tile_minimum;
Matt Roper024c9042015-09-24 15:53:11 -07003147 if (intel_rotation_90_or_270(plane->state->rotation)) {
3148 int bpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
3149 drm_format_plane_cpp(fb->pixel_format, 1) :
3150 drm_format_plane_cpp(fb->pixel_format, 0);
3151
3152 switch (bpp) {
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003153 case 1:
3154 min_scanlines = 16;
3155 break;
3156 case 2:
3157 min_scanlines = 8;
3158 break;
3159 case 8:
3160 WARN(1, "Unsupported pixel depth for rotation");
kbuild test robot2f0b5792015-03-26 22:30:21 +08003161 }
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +00003162 }
3163 y_tile_minimum = plane_blocks_per_line * min_scanlines;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003164 selected_result = max(method2, y_tile_minimum);
3165 } else {
3166 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3167 selected_result = min(method1, method2);
3168 else
3169 selected_result = method1;
3170 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003171
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003172 res_blocks = selected_result + 1;
3173 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003174
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003175 if (level >= 1 && level <= 7) {
Matt Roper024c9042015-09-24 15:53:11 -07003176 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3177 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003178 res_lines += 4;
3179 else
3180 res_blocks++;
3181 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003182
3183 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00003184 return false;
3185
3186 *out_blocks = res_blocks;
3187 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003188
3189 return true;
3190}
3191
3192static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3193 struct skl_ddb_allocation *ddb,
Matt Roper024c9042015-09-24 15:53:11 -07003194 struct intel_crtc_state *cstate,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003195 int level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003196 struct skl_wm_level *result)
3197{
Matt Roper024c9042015-09-24 15:53:11 -07003198 struct drm_device *dev = dev_priv->dev;
3199 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
3200 struct intel_plane *intel_plane;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003201 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003202 enum pipe pipe = intel_crtc->pipe;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003203
Matt Roper024c9042015-09-24 15:53:11 -07003204 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3205 int i = skl_wm_plane_id(intel_plane);
3206
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003207 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3208
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003209 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
Matt Roper024c9042015-09-24 15:53:11 -07003210 cstate,
3211 intel_plane,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003212 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003213 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003214 &result->plane_res_b[i],
3215 &result->plane_res_l[i]);
3216 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003217}
3218
Damien Lespiau407b50f2014-11-04 17:06:57 +00003219static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003220skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003221{
Matt Roper024c9042015-09-24 15:53:11 -07003222 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003223 return 0;
3224
Matt Roper024c9042015-09-24 15:53:11 -07003225 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003226 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003227
Matt Roper024c9042015-09-24 15:53:11 -07003228 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3229 skl_pipe_pixel_rate(cstate));
Damien Lespiau407b50f2014-11-04 17:06:57 +00003230}
3231
Matt Roper024c9042015-09-24 15:53:11 -07003232static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003233 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003234{
Matt Roper024c9042015-09-24 15:53:11 -07003235 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiau9414f562014-11-04 17:06:58 +00003236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003237 struct intel_plane *intel_plane;
Damien Lespiau9414f562014-11-04 17:06:58 +00003238
Matt Roper024c9042015-09-24 15:53:11 -07003239 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003240 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003241
3242 /* Until we know more, just disable transition WMs */
Matt Roper024c9042015-09-24 15:53:11 -07003243 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3244 int i = skl_wm_plane_id(intel_plane);
3245
Damien Lespiau9414f562014-11-04 17:06:58 +00003246 trans_wm->plane_en[i] = false;
Matt Roper024c9042015-09-24 15:53:11 -07003247 }
Damien Lespiau407b50f2014-11-04 17:06:57 +00003248}
3249
Matt Roper024c9042015-09-24 15:53:11 -07003250static void skl_compute_pipe_wm(struct intel_crtc_state *cstate,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003251 struct skl_ddb_allocation *ddb,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003252 struct skl_pipe_wm *pipe_wm)
3253{
Matt Roper024c9042015-09-24 15:53:11 -07003254 struct drm_device *dev = cstate->base.crtc->dev;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003255 const struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003256 int level, max_level = ilk_wm_max_level(dev);
3257
3258 for (level = 0; level <= max_level; level++) {
Matt Roper024c9042015-09-24 15:53:11 -07003259 skl_compute_wm_level(dev_priv, ddb, cstate,
3260 level, &pipe_wm->wm[level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003261 }
Matt Roper024c9042015-09-24 15:53:11 -07003262 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003263
Matt Roper024c9042015-09-24 15:53:11 -07003264 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003265}
3266
3267static void skl_compute_wm_results(struct drm_device *dev,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003268 struct skl_pipe_wm *p_wm,
3269 struct skl_wm_values *r,
3270 struct intel_crtc *intel_crtc)
3271{
3272 int level, max_level = ilk_wm_max_level(dev);
3273 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00003274 uint32_t temp;
3275 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003276
3277 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003278 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3279 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003280
3281 temp |= p_wm->wm[level].plane_res_l[i] <<
3282 PLANE_WM_LINES_SHIFT;
3283 temp |= p_wm->wm[level].plane_res_b[i];
3284 if (p_wm->wm[level].plane_en[i])
3285 temp |= PLANE_WM_EN;
3286
3287 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003288 }
3289
3290 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003291
Matt Roper4969d332015-09-24 15:53:10 -07003292 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3293 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003294
Matt Roper4969d332015-09-24 15:53:10 -07003295 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003296 temp |= PLANE_WM_EN;
3297
Matt Roper4969d332015-09-24 15:53:10 -07003298 r->plane[pipe][PLANE_CURSOR][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003299
3300 }
3301
Damien Lespiau9414f562014-11-04 17:06:58 +00003302 /* transition WMs */
3303 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3304 temp = 0;
3305 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3306 temp |= p_wm->trans_wm.plane_res_b[i];
3307 if (p_wm->trans_wm.plane_en[i])
3308 temp |= PLANE_WM_EN;
3309
3310 r->plane_trans[pipe][i] = temp;
3311 }
3312
3313 temp = 0;
Matt Roper4969d332015-09-24 15:53:10 -07003314 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3315 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3316 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
Damien Lespiau9414f562014-11-04 17:06:58 +00003317 temp |= PLANE_WM_EN;
3318
Matt Roper4969d332015-09-24 15:53:10 -07003319 r->plane_trans[pipe][PLANE_CURSOR] = temp;
Damien Lespiau9414f562014-11-04 17:06:58 +00003320
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003321 r->wm_linetime[pipe] = p_wm->linetime;
3322}
3323
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003324static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3325 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003326 const struct skl_ddb_entry *entry)
3327{
3328 if (entry->end)
3329 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3330 else
3331 I915_WRITE(reg, 0);
3332}
3333
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003334static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3335 const struct skl_wm_values *new)
3336{
3337 struct drm_device *dev = dev_priv->dev;
3338 struct intel_crtc *crtc;
3339
Jani Nikula19c80542015-12-16 12:48:16 +02003340 for_each_intel_crtc(dev, crtc) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003341 int i, level, max_level = ilk_wm_max_level(dev);
3342 enum pipe pipe = crtc->pipe;
3343
Damien Lespiau5d374d92014-11-04 17:07:00 +00003344 if (!new->dirty[pipe])
3345 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003346
Damien Lespiau5d374d92014-11-04 17:07:00 +00003347 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3348
3349 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003350 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003351 I915_WRITE(PLANE_WM(pipe, i, level),
3352 new->plane[pipe][i][level]);
3353 I915_WRITE(CUR_WM(pipe, level),
Matt Roper4969d332015-09-24 15:53:10 -07003354 new->plane[pipe][PLANE_CURSOR][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003355 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003356 for (i = 0; i < intel_num_planes(crtc); i++)
3357 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3358 new->plane_trans[pipe][i]);
Matt Roper4969d332015-09-24 15:53:10 -07003359 I915_WRITE(CUR_WM_TRANS(pipe),
3360 new->plane_trans[pipe][PLANE_CURSOR]);
Damien Lespiau5d374d92014-11-04 17:07:00 +00003361
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003362 for (i = 0; i < intel_num_planes(crtc); i++) {
Damien Lespiau5d374d92014-11-04 17:07:00 +00003363 skl_ddb_entry_write(dev_priv,
3364 PLANE_BUF_CFG(pipe, i),
3365 &new->ddb.plane[pipe][i]);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003366 skl_ddb_entry_write(dev_priv,
3367 PLANE_NV12_BUF_CFG(pipe, i),
3368 &new->ddb.y_plane[pipe][i]);
3369 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003370
3371 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
Matt Roper4969d332015-09-24 15:53:10 -07003372 &new->ddb.plane[pipe][PLANE_CURSOR]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003373 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003374}
3375
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003376/*
3377 * When setting up a new DDB allocation arrangement, we need to correctly
3378 * sequence the times at which the new allocations for the pipes are taken into
3379 * account or we'll have pipes fetching from space previously allocated to
3380 * another pipe.
3381 *
3382 * Roughly the sequence looks like:
3383 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3384 * overlapping with a previous light-up pipe (another way to put it is:
3385 * pipes with their new allocation strickly included into their old ones).
3386 * 2. re-allocate the other pipes that get their allocation reduced
3387 * 3. allocate the pipes having their allocation increased
3388 *
3389 * Steps 1. and 2. are here to take care of the following case:
3390 * - Initially DDB looks like this:
3391 * | B | C |
3392 * - enable pipe A.
3393 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3394 * allocation
3395 * | A | B | C |
3396 *
3397 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3398 */
3399
Damien Lespiaud21b7952014-11-04 17:07:03 +00003400static void
3401skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003402{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003403 int plane;
3404
Damien Lespiaud21b7952014-11-04 17:07:03 +00003405 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3406
Damien Lespiaudd740782015-02-28 14:54:08 +00003407 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003408 I915_WRITE(PLANE_SURF(pipe, plane),
3409 I915_READ(PLANE_SURF(pipe, plane)));
3410 }
3411 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3412}
3413
3414static bool
3415skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3416 const struct skl_ddb_allocation *new,
3417 enum pipe pipe)
3418{
3419 uint16_t old_size, new_size;
3420
3421 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3422 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3423
3424 return old_size != new_size &&
3425 new->pipe[pipe].start >= old->pipe[pipe].start &&
3426 new->pipe[pipe].end <= old->pipe[pipe].end;
3427}
3428
3429static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3430 struct skl_wm_values *new_values)
3431{
3432 struct drm_device *dev = dev_priv->dev;
3433 struct skl_ddb_allocation *cur_ddb, *new_ddb;
Ville Syrjäläc929cb42015-04-02 18:28:07 +03003434 bool reallocated[I915_MAX_PIPES] = {};
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003435 struct intel_crtc *crtc;
3436 enum pipe pipe;
3437
3438 new_ddb = &new_values->ddb;
3439 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3440
3441 /*
3442 * First pass: flush the pipes with the new allocation contained into
3443 * the old space.
3444 *
3445 * We'll wait for the vblank on those pipes to ensure we can safely
3446 * re-allocate the freed space without this pipe fetching from it.
3447 */
3448 for_each_intel_crtc(dev, crtc) {
3449 if (!crtc->active)
3450 continue;
3451
3452 pipe = crtc->pipe;
3453
3454 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3455 continue;
3456
Damien Lespiaud21b7952014-11-04 17:07:03 +00003457 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003458 intel_wait_for_vblank(dev, pipe);
3459
3460 reallocated[pipe] = true;
3461 }
3462
3463
3464 /*
3465 * Second pass: flush the pipes that are having their allocation
3466 * reduced, but overlapping with a previous allocation.
3467 *
3468 * Here as well we need to wait for the vblank to make sure the freed
3469 * space is not used anymore.
3470 */
3471 for_each_intel_crtc(dev, crtc) {
3472 if (!crtc->active)
3473 continue;
3474
3475 pipe = crtc->pipe;
3476
3477 if (reallocated[pipe])
3478 continue;
3479
3480 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3481 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003482 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003483 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303484 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003485 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003486 }
3487
3488 /*
3489 * Third pass: flush the pipes that got more space allocated.
3490 *
3491 * We don't need to actively wait for the update here, next vblank
3492 * will just get more DDB space with the correct WM values.
3493 */
3494 for_each_intel_crtc(dev, crtc) {
3495 if (!crtc->active)
3496 continue;
3497
3498 pipe = crtc->pipe;
3499
3500 /*
3501 * At this point, only the pipes more space than before are
3502 * left to re-allocate.
3503 */
3504 if (reallocated[pipe])
3505 continue;
3506
Damien Lespiaud21b7952014-11-04 17:07:03 +00003507 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003508 }
3509}
3510
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003511static bool skl_update_pipe_wm(struct drm_crtc *crtc,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003512 struct skl_ddb_allocation *ddb, /* out */
3513 struct skl_pipe_wm *pipe_wm /* out */)
3514{
3515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper024c9042015-09-24 15:53:11 -07003516 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003517
Matt Roperaa363132015-09-24 15:53:18 -07003518 skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper024c9042015-09-24 15:53:11 -07003519 skl_compute_pipe_wm(cstate, ddb, pipe_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003520
Matt Roper4e0963c2015-09-24 15:53:15 -07003521 if (!memcmp(&intel_crtc->wm.active.skl, pipe_wm, sizeof(*pipe_wm)))
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003522 return false;
3523
Matt Roper4e0963c2015-09-24 15:53:15 -07003524 intel_crtc->wm.active.skl = *pipe_wm;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003525
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003526 return true;
3527}
3528
3529static void skl_update_other_pipe_wm(struct drm_device *dev,
3530 struct drm_crtc *crtc,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003531 struct skl_wm_values *r)
3532{
3533 struct intel_crtc *intel_crtc;
3534 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3535
3536 /*
3537 * If the WM update hasn't changed the allocation for this_crtc (the
3538 * crtc we are currently computing the new WM values for), other
3539 * enabled crtcs will keep the same allocation and we don't need to
3540 * recompute anything for them.
3541 */
3542 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3543 return;
3544
3545 /*
3546 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3547 * other active pipes need new DDB allocation and WM values.
3548 */
Jani Nikula19c80542015-12-16 12:48:16 +02003549 for_each_intel_crtc(dev, intel_crtc) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003550 struct skl_pipe_wm pipe_wm = {};
3551 bool wm_changed;
3552
3553 if (this_crtc->pipe == intel_crtc->pipe)
3554 continue;
3555
3556 if (!intel_crtc->active)
3557 continue;
3558
Matt Roperaa363132015-09-24 15:53:18 -07003559 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003560 &r->ddb, &pipe_wm);
3561
3562 /*
3563 * If we end up re-computing the other pipe WM values, it's
3564 * because it was really needed, so we expect the WM values to
3565 * be different.
3566 */
3567 WARN_ON(!wm_changed);
3568
Matt Roper024c9042015-09-24 15:53:11 -07003569 skl_compute_wm_results(dev, &pipe_wm, r, intel_crtc);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003570 r->dirty[intel_crtc->pipe] = true;
3571 }
3572}
3573
Bob Paauweadda50b2015-07-21 10:42:53 -07003574static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
3575{
3576 watermarks->wm_linetime[pipe] = 0;
3577 memset(watermarks->plane[pipe], 0,
3578 sizeof(uint32_t) * 8 * I915_MAX_PLANES);
Bob Paauweadda50b2015-07-21 10:42:53 -07003579 memset(watermarks->plane_trans[pipe],
3580 0, sizeof(uint32_t) * I915_MAX_PLANES);
Matt Roper4969d332015-09-24 15:53:10 -07003581 watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
Bob Paauweadda50b2015-07-21 10:42:53 -07003582
3583 /* Clear ddb entries for pipe */
3584 memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
3585 memset(&watermarks->ddb.plane[pipe], 0,
3586 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3587 memset(&watermarks->ddb.y_plane[pipe], 0,
3588 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
Matt Roper4969d332015-09-24 15:53:10 -07003589 memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
3590 sizeof(struct skl_ddb_entry));
Bob Paauweadda50b2015-07-21 10:42:53 -07003591
3592}
3593
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003594static void skl_update_wm(struct drm_crtc *crtc)
3595{
3596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3597 struct drm_device *dev = crtc->dev;
3598 struct drm_i915_private *dev_priv = dev->dev_private;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003599 struct skl_wm_values *results = &dev_priv->wm.skl_results;
Matt Roper4e0963c2015-09-24 15:53:15 -07003600 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3601 struct skl_pipe_wm *pipe_wm = &cstate->wm.optimal.skl;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003602
Bob Paauweadda50b2015-07-21 10:42:53 -07003603
3604 /* Clear all dirty flags */
3605 memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
3606
3607 skl_clear_wm(results, intel_crtc->pipe);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003608
Matt Roperaa363132015-09-24 15:53:18 -07003609 if (!skl_update_pipe_wm(crtc, &results->ddb, pipe_wm))
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003610 return;
3611
Matt Roper4e0963c2015-09-24 15:53:15 -07003612 skl_compute_wm_results(dev, pipe_wm, results, intel_crtc);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003613 results->dirty[intel_crtc->pipe] = true;
3614
Matt Roperaa363132015-09-24 15:53:18 -07003615 skl_update_other_pipe_wm(dev, crtc, results);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003616 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003617 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003618
3619 /* store the new configuration */
3620 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003621}
3622
Ville Syrjäläd8905652016-01-14 14:53:35 +02003623static void ilk_compute_wm_config(struct drm_device *dev,
3624 struct intel_wm_config *config)
3625{
3626 struct intel_crtc *crtc;
3627
3628 /* Compute the currently _active_ config */
3629 for_each_intel_crtc(dev, crtc) {
3630 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
3631
3632 if (!wm->pipe_enabled)
3633 continue;
3634
3635 config->sprites_enabled |= wm->sprites_enabled;
3636 config->sprites_scaled |= wm->sprites_scaled;
3637 config->num_pipes_active++;
3638 }
3639}
3640
Matt Roperbf220452016-01-19 11:43:04 -08003641static void ilk_program_watermarks(struct intel_crtc_state *cstate)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003642{
Matt Roperbf220452016-01-19 11:43:04 -08003643 struct drm_crtc *crtc = cstate->base.crtc;
3644 struct drm_device *dev = crtc->dev;
3645 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003646 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02003647 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02003648 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02003649 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003650 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07003651
Ville Syrjäläd8905652016-01-14 14:53:35 +02003652 ilk_compute_wm_config(dev, &config);
3653
3654 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3655 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003656
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003657 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003658 if (INTEL_INFO(dev)->gen >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02003659 config.num_pipes_active == 1 && config.sprites_enabled) {
3660 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3661 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003662
Imre Deak820c1982013-12-17 14:46:36 +02003663 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003664 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003665 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003666 }
3667
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003668 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003669 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003670
Imre Deak820c1982013-12-17 14:46:36 +02003671 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003672
Imre Deak820c1982013-12-17 14:46:36 +02003673 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003674}
3675
Matt Roperbf220452016-01-19 11:43:04 -08003676static void ilk_update_wm(struct drm_crtc *crtc)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003677{
Matt Roperbf220452016-01-19 11:43:04 -08003678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3679 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003680
Matt Roperbf220452016-01-19 11:43:04 -08003681 WARN_ON(cstate->base.active != intel_crtc->active);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003682
Matt Roperbf220452016-01-19 11:43:04 -08003683 /*
3684 * IVB workaround: must disable low power watermarks for at least
3685 * one frame before enabling scaling. LP watermarks can be re-enabled
3686 * when scaling is disabled.
3687 *
3688 * WaCxSRDisabledForSpriteScaling:ivb
3689 */
3690 if (cstate->disable_lp_wm) {
3691 ilk_disable_lp_wm(crtc->dev);
3692 intel_wait_for_vblank(crtc->dev, intel_crtc->pipe);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003693 }
Matt Roperbf220452016-01-19 11:43:04 -08003694
3695 intel_crtc->wm.active.ilk = cstate->wm.optimal.ilk;
3696
3697 ilk_program_watermarks(cstate);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07003698}
3699
Pradeep Bhat30789992014-11-04 17:06:45 +00003700static void skl_pipe_wm_active_state(uint32_t val,
3701 struct skl_pipe_wm *active,
3702 bool is_transwm,
3703 bool is_cursor,
3704 int i,
3705 int level)
3706{
3707 bool is_enabled = (val & PLANE_WM_EN) != 0;
3708
3709 if (!is_transwm) {
3710 if (!is_cursor) {
3711 active->wm[level].plane_en[i] = is_enabled;
3712 active->wm[level].plane_res_b[i] =
3713 val & PLANE_WM_BLOCKS_MASK;
3714 active->wm[level].plane_res_l[i] =
3715 (val >> PLANE_WM_LINES_SHIFT) &
3716 PLANE_WM_LINES_MASK;
3717 } else {
Matt Roper4969d332015-09-24 15:53:10 -07003718 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3719 active->wm[level].plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003720 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07003721 active->wm[level].plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003722 (val >> PLANE_WM_LINES_SHIFT) &
3723 PLANE_WM_LINES_MASK;
3724 }
3725 } else {
3726 if (!is_cursor) {
3727 active->trans_wm.plane_en[i] = is_enabled;
3728 active->trans_wm.plane_res_b[i] =
3729 val & PLANE_WM_BLOCKS_MASK;
3730 active->trans_wm.plane_res_l[i] =
3731 (val >> PLANE_WM_LINES_SHIFT) &
3732 PLANE_WM_LINES_MASK;
3733 } else {
Matt Roper4969d332015-09-24 15:53:10 -07003734 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3735 active->trans_wm.plane_res_b[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003736 val & PLANE_WM_BLOCKS_MASK;
Matt Roper4969d332015-09-24 15:53:10 -07003737 active->trans_wm.plane_res_l[PLANE_CURSOR] =
Pradeep Bhat30789992014-11-04 17:06:45 +00003738 (val >> PLANE_WM_LINES_SHIFT) &
3739 PLANE_WM_LINES_MASK;
3740 }
3741 }
3742}
3743
3744static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3745{
3746 struct drm_device *dev = crtc->dev;
3747 struct drm_i915_private *dev_priv = dev->dev_private;
3748 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07003750 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3751 struct skl_pipe_wm *active = &cstate->wm.optimal.skl;
Pradeep Bhat30789992014-11-04 17:06:45 +00003752 enum pipe pipe = intel_crtc->pipe;
3753 int level, i, max_level;
3754 uint32_t temp;
3755
3756 max_level = ilk_wm_max_level(dev);
3757
3758 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3759
3760 for (level = 0; level <= max_level; level++) {
3761 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3762 hw->plane[pipe][i][level] =
3763 I915_READ(PLANE_WM(pipe, i, level));
Matt Roper4969d332015-09-24 15:53:10 -07003764 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
Pradeep Bhat30789992014-11-04 17:06:45 +00003765 }
3766
3767 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3768 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
Matt Roper4969d332015-09-24 15:53:10 -07003769 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00003770
Matt Roper3ef00282015-03-09 10:19:24 -07003771 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003772 return;
3773
3774 hw->dirty[pipe] = true;
3775
3776 active->linetime = hw->wm_linetime[pipe];
3777
3778 for (level = 0; level <= max_level; level++) {
3779 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3780 temp = hw->plane[pipe][i][level];
3781 skl_pipe_wm_active_state(temp, active, false,
3782 false, i, level);
3783 }
Matt Roper4969d332015-09-24 15:53:10 -07003784 temp = hw->plane[pipe][PLANE_CURSOR][level];
Pradeep Bhat30789992014-11-04 17:06:45 +00003785 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3786 }
3787
3788 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3789 temp = hw->plane_trans[pipe][i];
3790 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3791 }
3792
Matt Roper4969d332015-09-24 15:53:10 -07003793 temp = hw->plane_trans[pipe][PLANE_CURSOR];
Pradeep Bhat30789992014-11-04 17:06:45 +00003794 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
Matt Roper4e0963c2015-09-24 15:53:15 -07003795
3796 intel_crtc->wm.active.skl = *active;
Pradeep Bhat30789992014-11-04 17:06:45 +00003797}
3798
3799void skl_wm_get_hw_state(struct drm_device *dev)
3800{
Damien Lespiaua269c582014-11-04 17:06:49 +00003801 struct drm_i915_private *dev_priv = dev->dev_private;
3802 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003803 struct drm_crtc *crtc;
3804
Damien Lespiaua269c582014-11-04 17:06:49 +00003805 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003806 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3807 skl_pipe_wm_get_hw_state(crtc);
3808}
3809
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003810static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3811{
3812 struct drm_device *dev = crtc->dev;
3813 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003814 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07003816 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3817 struct intel_pipe_wm *active = &cstate->wm.optimal.ilk;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003818 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003819 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003820 [PIPE_A] = WM0_PIPEA_ILK,
3821 [PIPE_B] = WM0_PIPEB_ILK,
3822 [PIPE_C] = WM0_PIPEC_IVB,
3823 };
3824
3825 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003826 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003827 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003828
Matt Roper3ef00282015-03-09 10:19:24 -07003829 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003830
3831 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003832 u32 tmp = hw->wm_pipe[pipe];
3833
3834 /*
3835 * For active pipes LP0 watermark is marked as
3836 * enabled, and LP1+ watermaks as disabled since
3837 * we can't really reverse compute them in case
3838 * multiple pipes are active.
3839 */
3840 active->wm[0].enable = true;
3841 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3842 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3843 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3844 active->linetime = hw->wm_linetime[pipe];
3845 } else {
3846 int level, max_level = ilk_wm_max_level(dev);
3847
3848 /*
3849 * For inactive pipes, all watermark levels
3850 * should be marked as enabled but zeroed,
3851 * which is what we'd compute them to.
3852 */
3853 for (level = 0; level <= max_level; level++)
3854 active->wm[level].enable = true;
3855 }
Matt Roper4e0963c2015-09-24 15:53:15 -07003856
3857 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003858}
3859
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03003860#define _FW_WM(value, plane) \
3861 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3862#define _FW_WM_VLV(value, plane) \
3863 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3864
3865static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3866 struct vlv_wm_values *wm)
3867{
3868 enum pipe pipe;
3869 uint32_t tmp;
3870
3871 for_each_pipe(dev_priv, pipe) {
3872 tmp = I915_READ(VLV_DDL(pipe));
3873
3874 wm->ddl[pipe].primary =
3875 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3876 wm->ddl[pipe].cursor =
3877 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3878 wm->ddl[pipe].sprite[0] =
3879 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3880 wm->ddl[pipe].sprite[1] =
3881 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3882 }
3883
3884 tmp = I915_READ(DSPFW1);
3885 wm->sr.plane = _FW_WM(tmp, SR);
3886 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
3887 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
3888 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
3889
3890 tmp = I915_READ(DSPFW2);
3891 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
3892 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
3893 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
3894
3895 tmp = I915_READ(DSPFW3);
3896 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3897
3898 if (IS_CHERRYVIEW(dev_priv)) {
3899 tmp = I915_READ(DSPFW7_CHV);
3900 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3901 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3902
3903 tmp = I915_READ(DSPFW8_CHV);
3904 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
3905 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
3906
3907 tmp = I915_READ(DSPFW9_CHV);
3908 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
3909 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
3910
3911 tmp = I915_READ(DSPHOWM);
3912 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3913 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
3914 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
3915 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
3916 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3917 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3918 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
3919 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3920 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3921 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
3922 } else {
3923 tmp = I915_READ(DSPFW7);
3924 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3925 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3926
3927 tmp = I915_READ(DSPHOWM);
3928 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3929 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3930 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3931 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
3932 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3933 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3934 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
3935 }
3936}
3937
3938#undef _FW_WM
3939#undef _FW_WM_VLV
3940
3941void vlv_wm_get_hw_state(struct drm_device *dev)
3942{
3943 struct drm_i915_private *dev_priv = to_i915(dev);
3944 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
3945 struct intel_plane *plane;
3946 enum pipe pipe;
3947 u32 val;
3948
3949 vlv_read_wm_values(dev_priv, wm);
3950
3951 for_each_intel_plane(dev, plane) {
3952 switch (plane->base.type) {
3953 int sprite;
3954 case DRM_PLANE_TYPE_CURSOR:
3955 plane->wm.fifo_size = 63;
3956 break;
3957 case DRM_PLANE_TYPE_PRIMARY:
3958 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
3959 break;
3960 case DRM_PLANE_TYPE_OVERLAY:
3961 sprite = plane->plane;
3962 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
3963 break;
3964 }
3965 }
3966
3967 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
3968 wm->level = VLV_WM_LEVEL_PM2;
3969
3970 if (IS_CHERRYVIEW(dev_priv)) {
3971 mutex_lock(&dev_priv->rps.hw_lock);
3972
3973 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3974 if (val & DSP_MAXFIFO_PM5_ENABLE)
3975 wm->level = VLV_WM_LEVEL_PM5;
3976
Ville Syrjälä58590c12015-09-08 21:05:12 +03003977 /*
3978 * If DDR DVFS is disabled in the BIOS, Punit
3979 * will never ack the request. So if that happens
3980 * assume we don't have to enable/disable DDR DVFS
3981 * dynamically. To test that just set the REQ_ACK
3982 * bit to poke the Punit, but don't change the
3983 * HIGH/LOW bits so that we don't actually change
3984 * the current state.
3985 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03003986 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03003987 val |= FORCE_DDR_FREQ_REQ_ACK;
3988 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
3989
3990 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
3991 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
3992 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
3993 "assuming DDR DVFS is disabled\n");
3994 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
3995 } else {
3996 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
3997 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
3998 wm->level = VLV_WM_LEVEL_DDR_DVFS;
3999 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004000
4001 mutex_unlock(&dev_priv->rps.hw_lock);
4002 }
4003
4004 for_each_pipe(dev_priv, pipe)
4005 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4006 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4007 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4008
4009 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4010 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4011}
4012
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004013void ilk_wm_get_hw_state(struct drm_device *dev)
4014{
4015 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02004016 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004017 struct drm_crtc *crtc;
4018
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004019 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004020 ilk_pipe_wm_get_hw_state(crtc);
4021
4022 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4023 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4024 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4025
4026 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004027 if (INTEL_INFO(dev)->gen >= 7) {
4028 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4029 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4030 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004031
Ville Syrjäläa42a5712014-01-07 16:14:08 +02004032 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004033 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4034 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4035 else if (IS_IVYBRIDGE(dev))
4036 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4037 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004038
4039 hw->enable_fbc_wm =
4040 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4041}
4042
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004043/**
4044 * intel_update_watermarks - update FIFO watermark values based on current modes
4045 *
4046 * Calculate watermark values for the various WM regs based on current mode
4047 * and plane configuration.
4048 *
4049 * There are several cases to deal with here:
4050 * - normal (i.e. non-self-refresh)
4051 * - self-refresh (SR) mode
4052 * - lines are large relative to FIFO size (buffer can hold up to 2)
4053 * - lines are small relative to FIFO size (buffer can hold more than 2
4054 * lines), so need to account for TLB latency
4055 *
4056 * The normal calculation is:
4057 * watermark = dotclock * bytes per pixel * latency
4058 * where latency is platform & configuration dependent (we assume pessimal
4059 * values here).
4060 *
4061 * The SR calculation is:
4062 * watermark = (trunc(latency/line time)+1) * surface width *
4063 * bytes per pixel
4064 * where
4065 * line time = htotal / dotclock
4066 * surface width = hdisplay for normal plane and 64 for cursor
4067 * and latency is assumed to be high, as above.
4068 *
4069 * The final value programmed to the register should always be rounded up,
4070 * and include an extra 2 entries to account for clock crossings.
4071 *
4072 * We don't use the sprite, so we can ignore that. And on Crestline we have
4073 * to set the non-SR watermarks to 8.
4074 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004075void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004076{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004077 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004078
4079 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004080 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004081}
4082
Jani Nikulae2828912016-01-18 09:19:47 +02004083/*
Daniel Vetter92703882012-08-09 16:46:01 +02004084 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004085 */
4086DEFINE_SPINLOCK(mchdev_lock);
4087
4088/* Global for IPS driver to get at the current i915 device. Protected by
4089 * mchdev_lock. */
4090static struct drm_i915_private *i915_mch_dev;
4091
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004092bool ironlake_set_drps(struct drm_device *dev, u8 val)
4093{
4094 struct drm_i915_private *dev_priv = dev->dev_private;
4095 u16 rgvswctl;
4096
Daniel Vetter92703882012-08-09 16:46:01 +02004097 assert_spin_locked(&mchdev_lock);
4098
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004099 rgvswctl = I915_READ16(MEMSWCTL);
4100 if (rgvswctl & MEMCTL_CMD_STS) {
4101 DRM_DEBUG("gpu busy, RCS change rejected\n");
4102 return false; /* still busy with another command */
4103 }
4104
4105 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4106 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4107 I915_WRITE16(MEMSWCTL, rgvswctl);
4108 POSTING_READ16(MEMSWCTL);
4109
4110 rgvswctl |= MEMCTL_CMD_STS;
4111 I915_WRITE16(MEMSWCTL, rgvswctl);
4112
4113 return true;
4114}
4115
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004116static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004117{
4118 struct drm_i915_private *dev_priv = dev->dev_private;
4119 u32 rgvmodectl = I915_READ(MEMMODECTL);
4120 u8 fmax, fmin, fstart, vstart;
4121
Daniel Vetter92703882012-08-09 16:46:01 +02004122 spin_lock_irq(&mchdev_lock);
4123
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004124 /* Enable temp reporting */
4125 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4126 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4127
4128 /* 100ms RC evaluation intervals */
4129 I915_WRITE(RCUPEI, 100000);
4130 I915_WRITE(RCDNEI, 100000);
4131
4132 /* Set max/min thresholds to 90ms and 80ms respectively */
4133 I915_WRITE(RCBMAXAVG, 90000);
4134 I915_WRITE(RCBMINAVG, 80000);
4135
4136 I915_WRITE(MEMIHYST, 1);
4137
4138 /* Set up min, max, and cur for interrupt handling */
4139 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4140 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4141 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4142 MEMMODE_FSTART_SHIFT;
4143
Ville Syrjälä616847e2015-09-18 20:03:19 +03004144 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004145 PXVFREQ_PX_SHIFT;
4146
Daniel Vetter20e4d402012-08-08 23:35:39 +02004147 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4148 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004149
Daniel Vetter20e4d402012-08-08 23:35:39 +02004150 dev_priv->ips.max_delay = fstart;
4151 dev_priv->ips.min_delay = fmin;
4152 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004153
4154 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4155 fmax, fmin, fstart);
4156
4157 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4158
4159 /*
4160 * Interrupts will be enabled in ironlake_irq_postinstall
4161 */
4162
4163 I915_WRITE(VIDSTART, vstart);
4164 POSTING_READ(VIDSTART);
4165
4166 rgvmodectl |= MEMMODE_SWMODE_EN;
4167 I915_WRITE(MEMMODECTL, rgvmodectl);
4168
Daniel Vetter92703882012-08-09 16:46:01 +02004169 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004170 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004171 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004172
4173 ironlake_set_drps(dev, fstart);
4174
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004175 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4176 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02004177 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03004178 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00004179 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02004180
4181 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004182}
4183
Daniel Vetter8090c6b2012-06-24 16:42:32 +02004184static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004185{
4186 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02004187 u16 rgvswctl;
4188
4189 spin_lock_irq(&mchdev_lock);
4190
4191 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004192
4193 /* Ack interrupts, disable EFC interrupt */
4194 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4195 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4196 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4197 I915_WRITE(DEIIR, DE_PCU_EVENT);
4198 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4199
4200 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02004201 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004202 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004203 rgvswctl |= MEMCTL_CMD_STS;
4204 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004205 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004206
Daniel Vetter92703882012-08-09 16:46:01 +02004207 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004208}
4209
Daniel Vetteracbe9472012-07-26 11:50:05 +02004210/* There's a funny hw issue where the hw returns all 0 when reading from
4211 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4212 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4213 * all limits and the gpu stuck at whatever frequency it is at atm).
4214 */
Akash Goel74ef1172015-03-06 11:07:19 +05304215static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004216{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004217 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004218
Daniel Vetter20b46e52012-07-26 11:16:14 +02004219 /* Only set the down limit when we've reached the lowest level to avoid
4220 * getting more interrupts, otherwise leave this clear. This prevents a
4221 * race in the hw when coming out of rc6: There's a tiny window where
4222 * the hw runs at the minimal clock before selecting the desired
4223 * frequency, if the down threshold expires in that window we will not
4224 * receive a down interrupt. */
Akash Goel74ef1172015-03-06 11:07:19 +05304225 if (IS_GEN9(dev_priv->dev)) {
4226 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4227 if (val <= dev_priv->rps.min_freq_softlimit)
4228 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4229 } else {
4230 limits = dev_priv->rps.max_freq_softlimit << 24;
4231 if (val <= dev_priv->rps.min_freq_softlimit)
4232 limits |= dev_priv->rps.min_freq_softlimit << 16;
4233 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02004234
4235 return limits;
4236}
4237
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004238static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4239{
4240 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05304241 u32 threshold_up = 0, threshold_down = 0; /* in % */
4242 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004243
4244 new_power = dev_priv->rps.power;
4245 switch (dev_priv->rps.power) {
4246 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004247 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004248 new_power = BETWEEN;
4249 break;
4250
4251 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004252 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004253 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07004254 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004255 new_power = HIGH_POWER;
4256 break;
4257
4258 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07004259 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 +01004260 new_power = BETWEEN;
4261 break;
4262 }
4263 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004264 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004265 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00004266 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004267 new_power = HIGH_POWER;
4268 if (new_power == dev_priv->rps.power)
4269 return;
4270
4271 /* Note the units here are not exactly 1us, but 1280ns. */
4272 switch (new_power) {
4273 case LOW_POWER:
4274 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05304275 ei_up = 16000;
4276 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004277
4278 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304279 ei_down = 32000;
4280 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004281 break;
4282
4283 case BETWEEN:
4284 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05304285 ei_up = 13000;
4286 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004287
4288 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304289 ei_down = 32000;
4290 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004291 break;
4292
4293 case HIGH_POWER:
4294 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05304295 ei_up = 10000;
4296 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004297
4298 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05304299 ei_down = 32000;
4300 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004301 break;
4302 }
4303
Akash Goel8a586432015-03-06 11:07:18 +05304304 I915_WRITE(GEN6_RP_UP_EI,
4305 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4306 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4307 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4308
4309 I915_WRITE(GEN6_RP_DOWN_EI,
4310 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4311 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4312 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4313
4314 I915_WRITE(GEN6_RP_CONTROL,
4315 GEN6_RP_MEDIA_TURBO |
4316 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4317 GEN6_RP_MEDIA_IS_GFX |
4318 GEN6_RP_ENABLE |
4319 GEN6_RP_UP_BUSY_AVG |
4320 GEN6_RP_DOWN_IDLE_AVG);
4321
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004322 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01004323 dev_priv->rps.up_threshold = threshold_up;
4324 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004325 dev_priv->rps.last_adj = 0;
4326}
4327
Chris Wilson2876ce72014-03-28 08:03:34 +00004328static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4329{
4330 u32 mask = 0;
4331
4332 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004333 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00004334 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00004335 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00004336
Chris Wilson7b3c29f2014-07-10 20:31:19 +01004337 mask &= dev_priv->pm_rps_events;
4338
Imre Deak59d02a12014-12-19 19:33:26 +02004339 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00004340}
4341
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004342/* gen6_set_rps is called to update the frequency request, but should also be
4343 * called when the range (min_delay and max_delay) is modified so that we can
4344 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004345static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02004346{
4347 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004348
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304349 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Jani Nikulae87a0052015-10-20 15:22:02 +03004350 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304351 return;
4352
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004353 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004354 WARN_ON(val > dev_priv->rps.max_freq);
4355 WARN_ON(val < dev_priv->rps.min_freq);
Daniel Vetter004777c2012-08-09 15:07:01 +02004356
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004357 /* min/max delay may still have been modified so be sure to
4358 * write the limits value.
4359 */
4360 if (val != dev_priv->rps.cur_freq) {
4361 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004362
Akash Goel57041952015-03-06 11:07:17 +05304363 if (IS_GEN9(dev))
4364 I915_WRITE(GEN6_RPNSWREQ,
4365 GEN9_FREQUENCY(val));
4366 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00004367 I915_WRITE(GEN6_RPNSWREQ,
4368 HSW_FREQUENCY(val));
4369 else
4370 I915_WRITE(GEN6_RPNSWREQ,
4371 GEN6_FREQUENCY(val) |
4372 GEN6_OFFSET(0) |
4373 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06004374 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004375
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004376 /* Make sure we continue to get interrupts
4377 * until we hit the minimum or maximum frequencies.
4378 */
Akash Goel74ef1172015-03-06 11:07:19 +05304379 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00004380 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01004381
Ben Widawskyd5570a72012-09-07 19:43:41 -07004382 POSTING_READ(GEN6_RPNSWREQ);
4383
Ben Widawskyb39fb292014-03-19 18:31:11 -07004384 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02004385 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004386}
4387
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004388static void valleyview_set_rps(struct drm_device *dev, u8 val)
4389{
4390 struct drm_i915_private *dev_priv = dev->dev_private;
4391
4392 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Chris Wilsonaed242f2015-03-18 09:48:21 +00004393 WARN_ON(val > dev_priv->rps.max_freq);
4394 WARN_ON(val < dev_priv->rps.min_freq);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004395
4396 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4397 "Odd GPU freq value\n"))
4398 val &= ~1;
4399
Deepak Scd25dd52015-07-10 18:31:40 +05304400 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4401
Chris Wilson8fb55192015-04-07 16:20:28 +01004402 if (val != dev_priv->rps.cur_freq) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004403 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01004404 if (!IS_CHERRYVIEW(dev_priv))
4405 gen6_set_rps_thresholds(dev_priv, val);
4406 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004407
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004408 dev_priv->rps.cur_freq = val;
4409 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4410}
4411
Deepak Sa7f6e232015-05-09 18:04:44 +05304412/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05304413 *
4414 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05304415 * 1. Forcewake Media well.
4416 * 2. Request idle freq.
4417 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05304418*/
4419static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4420{
Chris Wilsonaed242f2015-03-18 09:48:21 +00004421 u32 val = dev_priv->rps.idle_freq;
Deepak S5549d252014-06-28 11:26:11 +05304422
Chris Wilsonaed242f2015-03-18 09:48:21 +00004423 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05304424 return;
4425
Deepak Sa7f6e232015-05-09 18:04:44 +05304426 /* Wake up the media well, as that takes a lot less
4427 * power than the Render well. */
4428 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4429 valleyview_set_rps(dev_priv->dev, val);
4430 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Deepak S76c3552f2014-01-30 23:08:16 +05304431}
4432
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004433void gen6_rps_busy(struct drm_i915_private *dev_priv)
4434{
4435 mutex_lock(&dev_priv->rps.hw_lock);
4436 if (dev_priv->rps.enabled) {
4437 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4438 gen6_rps_reset_ei(dev_priv);
4439 I915_WRITE(GEN6_PMINTRMSK,
4440 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4441 }
4442 mutex_unlock(&dev_priv->rps.hw_lock);
4443}
4444
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004445void gen6_rps_idle(struct drm_i915_private *dev_priv)
4446{
Damien Lespiau691bb712013-12-12 14:36:36 +00004447 struct drm_device *dev = dev_priv->dev;
4448
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004449 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004450 if (dev_priv->rps.enabled) {
Wayne Boyer666a4532015-12-09 12:29:35 -08004451 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05304452 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004453 else
Chris Wilsonaed242f2015-03-18 09:48:21 +00004454 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004455 dev_priv->rps.last_adj = 0;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004456 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
Chris Wilsonc0951f02013-10-10 21:58:50 +01004457 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004458 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004459
Chris Wilson8d3afd72015-05-21 21:01:47 +01004460 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004461 while (!list_empty(&dev_priv->rps.clients))
4462 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01004463 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004464}
4465
Chris Wilson1854d5c2015-04-07 16:20:32 +01004466void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01004467 struct intel_rps_client *rps,
4468 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004469{
Chris Wilson8d3afd72015-05-21 21:01:47 +01004470 /* This is intentionally racy! We peek at the state here, then
4471 * validate inside the RPS worker.
4472 */
4473 if (!(dev_priv->mm.busy &&
4474 dev_priv->rps.enabled &&
4475 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4476 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00004477
Chris Wilsone61b9952015-04-27 13:41:24 +01004478 /* Force a RPS boost (and don't count it against the client) if
4479 * the GPU is severely congested.
4480 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01004481 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01004482 rps = NULL;
4483
Chris Wilson8d3afd72015-05-21 21:01:47 +01004484 spin_lock(&dev_priv->rps.client_lock);
4485 if (rps == NULL || list_empty(&rps->link)) {
4486 spin_lock_irq(&dev_priv->irq_lock);
4487 if (dev_priv->rps.interrupts_enabled) {
4488 dev_priv->rps.client_boost = true;
4489 queue_work(dev_priv->wq, &dev_priv->rps.work);
4490 }
4491 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01004492
Chris Wilson2e1b8732015-04-27 13:41:22 +01004493 if (rps != NULL) {
4494 list_add(&rps->link, &dev_priv->rps.clients);
4495 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01004496 } else
4497 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01004498 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01004499 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01004500}
4501
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004502void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004503{
Wayne Boyer666a4532015-12-09 12:29:35 -08004504 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
Ville Syrjäläffe02b42015-02-02 19:09:50 +02004505 valleyview_set_rps(dev, val);
4506 else
4507 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004508}
4509
Zhe Wang20e49362014-11-04 17:07:05 +00004510static void gen9_disable_rps(struct drm_device *dev)
4511{
4512 struct drm_i915_private *dev_priv = dev->dev_private;
4513
4514 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004515 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004516}
4517
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004518static void gen6_disable_rps(struct drm_device *dev)
4519{
4520 struct drm_i915_private *dev_priv = dev->dev_private;
4521
4522 I915_WRITE(GEN6_RC_CONTROL, 0);
4523 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004524}
4525
Deepak S38807742014-05-23 21:00:15 +05304526static void cherryview_disable_rps(struct drm_device *dev)
4527{
4528 struct drm_i915_private *dev_priv = dev->dev_private;
4529
4530 I915_WRITE(GEN6_RC_CONTROL, 0);
4531}
4532
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004533static void valleyview_disable_rps(struct drm_device *dev)
4534{
4535 struct drm_i915_private *dev_priv = dev->dev_private;
4536
Deepak S98a2e5f2014-08-18 10:35:27 -07004537 /* we're doing forcewake before Disabling RC6,
4538 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004539 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004540
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004541 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004542
Mika Kuoppala59bad942015-01-16 11:34:40 +02004543 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004544}
4545
Ben Widawskydc39fff2013-10-18 12:32:07 -07004546static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4547{
Wayne Boyer666a4532015-12-09 12:29:35 -08004548 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
Imre Deak91ca6892014-04-14 20:24:25 +03004549 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4550 mode = GEN6_RC_CTL_RC6_ENABLE;
4551 else
4552 mode = 0;
4553 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004554 if (HAS_RC6p(dev))
4555 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004556 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
4557 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
4558 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004559
4560 else
4561 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
Jani Nikula87ad3212016-01-14 12:53:34 +02004562 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07004563}
4564
Imre Deake6069ca2014-04-18 16:01:02 +03004565static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004566{
Daniel Vettere7d66d82015-06-15 23:23:54 +02004567 /* No RC6 before Ironlake and code is gone for ilk. */
4568 if (INTEL_INFO(dev)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03004569 return 0;
4570
Daniel Vetter456470e2012-08-08 23:35:40 +02004571 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004572 if (enable_rc6 >= 0) {
4573 int mask;
4574
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004575 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004576 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4577 INTEL_RC6pp_ENABLE;
4578 else
4579 mask = INTEL_RC6_ENABLE;
4580
4581 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004582 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4583 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004584
4585 return enable_rc6 & mask;
4586 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004587
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004588 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004589 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004590
4591 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004592}
4593
Imre Deake6069ca2014-04-18 16:01:02 +03004594int intel_enable_rc6(const struct drm_device *dev)
4595{
4596 return i915.enable_rc6;
4597}
4598
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004599static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004600{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004601 struct drm_i915_private *dev_priv = dev->dev_private;
4602 uint32_t rp_state_cap;
4603 u32 ddcc_status = 0;
4604 int ret;
4605
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004606 /* All of these values are in units of 50MHz */
4607 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004608 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Bob Paauwe35040562015-06-25 14:54:07 -07004609 if (IS_BROXTON(dev)) {
4610 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4611 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4612 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4613 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4614 } else {
4615 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4616 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4617 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4618 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4619 }
4620
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004621 /* hw_max = RP0 until we check for overclocking */
4622 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4623
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004624 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07004625 if (IS_HASWELL(dev) || IS_BROADWELL(dev) ||
4626 IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004627 ret = sandybridge_pcode_read(dev_priv,
4628 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4629 &ddcc_status);
4630 if (0 == ret)
4631 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004632 clamp_t(u8,
4633 ((ddcc_status >> 8) & 0xff),
4634 dev_priv->rps.min_freq,
4635 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004636 }
4637
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07004638 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Akash Goelc5e06882015-06-29 14:50:19 +05304639 /* Store the frequency values in 16.66 MHZ units, which is
4640 the natural hardware unit for SKL */
4641 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4642 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4643 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4644 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4645 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4646 }
4647
Chris Wilsonaed242f2015-03-18 09:48:21 +00004648 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4649
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004650 /* Preserve min/max settings in case of re-init */
4651 if (dev_priv->rps.max_freq_softlimit == 0)
4652 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4653
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004654 if (dev_priv->rps.min_freq_softlimit == 0) {
4655 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4656 dev_priv->rps.min_freq_softlimit =
Ville Syrjälä813b5e62015-03-25 19:27:16 +02004657 max_t(int, dev_priv->rps.efficient_freq,
4658 intel_freq_opcode(dev_priv, 450));
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004659 else
4660 dev_priv->rps.min_freq_softlimit =
4661 dev_priv->rps.min_freq;
4662 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004663}
4664
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004665/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004666static void gen9_enable_rps(struct drm_device *dev)
4667{
4668 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004669
4670 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4671
Damien Lespiauba1c5542015-01-16 18:07:26 +00004672 gen6_init_rps_frequencies(dev);
4673
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304674 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
Jani Nikulae87a0052015-10-20 15:22:02 +03004675 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
Sagar Arun Kamble23eafea2015-08-23 17:52:48 +05304676 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4677 return;
4678 }
4679
Akash Goel0beb0592015-03-06 11:07:20 +05304680 /* Program defaults and thresholds for RPS*/
4681 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4682 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004683
Akash Goel0beb0592015-03-06 11:07:20 +05304684 /* 1 second timeout*/
4685 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4686 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4687
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004688 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004689
Akash Goel0beb0592015-03-06 11:07:20 +05304690 /* Leaning on the below call to gen6_set_rps to program/setup the
4691 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4692 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4693 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4694 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004695
4696 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4697}
4698
4699static void gen9_enable_rc6(struct drm_device *dev)
4700{
4701 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004702 struct intel_engine_cs *ring;
4703 uint32_t rc6_mask = 0;
4704 int unused;
4705
4706 /* 1a: Software RC state - RC0 */
4707 I915_WRITE(GEN6_RC_STATE, 0);
4708
4709 /* 1b: Get forcewake during program sequence. Although the driver
4710 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004711 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004712
4713 /* 2a: Disable RC states. */
4714 I915_WRITE(GEN6_RC_CONTROL, 0);
4715
4716 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05304717
4718 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Mika Kuoppalae7674b82015-12-07 18:29:45 +02004719 if (IS_SKYLAKE(dev))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05304720 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
4721 else
4722 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00004723 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4724 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4725 for_each_ring(ring, dev_priv, unused)
4726 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05304727
4728 if (HAS_GUC_UCODE(dev))
4729 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
4730
Zhe Wang20e49362014-11-04 17:07:05 +00004731 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004732
Zhe Wang38c23522015-01-20 12:23:04 +00004733 /* 2c: Program Coarse Power Gating Policies. */
4734 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4735 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4736
Zhe Wang20e49362014-11-04 17:07:05 +00004737 /* 3a: Enable RC6 */
4738 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4739 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02004740 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304741 /* WaRsUseTimeoutMode */
Jani Nikulae87a0052015-10-20 15:22:02 +03004742 if (IS_SKL_REVID(dev, 0, SKL_REVID_D0) ||
Tim Gorecbdc12a2015-10-26 10:48:58 +00004743 IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304744 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05304745 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4746 GEN7_RC_CTL_TO_MODE |
4747 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304748 } else {
4749 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
Sagar Arun Kamblee3429cd2015-09-12 10:17:52 +05304750 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4751 GEN6_RC_CTL_EI_MODE(1) |
4752 rc6_mask);
Sagar Arun Kamble3e7732a2015-10-01 20:29:27 +05304753 }
Zhe Wang20e49362014-11-04 17:07:05 +00004754
Sagar Kamblecb07bae2015-04-12 11:28:14 +05304755 /*
4756 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05304757 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05304758 */
Mika Kuoppala06e668a2015-12-16 19:18:37 +02004759 if (NEEDS_WaRsDisableCoarsePowerGating(dev))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05304760 I915_WRITE(GEN9_PG_ENABLE, 0);
4761 else
4762 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4763 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004764
Mika Kuoppala59bad942015-01-16 11:34:40 +02004765 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004766
4767}
4768
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004769static void gen8_enable_rps(struct drm_device *dev)
4770{
4771 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004772 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004773 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004774 int unused;
4775
4776 /* 1a: Software RC state - RC0 */
4777 I915_WRITE(GEN6_RC_STATE, 0);
4778
4779 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4780 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004781 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004782
4783 /* 2a: Disable RC states. */
4784 I915_WRITE(GEN6_RC_CONTROL, 0);
4785
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004786 /* Initialize rps frequencies */
4787 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004788
4789 /* 2b: Program RC6 thresholds.*/
4790 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4791 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4792 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4793 for_each_ring(ring, dev_priv, unused)
4794 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4795 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004796 if (IS_BROADWELL(dev))
4797 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4798 else
4799 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004800
4801 /* 3: Enable RC6 */
4802 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4803 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004804 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004805 if (IS_BROADWELL(dev))
4806 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4807 GEN7_RC_CTL_TO_MODE |
4808 rc6_mask);
4809 else
4810 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4811 GEN6_RC_CTL_EI_MODE(1) |
4812 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004813
4814 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004815 I915_WRITE(GEN6_RPNSWREQ,
4816 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4817 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4818 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004819 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4820 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004821
Daniel Vetter7526ed72014-09-29 15:07:19 +02004822 /* Docs recommend 900MHz, and 300 MHz respectively */
4823 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4824 dev_priv->rps.max_freq_softlimit << 24 |
4825 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004826
Daniel Vetter7526ed72014-09-29 15:07:19 +02004827 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4828 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4829 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4830 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004831
Daniel Vetter7526ed72014-09-29 15:07:19 +02004832 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004833
4834 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004835 I915_WRITE(GEN6_RP_CONTROL,
4836 GEN6_RP_MEDIA_TURBO |
4837 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4838 GEN6_RP_MEDIA_IS_GFX |
4839 GEN6_RP_ENABLE |
4840 GEN6_RP_UP_BUSY_AVG |
4841 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004842
Daniel Vetter7526ed72014-09-29 15:07:19 +02004843 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004844
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004845 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004846 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004847
Mika Kuoppala59bad942015-01-16 11:34:40 +02004848 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004849}
4850
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004851static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004852{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004853 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004854 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004855 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004856 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004857 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004858 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004859
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004860 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004861
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004862 /* Here begins a magic sequence of register writes to enable
4863 * auto-downclocking.
4864 *
4865 * Perhaps there might be some value in exposing these to
4866 * userspace...
4867 */
4868 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004869
4870 /* Clear the DBG now so we don't confuse earlier errors */
4871 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4872 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4873 I915_WRITE(GTFIFODBG, gtfifodbg);
4874 }
4875
Mika Kuoppala59bad942015-01-16 11:34:40 +02004876 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004877
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004878 /* Initialize rps frequencies */
4879 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004880
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004881 /* disable the counters and set deterministic thresholds */
4882 I915_WRITE(GEN6_RC_CONTROL, 0);
4883
4884 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4885 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4886 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4887 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4888 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4889
Chris Wilsonb4519512012-05-11 14:29:30 +01004890 for_each_ring(ring, dev_priv, i)
4891 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004892
4893 I915_WRITE(GEN6_RC_SLEEP, 0);
4894 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004895 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004896 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4897 else
4898 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004899 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004900 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4901
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004902 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004903 rc6_mode = intel_enable_rc6(dev_priv->dev);
4904 if (rc6_mode & INTEL_RC6_ENABLE)
4905 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4906
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004907 /* We don't use those on Haswell */
4908 if (!IS_HASWELL(dev)) {
4909 if (rc6_mode & INTEL_RC6p_ENABLE)
4910 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004911
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004912 if (rc6_mode & INTEL_RC6pp_ENABLE)
4913 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4914 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004915
Ben Widawskydc39fff2013-10-18 12:32:07 -07004916 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004917
4918 I915_WRITE(GEN6_RC_CONTROL,
4919 rc6_mask |
4920 GEN6_RC_CTL_EI_MODE(1) |
4921 GEN6_RC_CTL_HW_ENABLE);
4922
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004923 /* Power down if completely idle for over 50ms */
4924 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004925 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004926
Ben Widawsky42c05262012-09-26 10:34:00 -07004927 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004928 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004929 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004930
4931 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4932 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4933 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004934 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004935 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004936 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004937 }
4938
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004939 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Chris Wilsonaed242f2015-03-18 09:48:21 +00004940 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004941
Ben Widawsky31643d52012-09-26 10:34:01 -07004942 rc6vids = 0;
4943 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4944 if (IS_GEN6(dev) && ret) {
4945 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4946 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4947 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4948 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4949 rc6vids &= 0xffff00;
4950 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4951 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4952 if (ret)
4953 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4954 }
4955
Mika Kuoppala59bad942015-01-16 11:34:40 +02004956 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004957}
4958
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004959static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004960{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004961 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004962 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004963 unsigned int gpu_freq;
4964 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05304965 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004966 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004967 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004968
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004969 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004970
Ben Widawskyeda79642013-10-07 17:15:48 -03004971 policy = cpufreq_cpu_get(0);
4972 if (policy) {
4973 max_ia_freq = policy->cpuinfo.max_freq;
4974 cpufreq_cpu_put(policy);
4975 } else {
4976 /*
4977 * Default to measured freq if none found, PCU will ensure we
4978 * don't go over
4979 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004980 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004981 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004982
4983 /* Convert from kHz to MHz */
4984 max_ia_freq /= 1000;
4985
Ben Widawsky153b4b952013-10-22 22:05:09 -07004986 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004987 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4988 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004989
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07004990 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05304991 /* Convert GT frequency to 50 HZ units */
4992 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
4993 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
4994 } else {
4995 min_gpu_freq = dev_priv->rps.min_freq;
4996 max_gpu_freq = dev_priv->rps.max_freq;
4997 }
4998
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004999 /*
5000 * For each potential GPU frequency, load a ring frequency we'd like
5001 * to use for memory access. We do this by specifying the IA frequency
5002 * the PCU should use as a reference to determine the ring frequency.
5003 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305004 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5005 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005006 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005007
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07005008 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305009 /*
5010 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5011 * No floor required for ring frequency on SKL.
5012 */
5013 ring_freq = gpu_freq;
5014 } else if (INTEL_INFO(dev)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005015 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5016 ring_freq = max(min_ring_freq, gpu_freq);
5017 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005018 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005019 ring_freq = max(min_ring_freq, ring_freq);
5020 /* leave ia_freq as the default, chosen by cpufreq */
5021 } else {
5022 /* On older processors, there is no separate ring
5023 * clock domain, so in order to boost the bandwidth
5024 * of the ring, we need to upclock the CPU (ia_freq).
5025 *
5026 * For GPU frequencies less than 750MHz,
5027 * just use the lowest ring freq.
5028 */
5029 if (gpu_freq < min_freq)
5030 ia_freq = 800;
5031 else
5032 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5033 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5034 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005035
Ben Widawsky42c05262012-09-26 10:34:00 -07005036 sandybridge_pcode_write(dev_priv,
5037 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005038 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5039 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5040 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005041 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005042}
5043
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005044void gen6_update_ring_freq(struct drm_device *dev)
5045{
5046 struct drm_i915_private *dev_priv = dev->dev_private;
5047
Akash Goel97d33082015-06-29 14:50:23 +05305048 if (!HAS_CORE_RING_FREQ(dev))
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005049 return;
5050
5051 mutex_lock(&dev_priv->rps.hw_lock);
5052 __gen6_update_ring_freq(dev);
5053 mutex_unlock(&dev_priv->rps.hw_lock);
5054}
5055
Ville Syrjälä03af2042014-06-28 02:03:53 +03005056static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305057{
Deepak S095acd52015-01-17 11:05:59 +05305058 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05305059 u32 val, rp0;
5060
Jani Nikula5b5929c2015-10-07 11:17:46 +03005061 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305062
Jani Nikula5b5929c2015-10-07 11:17:46 +03005063 switch (INTEL_INFO(dev)->eu_total) {
5064 case 8:
5065 /* (2 * 4) config */
5066 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5067 break;
5068 case 12:
5069 /* (2 * 6) config */
5070 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5071 break;
5072 case 16:
5073 /* (2 * 8) config */
5074 default:
5075 /* Setting (2 * 8) Min RP0 for any other combination */
5076 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5077 break;
Deepak S095acd52015-01-17 11:05:59 +05305078 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005079
5080 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5081
Deepak S2b6b3a02014-05-27 15:59:30 +05305082 return rp0;
5083}
5084
5085static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5086{
5087 u32 val, rpe;
5088
5089 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5090 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5091
5092 return rpe;
5093}
5094
Deepak S7707df42014-07-12 18:46:14 +05305095static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5096{
5097 u32 val, rp1;
5098
Jani Nikula5b5929c2015-10-07 11:17:46 +03005099 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5100 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5101
Deepak S7707df42014-07-12 18:46:14 +05305102 return rp1;
5103}
5104
Deepak Sf8f2b002014-07-10 13:16:21 +05305105static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5106{
5107 u32 val, rp1;
5108
5109 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5110
5111 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5112
5113 return rp1;
5114}
5115
Ville Syrjälä03af2042014-06-28 02:03:53 +03005116static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005117{
5118 u32 val, rp0;
5119
Jani Nikula64936252013-05-22 15:36:20 +03005120 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005121
5122 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5123 /* Clamp to max */
5124 rp0 = min_t(u32, rp0, 0xea);
5125
5126 return rp0;
5127}
5128
5129static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5130{
5131 u32 val, rpe;
5132
Jani Nikula64936252013-05-22 15:36:20 +03005133 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005134 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03005135 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005136 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5137
5138 return rpe;
5139}
5140
Ville Syrjälä03af2042014-06-28 02:03:53 +03005141static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005142{
Imre Deak36146032014-12-04 18:39:35 +02005143 u32 val;
5144
5145 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5146 /*
5147 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5148 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5149 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5150 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5151 * to make sure it matches what Punit accepts.
5152 */
5153 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005154}
5155
Imre Deakae484342014-03-31 15:10:44 +03005156/* Check that the pctx buffer wasn't move under us. */
5157static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5158{
5159 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5160
5161 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5162 dev_priv->vlv_pctx->stolen->start);
5163}
5164
Deepak S38807742014-05-23 21:00:15 +05305165
5166/* Check that the pcbr address is not empty. */
5167static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5168{
5169 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5170
5171 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5172}
5173
5174static void cherryview_setup_pctx(struct drm_device *dev)
5175{
5176 struct drm_i915_private *dev_priv = dev->dev_private;
5177 unsigned long pctx_paddr, paddr;
5178 struct i915_gtt *gtt = &dev_priv->gtt;
5179 u32 pcbr;
5180 int pctx_size = 32*1024;
5181
5182 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5183
5184 pcbr = I915_READ(VLV_PCBR);
5185 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005186 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05305187 paddr = (dev_priv->mm.stolen_base +
5188 (gtt->stolen_size - pctx_size));
5189
5190 pctx_paddr = (paddr & (~4095));
5191 I915_WRITE(VLV_PCBR, pctx_paddr);
5192 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005193
5194 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05305195}
5196
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005197static void valleyview_setup_pctx(struct drm_device *dev)
5198{
5199 struct drm_i915_private *dev_priv = dev->dev_private;
5200 struct drm_i915_gem_object *pctx;
5201 unsigned long pctx_paddr;
5202 u32 pcbr;
5203 int pctx_size = 24*1024;
5204
Imre Deak17b0c1f2014-02-11 21:39:06 +02005205 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
5206
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005207 pcbr = I915_READ(VLV_PCBR);
5208 if (pcbr) {
5209 /* BIOS set it up already, grab the pre-alloc'd space */
5210 int pcbr_offset;
5211
5212 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5213 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5214 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02005215 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005216 pctx_size);
5217 goto out;
5218 }
5219
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005220 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5221
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005222 /*
5223 * From the Gunit register HAS:
5224 * The Gfx driver is expected to program this register and ensure
5225 * proper allocation within Gfx stolen memory. For example, this
5226 * register should be programmed such than the PCBR range does not
5227 * overlap with other ranges, such as the frame buffer, protected
5228 * memory, or any other relevant ranges.
5229 */
5230 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5231 if (!pctx) {
5232 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5233 return;
5234 }
5235
5236 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5237 I915_WRITE(VLV_PCBR, pctx_paddr);
5238
5239out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02005240 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07005241 dev_priv->vlv_pctx = pctx;
5242}
5243
Imre Deakae484342014-03-31 15:10:44 +03005244static void valleyview_cleanup_pctx(struct drm_device *dev)
5245{
5246 struct drm_i915_private *dev_priv = dev->dev_private;
5247
5248 if (WARN_ON(!dev_priv->vlv_pctx))
5249 return;
5250
5251 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
5252 dev_priv->vlv_pctx = NULL;
5253}
5254
Imre Deak4e805192014-04-14 20:24:41 +03005255static void valleyview_init_gt_powersave(struct drm_device *dev)
5256{
5257 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005258 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03005259
5260 valleyview_setup_pctx(dev);
5261
5262 mutex_lock(&dev_priv->rps.hw_lock);
5263
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005264 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5265 switch ((val >> 6) & 3) {
5266 case 0:
5267 case 1:
5268 dev_priv->mem_freq = 800;
5269 break;
5270 case 2:
5271 dev_priv->mem_freq = 1066;
5272 break;
5273 case 3:
5274 dev_priv->mem_freq = 1333;
5275 break;
5276 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005277 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005278
Imre Deak4e805192014-04-14 20:24:41 +03005279 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5280 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5281 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005282 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005283 dev_priv->rps.max_freq);
5284
5285 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5286 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005287 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005288 dev_priv->rps.efficient_freq);
5289
Deepak Sf8f2b002014-07-10 13:16:21 +05305290 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5291 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005292 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05305293 dev_priv->rps.rp1_freq);
5294
Imre Deak4e805192014-04-14 20:24:41 +03005295 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5296 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005297 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03005298 dev_priv->rps.min_freq);
5299
Chris Wilsonaed242f2015-03-18 09:48:21 +00005300 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5301
Imre Deak4e805192014-04-14 20:24:41 +03005302 /* Preserve min/max settings in case of re-init */
5303 if (dev_priv->rps.max_freq_softlimit == 0)
5304 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5305
5306 if (dev_priv->rps.min_freq_softlimit == 0)
5307 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5308
5309 mutex_unlock(&dev_priv->rps.hw_lock);
5310}
5311
Deepak S38807742014-05-23 21:00:15 +05305312static void cherryview_init_gt_powersave(struct drm_device *dev)
5313{
Deepak S2b6b3a02014-05-27 15:59:30 +05305314 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005315 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05305316
Deepak S38807742014-05-23 21:00:15 +05305317 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05305318
5319 mutex_lock(&dev_priv->rps.hw_lock);
5320
Ville Syrjäläa5805162015-05-26 20:42:30 +03005321 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005322 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03005323 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02005324
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005325 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005326 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005327 dev_priv->mem_freq = 2000;
5328 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03005329 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005330 dev_priv->mem_freq = 1600;
5331 break;
5332 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02005333 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03005334
Deepak S2b6b3a02014-05-27 15:59:30 +05305335 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5336 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5337 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005338 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305339 dev_priv->rps.max_freq);
5340
5341 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5342 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005343 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305344 dev_priv->rps.efficient_freq);
5345
Deepak S7707df42014-07-12 18:46:14 +05305346 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5347 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005348 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05305349 dev_priv->rps.rp1_freq);
5350
Deepak S5b7c91b2015-05-09 18:15:46 +05305351 /* PUnit validated range is only [RPe, RP0] */
5352 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
Deepak S2b6b3a02014-05-27 15:59:30 +05305353 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005354 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305355 dev_priv->rps.min_freq);
5356
Ville Syrjälä1c147622014-08-18 14:42:43 +03005357 WARN_ONCE((dev_priv->rps.max_freq |
5358 dev_priv->rps.efficient_freq |
5359 dev_priv->rps.rp1_freq |
5360 dev_priv->rps.min_freq) & 1,
5361 "Odd GPU freq values\n");
5362
Chris Wilsonaed242f2015-03-18 09:48:21 +00005363 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5364
Deepak S2b6b3a02014-05-27 15:59:30 +05305365 /* Preserve min/max settings in case of re-init */
5366 if (dev_priv->rps.max_freq_softlimit == 0)
5367 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5368
5369 if (dev_priv->rps.min_freq_softlimit == 0)
5370 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5371
5372 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05305373}
5374
Imre Deak4e805192014-04-14 20:24:41 +03005375static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5376{
5377 valleyview_cleanup_pctx(dev);
5378}
5379
Deepak S38807742014-05-23 21:00:15 +05305380static void cherryview_enable_rps(struct drm_device *dev)
5381{
5382 struct drm_i915_private *dev_priv = dev->dev_private;
5383 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05305384 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05305385 int i;
5386
5387 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5388
5389 gtfifodbg = I915_READ(GTFIFODBG);
5390 if (gtfifodbg) {
5391 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5392 gtfifodbg);
5393 I915_WRITE(GTFIFODBG, gtfifodbg);
5394 }
5395
5396 cherryview_check_pctx(dev_priv);
5397
5398 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5399 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005400 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305401
Ville Syrjälä160614a2015-01-19 13:50:47 +02005402 /* Disable RC states. */
5403 I915_WRITE(GEN6_RC_CONTROL, 0);
5404
Deepak S38807742014-05-23 21:00:15 +05305405 /* 2a: Program RC6 thresholds.*/
5406 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5407 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5408 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5409
5410 for_each_ring(ring, dev_priv, i)
5411 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5412 I915_WRITE(GEN6_RC_SLEEP, 0);
5413
Deepak Sf4f71c72015-03-28 15:23:35 +05305414 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5415 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05305416
5417 /* allows RC6 residency counter to work */
5418 I915_WRITE(VLV_COUNTER_CONTROL,
5419 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5420 VLV_MEDIA_RC6_COUNT_EN |
5421 VLV_RENDER_RC6_COUNT_EN));
5422
5423 /* For now we assume BIOS is allocating and populating the PCBR */
5424 pcbr = I915_READ(VLV_PCBR);
5425
Deepak S38807742014-05-23 21:00:15 +05305426 /* 3: Enable RC6 */
5427 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5428 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02005429 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05305430
5431 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5432
Deepak S2b6b3a02014-05-27 15:59:30 +05305433 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02005434 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05305435 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5436 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5437 I915_WRITE(GEN6_RP_UP_EI, 66000);
5438 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5439
5440 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5441
5442 /* 5: Enable RPS */
5443 I915_WRITE(GEN6_RP_CONTROL,
5444 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02005445 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05305446 GEN6_RP_ENABLE |
5447 GEN6_RP_UP_BUSY_AVG |
5448 GEN6_RP_DOWN_IDLE_AVG);
5449
Deepak S3ef62342015-04-29 08:36:24 +05305450 /* Setting Fixed Bias */
5451 val = VLV_OVERRIDE_EN |
5452 VLV_SOC_TDP_EN |
5453 CHV_BIAS_CPU_50_SOC_50;
5454 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5455
Deepak S2b6b3a02014-05-27 15:59:30 +05305456 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5457
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005458 /* RPS code assumes GPLL is used */
5459 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5460
Jani Nikula742f4912015-09-03 11:16:09 +03005461 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05305462 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5463
5464 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5465 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005466 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305467 dev_priv->rps.cur_freq);
5468
5469 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005470 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05305471 dev_priv->rps.efficient_freq);
5472
5473 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5474
Mika Kuoppala59bad942015-01-16 11:34:40 +02005475 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05305476}
5477
Jesse Barnes0a073b82013-04-17 15:54:58 -07005478static void valleyview_enable_rps(struct drm_device *dev)
5479{
5480 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005481 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07005482 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005483 int i;
5484
5485 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5486
Imre Deakae484342014-03-31 15:10:44 +03005487 valleyview_check_pctx(dev_priv);
5488
Jesse Barnes0a073b82013-04-17 15:54:58 -07005489 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07005490 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5491 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005492 I915_WRITE(GTFIFODBG, gtfifodbg);
5493 }
5494
Deepak Sc8d9a592013-11-23 14:55:42 +05305495 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005496 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005497
Ville Syrjälä160614a2015-01-19 13:50:47 +02005498 /* Disable RC states. */
5499 I915_WRITE(GEN6_RC_CONTROL, 0);
5500
Ville Syrjäläcad725f2015-01-19 13:50:48 +02005501 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005502 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5503 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5504 I915_WRITE(GEN6_RP_UP_EI, 66000);
5505 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5506
5507 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5508
5509 I915_WRITE(GEN6_RP_CONTROL,
5510 GEN6_RP_MEDIA_TURBO |
5511 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5512 GEN6_RP_MEDIA_IS_GFX |
5513 GEN6_RP_ENABLE |
5514 GEN6_RP_UP_BUSY_AVG |
5515 GEN6_RP_DOWN_IDLE_CONT);
5516
5517 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5518 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5519 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5520
5521 for_each_ring(ring, dev_priv, i)
5522 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5523
Jesse Barnes2f0aa302013-11-15 09:32:11 -08005524 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005525
5526 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07005527 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04005528 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5529 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07005530 VLV_MEDIA_RC6_COUNT_EN |
5531 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04005532
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005533 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08005534 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07005535
5536 intel_print_rc6_info(dev, rc6_mode);
5537
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07005538 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005539
Deepak S3ef62342015-04-29 08:36:24 +05305540 /* Setting Fixed Bias */
5541 val = VLV_OVERRIDE_EN |
5542 VLV_SOC_TDP_EN |
5543 VLV_BIAS_CPU_125_SOC_875;
5544 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5545
Jani Nikula64936252013-05-22 15:36:20 +03005546 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005547
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02005548 /* RPS code assumes GPLL is used */
5549 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5550
Jani Nikula742f4912015-09-03 11:16:09 +03005551 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07005552 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5553
Ben Widawskyb39fb292014-03-19 18:31:11 -07005554 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005555 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005556 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005557 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005558
Ville Syrjälä73008b92013-06-25 19:21:01 +03005559 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005560 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005561 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005562
Ben Widawskyb39fb292014-03-19 18:31:11 -07005563 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005564
Mika Kuoppala59bad942015-01-16 11:34:40 +02005565 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005566}
5567
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005568static unsigned long intel_pxfreq(u32 vidfreq)
5569{
5570 unsigned long freq;
5571 int div = (vidfreq & 0x3f0000) >> 16;
5572 int post = (vidfreq & 0x3000) >> 12;
5573 int pre = (vidfreq & 0x7);
5574
5575 if (!pre)
5576 return 0;
5577
5578 freq = ((div * 133333) / ((1<<post) * pre));
5579
5580 return freq;
5581}
5582
Daniel Vettereb48eb02012-04-26 23:28:12 +02005583static const struct cparams {
5584 u16 i;
5585 u16 t;
5586 u16 m;
5587 u16 c;
5588} cparams[] = {
5589 { 1, 1333, 301, 28664 },
5590 { 1, 1066, 294, 24460 },
5591 { 1, 800, 294, 25192 },
5592 { 0, 1333, 276, 27605 },
5593 { 0, 1066, 276, 27605 },
5594 { 0, 800, 231, 23784 },
5595};
5596
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005597static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005598{
5599 u64 total_count, diff, ret;
5600 u32 count1, count2, count3, m = 0, c = 0;
5601 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5602 int i;
5603
Daniel Vetter02d71952012-08-09 16:44:54 +02005604 assert_spin_locked(&mchdev_lock);
5605
Daniel Vetter20e4d402012-08-08 23:35:39 +02005606 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005607
5608 /* Prevent division-by-zero if we are asking too fast.
5609 * Also, we don't get interesting results if we are polling
5610 * faster than once in 10ms, so just return the saved value
5611 * in such cases.
5612 */
5613 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005614 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005615
5616 count1 = I915_READ(DMIEC);
5617 count2 = I915_READ(DDREC);
5618 count3 = I915_READ(CSIEC);
5619
5620 total_count = count1 + count2 + count3;
5621
5622 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005623 if (total_count < dev_priv->ips.last_count1) {
5624 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005625 diff += total_count;
5626 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005627 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005628 }
5629
5630 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005631 if (cparams[i].i == dev_priv->ips.c_m &&
5632 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005633 m = cparams[i].m;
5634 c = cparams[i].c;
5635 break;
5636 }
5637 }
5638
5639 diff = div_u64(diff, diff1);
5640 ret = ((m * diff) + c);
5641 ret = div_u64(ret, 10);
5642
Daniel Vetter20e4d402012-08-08 23:35:39 +02005643 dev_priv->ips.last_count1 = total_count;
5644 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005645
Daniel Vetter20e4d402012-08-08 23:35:39 +02005646 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005647
5648 return ret;
5649}
5650
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005651unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5652{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005653 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005654 unsigned long val;
5655
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005656 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005657 return 0;
5658
5659 spin_lock_irq(&mchdev_lock);
5660
5661 val = __i915_chipset_val(dev_priv);
5662
5663 spin_unlock_irq(&mchdev_lock);
5664
5665 return val;
5666}
5667
Daniel Vettereb48eb02012-04-26 23:28:12 +02005668unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5669{
5670 unsigned long m, x, b;
5671 u32 tsfs;
5672
5673 tsfs = I915_READ(TSFS);
5674
5675 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5676 x = I915_READ8(TR1);
5677
5678 b = tsfs & TSFS_INTR_MASK;
5679
5680 return ((m * x) / 127) - b;
5681}
5682
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005683static int _pxvid_to_vd(u8 pxvid)
5684{
5685 if (pxvid == 0)
5686 return 0;
5687
5688 if (pxvid >= 8 && pxvid < 31)
5689 pxvid = 31;
5690
5691 return (pxvid + 2) * 125;
5692}
5693
5694static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005695{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005696 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005697 const int vd = _pxvid_to_vd(pxvid);
5698 const int vm = vd - 1125;
5699
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005700 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005701 return vm > 0 ? vm : 0;
5702
5703 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005704}
5705
Daniel Vetter02d71952012-08-09 16:44:54 +02005706static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005707{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005708 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005709 u32 count;
5710
Daniel Vetter02d71952012-08-09 16:44:54 +02005711 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005712
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005713 now = ktime_get_raw_ns();
5714 diffms = now - dev_priv->ips.last_time2;
5715 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005716
5717 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005718 if (!diffms)
5719 return;
5720
5721 count = I915_READ(GFXEC);
5722
Daniel Vetter20e4d402012-08-08 23:35:39 +02005723 if (count < dev_priv->ips.last_count2) {
5724 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005725 diff += count;
5726 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005727 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005728 }
5729
Daniel Vetter20e4d402012-08-08 23:35:39 +02005730 dev_priv->ips.last_count2 = count;
5731 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005732
5733 /* More magic constants... */
5734 diff = diff * 1181;
5735 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005736 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005737}
5738
Daniel Vetter02d71952012-08-09 16:44:54 +02005739void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5740{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005741 struct drm_device *dev = dev_priv->dev;
5742
5743 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005744 return;
5745
Daniel Vetter92703882012-08-09 16:46:01 +02005746 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005747
5748 __i915_update_gfx_val(dev_priv);
5749
Daniel Vetter92703882012-08-09 16:46:01 +02005750 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005751}
5752
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005753static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005754{
5755 unsigned long t, corr, state1, corr2, state2;
5756 u32 pxvid, ext_v;
5757
Daniel Vetter02d71952012-08-09 16:44:54 +02005758 assert_spin_locked(&mchdev_lock);
5759
Ville Syrjälä616847e2015-09-18 20:03:19 +03005760 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005761 pxvid = (pxvid >> 24) & 0x7f;
5762 ext_v = pvid_to_extvid(dev_priv, pxvid);
5763
5764 state1 = ext_v;
5765
5766 t = i915_mch_val(dev_priv);
5767
5768 /* Revel in the empirically derived constants */
5769
5770 /* Correction factor in 1/100000 units */
5771 if (t > 80)
5772 corr = ((t * 2349) + 135940);
5773 else if (t >= 50)
5774 corr = ((t * 964) + 29317);
5775 else /* < 50 */
5776 corr = ((t * 301) + 1004);
5777
5778 corr = corr * ((150142 * state1) / 10000 - 78642);
5779 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005780 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005781
5782 state2 = (corr2 * state1) / 10000;
5783 state2 /= 100; /* convert to mW */
5784
Daniel Vetter02d71952012-08-09 16:44:54 +02005785 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005786
Daniel Vetter20e4d402012-08-08 23:35:39 +02005787 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005788}
5789
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005790unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5791{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005792 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005793 unsigned long val;
5794
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005795 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005796 return 0;
5797
5798 spin_lock_irq(&mchdev_lock);
5799
5800 val = __i915_gfx_val(dev_priv);
5801
5802 spin_unlock_irq(&mchdev_lock);
5803
5804 return val;
5805}
5806
Daniel Vettereb48eb02012-04-26 23:28:12 +02005807/**
5808 * i915_read_mch_val - return value for IPS use
5809 *
5810 * Calculate and return a value for the IPS driver to use when deciding whether
5811 * we have thermal and power headroom to increase CPU or GPU power budget.
5812 */
5813unsigned long i915_read_mch_val(void)
5814{
5815 struct drm_i915_private *dev_priv;
5816 unsigned long chipset_val, graphics_val, ret = 0;
5817
Daniel Vetter92703882012-08-09 16:46:01 +02005818 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005819 if (!i915_mch_dev)
5820 goto out_unlock;
5821 dev_priv = i915_mch_dev;
5822
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005823 chipset_val = __i915_chipset_val(dev_priv);
5824 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005825
5826 ret = chipset_val + graphics_val;
5827
5828out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005829 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005830
5831 return ret;
5832}
5833EXPORT_SYMBOL_GPL(i915_read_mch_val);
5834
5835/**
5836 * i915_gpu_raise - raise GPU frequency limit
5837 *
5838 * Raise the limit; IPS indicates we have thermal headroom.
5839 */
5840bool i915_gpu_raise(void)
5841{
5842 struct drm_i915_private *dev_priv;
5843 bool ret = true;
5844
Daniel Vetter92703882012-08-09 16:46:01 +02005845 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005846 if (!i915_mch_dev) {
5847 ret = false;
5848 goto out_unlock;
5849 }
5850 dev_priv = i915_mch_dev;
5851
Daniel Vetter20e4d402012-08-08 23:35:39 +02005852 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5853 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005854
5855out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005856 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005857
5858 return ret;
5859}
5860EXPORT_SYMBOL_GPL(i915_gpu_raise);
5861
5862/**
5863 * i915_gpu_lower - lower GPU frequency limit
5864 *
5865 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5866 * frequency maximum.
5867 */
5868bool i915_gpu_lower(void)
5869{
5870 struct drm_i915_private *dev_priv;
5871 bool ret = true;
5872
Daniel Vetter92703882012-08-09 16:46:01 +02005873 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005874 if (!i915_mch_dev) {
5875 ret = false;
5876 goto out_unlock;
5877 }
5878 dev_priv = i915_mch_dev;
5879
Daniel Vetter20e4d402012-08-08 23:35:39 +02005880 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5881 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005882
5883out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005884 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005885
5886 return ret;
5887}
5888EXPORT_SYMBOL_GPL(i915_gpu_lower);
5889
5890/**
5891 * i915_gpu_busy - indicate GPU business to IPS
5892 *
5893 * Tell the IPS driver whether or not the GPU is busy.
5894 */
5895bool i915_gpu_busy(void)
5896{
5897 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005898 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005899 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005900 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005901
Daniel Vetter92703882012-08-09 16:46:01 +02005902 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005903 if (!i915_mch_dev)
5904 goto out_unlock;
5905 dev_priv = i915_mch_dev;
5906
Chris Wilsonf047e392012-07-21 12:31:41 +01005907 for_each_ring(ring, dev_priv, i)
5908 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005909
5910out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005911 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005912
5913 return ret;
5914}
5915EXPORT_SYMBOL_GPL(i915_gpu_busy);
5916
5917/**
5918 * i915_gpu_turbo_disable - disable graphics turbo
5919 *
5920 * Disable graphics turbo by resetting the max frequency and setting the
5921 * current frequency to the default.
5922 */
5923bool i915_gpu_turbo_disable(void)
5924{
5925 struct drm_i915_private *dev_priv;
5926 bool ret = true;
5927
Daniel Vetter92703882012-08-09 16:46:01 +02005928 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005929 if (!i915_mch_dev) {
5930 ret = false;
5931 goto out_unlock;
5932 }
5933 dev_priv = i915_mch_dev;
5934
Daniel Vetter20e4d402012-08-08 23:35:39 +02005935 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005936
Daniel Vetter20e4d402012-08-08 23:35:39 +02005937 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005938 ret = false;
5939
5940out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005941 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005942
5943 return ret;
5944}
5945EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5946
5947/**
5948 * Tells the intel_ips driver that the i915 driver is now loaded, if
5949 * IPS got loaded first.
5950 *
5951 * This awkward dance is so that neither module has to depend on the
5952 * other in order for IPS to do the appropriate communication of
5953 * GPU turbo limits to i915.
5954 */
5955static void
5956ips_ping_for_i915_load(void)
5957{
5958 void (*link)(void);
5959
5960 link = symbol_get(ips_link_to_i915_driver);
5961 if (link) {
5962 link();
5963 symbol_put(ips_link_to_i915_driver);
5964 }
5965}
5966
5967void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5968{
Daniel Vetter02d71952012-08-09 16:44:54 +02005969 /* We only register the i915 ips part with intel-ips once everything is
5970 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005971 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005972 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005973 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005974
5975 ips_ping_for_i915_load();
5976}
5977
5978void intel_gpu_ips_teardown(void)
5979{
Daniel Vetter92703882012-08-09 16:46:01 +02005980 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005981 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005982 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005983}
Deepak S76c3552f2014-01-30 23:08:16 +05305984
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005985static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005986{
5987 struct drm_i915_private *dev_priv = dev->dev_private;
5988 u32 lcfuse;
5989 u8 pxw[16];
5990 int i;
5991
5992 /* Disable to program */
5993 I915_WRITE(ECR, 0);
5994 POSTING_READ(ECR);
5995
5996 /* Program energy weights for various events */
5997 I915_WRITE(SDEW, 0x15040d00);
5998 I915_WRITE(CSIEW0, 0x007f0000);
5999 I915_WRITE(CSIEW1, 0x1e220004);
6000 I915_WRITE(CSIEW2, 0x04000004);
6001
6002 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006003 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006004 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006005 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006006
6007 /* Program P-state weights to account for frequency power adjustment */
6008 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006009 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006010 unsigned long freq = intel_pxfreq(pxvidfreq);
6011 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6012 PXVFREQ_PX_SHIFT;
6013 unsigned long val;
6014
6015 val = vid * vid;
6016 val *= (freq / 1000);
6017 val *= 255;
6018 val /= (127*127*900);
6019 if (val > 0xff)
6020 DRM_ERROR("bad pxval: %ld\n", val);
6021 pxw[i] = val;
6022 }
6023 /* Render standby states get 0 weight */
6024 pxw[14] = 0;
6025 pxw[15] = 0;
6026
6027 for (i = 0; i < 4; i++) {
6028 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6029 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006030 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006031 }
6032
6033 /* Adjust magic regs to magic values (more experimental results) */
6034 I915_WRITE(OGW0, 0);
6035 I915_WRITE(OGW1, 0);
6036 I915_WRITE(EG0, 0x00007f00);
6037 I915_WRITE(EG1, 0x0000000e);
6038 I915_WRITE(EG2, 0x000e0000);
6039 I915_WRITE(EG3, 0x68000300);
6040 I915_WRITE(EG4, 0x42000000);
6041 I915_WRITE(EG5, 0x00140031);
6042 I915_WRITE(EG6, 0);
6043 I915_WRITE(EG7, 0);
6044
6045 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006046 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006047
6048 /* Enable PMON + select events */
6049 I915_WRITE(ECR, 0x80000019);
6050
6051 lcfuse = I915_READ(LCFUSE02);
6052
Daniel Vetter20e4d402012-08-08 23:35:39 +02006053 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006054}
6055
Imre Deakae484342014-03-31 15:10:44 +03006056void intel_init_gt_powersave(struct drm_device *dev)
6057{
Imre Deakb268c692015-12-15 20:10:31 +02006058 struct drm_i915_private *dev_priv = dev->dev_private;
6059
Imre Deake6069ca2014-04-18 16:01:02 +03006060 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
Imre Deakb268c692015-12-15 20:10:31 +02006061 /*
6062 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6063 * requirement.
6064 */
6065 if (!i915.enable_rc6) {
6066 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6067 intel_runtime_pm_get(dev_priv);
6068 }
Imre Deake6069ca2014-04-18 16:01:02 +03006069
Deepak S38807742014-05-23 21:00:15 +05306070 if (IS_CHERRYVIEW(dev))
6071 cherryview_init_gt_powersave(dev);
6072 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006073 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03006074}
6075
6076void intel_cleanup_gt_powersave(struct drm_device *dev)
6077{
Imre Deakb268c692015-12-15 20:10:31 +02006078 struct drm_i915_private *dev_priv = dev->dev_private;
6079
Deepak S38807742014-05-23 21:00:15 +05306080 if (IS_CHERRYVIEW(dev))
6081 return;
6082 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03006083 valleyview_cleanup_gt_powersave(dev);
Imre Deakb268c692015-12-15 20:10:31 +02006084
6085 if (!i915.enable_rc6)
6086 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006087}
6088
Imre Deakdbea3ce2014-12-15 18:59:28 +02006089static void gen6_suspend_rps(struct drm_device *dev)
6090{
6091 struct drm_i915_private *dev_priv = dev->dev_private;
6092
6093 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6094
Akash Goel4c2a8892015-03-06 11:07:24 +05306095 gen6_disable_rps_interrupts(dev);
Imre Deakdbea3ce2014-12-15 18:59:28 +02006096}
6097
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006098/**
6099 * intel_suspend_gt_powersave - suspend PM work and helper threads
6100 * @dev: drm device
6101 *
6102 * We don't want to disable RC6 or other features here, we just want
6103 * to make sure any work we've queued has finished and won't bother
6104 * us while we're suspended.
6105 */
6106void intel_suspend_gt_powersave(struct drm_device *dev)
6107{
6108 struct drm_i915_private *dev_priv = dev->dev_private;
6109
Imre Deakd4d70aa2014-11-19 15:30:04 +02006110 if (INTEL_INFO(dev)->gen < 6)
6111 return;
6112
Imre Deakdbea3ce2014-12-15 18:59:28 +02006113 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05306114
6115 /* Force GPU to min freq during suspend */
6116 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006117}
6118
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006119void intel_disable_gt_powersave(struct drm_device *dev)
6120{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006121 struct drm_i915_private *dev_priv = dev->dev_private;
6122
Daniel Vetter930ebb42012-06-29 23:32:16 +02006123 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006124 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05306125 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02006126 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03006127
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006128 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00006129 if (INTEL_INFO(dev)->gen >= 9)
6130 gen9_disable_rps(dev);
6131 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05306132 cherryview_disable_rps(dev);
6133 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07006134 valleyview_disable_rps(dev);
6135 else
6136 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02006137
Chris Wilsonc0951f02013-10-10 21:58:50 +01006138 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006139 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02006140 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006141}
6142
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006143static void intel_gen6_powersave_work(struct work_struct *work)
6144{
6145 struct drm_i915_private *dev_priv =
6146 container_of(work, struct drm_i915_private,
6147 rps.delayed_resume_work.work);
6148 struct drm_device *dev = dev_priv->dev;
6149
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006150 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006151
Akash Goel4c2a8892015-03-06 11:07:24 +05306152 gen6_reset_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02006153
Deepak S38807742014-05-23 21:00:15 +05306154 if (IS_CHERRYVIEW(dev)) {
6155 cherryview_enable_rps(dev);
6156 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07006157 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006158 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00006159 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00006160 gen9_enable_rps(dev);
Rodrigo Vivief11bdb2015-10-28 04:16:45 -07006161 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
Akash Goelcc017fb2015-06-29 14:50:21 +05306162 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07006163 } else if (IS_BROADWELL(dev)) {
6164 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006165 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006166 } else {
6167 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03006168 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006169 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00006170
6171 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6172 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6173
6174 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6175 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6176
Chris Wilsonc0951f02013-10-10 21:58:50 +01006177 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02006178
Akash Goel4c2a8892015-03-06 11:07:24 +05306179 gen6_enable_rps_interrupts(dev);
Imre Deak3cc134e2014-11-19 15:30:03 +02006180
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006181 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03006182
6183 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006184}
6185
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006186void intel_enable_gt_powersave(struct drm_device *dev)
6187{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006188 struct drm_i915_private *dev_priv = dev->dev_private;
6189
Yu Zhangf61018b2015-02-10 19:05:52 +08006190 /* Powersaving is controlled by the host when inside a VM */
6191 if (intel_vgpu_active(dev))
6192 return;
6193
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006194 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03006195 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006196 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006197 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03006198 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05306199 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006200 /*
6201 * PCU communication is slow and this doesn't need to be
6202 * done at any specific time, so do this out of our fast path
6203 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03006204 *
6205 * We depend on the HW RC6 power context save/restore
6206 * mechanism when entering D3 through runtime PM suspend. So
6207 * disable RPM until RPS/RC6 is properly setup. We can only
6208 * get here via the driver load/system resume/runtime resume
6209 * paths, so the _noresume version is enough (and in case of
6210 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006211 */
Imre Deakc6df39b2014-04-14 20:24:29 +03006212 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6213 round_jiffies_up_relative(HZ)))
6214 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006215 }
6216}
6217
Imre Deakc6df39b2014-04-14 20:24:29 +03006218void intel_reset_gt_powersave(struct drm_device *dev)
6219{
6220 struct drm_i915_private *dev_priv = dev->dev_private;
6221
Imre Deakdbea3ce2014-12-15 18:59:28 +02006222 if (INTEL_INFO(dev)->gen < 6)
6223 return;
6224
6225 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03006226 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03006227}
6228
Daniel Vetter3107bd42012-10-31 22:52:31 +01006229static void ibx_init_clock_gating(struct drm_device *dev)
6230{
6231 struct drm_i915_private *dev_priv = dev->dev_private;
6232
6233 /*
6234 * On Ibex Peak and Cougar Point, we need to disable clock
6235 * gating for the panel power sequencer or it will fail to
6236 * start up when no ports are active.
6237 */
6238 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6239}
6240
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006241static void g4x_disable_trickle_feed(struct drm_device *dev)
6242{
6243 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006244 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006245
Damien Lespiau055e3932014-08-18 13:49:10 +01006246 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006247 I915_WRITE(DSPCNTR(pipe),
6248 I915_READ(DSPCNTR(pipe)) |
6249 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03006250
6251 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6252 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006253 }
6254}
6255
Ville Syrjälä017636c2013-12-05 15:51:37 +02006256static void ilk_init_lp_watermarks(struct drm_device *dev)
6257{
6258 struct drm_i915_private *dev_priv = dev->dev_private;
6259
6260 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6261 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6262 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6263
6264 /*
6265 * Don't touch WM1S_LP_EN here.
6266 * Doing so could cause underruns.
6267 */
6268}
6269
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006270static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006271{
6272 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006273 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006274
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01006275 /*
6276 * Required for FBC
6277 * WaFbcDisableDpfcClockGating:ilk
6278 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006279 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6280 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6281 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006282
6283 I915_WRITE(PCH_3DCGDIS0,
6284 MARIUNIT_CLOCK_GATE_DISABLE |
6285 SVSMUNIT_CLOCK_GATE_DISABLE);
6286 I915_WRITE(PCH_3DCGDIS1,
6287 VFMUNIT_CLOCK_GATE_DISABLE);
6288
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006289 /*
6290 * According to the spec the following bits should be set in
6291 * order to enable memory self-refresh
6292 * The bit 22/21 of 0x42004
6293 * The bit 5 of 0x42020
6294 * The bit 15 of 0x45000
6295 */
6296 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6297 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6298 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006299 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006300 I915_WRITE(DISP_ARB_CTL,
6301 (I915_READ(DISP_ARB_CTL) |
6302 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02006303
6304 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006305
6306 /*
6307 * Based on the document from hardware guys the following bits
6308 * should be set unconditionally in order to enable FBC.
6309 * The bit 22 of 0x42000
6310 * The bit 22 of 0x42004
6311 * The bit 7,8,9 of 0x42020.
6312 */
6313 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01006314 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006315 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6316 I915_READ(ILK_DISPLAY_CHICKEN1) |
6317 ILK_FBCQ_DIS);
6318 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6319 I915_READ(ILK_DISPLAY_CHICKEN2) |
6320 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006321 }
6322
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01006323 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6324
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006325 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6326 I915_READ(ILK_DISPLAY_CHICKEN2) |
6327 ILK_ELPIN_409_SELECT);
6328 I915_WRITE(_3D_CHICKEN2,
6329 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6330 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02006331
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006332 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02006333 I915_WRITE(CACHE_MODE_0,
6334 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01006335
Akash Goel4e046322014-04-04 17:14:38 +05306336 /* WaDisable_RenderCache_OperationalFlush:ilk */
6337 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6338
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006339 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03006340
Daniel Vetter3107bd42012-10-31 22:52:31 +01006341 ibx_init_clock_gating(dev);
6342}
6343
6344static void cpt_init_clock_gating(struct drm_device *dev)
6345{
6346 struct drm_i915_private *dev_priv = dev->dev_private;
6347 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006348 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01006349
6350 /*
6351 * On Ibex Peak and Cougar Point, we need to disable clock
6352 * gating for the panel power sequencer or it will fail to
6353 * start up when no ports are active.
6354 */
Jesse Barnescd664072013-10-02 10:34:19 -07006355 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6356 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6357 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006358 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6359 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01006360 /* The below fixes the weird display corruption, a few pixels shifted
6361 * downward, on (only) LVDS of some HP laptops with IVY.
6362 */
Damien Lespiau055e3932014-08-18 13:49:10 +01006363 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006364 val = I915_READ(TRANS_CHICKEN2(pipe));
6365 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6366 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006367 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006368 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03006369 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6370 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6371 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03006372 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6373 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01006374 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01006375 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01006376 I915_WRITE(TRANS_CHICKEN1(pipe),
6377 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6378 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006379}
6380
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006381static void gen6_check_mch_setup(struct drm_device *dev)
6382{
6383 struct drm_i915_private *dev_priv = dev->dev_private;
6384 uint32_t tmp;
6385
6386 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02006387 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6388 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6389 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006390}
6391
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006392static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006393{
6394 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01006395 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006396
Damien Lespiau231e54f2012-10-19 17:55:41 +01006397 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006398
6399 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6400 I915_READ(ILK_DISPLAY_CHICKEN2) |
6401 ILK_ELPIN_409_SELECT);
6402
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006403 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01006404 I915_WRITE(_3D_CHICKEN,
6405 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6406
Akash Goel4e046322014-04-04 17:14:38 +05306407 /* WaDisable_RenderCache_OperationalFlush:snb */
6408 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6409
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006410 /*
6411 * BSpec recoomends 8x4 when MSAA is used,
6412 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006413 *
6414 * Note that PS/WM thread counts depend on the WIZ hashing
6415 * disable bit, which we don't touch here, but it's good
6416 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006417 */
6418 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006419 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02006420
Ville Syrjälä017636c2013-12-05 15:51:37 +02006421 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006422
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006423 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02006424 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006425
6426 I915_WRITE(GEN6_UCGCTL1,
6427 I915_READ(GEN6_UCGCTL1) |
6428 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6429 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6430
6431 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6432 * gating disable must be set. Failure to set it results in
6433 * flickering pixels due to Z write ordering failures after
6434 * some amount of runtime in the Mesa "fire" demo, and Unigine
6435 * Sanctuary and Tropics, and apparently anything else with
6436 * alpha test or pixel discard.
6437 *
6438 * According to the spec, bit 11 (RCCUNIT) must also be set,
6439 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006440 *
Ville Syrjäläef593182014-01-22 21:32:47 +02006441 * WaDisableRCCUnitClockGating:snb
6442 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006443 */
6444 I915_WRITE(GEN6_UCGCTL2,
6445 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6446 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6447
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02006448 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02006449 I915_WRITE(_3D_CHICKEN3,
6450 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006451
6452 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02006453 * Bspec says:
6454 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6455 * 3DSTATE_SF number of SF output attributes is more than 16."
6456 */
6457 I915_WRITE(_3D_CHICKEN3,
6458 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6459
6460 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006461 * According to the spec the following bits should be
6462 * set in order to enable memory self-refresh and fbc:
6463 * The bit21 and bit22 of 0x42000
6464 * The bit21 and bit22 of 0x42004
6465 * The bit5 and bit7 of 0x42020
6466 * The bit14 of 0x70180
6467 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01006468 *
6469 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006470 */
6471 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6472 I915_READ(ILK_DISPLAY_CHICKEN1) |
6473 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6474 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6475 I915_READ(ILK_DISPLAY_CHICKEN2) |
6476 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01006477 I915_WRITE(ILK_DSPCLK_GATE_D,
6478 I915_READ(ILK_DSPCLK_GATE_D) |
6479 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6480 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006481
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006482 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07006483
Daniel Vetter3107bd42012-10-31 22:52:31 +01006484 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006485
6486 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006487}
6488
6489static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6490{
6491 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6492
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006493 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02006494 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02006495 *
6496 * This actually overrides the dispatch
6497 * mode for all thread types.
6498 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006499 reg &= ~GEN7_FF_SCHED_MASK;
6500 reg |= GEN7_FF_TS_SCHED_HW;
6501 reg |= GEN7_FF_VS_SCHED_HW;
6502 reg |= GEN7_FF_DS_SCHED_HW;
6503
6504 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6505}
6506
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006507static void lpt_init_clock_gating(struct drm_device *dev)
6508{
6509 struct drm_i915_private *dev_priv = dev->dev_private;
6510
6511 /*
6512 * TODO: this bit should only be enabled when really needed, then
6513 * disabled when not needed anymore in order to save power.
6514 */
Ville Syrjäläc2699522015-08-27 23:55:59 +03006515 if (HAS_PCH_LPT_LP(dev))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006516 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6517 I915_READ(SOUTH_DSPCLK_GATE_D) |
6518 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006519
6520 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03006521 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6522 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03006523 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006524}
6525
Imre Deak7d708ee2013-04-17 14:04:50 +03006526static void lpt_suspend_hw(struct drm_device *dev)
6527{
6528 struct drm_i915_private *dev_priv = dev->dev_private;
6529
Ville Syrjäläc2699522015-08-27 23:55:59 +03006530 if (HAS_PCH_LPT_LP(dev)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03006531 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6532
6533 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6534 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6535 }
6536}
6537
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006538static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006539{
6540 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00006541 enum pipe pipe;
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006542 uint32_t misccpctl;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006543
Ville Syrjälä7ad0dba2015-05-19 20:32:55 +03006544 ilk_init_lp_watermarks(dev);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006545
Ben Widawskyab57fff2013-12-12 15:28:04 -08006546 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07006547 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006548
Ben Widawskyab57fff2013-12-12 15:28:04 -08006549 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006550 I915_WRITE(CHICKEN_PAR1_1,
6551 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6552
Ben Widawskyab57fff2013-12-12 15:28:04 -08006553 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01006554 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00006555 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006556 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006557 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006558 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006559
Ben Widawskyab57fff2013-12-12 15:28:04 -08006560 /* WaVSRefCountFullforceMissDisable:bdw */
6561 /* WaDSRefCountFullforceMissDisable:bdw */
6562 I915_WRITE(GEN7_FF_THREAD_MODE,
6563 I915_READ(GEN7_FF_THREAD_MODE) &
6564 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006565
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006566 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6567 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006568
6569 /* WaDisableSDEUnitClockGating:bdw */
6570 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6571 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006572
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03006573 /*
6574 * WaProgramL3SqcReg1Default:bdw
6575 * WaTempDisableDOPClkGating:bdw
6576 */
6577 misccpctl = I915_READ(GEN7_MISCCPCTL);
6578 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6579 I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
6580 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6581
Ville Syrjälä6d50b062015-05-19 20:32:57 +03006582 /*
6583 * WaGttCachingOffByDefault:bdw
6584 * GTT cache may not work with big pages, so if those
6585 * are ever enabled GTT cache may need to be disabled.
6586 */
6587 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6588
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006589 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006590}
6591
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006592static void haswell_init_clock_gating(struct drm_device *dev)
6593{
6594 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006595
Ville Syrjälä017636c2013-12-05 15:51:37 +02006596 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006597
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006598 /* L3 caching of data atomics doesn't work -- disable it. */
6599 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6600 I915_WRITE(HSW_ROW_CHICKEN3,
6601 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6602
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006603 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006604 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6605 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6606 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6607
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006608 /* WaVSRefCountFullforceMissDisable:hsw */
6609 I915_WRITE(GEN7_FF_THREAD_MODE,
6610 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006611
Akash Goel4e046322014-04-04 17:14:38 +05306612 /* WaDisable_RenderCache_OperationalFlush:hsw */
6613 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6614
Chia-I Wufe27c602014-01-28 13:29:33 +08006615 /* enable HiZ Raw Stall Optimization */
6616 I915_WRITE(CACHE_MODE_0_GEN7,
6617 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6618
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006619 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006620 I915_WRITE(CACHE_MODE_1,
6621 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006622
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006623 /*
6624 * BSpec recommends 8x4 when MSAA is used,
6625 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006626 *
6627 * Note that PS/WM thread counts depend on the WIZ hashing
6628 * disable bit, which we don't touch here, but it's good
6629 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006630 */
6631 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006632 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006633
Kenneth Graunke94411592014-12-31 16:23:00 -08006634 /* WaSampleCChickenBitEnable:hsw */
6635 I915_WRITE(HALF_SLICE_CHICKEN3,
6636 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6637
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006638 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006639 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6640
Paulo Zanoni90a88642013-05-03 17:23:45 -03006641 /* WaRsPkgCStateDisplayPMReq:hsw */
6642 I915_WRITE(CHICKEN_PAR1_1,
6643 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006644
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006645 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006646}
6647
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006648static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006649{
6650 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006651 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006652
Ville Syrjälä017636c2013-12-05 15:51:37 +02006653 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006654
Damien Lespiau231e54f2012-10-19 17:55:41 +01006655 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006656
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006657 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006658 I915_WRITE(_3D_CHICKEN3,
6659 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6660
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006661 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006662 I915_WRITE(IVB_CHICKEN3,
6663 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6664 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6665
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006666 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006667 if (IS_IVB_GT1(dev))
6668 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6669 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006670
Akash Goel4e046322014-04-04 17:14:38 +05306671 /* WaDisable_RenderCache_OperationalFlush:ivb */
6672 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6673
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006674 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006675 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6676 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6677
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006678 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006679 I915_WRITE(GEN7_L3CNTLREG1,
6680 GEN7_WA_FOR_GEN7_L3_CONTROL);
6681 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006682 GEN7_WA_L3_CHICKEN_MODE);
6683 if (IS_IVB_GT1(dev))
6684 I915_WRITE(GEN7_ROW_CHICKEN2,
6685 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006686 else {
6687 /* must write both registers */
6688 I915_WRITE(GEN7_ROW_CHICKEN2,
6689 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006690 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6691 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006692 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006693
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006694 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006695 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6696 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6697
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006698 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006699 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006700 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006701 */
6702 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006703 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006704
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006705 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006706 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6707 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6708 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6709
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006710 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006711
6712 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006713
Chris Wilson22721342014-03-04 09:41:43 +00006714 if (0) { /* causes HiZ corruption on ivb:gt1 */
6715 /* enable HiZ Raw Stall Optimization */
6716 I915_WRITE(CACHE_MODE_0_GEN7,
6717 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6718 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006719
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006720 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006721 I915_WRITE(CACHE_MODE_1,
6722 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006723
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006724 /*
6725 * BSpec recommends 8x4 when MSAA is used,
6726 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006727 *
6728 * Note that PS/WM thread counts depend on the WIZ hashing
6729 * disable bit, which we don't touch here, but it's good
6730 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006731 */
6732 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006733 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006734
Ben Widawsky20848222012-05-04 18:58:59 -07006735 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6736 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6737 snpcr |= GEN6_MBC_SNPCR_MED;
6738 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006739
Ben Widawskyab5c6082013-04-05 13:12:41 -07006740 if (!HAS_PCH_NOP(dev))
6741 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006742
6743 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006744}
6745
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006746static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6747{
6748 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6749
6750 /*
6751 * Disable trickle feed and enable pnd deadline calculation
6752 */
6753 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6754 I915_WRITE(CBR1_VLV, 0);
6755}
6756
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006757static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006758{
6759 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006760
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006761 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006762
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006763 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006764 I915_WRITE(_3D_CHICKEN3,
6765 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6766
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006767 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006768 I915_WRITE(IVB_CHICKEN3,
6769 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6770 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6771
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006772 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006773 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006774 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006775 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6776 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006777
Akash Goel4e046322014-04-04 17:14:38 +05306778 /* WaDisable_RenderCache_OperationalFlush:vlv */
6779 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6780
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006781 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006782 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6783 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6784
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006785 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006786 I915_WRITE(GEN7_ROW_CHICKEN2,
6787 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6788
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006789 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006790 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6791 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6792 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6793
Ville Syrjälä46680e02014-01-22 21:33:01 +02006794 gen7_setup_fixed_func_scheduler(dev_priv);
6795
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006796 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006797 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006798 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006799 */
6800 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006801 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006802
Akash Goelc98f5062014-03-24 23:00:07 +05306803 /* WaDisableL3Bank2xClockGate:vlv
6804 * Disabling L3 clock gating- MMIO 940c[25] = 1
6805 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6806 I915_WRITE(GEN7_UCGCTL4,
6807 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006808
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006809 /*
6810 * BSpec says this must be set, even though
6811 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6812 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006813 I915_WRITE(CACHE_MODE_1,
6814 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006815
6816 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006817 * BSpec recommends 8x4 when MSAA is used,
6818 * however in practice 16x4 seems fastest.
6819 *
6820 * Note that PS/WM thread counts depend on the WIZ hashing
6821 * disable bit, which we don't touch here, but it's good
6822 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6823 */
6824 I915_WRITE(GEN7_GT_MODE,
6825 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6826
6827 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006828 * WaIncreaseL3CreditsForVLVB0:vlv
6829 * This is the hardware default actually.
6830 */
6831 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6832
6833 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006834 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006835 * Disable clock gating on th GCFG unit to prevent a delay
6836 * in the reporting of vblank events.
6837 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006838 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006839}
6840
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006841static void cherryview_init_clock_gating(struct drm_device *dev)
6842{
6843 struct drm_i915_private *dev_priv = dev->dev_private;
6844
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006845 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006846
Ville Syrjälä232ce332014-04-09 13:28:35 +03006847 /* WaVSRefCountFullforceMissDisable:chv */
6848 /* WaDSRefCountFullforceMissDisable:chv */
6849 I915_WRITE(GEN7_FF_THREAD_MODE,
6850 I915_READ(GEN7_FF_THREAD_MODE) &
6851 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006852
6853 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6854 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6855 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006856
6857 /* WaDisableCSUnitClockGating:chv */
6858 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6859 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006860
6861 /* WaDisableSDEUnitClockGating:chv */
6862 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6863 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03006864
6865 /*
6866 * GTT cache may not work with big pages, so if those
6867 * are ever enabled GTT cache may need to be disabled.
6868 */
6869 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006870}
6871
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006872static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006873{
6874 struct drm_i915_private *dev_priv = dev->dev_private;
6875 uint32_t dspclk_gate;
6876
6877 I915_WRITE(RENCLK_GATE_D1, 0);
6878 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6879 GS_UNIT_CLOCK_GATE_DISABLE |
6880 CL_UNIT_CLOCK_GATE_DISABLE);
6881 I915_WRITE(RAMCLK_GATE_D, 0);
6882 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6883 OVRUNIT_CLOCK_GATE_DISABLE |
6884 OVCUNIT_CLOCK_GATE_DISABLE;
6885 if (IS_GM45(dev))
6886 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6887 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006888
6889 /* WaDisableRenderCachePipelinedFlush */
6890 I915_WRITE(CACHE_MODE_0,
6891 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006892
Akash Goel4e046322014-04-04 17:14:38 +05306893 /* WaDisable_RenderCache_OperationalFlush:g4x */
6894 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6895
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006896 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006897}
6898
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006899static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006900{
6901 struct drm_i915_private *dev_priv = dev->dev_private;
6902
6903 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6904 I915_WRITE(RENCLK_GATE_D2, 0);
6905 I915_WRITE(DSPCLK_GATE_D, 0);
6906 I915_WRITE(RAMCLK_GATE_D, 0);
6907 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006908 I915_WRITE(MI_ARB_STATE,
6909 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306910
6911 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6912 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006913}
6914
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006915static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006916{
6917 struct drm_i915_private *dev_priv = dev->dev_private;
6918
6919 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6920 I965_RCC_CLOCK_GATE_DISABLE |
6921 I965_RCPB_CLOCK_GATE_DISABLE |
6922 I965_ISC_CLOCK_GATE_DISABLE |
6923 I965_FBC_CLOCK_GATE_DISABLE);
6924 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006925 I915_WRITE(MI_ARB_STATE,
6926 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306927
6928 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6929 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006930}
6931
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006932static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006933{
6934 struct drm_i915_private *dev_priv = dev->dev_private;
6935 u32 dstate = I915_READ(D_STATE);
6936
6937 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6938 DSTATE_DOT_CLOCK_GATING;
6939 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006940
6941 if (IS_PINEVIEW(dev))
6942 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006943
6944 /* IIR "flip pending" means done if this bit is set */
6945 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006946
6947 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006948 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006949
6950 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6951 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006952
6953 I915_WRITE(MI_ARB_STATE,
6954 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006955}
6956
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006957static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006958{
6959 struct drm_i915_private *dev_priv = dev->dev_private;
6960
6961 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006962
6963 /* interrupts should cause a wake up from C3 */
6964 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6965 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006966
6967 I915_WRITE(MEM_MODE,
6968 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006969}
6970
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006971static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006972{
6973 struct drm_i915_private *dev_priv = dev->dev_private;
6974
6975 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006976
6977 I915_WRITE(MEM_MODE,
6978 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6979 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006980}
6981
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006982void intel_init_clock_gating(struct drm_device *dev)
6983{
6984 struct drm_i915_private *dev_priv = dev->dev_private;
6985
Damien Lespiauc57e3552015-02-09 19:33:05 +00006986 if (dev_priv->display.init_clock_gating)
6987 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006988}
6989
Imre Deak7d708ee2013-04-17 14:04:50 +03006990void intel_suspend_hw(struct drm_device *dev)
6991{
6992 if (HAS_PCH_LPT(dev))
6993 lpt_suspend_hw(dev);
6994}
6995
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006996/* Set up chip specific power management-related functions */
6997void intel_init_pm(struct drm_device *dev)
6998{
6999 struct drm_i915_private *dev_priv = dev->dev_private;
7000
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007001 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007002
Daniel Vetterc921aba2012-04-26 23:28:17 +02007003 /* For cxsr */
7004 if (IS_PINEVIEW(dev))
7005 i915_pineview_get_mem_freq(dev);
7006 else if (IS_GEN5(dev))
7007 i915_ironlake_get_mem_freq(dev);
7008
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007009 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00007010 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00007011 skl_setup_wm_latency(dev);
7012
Imre Deaka82abe42015-03-27 14:00:04 +02007013 if (IS_BROXTON(dev))
7014 dev_priv->display.init_clock_gating =
7015 bxt_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00007016 dev_priv->display.update_wm = skl_update_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05307017 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00007018 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007019
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007020 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7021 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7022 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7023 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roperbf220452016-01-19 11:43:04 -08007024 dev_priv->display.update_wm = ilk_update_wm;
Matt Roper86c8bbb2015-09-24 15:53:16 -07007025 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Roperbf220452016-01-19 11:43:04 -08007026 dev_priv->display.program_watermarks = ilk_program_watermarks;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007027 } else {
7028 DRM_DEBUG_KMS("Failed to read display plane latency. "
7029 "Disable CxSR\n");
7030 }
7031
7032 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007033 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007034 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007035 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007036 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007037 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007038 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007039 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02007040 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03007041 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007042 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03007043 vlv_setup_wm_latency(dev);
7044
7045 dev_priv->display.update_wm = vlv_update_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007046 dev_priv->display.init_clock_gating =
7047 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007048 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä26e1fe42015-06-24 22:00:06 +03007049 vlv_setup_wm_latency(dev);
7050
7051 dev_priv->display.update_wm = vlv_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007052 dev_priv->display.init_clock_gating =
7053 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007054 } else if (IS_PINEVIEW(dev)) {
7055 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7056 dev_priv->is_ddr3,
7057 dev_priv->fsb_freq,
7058 dev_priv->mem_freq)) {
7059 DRM_INFO("failed to find known CxSR latency "
7060 "(found ddr%s fsb freq %d, mem freq %d), "
7061 "disabling CxSR\n",
7062 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7063 dev_priv->fsb_freq, dev_priv->mem_freq);
7064 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03007065 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007066 dev_priv->display.update_wm = NULL;
7067 } else
7068 dev_priv->display.update_wm = pineview_update_wm;
7069 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7070 } else if (IS_G4X(dev)) {
7071 dev_priv->display.update_wm = g4x_update_wm;
7072 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7073 } else if (IS_GEN4(dev)) {
7074 dev_priv->display.update_wm = i965_update_wm;
7075 if (IS_CRESTLINE(dev))
7076 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7077 else if (IS_BROADWATER(dev))
7078 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7079 } else if (IS_GEN3(dev)) {
7080 dev_priv->display.update_wm = i9xx_update_wm;
7081 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7082 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007083 } else if (IS_GEN2(dev)) {
7084 if (INTEL_INFO(dev)->num_pipes == 1) {
7085 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007086 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007087 } else {
7088 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007089 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02007090 }
7091
7092 if (IS_I85X(dev) || IS_I865G(dev))
7093 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7094 else
7095 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7096 } else {
7097 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007098 }
7099}
7100
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007101int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007102{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007103 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007104
7105 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7106 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7107 return -EAGAIN;
7108 }
7109
7110 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00007111 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07007112 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7113
7114 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7115 500)) {
7116 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7117 return -ETIMEDOUT;
7118 }
7119
7120 *val = I915_READ(GEN6_PCODE_DATA);
7121 I915_WRITE(GEN6_PCODE_DATA, 0);
7122
7123 return 0;
7124}
7125
Tom O'Rourke151a49d2014-11-13 18:50:10 -08007126int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07007127{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007128 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07007129
7130 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7131 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7132 return -EAGAIN;
7133 }
7134
7135 I915_WRITE(GEN6_PCODE_DATA, val);
7136 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7137
7138 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7139 500)) {
7140 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7141 return -ETIMEDOUT;
7142 }
7143
7144 I915_WRITE(GEN6_PCODE_DATA, 0);
7145
7146 return 0;
7147}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07007148
Ville Syrjälädd06f882014-11-10 22:55:12 +02007149static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007150{
Ville Syrjälädd06f882014-11-10 22:55:12 +02007151 switch (czclk_freq) {
7152 case 200:
7153 return 10;
7154 case 267:
7155 return 12;
7156 case 320:
7157 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02007158 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02007159 case 400:
7160 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007161 default:
7162 return -1;
7163 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02007164}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007165
Ville Syrjälädd06f882014-11-10 22:55:12 +02007166static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7167{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007168 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
Ville Syrjälädd06f882014-11-10 22:55:12 +02007169
7170 div = vlv_gpu_freq_div(czclk_freq);
7171 if (div < 0)
7172 return div;
7173
7174 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007175}
7176
Fengguang Wub55dd642014-07-12 11:21:39 +02007177static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007178{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007179 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007180
Ville Syrjälädd06f882014-11-10 22:55:12 +02007181 mul = vlv_gpu_freq_div(czclk_freq);
7182 if (mul < 0)
7183 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007184
Ville Syrjälädd06f882014-11-10 22:55:12 +02007185 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07007186}
7187
Fengguang Wub55dd642014-07-12 11:21:39 +02007188static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307189{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007190 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307191
Ville Syrjälädd06f882014-11-10 22:55:12 +02007192 div = vlv_gpu_freq_div(czclk_freq) / 2;
7193 if (div < 0)
7194 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05307195
Ville Syrjälädd06f882014-11-10 22:55:12 +02007196 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307197}
7198
Fengguang Wub55dd642014-07-12 11:21:39 +02007199static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05307200{
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03007201 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05307202
Ville Syrjälädd06f882014-11-10 22:55:12 +02007203 mul = vlv_gpu_freq_div(czclk_freq) / 2;
7204 if (mul < 0)
7205 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05307206
Ville Syrjälä1c147622014-08-18 14:42:43 +03007207 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02007208 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05307209}
7210
Ville Syrjälä616bc822015-01-23 21:04:25 +02007211int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7212{
Akash Goel80b6dda2015-03-06 11:07:15 +05307213 if (IS_GEN9(dev_priv->dev))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007214 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7215 GEN9_FREQ_SCALER);
Akash Goel80b6dda2015-03-06 11:07:15 +05307216 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007217 return chv_gpu_freq(dev_priv, val);
7218 else if (IS_VALLEYVIEW(dev_priv->dev))
7219 return byt_gpu_freq(dev_priv, val);
7220 else
7221 return val * GT_FREQUENCY_MULTIPLIER;
7222}
7223
Ville Syrjälä616bc822015-01-23 21:04:25 +02007224int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7225{
Akash Goel80b6dda2015-03-06 11:07:15 +05307226 if (IS_GEN9(dev_priv->dev))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007227 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7228 GT_FREQUENCY_MULTIPLIER);
Akash Goel80b6dda2015-03-06 11:07:15 +05307229 else if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007230 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05307231 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02007232 return byt_freq_opcode(dev_priv, val);
7233 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02007234 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05307235}
7236
Chris Wilson6ad790c2015-04-07 16:20:31 +01007237struct request_boost {
7238 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02007239 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007240};
7241
7242static void __intel_rps_boost_work(struct work_struct *work)
7243{
7244 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01007245 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007246
Chris Wilsone61b9952015-04-27 13:41:24 +01007247 if (!i915_gem_request_completed(req, true))
7248 gen6_rps_boost(to_i915(req->ring->dev), NULL,
7249 req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007250
Chris Wilsone61b9952015-04-27 13:41:24 +01007251 i915_gem_request_unreference__unlocked(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01007252 kfree(boost);
7253}
7254
7255void intel_queue_rps_boost_for_request(struct drm_device *dev,
Daniel Vettereed29a52015-05-21 14:21:25 +02007256 struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007257{
7258 struct request_boost *boost;
7259
Daniel Vettereed29a52015-05-21 14:21:25 +02007260 if (req == NULL || INTEL_INFO(dev)->gen < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01007261 return;
7262
Chris Wilsone61b9952015-04-27 13:41:24 +01007263 if (i915_gem_request_completed(req, true))
7264 return;
7265
Chris Wilson6ad790c2015-04-07 16:20:31 +01007266 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7267 if (boost == NULL)
7268 return;
7269
Daniel Vettereed29a52015-05-21 14:21:25 +02007270 i915_gem_request_reference(req);
7271 boost->req = req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01007272
7273 INIT_WORK(&boost->work, __intel_rps_boost_work);
7274 queue_work(to_i915(dev)->wq, &boost->work);
7275}
7276
Daniel Vetterf742a552013-12-06 10:17:53 +01007277void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01007278{
7279 struct drm_i915_private *dev_priv = dev->dev_private;
7280
Daniel Vetterf742a552013-12-06 10:17:53 +01007281 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01007282 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01007283
Chris Wilson907b28c2013-07-19 20:36:52 +01007284 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7285 intel_gen6_powersave_work);
Chris Wilson1854d5c2015-04-07 16:20:32 +01007286 INIT_LIST_HEAD(&dev_priv->rps.clients);
Chris Wilson2e1b8732015-04-27 13:41:22 +01007287 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7288 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03007289
Paulo Zanoni33688d92014-03-07 20:08:19 -03007290 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02007291 atomic_set(&dev_priv->pm.wakeref_count, 0);
Imre Deak2b19efe2015-12-15 20:10:37 +02007292 atomic_set(&dev_priv->pm.atomic_seq, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01007293}