blob: d277d93cffbf29af5bc3faa5979b426b82ad5e0f [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawskydc39fff2013-10-18 12:32:07 -070034/**
35 * RC6 is a special power stage which allows the GPU to enter an very
36 * low-voltage mode when idle, using down to 0V while at this stage. This
37 * stage is entered automatically when the GPU is idle when RC6 support is
38 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
39 *
40 * There are different RC6 modes available in Intel GPU, which differentiate
41 * among each other with the latency required to enter and leave RC6 and
42 * voltage consumed by the GPU in different states.
43 *
44 * The combination of the following flags define which states GPU is allowed
45 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
46 * RC6pp is deepest RC6. Their support by hardware varies according to the
47 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
48 * which brings the most power savings; deeper states save more power, but
49 * require higher latency to switch to and wake up.
50 */
51#define INTEL_RC6_ENABLE (1<<0)
52#define INTEL_RC6p_ENABLE (1<<1)
53#define INTEL_RC6pp_ENABLE (1<<2)
54
Damien Lespiauda2078c2013-02-13 15:27:27 +000055static void gen9_init_clock_gating(struct drm_device *dev)
56{
Damien Lespiauacd5c342014-03-26 16:55:46 +000057 struct drm_i915_private *dev_priv = dev->dev_private;
58
Damien Lespiau77719d22015-02-09 19:33:13 +000059 /* WaEnableLbsSlaRetryTimerDecrement:skl */
60 I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
61 GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
62}
Damien Lespiau91e41d12014-03-26 17:42:50 +000063
Damien Lespiau45db2192015-02-09 19:33:09 +000064static void skl_init_clock_gating(struct drm_device *dev)
Damien Lespiauda2078c2013-02-13 15:27:27 +000065{
Damien Lespiauacd5c342014-03-26 16:55:46 +000066 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau3ca5da42014-03-26 18:18:01 +000067
Damien Lespiau77719d22015-02-09 19:33:13 +000068 gen9_init_clock_gating(dev);
69
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000070 if (INTEL_REVID(dev) == SKL_REVID_A0) {
71 /*
72 * WaDisableSDEUnitClockGating:skl
Damien Lespiau9253c2e2015-02-09 19:33:10 +000073 * WaSetGAPSunitClckGateDisable:skl
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000074 */
75 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Damien Lespiau9253c2e2015-02-09 19:33:10 +000076 GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000077 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
78 }
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000079
Damien Lespiau2caa3b22015-02-09 19:33:20 +000080 if (INTEL_REVID(dev) <= SKL_REVID_D0) {
Damien Lespiau81e231a2015-02-09 19:33:19 +000081 /* WaDisableHDCInvalidation:skl */
82 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
83 BDW_DISABLE_HDC_INVALIDATION);
84
Damien Lespiau2caa3b22015-02-09 19:33:20 +000085 /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
86 I915_WRITE(FF_SLICE_CS_CHICKEN2,
87 I915_READ(FF_SLICE_CS_CHICKEN2) |
88 GEN9_TSG_BARRIER_ACK_DISABLE);
89 }
Damien Lespiau81e231a2015-02-09 19:33:19 +000090
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000091 if (INTEL_REVID(dev) <= SKL_REVID_E0)
92 /* WaDisableLSQCROPERFforOCL:skl */
93 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
94 GEN8_LQSC_RO_PERF_DIS);
Damien Lespiauda2078c2013-02-13 15:27:27 +000095}
96
Daniel Vetterc921aba2012-04-26 23:28:17 +020097static void i915_pineview_get_mem_freq(struct drm_device *dev)
98{
Jani Nikula50227e12014-03-31 14:27:21 +030099 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200100 u32 tmp;
101
102 tmp = I915_READ(CLKCFG);
103
104 switch (tmp & CLKCFG_FSB_MASK) {
105 case CLKCFG_FSB_533:
106 dev_priv->fsb_freq = 533; /* 133*4 */
107 break;
108 case CLKCFG_FSB_800:
109 dev_priv->fsb_freq = 800; /* 200*4 */
110 break;
111 case CLKCFG_FSB_667:
112 dev_priv->fsb_freq = 667; /* 167*4 */
113 break;
114 case CLKCFG_FSB_400:
115 dev_priv->fsb_freq = 400; /* 100*4 */
116 break;
117 }
118
119 switch (tmp & CLKCFG_MEM_MASK) {
120 case CLKCFG_MEM_533:
121 dev_priv->mem_freq = 533;
122 break;
123 case CLKCFG_MEM_667:
124 dev_priv->mem_freq = 667;
125 break;
126 case CLKCFG_MEM_800:
127 dev_priv->mem_freq = 800;
128 break;
129 }
130
131 /* detect pineview DDR3 setting */
132 tmp = I915_READ(CSHRDDR3CTL);
133 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
134}
135
136static void i915_ironlake_get_mem_freq(struct drm_device *dev)
137{
Jani Nikula50227e12014-03-31 14:27:21 +0300138 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200139 u16 ddrpll, csipll;
140
141 ddrpll = I915_READ16(DDRMPLL1);
142 csipll = I915_READ16(CSIPLL0);
143
144 switch (ddrpll & 0xff) {
145 case 0xc:
146 dev_priv->mem_freq = 800;
147 break;
148 case 0x10:
149 dev_priv->mem_freq = 1066;
150 break;
151 case 0x14:
152 dev_priv->mem_freq = 1333;
153 break;
154 case 0x18:
155 dev_priv->mem_freq = 1600;
156 break;
157 default:
158 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
159 ddrpll & 0xff);
160 dev_priv->mem_freq = 0;
161 break;
162 }
163
Daniel Vetter20e4d402012-08-08 23:35:39 +0200164 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200165
166 switch (csipll & 0x3ff) {
167 case 0x00c:
168 dev_priv->fsb_freq = 3200;
169 break;
170 case 0x00e:
171 dev_priv->fsb_freq = 3733;
172 break;
173 case 0x010:
174 dev_priv->fsb_freq = 4266;
175 break;
176 case 0x012:
177 dev_priv->fsb_freq = 4800;
178 break;
179 case 0x014:
180 dev_priv->fsb_freq = 5333;
181 break;
182 case 0x016:
183 dev_priv->fsb_freq = 5866;
184 break;
185 case 0x018:
186 dev_priv->fsb_freq = 6400;
187 break;
188 default:
189 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
190 csipll & 0x3ff);
191 dev_priv->fsb_freq = 0;
192 break;
193 }
194
195 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200196 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200197 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200198 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200199 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200200 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200201 }
202}
203
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300204static const struct cxsr_latency cxsr_latency_table[] = {
205 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
206 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
207 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
208 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
209 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
210
211 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
212 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
213 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
214 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
215 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
216
217 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
218 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
219 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
220 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
221 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
222
223 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
224 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
225 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
226 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
227 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
228
229 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
230 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
231 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
232 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
233 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
234
235 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
236 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
237 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
238 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
239 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
240};
241
Daniel Vetter63c62272012-04-21 23:17:55 +0200242static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300243 int is_ddr3,
244 int fsb,
245 int mem)
246{
247 const struct cxsr_latency *latency;
248 int i;
249
250 if (fsb == 0 || mem == 0)
251 return NULL;
252
253 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
254 latency = &cxsr_latency_table[i];
255 if (is_desktop == latency->is_desktop &&
256 is_ddr3 == latency->is_ddr3 &&
257 fsb == latency->fsb_freq && mem == latency->mem_freq)
258 return latency;
259 }
260
261 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
262
263 return NULL;
264}
265
Imre Deak5209b1f2014-07-01 12:36:17 +0300266void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300267{
Imre Deak5209b1f2014-07-01 12:36:17 +0300268 struct drm_device *dev = dev_priv->dev;
269 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300270
Imre Deak5209b1f2014-07-01 12:36:17 +0300271 if (IS_VALLEYVIEW(dev)) {
272 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
273 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
274 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
275 } else if (IS_PINEVIEW(dev)) {
276 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
277 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
278 I915_WRITE(DSPFW3, val);
279 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
280 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
281 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
282 I915_WRITE(FW_BLC_SELF, val);
283 } else if (IS_I915GM(dev)) {
284 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
285 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
286 I915_WRITE(INSTPM, val);
287 } else {
288 return;
289 }
290
291 DRM_DEBUG_KMS("memory self-refresh is %s\n",
292 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300293}
294
295/*
296 * Latency for FIFO fetches is dependent on several factors:
297 * - memory configuration (speed, channels)
298 * - chipset
299 * - current MCH state
300 * It can be fairly high in some situations, so here we assume a fairly
301 * pessimal value. It's a tradeoff between extra memory fetches (if we
302 * set this value too high, the FIFO will fetch frequently to stay full)
303 * and power consumption (set it too low to save power and we might see
304 * FIFO underruns and display "flicker").
305 *
306 * A value of 5us seems to be a good balance; safe for very low end
307 * platforms but not overly aggressive on lower latency configs.
308 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100309static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300310
Ville Syrjäläb5004722015-03-05 21:19:47 +0200311#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
312 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
313
314static int vlv_get_fifo_size(struct drm_device *dev,
315 enum pipe pipe, int plane)
316{
317 struct drm_i915_private *dev_priv = dev->dev_private;
318 int sprite0_start, sprite1_start, size;
319
320 switch (pipe) {
321 uint32_t dsparb, dsparb2, dsparb3;
322 case PIPE_A:
323 dsparb = I915_READ(DSPARB);
324 dsparb2 = I915_READ(DSPARB2);
325 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
326 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
327 break;
328 case PIPE_B:
329 dsparb = I915_READ(DSPARB);
330 dsparb2 = I915_READ(DSPARB2);
331 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
332 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
333 break;
334 case PIPE_C:
335 dsparb2 = I915_READ(DSPARB2);
336 dsparb3 = I915_READ(DSPARB3);
337 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
338 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
339 break;
340 default:
341 return 0;
342 }
343
344 switch (plane) {
345 case 0:
346 size = sprite0_start;
347 break;
348 case 1:
349 size = sprite1_start - sprite0_start;
350 break;
351 case 2:
352 size = 512 - 1 - sprite1_start;
353 break;
354 default:
355 return 0;
356 }
357
358 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
359 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
360 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
361 size);
362
363 return size;
364}
365
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300366static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367{
368 struct drm_i915_private *dev_priv = dev->dev_private;
369 uint32_t dsparb = I915_READ(DSPARB);
370 int size;
371
372 size = dsparb & 0x7f;
373 if (plane)
374 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
375
376 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
377 plane ? "B" : "A", size);
378
379 return size;
380}
381
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200382static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300383{
384 struct drm_i915_private *dev_priv = dev->dev_private;
385 uint32_t dsparb = I915_READ(DSPARB);
386 int size;
387
388 size = dsparb & 0x1ff;
389 if (plane)
390 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
391 size >>= 1; /* Convert to cachelines */
392
393 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
394 plane ? "B" : "A", size);
395
396 return size;
397}
398
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300399static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300400{
401 struct drm_i915_private *dev_priv = dev->dev_private;
402 uint32_t dsparb = I915_READ(DSPARB);
403 int size;
404
405 size = dsparb & 0x7f;
406 size >>= 2; /* Convert to cachelines */
407
408 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
409 plane ? "B" : "A",
410 size);
411
412 return size;
413}
414
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300415/* Pineview has different values for various configs */
416static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300417 .fifo_size = PINEVIEW_DISPLAY_FIFO,
418 .max_wm = PINEVIEW_MAX_WM,
419 .default_wm = PINEVIEW_DFT_WM,
420 .guard_size = PINEVIEW_GUARD_WM,
421 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300422};
423static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300424 .fifo_size = PINEVIEW_DISPLAY_FIFO,
425 .max_wm = PINEVIEW_MAX_WM,
426 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
427 .guard_size = PINEVIEW_GUARD_WM,
428 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300429};
430static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300431 .fifo_size = PINEVIEW_CURSOR_FIFO,
432 .max_wm = PINEVIEW_CURSOR_MAX_WM,
433 .default_wm = PINEVIEW_CURSOR_DFT_WM,
434 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
435 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300436};
437static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300438 .fifo_size = PINEVIEW_CURSOR_FIFO,
439 .max_wm = PINEVIEW_CURSOR_MAX_WM,
440 .default_wm = PINEVIEW_CURSOR_DFT_WM,
441 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
442 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300443};
444static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300445 .fifo_size = G4X_FIFO_SIZE,
446 .max_wm = G4X_MAX_WM,
447 .default_wm = G4X_MAX_WM,
448 .guard_size = 2,
449 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300450};
451static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300452 .fifo_size = I965_CURSOR_FIFO,
453 .max_wm = I965_CURSOR_MAX_WM,
454 .default_wm = I965_CURSOR_DFT_WM,
455 .guard_size = 2,
456 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300457};
458static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300459 .fifo_size = VALLEYVIEW_FIFO_SIZE,
460 .max_wm = VALLEYVIEW_MAX_WM,
461 .default_wm = VALLEYVIEW_MAX_WM,
462 .guard_size = 2,
463 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300464};
465static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300466 .fifo_size = I965_CURSOR_FIFO,
467 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
468 .default_wm = I965_CURSOR_DFT_WM,
469 .guard_size = 2,
470 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300471};
472static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300473 .fifo_size = I965_CURSOR_FIFO,
474 .max_wm = I965_CURSOR_MAX_WM,
475 .default_wm = I965_CURSOR_DFT_WM,
476 .guard_size = 2,
477 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300478};
479static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300480 .fifo_size = I945_FIFO_SIZE,
481 .max_wm = I915_MAX_WM,
482 .default_wm = 1,
483 .guard_size = 2,
484 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300485};
486static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300487 .fifo_size = I915_FIFO_SIZE,
488 .max_wm = I915_MAX_WM,
489 .default_wm = 1,
490 .guard_size = 2,
491 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300492};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300493static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300494 .fifo_size = I855GM_FIFO_SIZE,
495 .max_wm = I915_MAX_WM,
496 .default_wm = 1,
497 .guard_size = 2,
498 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300499};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300500static const struct intel_watermark_params i830_bc_wm_info = {
501 .fifo_size = I855GM_FIFO_SIZE,
502 .max_wm = I915_MAX_WM/2,
503 .default_wm = 1,
504 .guard_size = 2,
505 .cacheline_size = I830_FIFO_LINE_SIZE,
506};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200507static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300508 .fifo_size = I830_FIFO_SIZE,
509 .max_wm = I915_MAX_WM,
510 .default_wm = 1,
511 .guard_size = 2,
512 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300513};
514
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515/**
516 * intel_calculate_wm - calculate watermark level
517 * @clock_in_khz: pixel clock
518 * @wm: chip FIFO params
519 * @pixel_size: display pixel size
520 * @latency_ns: memory latency for the platform
521 *
522 * Calculate the watermark level (the level at which the display plane will
523 * start fetching from memory again). Each chip has a different display
524 * FIFO size and allocation, so the caller needs to figure that out and pass
525 * in the correct intel_watermark_params structure.
526 *
527 * As the pixel clock runs, the FIFO will be drained at a rate that depends
528 * on the pixel size. When it reaches the watermark level, it'll start
529 * fetching FIFO line sized based chunks from memory until the FIFO fills
530 * past the watermark point. If the FIFO drains completely, a FIFO underrun
531 * will occur, and a display engine hang could result.
532 */
533static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
534 const struct intel_watermark_params *wm,
535 int fifo_size,
536 int pixel_size,
537 unsigned long latency_ns)
538{
539 long entries_required, wm_size;
540
541 /*
542 * Note: we need to make sure we don't overflow for various clock &
543 * latency values.
544 * clocks go from a few thousand to several hundred thousand.
545 * latency is usually a few thousand
546 */
547 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
548 1000;
549 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
550
551 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
552
553 wm_size = fifo_size - (entries_required + wm->guard_size);
554
555 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
556
557 /* Don't promote wm_size to unsigned... */
558 if (wm_size > (long)wm->max_wm)
559 wm_size = wm->max_wm;
560 if (wm_size <= 0)
561 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300562
563 /*
564 * Bspec seems to indicate that the value shouldn't be lower than
565 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
566 * Lets go for 8 which is the burst size since certain platforms
567 * already use a hardcoded 8 (which is what the spec says should be
568 * done).
569 */
570 if (wm_size <= 8)
571 wm_size = 8;
572
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573 return wm_size;
574}
575
576static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
577{
578 struct drm_crtc *crtc, *enabled = NULL;
579
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100580 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000581 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300582 if (enabled)
583 return NULL;
584 enabled = crtc;
585 }
586 }
587
588 return enabled;
589}
590
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300591static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300592{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300593 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300594 struct drm_i915_private *dev_priv = dev->dev_private;
595 struct drm_crtc *crtc;
596 const struct cxsr_latency *latency;
597 u32 reg;
598 unsigned long wm;
599
600 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
601 dev_priv->fsb_freq, dev_priv->mem_freq);
602 if (!latency) {
603 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300604 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605 return;
606 }
607
608 crtc = single_enabled_crtc(dev);
609 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100610 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -0800611 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100612 int clock;
613
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200614 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100615 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300616
617 /* Display SR */
618 wm = intel_calculate_wm(clock, &pineview_display_wm,
619 pineview_display_wm.fifo_size,
620 pixel_size, latency->display_sr);
621 reg = I915_READ(DSPFW1);
622 reg &= ~DSPFW_SR_MASK;
623 reg |= wm << DSPFW_SR_SHIFT;
624 I915_WRITE(DSPFW1, reg);
625 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
626
627 /* cursor SR */
628 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
629 pineview_display_wm.fifo_size,
630 pixel_size, latency->cursor_sr);
631 reg = I915_READ(DSPFW3);
632 reg &= ~DSPFW_CURSOR_SR_MASK;
633 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
634 I915_WRITE(DSPFW3, reg);
635
636 /* Display HPLL off SR */
637 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
638 pineview_display_hplloff_wm.fifo_size,
639 pixel_size, latency->display_hpll_disable);
640 reg = I915_READ(DSPFW3);
641 reg &= ~DSPFW_HPLL_SR_MASK;
642 reg |= wm & DSPFW_HPLL_SR_MASK;
643 I915_WRITE(DSPFW3, reg);
644
645 /* cursor HPLL off SR */
646 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
647 pineview_display_hplloff_wm.fifo_size,
648 pixel_size, latency->cursor_hpll_disable);
649 reg = I915_READ(DSPFW3);
650 reg &= ~DSPFW_HPLL_CURSOR_MASK;
651 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
652 I915_WRITE(DSPFW3, reg);
653 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
654
Imre Deak5209b1f2014-07-01 12:36:17 +0300655 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300656 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300657 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300658 }
659}
660
661static bool g4x_compute_wm0(struct drm_device *dev,
662 int plane,
663 const struct intel_watermark_params *display,
664 int display_latency_ns,
665 const struct intel_watermark_params *cursor,
666 int cursor_latency_ns,
667 int *plane_wm,
668 int *cursor_wm)
669{
670 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300671 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300672 int htotal, hdisplay, clock, pixel_size;
673 int line_time_us, line_count;
674 int entries, tlb_miss;
675
676 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000677 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300678 *cursor_wm = cursor->guard_size;
679 *plane_wm = display->guard_size;
680 return false;
681 }
682
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200683 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100684 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800685 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200686 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800687 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300688
689 /* Use the small buffer method to calculate plane watermark */
690 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
691 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
692 if (tlb_miss > 0)
693 entries += tlb_miss;
694 entries = DIV_ROUND_UP(entries, display->cacheline_size);
695 *plane_wm = entries + display->guard_size;
696 if (*plane_wm > (int)display->max_wm)
697 *plane_wm = display->max_wm;
698
699 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200700 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300701 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Matt Roper3dd512f2015-02-27 10:12:00 -0800702 entries = line_count * crtc->cursor->state->crtc_w * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300703 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
704 if (tlb_miss > 0)
705 entries += tlb_miss;
706 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
707 *cursor_wm = entries + cursor->guard_size;
708 if (*cursor_wm > (int)cursor->max_wm)
709 *cursor_wm = (int)cursor->max_wm;
710
711 return true;
712}
713
714/*
715 * Check the wm result.
716 *
717 * If any calculated watermark values is larger than the maximum value that
718 * can be programmed into the associated watermark register, that watermark
719 * must be disabled.
720 */
721static bool g4x_check_srwm(struct drm_device *dev,
722 int display_wm, int cursor_wm,
723 const struct intel_watermark_params *display,
724 const struct intel_watermark_params *cursor)
725{
726 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
727 display_wm, cursor_wm);
728
729 if (display_wm > display->max_wm) {
730 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
731 display_wm, display->max_wm);
732 return false;
733 }
734
735 if (cursor_wm > cursor->max_wm) {
736 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
737 cursor_wm, cursor->max_wm);
738 return false;
739 }
740
741 if (!(display_wm || cursor_wm)) {
742 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
743 return false;
744 }
745
746 return true;
747}
748
749static bool g4x_compute_srwm(struct drm_device *dev,
750 int plane,
751 int latency_ns,
752 const struct intel_watermark_params *display,
753 const struct intel_watermark_params *cursor,
754 int *display_wm, int *cursor_wm)
755{
756 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300757 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758 int hdisplay, htotal, pixel_size, clock;
759 unsigned long line_time_us;
760 int line_count, line_size;
761 int small, large;
762 int entries;
763
764 if (!latency_ns) {
765 *display_wm = *cursor_wm = 0;
766 return false;
767 }
768
769 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200770 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100771 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800772 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200773 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -0800774 pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775
Ville Syrjälä922044c2014-02-14 14:18:57 +0200776 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777 line_count = (latency_ns / line_time_us + 1000) / 1000;
778 line_size = hdisplay * pixel_size;
779
780 /* Use the minimum of the small and large buffer method for primary */
781 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
782 large = line_count * line_size;
783
784 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
785 *display_wm = entries + display->guard_size;
786
787 /* calculate the self-refresh watermark for display cursor */
Matt Roper3dd512f2015-02-27 10:12:00 -0800788 entries = line_count * pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300789 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
790 *cursor_wm = entries + cursor->guard_size;
791
792 return g4x_check_srwm(dev,
793 *display_wm, *cursor_wm,
794 display, cursor);
795}
796
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200797static void vlv_write_wm_values(struct intel_crtc *crtc,
798 const struct vlv_wm_values *wm)
799{
800 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
801 enum pipe pipe = crtc->pipe;
802
803 I915_WRITE(VLV_DDL(pipe),
804 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
805 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
806 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
807 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
808
Ville Syrjäläae801522015-03-05 21:19:49 +0200809 I915_WRITE(DSPFW1,
810 ((wm->sr.plane << DSPFW_SR_SHIFT) & DSPFW_SR_MASK) |
811 ((wm->pipe[PIPE_B].cursor << DSPFW_CURSORB_SHIFT) & DSPFW_CURSORB_MASK) |
812 ((wm->pipe[PIPE_B].primary << DSPFW_PLANEB_SHIFT) & DSPFW_PLANEB_MASK_VLV) |
813 ((wm->pipe[PIPE_A].primary << DSPFW_PLANEA_SHIFT) & DSPFW_PLANEA_MASK_VLV));
814 I915_WRITE(DSPFW2,
815 ((wm->pipe[PIPE_A].sprite[1] << DSPFW_SPRITEB_SHIFT) & DSPFW_SPRITEB_MASK_VLV) |
816 ((wm->pipe[PIPE_A].cursor << DSPFW_CURSORA_SHIFT) & DSPFW_CURSORA_MASK) |
817 ((wm->pipe[PIPE_A].sprite[0] << DSPFW_SPRITEA_SHIFT) & DSPFW_SPRITEA_MASK_VLV));
818 I915_WRITE(DSPFW3,
819 ((wm->sr.cursor << DSPFW_CURSOR_SR_SHIFT) & DSPFW_CURSOR_SR_MASK));
820
821 if (IS_CHERRYVIEW(dev_priv)) {
822 I915_WRITE(DSPFW7_CHV,
823 ((wm->pipe[PIPE_B].sprite[1] << DSPFW_SPRITED_SHIFT) & DSPFW_SPRITED_MASK) |
824 ((wm->pipe[PIPE_B].sprite[0] << DSPFW_SPRITEC_SHIFT) & DSPFW_SPRITEC_MASK));
825 I915_WRITE(DSPFW8_CHV,
826 ((wm->pipe[PIPE_C].sprite[1] << DSPFW_SPRITEF_SHIFT) & DSPFW_SPRITEF_MASK) |
827 ((wm->pipe[PIPE_C].sprite[0] << DSPFW_SPRITEE_SHIFT) & DSPFW_SPRITEE_MASK));
828 I915_WRITE(DSPFW9_CHV,
829 ((wm->pipe[PIPE_C].primary << DSPFW_PLANEC_SHIFT) & DSPFW_PLANEC_MASK) |
830 ((wm->pipe[PIPE_C].cursor << DSPFW_CURSORC_SHIFT) & DSPFW_CURSORC_MASK));
831 I915_WRITE(DSPHOWM,
832 (((wm->sr.plane >> 9) << DSPFW_SR_HI_SHIFT) & DSPFW_SR_HI_MASK) |
833 (((wm->pipe[PIPE_C].sprite[1] >> 8) << DSPFW_SPRITEF_HI_SHIFT) & DSPFW_SPRITEF_HI_MASK) |
834 (((wm->pipe[PIPE_C].sprite[0] >> 8) << DSPFW_SPRITEE_HI_SHIFT) & DSPFW_SPRITEE_HI_MASK) |
835 (((wm->pipe[PIPE_C].primary >> 8) << DSPFW_PLANEC_HI_SHIFT) & DSPFW_PLANEC_HI_MASK) |
836 (((wm->pipe[PIPE_B].sprite[1] >> 8) << DSPFW_SPRITED_HI_SHIFT) & DSPFW_SPRITED_HI_MASK) |
837 (((wm->pipe[PIPE_B].sprite[0] >> 8) << DSPFW_SPRITEC_HI_SHIFT) & DSPFW_SPRITEC_HI_MASK) |
838 (((wm->pipe[PIPE_B].primary >> 8) << DSPFW_PLANEB_HI_SHIFT) & DSPFW_PLANEB_HI_MASK) |
839 (((wm->pipe[PIPE_A].sprite[1] >> 8) << DSPFW_SPRITEB_HI_SHIFT) & DSPFW_SPRITEB_HI_MASK) |
840 (((wm->pipe[PIPE_A].sprite[0] >> 8) << DSPFW_SPRITEA_HI_SHIFT) & DSPFW_SPRITEA_HI_MASK) |
841 (((wm->pipe[PIPE_A].primary >> 8) << DSPFW_PLANEA_HI_SHIFT) & DSPFW_PLANEA_HI_MASK));
842 } else {
843 I915_WRITE(DSPFW7,
844 ((wm->pipe[PIPE_B].sprite[1] << DSPFW_SPRITED_SHIFT) & DSPFW_SPRITED_MASK) |
845 ((wm->pipe[PIPE_B].sprite[0] << DSPFW_SPRITEC_SHIFT) & DSPFW_SPRITEC_MASK));
846 I915_WRITE(DSPHOWM,
847 (((wm->sr.plane >> 9) << DSPFW_SR_HI_SHIFT) & DSPFW_SR_HI_MASK) |
848 (((wm->pipe[PIPE_B].sprite[1] >> 8) << DSPFW_SPRITED_HI_SHIFT) & DSPFW_SPRITED_HI_MASK) |
849 (((wm->pipe[PIPE_B].sprite[0] >> 8) << DSPFW_SPRITEC_HI_SHIFT) & DSPFW_SPRITEC_HI_MASK) |
850 (((wm->pipe[PIPE_B].primary >> 8) << DSPFW_PLANEB_HI_SHIFT) & DSPFW_PLANEB_HI_MASK) |
851 (((wm->pipe[PIPE_A].sprite[1] >> 8) << DSPFW_SPRITEB_HI_SHIFT) & DSPFW_SPRITEB_HI_MASK) |
852 (((wm->pipe[PIPE_A].sprite[0] >> 8) << DSPFW_SPRITEA_HI_SHIFT) & DSPFW_SPRITEA_HI_MASK) |
853 (((wm->pipe[PIPE_A].primary >> 8) << DSPFW_PLANEA_HI_SHIFT) & DSPFW_PLANEA_HI_MASK));
854 }
855
856 POSTING_READ(DSPFW1);
857
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200858 dev_priv->wm.vlv = *wm;
859}
860
Ville Syrjälä341c5262015-03-05 21:19:44 +0200861static uint8_t vlv_compute_drain_latency(struct drm_crtc *crtc,
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200862 struct drm_plane *plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300863{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700864 struct drm_device *dev = crtc->dev;
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
866 int entries, prec_mult, drain_latency, pixel_size;
867 int clock = intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä341c5262015-03-05 21:19:44 +0200868 const int high_precision = IS_CHERRYVIEW(dev) ? 16 : 64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200870 /*
871 * FIXME the plane might have an fb
872 * but be invisible (eg. due to clipping)
873 */
874 if (!intel_crtc->active || !plane->state->fb)
875 return 0;
876
Gajanan Bhat0948c262014-08-07 01:58:24 +0530877 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200878 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300879
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200880 pixel_size = drm_format_plane_cpp(plane->state->fb->pixel_format, 0);
881
Gajanan Bhat0948c262014-08-07 01:58:24 +0530882 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
Ville Syrjälä341c5262015-03-05 21:19:44 +0200883 return 0;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300884
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530885 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200886
Ville Syrjälä341c5262015-03-05 21:19:44 +0200887 prec_mult = high_precision;
888 drain_latency = 64 * prec_mult * 4 / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300889
Ville Syrjälä341c5262015-03-05 21:19:44 +0200890 if (drain_latency > DRAIN_LATENCY_MASK) {
891 prec_mult /= 2;
892 drain_latency = 64 * prec_mult * 4 / entries;
Ville Syrjäläabfc00b2015-03-05 21:19:43 +0200893 }
894
Ville Syrjälä341c5262015-03-05 21:19:44 +0200895 if (drain_latency > DRAIN_LATENCY_MASK)
896 drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300897
Ville Syrjälä341c5262015-03-05 21:19:44 +0200898 return drain_latency | (prec_mult == high_precision ?
899 DDL_PRECISION_HIGH : DDL_PRECISION_LOW);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300900}
901
Ville Syrjäläae801522015-03-05 21:19:49 +0200902static int vlv_compute_wm(struct intel_crtc *crtc,
903 struct intel_plane *plane,
904 int fifo_size)
905{
906 int clock, entries, pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300907
Ville Syrjäläae801522015-03-05 21:19:49 +0200908 /*
909 * FIXME the plane might have an fb
910 * but be invisible (eg. due to clipping)
911 */
912 if (!crtc->active || !plane->base.state->fb)
913 return 0;
914
915 pixel_size = drm_format_plane_cpp(plane->base.state->fb->pixel_format, 0);
916 clock = crtc->config->base.adjusted_mode.crtc_clock;
917
918 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
919
920 /*
921 * Set up the watermark such that we don't start issuing memory
922 * requests until we are within PND's max deadline value (256us).
923 * Idea being to be idle as long as possible while still taking
924 * advatange of PND's deadline scheduling. The limit of 8
925 * cachelines (used when the FIFO will anyway drain in less time
926 * than 256us) should match what we would be done if trickle
927 * feed were enabled.
928 */
929 return fifo_size - clamp(DIV_ROUND_UP(256 * entries, 64), 0, fifo_size - 8);
930}
931
932static bool vlv_compute_sr_wm(struct drm_device *dev,
933 struct vlv_wm_values *wm)
934{
935 struct drm_i915_private *dev_priv = to_i915(dev);
936 struct drm_crtc *crtc;
937 enum pipe pipe = INVALID_PIPE;
938 int num_planes = 0;
939 int fifo_size = 0;
940 struct intel_plane *plane;
941
942 wm->sr.cursor = wm->sr.plane = 0;
943
944 crtc = single_enabled_crtc(dev);
945 /* maxfifo not supported on pipe C */
946 if (crtc && to_intel_crtc(crtc)->pipe != PIPE_C) {
947 pipe = to_intel_crtc(crtc)->pipe;
948 num_planes = !!wm->pipe[pipe].primary +
949 !!wm->pipe[pipe].sprite[0] +
950 !!wm->pipe[pipe].sprite[1];
951 fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
952 }
953
954 if (fifo_size == 0 || num_planes > 1)
955 return false;
956
957 wm->sr.cursor = vlv_compute_wm(to_intel_crtc(crtc),
958 to_intel_plane(crtc->cursor), 0x3f);
959
960 list_for_each_entry(plane, &dev->mode_config.plane_list, base.head) {
961 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
962 continue;
963
964 if (plane->pipe != pipe)
965 continue;
966
967 wm->sr.plane = vlv_compute_wm(to_intel_crtc(crtc),
968 plane, fifo_size);
969 if (wm->sr.plane != 0)
970 break;
971 }
972
973 return true;
974}
975
976static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300977{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700978 struct drm_device *dev = crtc->dev;
979 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gajanan Bhat0948c262014-08-07 01:58:24 +0530981 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläae801522015-03-05 21:19:49 +0200982 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200983 struct vlv_wm_values wm = dev_priv->wm.vlv;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300984
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200985 wm.ddl[pipe].primary = vlv_compute_drain_latency(crtc, crtc->primary);
Ville Syrjäläae801522015-03-05 21:19:49 +0200986 wm.pipe[pipe].primary = vlv_compute_wm(intel_crtc,
987 to_intel_plane(crtc->primary),
988 vlv_get_fifo_size(dev, pipe, 0));
989
Ville Syrjälä883a3d22015-03-05 21:19:46 +0200990 wm.ddl[pipe].cursor = vlv_compute_drain_latency(crtc, crtc->cursor);
Ville Syrjäläae801522015-03-05 21:19:49 +0200991 wm.pipe[pipe].cursor = vlv_compute_wm(intel_crtc,
992 to_intel_plane(crtc->cursor),
993 0x3f);
994
995 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
996
997 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
998 return;
999
1000 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1001 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1002 wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1003 wm.sr.plane, wm.sr.cursor);
1004
1005 if (!cxsr_enabled)
1006 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat0948c262014-08-07 01:58:24 +05301007
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001008 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001009
1010 if (cxsr_enabled)
1011 intel_set_memory_cxsr(dev_priv, true);
1012}
1013
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301014static void valleyview_update_sprite_wm(struct drm_plane *plane,
1015 struct drm_crtc *crtc,
1016 uint32_t sprite_width,
1017 uint32_t sprite_height,
1018 int pixel_size,
1019 bool enabled, bool scaled)
1020{
1021 struct drm_device *dev = crtc->dev;
1022 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001023 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1024 enum pipe pipe = intel_crtc->pipe;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301025 int sprite = to_intel_plane(plane)->plane;
Ville Syrjäläae801522015-03-05 21:19:49 +02001026 bool cxsr_enabled;
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001027 struct vlv_wm_values wm = dev_priv->wm.vlv;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301028
Ville Syrjäläae801522015-03-05 21:19:49 +02001029 if (enabled) {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001030 wm.ddl[pipe].sprite[sprite] =
Ville Syrjälä883a3d22015-03-05 21:19:46 +02001031 vlv_compute_drain_latency(crtc, plane);
Ville Syrjäläae801522015-03-05 21:19:49 +02001032
1033 wm.pipe[pipe].sprite[sprite] =
1034 vlv_compute_wm(intel_crtc,
1035 to_intel_plane(plane),
1036 vlv_get_fifo_size(dev, pipe, sprite+1));
1037 } else {
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001038 wm.ddl[pipe].sprite[sprite] = 0;
Ville Syrjäläae801522015-03-05 21:19:49 +02001039 wm.pipe[pipe].sprite[sprite] = 0;
1040 }
1041
1042 cxsr_enabled = vlv_compute_sr_wm(dev, &wm);
1043
1044 if (memcmp(&wm, &dev_priv->wm.vlv, sizeof(wm)) == 0)
1045 return;
1046
1047 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: sprite %c=%d, "
1048 "SR: plane=%d, cursor=%d\n", pipe_name(pipe),
1049 sprite_name(pipe, sprite),
1050 wm.pipe[pipe].sprite[sprite],
1051 wm.sr.plane, wm.sr.cursor);
1052
1053 if (!cxsr_enabled)
1054 intel_set_memory_cxsr(dev_priv, false);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301055
Ville Syrjälä0018fda2015-03-05 21:19:45 +02001056 vlv_write_wm_values(intel_crtc, &wm);
Ville Syrjäläae801522015-03-05 21:19:49 +02001057
1058 if (cxsr_enabled)
1059 intel_set_memory_cxsr(dev_priv, true);
Gajanan Bhat01e184c2014-08-07 17:03:30 +05301060}
1061
Ville Syrjäläae801522015-03-05 21:19:49 +02001062#define single_plane_enabled(mask) is_power_of_2(mask)
1063
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001064static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001065{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001066 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001067 static const int sr_latency_ns = 12000;
1068 struct drm_i915_private *dev_priv = dev->dev_private;
1069 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1070 int plane_sr, cursor_sr;
1071 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001072 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001073
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001074 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001075 &g4x_wm_info, pessimal_latency_ns,
1076 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001077 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001078 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001079
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001080 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001081 &g4x_wm_info, pessimal_latency_ns,
1082 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001083 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001084 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001085
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001086 if (single_plane_enabled(enabled) &&
1087 g4x_compute_srwm(dev, ffs(enabled) - 1,
1088 sr_latency_ns,
1089 &g4x_wm_info,
1090 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001091 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001092 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001093 } else {
Imre Deak98584252014-06-13 14:54:20 +03001094 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001095 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001096 plane_sr = cursor_sr = 0;
1097 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001098
Ville Syrjäläa5043452014-06-28 02:04:18 +03001099 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1100 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001101 planea_wm, cursora_wm,
1102 planeb_wm, cursorb_wm,
1103 plane_sr, cursor_sr);
1104
1105 I915_WRITE(DSPFW1,
1106 (plane_sr << DSPFW_SR_SHIFT) |
1107 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1108 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001109 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001110 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001111 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001112 (cursora_wm << DSPFW_CURSORA_SHIFT));
1113 /* HPLL off in SR has some issues on G4x... disable it */
1114 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001115 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001116 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001117
1118 if (cxsr_enabled)
1119 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001120}
1121
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001122static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001123{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001124 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001125 struct drm_i915_private *dev_priv = dev->dev_private;
1126 struct drm_crtc *crtc;
1127 int srwm = 1;
1128 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001129 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001130
1131 /* Calc sr entries for one plane configs */
1132 crtc = single_enabled_crtc(dev);
1133 if (crtc) {
1134 /* self-refresh has much higher latency */
1135 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001136 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001137 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001138 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001139 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001140 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001141 int pixel_size = crtc->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001142 unsigned long line_time_us;
1143 int entries;
1144
Ville Syrjälä922044c2014-02-14 14:18:57 +02001145 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001146
1147 /* Use ns/us then divide to preserve precision */
1148 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1149 pixel_size * hdisplay;
1150 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1151 srwm = I965_FIFO_SIZE - entries;
1152 if (srwm < 0)
1153 srwm = 1;
1154 srwm &= 0x1ff;
1155 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1156 entries, srwm);
1157
1158 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Matt Roper3dd512f2015-02-27 10:12:00 -08001159 pixel_size * crtc->cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001160 entries = DIV_ROUND_UP(entries,
1161 i965_cursor_wm_info.cacheline_size);
1162 cursor_sr = i965_cursor_wm_info.fifo_size -
1163 (entries + i965_cursor_wm_info.guard_size);
1164
1165 if (cursor_sr > i965_cursor_wm_info.max_wm)
1166 cursor_sr = i965_cursor_wm_info.max_wm;
1167
1168 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1169 "cursor %d\n", srwm, cursor_sr);
1170
Imre Deak98584252014-06-13 14:54:20 +03001171 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001172 } else {
Imre Deak98584252014-06-13 14:54:20 +03001173 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001174 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001175 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001176 }
1177
1178 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1179 srwm);
1180
1181 /* 965 has limitations... */
1182 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001183 (8 << DSPFW_CURSORB_SHIFT) |
1184 (8 << DSPFW_PLANEB_SHIFT) |
1185 (8 << DSPFW_PLANEA_SHIFT));
1186 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1187 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001188 /* update cursor SR watermark */
1189 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001190
1191 if (cxsr_enabled)
1192 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001193}
1194
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001195static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001196{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001197 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001198 struct drm_i915_private *dev_priv = dev->dev_private;
1199 const struct intel_watermark_params *wm_info;
1200 uint32_t fwater_lo;
1201 uint32_t fwater_hi;
1202 int cwm, srwm = 1;
1203 int fifo_size;
1204 int planea_wm, planeb_wm;
1205 struct drm_crtc *crtc, *enabled = NULL;
1206
1207 if (IS_I945GM(dev))
1208 wm_info = &i945_wm_info;
1209 else if (!IS_GEN2(dev))
1210 wm_info = &i915_wm_info;
1211 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001212 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001213
1214 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1215 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001216 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001217 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001218 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001219 if (IS_GEN2(dev))
1220 cpp = 4;
1221
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001222 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001223 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001224 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001225 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001226 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001227 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001228 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001229 if (planea_wm > (long)wm_info->max_wm)
1230 planea_wm = wm_info->max_wm;
1231 }
1232
1233 if (IS_GEN2(dev))
1234 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001235
1236 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1237 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001238 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001239 const struct drm_display_mode *adjusted_mode;
Matt Roper59bea882015-02-27 10:12:01 -08001240 int cpp = crtc->primary->state->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001241 if (IS_GEN2(dev))
1242 cpp = 4;
1243
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001244 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001245 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001246 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001247 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001248 if (enabled == NULL)
1249 enabled = crtc;
1250 else
1251 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001252 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001253 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001254 if (planeb_wm > (long)wm_info->max_wm)
1255 planeb_wm = wm_info->max_wm;
1256 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001257
1258 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1259
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001260 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001261 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001262
Matt Roper59bea882015-02-27 10:12:01 -08001263 obj = intel_fb_obj(enabled->primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001264
1265 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001266 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001267 enabled = NULL;
1268 }
1269
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001270 /*
1271 * Overlay gets an aggressive default since video jitter is bad.
1272 */
1273 cwm = 2;
1274
1275 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001276 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001277
1278 /* Calc sr entries for one plane configs */
1279 if (HAS_FW_BLC(dev) && enabled) {
1280 /* self-refresh has much higher latency */
1281 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001282 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001283 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001284 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001285 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001286 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roper59bea882015-02-27 10:12:01 -08001287 int pixel_size = enabled->primary->state->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001288 unsigned long line_time_us;
1289 int entries;
1290
Ville Syrjälä922044c2014-02-14 14:18:57 +02001291 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001292
1293 /* Use ns/us then divide to preserve precision */
1294 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1295 pixel_size * hdisplay;
1296 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1297 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1298 srwm = wm_info->fifo_size - entries;
1299 if (srwm < 0)
1300 srwm = 1;
1301
1302 if (IS_I945G(dev) || IS_I945GM(dev))
1303 I915_WRITE(FW_BLC_SELF,
1304 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1305 else if (IS_I915GM(dev))
1306 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1307 }
1308
1309 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1310 planea_wm, planeb_wm, cwm, srwm);
1311
1312 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1313 fwater_hi = (cwm & 0x1f);
1314
1315 /* Set request length to 8 cachelines per fetch */
1316 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1317 fwater_hi = fwater_hi | (1 << 8);
1318
1319 I915_WRITE(FW_BLC, fwater_lo);
1320 I915_WRITE(FW_BLC2, fwater_hi);
1321
Imre Deak5209b1f2014-07-01 12:36:17 +03001322 if (enabled)
1323 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001324}
1325
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001326static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001327{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001328 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001329 struct drm_i915_private *dev_priv = dev->dev_private;
1330 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001331 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001332 uint32_t fwater_lo;
1333 int planea_wm;
1334
1335 crtc = single_enabled_crtc(dev);
1336 if (crtc == NULL)
1337 return;
1338
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001339 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001340 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001341 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001342 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001343 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001344 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1345 fwater_lo |= (3<<8) | planea_wm;
1346
1347 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1348
1349 I915_WRITE(FW_BLC, fwater_lo);
1350}
1351
Ville Syrjälä36587292013-07-05 11:57:16 +03001352static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1353 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001354{
1355 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001356 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001357
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001358 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001359
1360 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1361 * adjust the pixel_rate here. */
1362
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001363 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001364 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001365 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001366
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001367 pipe_w = intel_crtc->config->pipe_src_w;
1368 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001369 pfit_w = (pfit_size >> 16) & 0xFFFF;
1370 pfit_h = pfit_size & 0xFFFF;
1371 if (pipe_w < pfit_w)
1372 pipe_w = pfit_w;
1373 if (pipe_h < pfit_h)
1374 pipe_h = pfit_h;
1375
1376 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1377 pfit_w * pfit_h);
1378 }
1379
1380 return pixel_rate;
1381}
1382
Ville Syrjälä37126462013-08-01 16:18:55 +03001383/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001384static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001385 uint32_t latency)
1386{
1387 uint64_t ret;
1388
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001389 if (WARN(latency == 0, "Latency value missing\n"))
1390 return UINT_MAX;
1391
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001392 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1393 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1394
1395 return ret;
1396}
1397
Ville Syrjälä37126462013-08-01 16:18:55 +03001398/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001399static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001400 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1401 uint32_t latency)
1402{
1403 uint32_t ret;
1404
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001405 if (WARN(latency == 0, "Latency value missing\n"))
1406 return UINT_MAX;
1407
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001408 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1409 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1410 ret = DIV_ROUND_UP(ret, 64) + 2;
1411 return ret;
1412}
1413
Ville Syrjälä23297042013-07-05 11:57:17 +03001414static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001415 uint8_t bytes_per_pixel)
1416{
1417 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1418}
1419
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001420struct skl_pipe_wm_parameters {
1421 bool active;
1422 uint32_t pipe_htotal;
1423 uint32_t pixel_rate; /* in KHz */
1424 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1425 struct intel_plane_wm_parameters cursor;
1426};
1427
Imre Deak820c1982013-12-17 14:46:36 +02001428struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001429 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001430 uint32_t pipe_htotal;
1431 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001432 struct intel_plane_wm_parameters pri;
1433 struct intel_plane_wm_parameters spr;
1434 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001435};
1436
Imre Deak820c1982013-12-17 14:46:36 +02001437struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001438 uint16_t pri;
1439 uint16_t spr;
1440 uint16_t cur;
1441 uint16_t fbc;
1442};
1443
Ville Syrjälä240264f2013-08-07 13:29:12 +03001444/* used in computing the new watermarks state */
1445struct intel_wm_config {
1446 unsigned int num_pipes_active;
1447 bool sprites_enabled;
1448 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001449};
1450
Ville Syrjälä37126462013-08-01 16:18:55 +03001451/*
1452 * For both WM_PIPE and WM_LP.
1453 * mem_value must be in 0.1us units.
1454 */
Imre Deak820c1982013-12-17 14:46:36 +02001455static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001456 uint32_t mem_value,
1457 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001458{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001459 uint32_t method1, method2;
1460
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001461 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001462 return 0;
1463
Ville Syrjälä23297042013-07-05 11:57:17 +03001464 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001465 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001466 mem_value);
1467
1468 if (!is_lp)
1469 return method1;
1470
Ville Syrjälä23297042013-07-05 11:57:17 +03001471 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001472 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001473 params->pri.horiz_pixels,
1474 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001475 mem_value);
1476
1477 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001478}
1479
Ville Syrjälä37126462013-08-01 16:18:55 +03001480/*
1481 * For both WM_PIPE and WM_LP.
1482 * mem_value must be in 0.1us units.
1483 */
Imre Deak820c1982013-12-17 14:46:36 +02001484static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001485 uint32_t mem_value)
1486{
1487 uint32_t method1, method2;
1488
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001489 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001490 return 0;
1491
Ville Syrjälä23297042013-07-05 11:57:17 +03001492 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001493 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001494 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001495 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001496 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001497 params->spr.horiz_pixels,
1498 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001499 mem_value);
1500 return min(method1, method2);
1501}
1502
Ville Syrjälä37126462013-08-01 16:18:55 +03001503/*
1504 * For both WM_PIPE and WM_LP.
1505 * mem_value must be in 0.1us units.
1506 */
Imre Deak820c1982013-12-17 14:46:36 +02001507static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001508 uint32_t mem_value)
1509{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001510 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001511 return 0;
1512
Ville Syrjälä23297042013-07-05 11:57:17 +03001513 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001514 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001515 params->cur.horiz_pixels,
1516 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001517 mem_value);
1518}
1519
Paulo Zanonicca32e92013-05-31 11:45:06 -03001520/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001521static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001522 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001523{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001524 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001525 return 0;
1526
Ville Syrjälä23297042013-07-05 11:57:17 +03001527 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001528 params->pri.horiz_pixels,
1529 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001530}
1531
Ville Syrjälä158ae642013-08-07 13:28:19 +03001532static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1533{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001534 if (INTEL_INFO(dev)->gen >= 8)
1535 return 3072;
1536 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001537 return 768;
1538 else
1539 return 512;
1540}
1541
Ville Syrjälä4e975082014-03-07 18:32:11 +02001542static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1543 int level, bool is_sprite)
1544{
1545 if (INTEL_INFO(dev)->gen >= 8)
1546 /* BDW primary/sprite plane watermarks */
1547 return level == 0 ? 255 : 2047;
1548 else if (INTEL_INFO(dev)->gen >= 7)
1549 /* IVB/HSW primary/sprite plane watermarks */
1550 return level == 0 ? 127 : 1023;
1551 else if (!is_sprite)
1552 /* ILK/SNB primary plane watermarks */
1553 return level == 0 ? 127 : 511;
1554 else
1555 /* ILK/SNB sprite plane watermarks */
1556 return level == 0 ? 63 : 255;
1557}
1558
1559static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1560 int level)
1561{
1562 if (INTEL_INFO(dev)->gen >= 7)
1563 return level == 0 ? 63 : 255;
1564 else
1565 return level == 0 ? 31 : 63;
1566}
1567
1568static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1569{
1570 if (INTEL_INFO(dev)->gen >= 8)
1571 return 31;
1572 else
1573 return 15;
1574}
1575
Ville Syrjälä158ae642013-08-07 13:28:19 +03001576/* Calculate the maximum primary/sprite plane watermark */
1577static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1578 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001579 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001580 enum intel_ddb_partitioning ddb_partitioning,
1581 bool is_sprite)
1582{
1583 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001584
1585 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001586 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001587 return 0;
1588
1589 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001590 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001591 fifo_size /= INTEL_INFO(dev)->num_pipes;
1592
1593 /*
1594 * For some reason the non self refresh
1595 * FIFO size is only half of the self
1596 * refresh FIFO size on ILK/SNB.
1597 */
1598 if (INTEL_INFO(dev)->gen <= 6)
1599 fifo_size /= 2;
1600 }
1601
Ville Syrjälä240264f2013-08-07 13:29:12 +03001602 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001603 /* level 0 is always calculated with 1:1 split */
1604 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1605 if (is_sprite)
1606 fifo_size *= 5;
1607 fifo_size /= 6;
1608 } else {
1609 fifo_size /= 2;
1610 }
1611 }
1612
1613 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001614 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001615}
1616
1617/* Calculate the maximum cursor plane watermark */
1618static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001619 int level,
1620 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001621{
1622 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001623 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001624 return 64;
1625
1626 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001627 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001628}
1629
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001630static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001631 int level,
1632 const struct intel_wm_config *config,
1633 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001634 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001635{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001636 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1637 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1638 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001639 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001640}
1641
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001642static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1643 int level,
1644 struct ilk_wm_maximums *max)
1645{
1646 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1647 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1648 max->cur = ilk_cursor_wm_reg_max(dev, level);
1649 max->fbc = ilk_fbc_wm_reg_max(dev);
1650}
1651
Ville Syrjäläd9395652013-10-09 19:18:10 +03001652static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001653 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001654 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001655{
1656 bool ret;
1657
1658 /* already determined to be invalid? */
1659 if (!result->enable)
1660 return false;
1661
1662 result->enable = result->pri_val <= max->pri &&
1663 result->spr_val <= max->spr &&
1664 result->cur_val <= max->cur;
1665
1666 ret = result->enable;
1667
1668 /*
1669 * HACK until we can pre-compute everything,
1670 * and thus fail gracefully if LP0 watermarks
1671 * are exceeded...
1672 */
1673 if (level == 0 && !result->enable) {
1674 if (result->pri_val > max->pri)
1675 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1676 level, result->pri_val, max->pri);
1677 if (result->spr_val > max->spr)
1678 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1679 level, result->spr_val, max->spr);
1680 if (result->cur_val > max->cur)
1681 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1682 level, result->cur_val, max->cur);
1683
1684 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1685 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1686 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1687 result->enable = true;
1688 }
1689
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001690 return ret;
1691}
1692
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001693static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001694 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001695 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001696 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001697{
1698 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1699 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1700 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1701
1702 /* WM1+ latency values stored in 0.5us units */
1703 if (level > 0) {
1704 pri_latency *= 5;
1705 spr_latency *= 5;
1706 cur_latency *= 5;
1707 }
1708
1709 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1710 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1711 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1712 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1713 result->enable = true;
1714}
1715
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001716static uint32_t
1717hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001718{
1719 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001721 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001722 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001723
Matt Roper3ef00282015-03-09 10:19:24 -07001724 if (!intel_crtc->active)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001725 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001726
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001727 /* The WM are computed with base on how long it takes to fill a single
1728 * row at the given clock rate, multiplied by 8.
1729 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001730 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1731 mode->crtc_clock);
1732 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001733 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001734
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001735 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1736 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001737}
1738
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001739static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001740{
1741 struct drm_i915_private *dev_priv = dev->dev_private;
1742
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001743 if (IS_GEN9(dev)) {
1744 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001745 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001746 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001747
1748 /* read the first set of memory latencies[0:3] */
1749 val = 0; /* data0 to be programmed to 0 for first set */
1750 mutex_lock(&dev_priv->rps.hw_lock);
1751 ret = sandybridge_pcode_read(dev_priv,
1752 GEN9_PCODE_READ_MEM_LATENCY,
1753 &val);
1754 mutex_unlock(&dev_priv->rps.hw_lock);
1755
1756 if (ret) {
1757 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1758 return;
1759 }
1760
1761 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1762 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1763 GEN9_MEM_LATENCY_LEVEL_MASK;
1764 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1765 GEN9_MEM_LATENCY_LEVEL_MASK;
1766 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1767 GEN9_MEM_LATENCY_LEVEL_MASK;
1768
1769 /* read the second set of memory latencies[4:7] */
1770 val = 1; /* data0 to be programmed to 1 for second set */
1771 mutex_lock(&dev_priv->rps.hw_lock);
1772 ret = sandybridge_pcode_read(dev_priv,
1773 GEN9_PCODE_READ_MEM_LATENCY,
1774 &val);
1775 mutex_unlock(&dev_priv->rps.hw_lock);
1776 if (ret) {
1777 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1778 return;
1779 }
1780
1781 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1782 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1783 GEN9_MEM_LATENCY_LEVEL_MASK;
1784 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1785 GEN9_MEM_LATENCY_LEVEL_MASK;
1786 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1787 GEN9_MEM_LATENCY_LEVEL_MASK;
1788
Vandana Kannan367294b2014-11-04 17:06:46 +00001789 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001790 * WaWmMemoryReadLatency:skl
1791 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001792 * punit doesn't take into account the read latency so we need
1793 * to add 2us to the various latency levels we retrieve from
1794 * the punit.
1795 * - W0 is a bit special in that it's the only level that
1796 * can't be disabled if we want to have display working, so
1797 * we always add 2us there.
1798 * - For levels >=1, punit returns 0us latency when they are
1799 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001800 *
1801 * Additionally, if a level n (n > 1) has a 0us latency, all
1802 * levels m (m >= n) need to be disabled. We make sure to
1803 * sanitize the values out of the punit to satisfy this
1804 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001805 */
1806 wm[0] += 2;
1807 for (level = 1; level <= max_level; level++)
1808 if (wm[level] != 0)
1809 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001810 else {
1811 for (i = level + 1; i <= max_level; i++)
1812 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001813
Vandana Kannan4f947382014-11-04 17:06:47 +00001814 break;
1815 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001816 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001817 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1818
1819 wm[0] = (sskpd >> 56) & 0xFF;
1820 if (wm[0] == 0)
1821 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001822 wm[1] = (sskpd >> 4) & 0xFF;
1823 wm[2] = (sskpd >> 12) & 0xFF;
1824 wm[3] = (sskpd >> 20) & 0x1FF;
1825 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001826 } else if (INTEL_INFO(dev)->gen >= 6) {
1827 uint32_t sskpd = I915_READ(MCH_SSKPD);
1828
1829 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1830 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1831 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1832 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001833 } else if (INTEL_INFO(dev)->gen >= 5) {
1834 uint32_t mltr = I915_READ(MLTR_ILK);
1835
1836 /* ILK primary LP0 latency is 700 ns */
1837 wm[0] = 7;
1838 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1839 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001840 }
1841}
1842
Ville Syrjälä53615a52013-08-01 16:18:50 +03001843static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1844{
1845 /* ILK sprite LP0 latency is 1300 ns */
1846 if (INTEL_INFO(dev)->gen == 5)
1847 wm[0] = 13;
1848}
1849
1850static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1851{
1852 /* ILK cursor LP0 latency is 1300 ns */
1853 if (INTEL_INFO(dev)->gen == 5)
1854 wm[0] = 13;
1855
1856 /* WaDoubleCursorLP3Latency:ivb */
1857 if (IS_IVYBRIDGE(dev))
1858 wm[3] *= 2;
1859}
1860
Damien Lespiau546c81f2014-05-13 15:30:26 +01001861int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001862{
1863 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001864 if (IS_GEN9(dev))
1865 return 7;
1866 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001867 return 4;
1868 else if (INTEL_INFO(dev)->gen >= 6)
1869 return 3;
1870 else
1871 return 2;
1872}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001873
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001874static void intel_print_wm_latency(struct drm_device *dev,
1875 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001876 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001877{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001878 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001879
1880 for (level = 0; level <= max_level; level++) {
1881 unsigned int latency = wm[level];
1882
1883 if (latency == 0) {
1884 DRM_ERROR("%s WM%d latency not provided\n",
1885 name, level);
1886 continue;
1887 }
1888
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001889 /*
1890 * - latencies are in us on gen9.
1891 * - before then, WM1+ latency values are in 0.5us units
1892 */
1893 if (IS_GEN9(dev))
1894 latency *= 10;
1895 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001896 latency *= 5;
1897
1898 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1899 name, level, wm[level],
1900 latency / 10, latency % 10);
1901 }
1902}
1903
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001904static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1905 uint16_t wm[5], uint16_t min)
1906{
1907 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1908
1909 if (wm[0] >= min)
1910 return false;
1911
1912 wm[0] = max(wm[0], min);
1913 for (level = 1; level <= max_level; level++)
1914 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1915
1916 return true;
1917}
1918
1919static void snb_wm_latency_quirk(struct drm_device *dev)
1920{
1921 struct drm_i915_private *dev_priv = dev->dev_private;
1922 bool changed;
1923
1924 /*
1925 * The BIOS provided WM memory latency values are often
1926 * inadequate for high resolution displays. Adjust them.
1927 */
1928 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1929 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1930 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1931
1932 if (!changed)
1933 return;
1934
1935 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1936 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1937 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1938 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1939}
1940
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001941static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001942{
1943 struct drm_i915_private *dev_priv = dev->dev_private;
1944
1945 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1946
1947 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1948 sizeof(dev_priv->wm.pri_latency));
1949 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1950 sizeof(dev_priv->wm.pri_latency));
1951
1952 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1953 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001954
1955 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1956 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1957 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001958
1959 if (IS_GEN6(dev))
1960 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001961}
1962
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001963static void skl_setup_wm_latency(struct drm_device *dev)
1964{
1965 struct drm_i915_private *dev_priv = dev->dev_private;
1966
1967 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1968 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1969}
1970
Imre Deak820c1982013-12-17 14:46:36 +02001971static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001972 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001973{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001974 struct drm_device *dev = crtc->dev;
1975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1976 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001977 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001978
Matt Roper3ef00282015-03-09 10:19:24 -07001979 if (!intel_crtc->active)
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001980 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001981
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001982 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001983 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001984 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
Matt Roper59bea882015-02-27 10:12:01 -08001985 p->pri.bytes_per_pixel = crtc->primary->state->fb->bits_per_pixel / 8;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001986 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001987 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Matt Roper3dd512f2015-02-27 10:12:00 -08001988 p->cur.horiz_pixels = intel_crtc->base.cursor->state->crtc_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001989 /* TODO: for now, assume primary and cursor planes are always enabled. */
1990 p->pri.enabled = true;
1991 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001992
Matt Roperaf2b6532014-04-01 15:22:32 -07001993 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001994 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001995
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001996 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001997 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001998 break;
1999 }
2000 }
2001}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002002
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002003static void ilk_compute_wm_config(struct drm_device *dev,
2004 struct intel_wm_config *config)
2005{
2006 struct intel_crtc *intel_crtc;
2007
2008 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002009 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002010 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
2011
2012 if (!wm->pipe_enabled)
2013 continue;
2014
2015 config->sprites_enabled |= wm->sprites_enabled;
2016 config->sprites_scaled |= wm->sprites_scaled;
2017 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002018 }
2019}
2020
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002021/* Compute new watermarks for the pipe */
2022static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02002023 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002024 struct intel_pipe_wm *pipe_wm)
2025{
2026 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002027 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002028 int level, max_level = ilk_wm_max_level(dev);
2029 /* LP0 watermark maximums depend on this pipe alone */
2030 struct intel_wm_config config = {
2031 .num_pipes_active = 1,
2032 .sprites_enabled = params->spr.enabled,
2033 .sprites_scaled = params->spr.scaled,
2034 };
Imre Deak820c1982013-12-17 14:46:36 +02002035 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002036
Ville Syrjälä2a44b762014-03-07 18:32:09 +02002037 pipe_wm->pipe_enabled = params->active;
2038 pipe_wm->sprites_enabled = params->spr.enabled;
2039 pipe_wm->sprites_scaled = params->spr.scaled;
2040
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002041 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2042 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
2043 max_level = 1;
2044
2045 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2046 if (params->spr.scaled)
2047 max_level = 0;
2048
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002049 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002050
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002051 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02002052 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002053
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002054 /* LP0 watermarks always use 1/2 DDB partitioning */
2055 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2056
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002057 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002058 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2059 return false;
2060
2061 ilk_compute_wm_reg_maximums(dev, 1, &max);
2062
2063 for (level = 1; level <= max_level; level++) {
2064 struct intel_wm_level wm = {};
2065
2066 ilk_compute_wm_level(dev_priv, level, params, &wm);
2067
2068 /*
2069 * Disable any watermark level that exceeds the
2070 * register maximums since such watermarks are
2071 * always invalid.
2072 */
2073 if (!ilk_validate_wm_level(level, &max, &wm))
2074 break;
2075
2076 pipe_wm->wm[level] = wm;
2077 }
2078
2079 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002080}
2081
2082/*
2083 * Merge the watermarks from all active pipes for a specific level.
2084 */
2085static void ilk_merge_wm_level(struct drm_device *dev,
2086 int level,
2087 struct intel_wm_level *ret_wm)
2088{
2089 const struct intel_crtc *intel_crtc;
2090
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002091 ret_wm->enable = true;
2092
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002093 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002094 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2095 const struct intel_wm_level *wm = &active->wm[level];
2096
2097 if (!active->pipe_enabled)
2098 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002099
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002100 /*
2101 * The watermark values may have been used in the past,
2102 * so we must maintain them in the registers for some
2103 * time even if the level is now disabled.
2104 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002105 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002106 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002107
2108 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2109 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2110 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2111 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2112 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002113}
2114
2115/*
2116 * Merge all low power watermarks for all active pipes.
2117 */
2118static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002119 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002120 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002121 struct intel_pipe_wm *merged)
2122{
2123 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002124 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002125
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002126 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2127 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2128 config->num_pipes_active > 1)
2129 return;
2130
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002131 /* ILK: FBC WM must be disabled always */
2132 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002133
2134 /* merge each WM1+ level */
2135 for (level = 1; level <= max_level; level++) {
2136 struct intel_wm_level *wm = &merged->wm[level];
2137
2138 ilk_merge_wm_level(dev, level, wm);
2139
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002140 if (level > last_enabled_level)
2141 wm->enable = false;
2142 else if (!ilk_validate_wm_level(level, max, wm))
2143 /* make sure all following levels get disabled */
2144 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002145
2146 /*
2147 * The spec says it is preferred to disable
2148 * FBC WMs instead of disabling a WM level.
2149 */
2150 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002151 if (wm->enable)
2152 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002153 wm->fbc_val = 0;
2154 }
2155 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002156
2157 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2158 /*
2159 * FIXME this is racy. FBC might get enabled later.
2160 * What we should check here is whether FBC can be
2161 * enabled sometime later.
2162 */
2163 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2164 for (level = 2; level <= max_level; level++) {
2165 struct intel_wm_level *wm = &merged->wm[level];
2166
2167 wm->enable = false;
2168 }
2169 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002170}
2171
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002172static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2173{
2174 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2175 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2176}
2177
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002178/* The value we need to program into the WM_LPx latency field */
2179static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2180{
2181 struct drm_i915_private *dev_priv = dev->dev_private;
2182
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002183 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002184 return 2 * level;
2185 else
2186 return dev_priv->wm.pri_latency[level];
2187}
2188
Imre Deak820c1982013-12-17 14:46:36 +02002189static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002190 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002191 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002192 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002193{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002194 struct intel_crtc *intel_crtc;
2195 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002196
Ville Syrjälä0362c782013-10-09 19:17:57 +03002197 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002198 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002199
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002200 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002201 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002202 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002203
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002204 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002205
Ville Syrjälä0362c782013-10-09 19:17:57 +03002206 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002207
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002208 /*
2209 * Maintain the watermark values even if the level is
2210 * disabled. Doing otherwise could cause underruns.
2211 */
2212 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002213 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002214 (r->pri_val << WM1_LP_SR_SHIFT) |
2215 r->cur_val;
2216
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002217 if (r->enable)
2218 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2219
Ville Syrjälä416f4722013-11-02 21:07:46 -07002220 if (INTEL_INFO(dev)->gen >= 8)
2221 results->wm_lp[wm_lp - 1] |=
2222 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2223 else
2224 results->wm_lp[wm_lp - 1] |=
2225 r->fbc_val << WM1_LP_FBC_SHIFT;
2226
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002227 /*
2228 * Always set WM1S_LP_EN when spr_val != 0, even if the
2229 * level is disabled. Doing otherwise could cause underruns.
2230 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002231 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2232 WARN_ON(wm_lp != 1);
2233 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2234 } else
2235 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002236 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002237
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002238 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002239 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002240 enum pipe pipe = intel_crtc->pipe;
2241 const struct intel_wm_level *r =
2242 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002243
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002244 if (WARN_ON(!r->enable))
2245 continue;
2246
2247 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2248
2249 results->wm_pipe[pipe] =
2250 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2251 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2252 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002253 }
2254}
2255
Paulo Zanoni861f3382013-05-31 10:19:21 -03002256/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2257 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002258static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002259 struct intel_pipe_wm *r1,
2260 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002261{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002262 int level, max_level = ilk_wm_max_level(dev);
2263 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002264
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002265 for (level = 1; level <= max_level; level++) {
2266 if (r1->wm[level].enable)
2267 level1 = level;
2268 if (r2->wm[level].enable)
2269 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002270 }
2271
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002272 if (level1 == level2) {
2273 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002274 return r2;
2275 else
2276 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002277 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002278 return r1;
2279 } else {
2280 return r2;
2281 }
2282}
2283
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002284/* dirty bits used to track which watermarks need changes */
2285#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2286#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2287#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2288#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2289#define WM_DIRTY_FBC (1 << 24)
2290#define WM_DIRTY_DDB (1 << 25)
2291
Damien Lespiau055e3932014-08-18 13:49:10 +01002292static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002293 const struct ilk_wm_values *old,
2294 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002295{
2296 unsigned int dirty = 0;
2297 enum pipe pipe;
2298 int wm_lp;
2299
Damien Lespiau055e3932014-08-18 13:49:10 +01002300 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002301 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2302 dirty |= WM_DIRTY_LINETIME(pipe);
2303 /* Must disable LP1+ watermarks too */
2304 dirty |= WM_DIRTY_LP_ALL;
2305 }
2306
2307 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2308 dirty |= WM_DIRTY_PIPE(pipe);
2309 /* Must disable LP1+ watermarks too */
2310 dirty |= WM_DIRTY_LP_ALL;
2311 }
2312 }
2313
2314 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2315 dirty |= WM_DIRTY_FBC;
2316 /* Must disable LP1+ watermarks too */
2317 dirty |= WM_DIRTY_LP_ALL;
2318 }
2319
2320 if (old->partitioning != new->partitioning) {
2321 dirty |= WM_DIRTY_DDB;
2322 /* Must disable LP1+ watermarks too */
2323 dirty |= WM_DIRTY_LP_ALL;
2324 }
2325
2326 /* LP1+ watermarks already deemed dirty, no need to continue */
2327 if (dirty & WM_DIRTY_LP_ALL)
2328 return dirty;
2329
2330 /* Find the lowest numbered LP1+ watermark in need of an update... */
2331 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2332 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2333 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2334 break;
2335 }
2336
2337 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2338 for (; wm_lp <= 3; wm_lp++)
2339 dirty |= WM_DIRTY_LP(wm_lp);
2340
2341 return dirty;
2342}
2343
Ville Syrjälä8553c182013-12-05 15:51:39 +02002344static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2345 unsigned int dirty)
2346{
Imre Deak820c1982013-12-17 14:46:36 +02002347 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002348 bool changed = false;
2349
2350 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2351 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2352 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2353 changed = true;
2354 }
2355 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2356 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2357 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2358 changed = true;
2359 }
2360 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2361 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2362 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2363 changed = true;
2364 }
2365
2366 /*
2367 * Don't touch WM1S_LP_EN here.
2368 * Doing so could cause underruns.
2369 */
2370
2371 return changed;
2372}
2373
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002374/*
2375 * The spec says we shouldn't write when we don't need, because every write
2376 * causes WMs to be re-evaluated, expending some power.
2377 */
Imre Deak820c1982013-12-17 14:46:36 +02002378static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2379 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002380{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002381 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002382 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002383 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002384 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002385
Damien Lespiau055e3932014-08-18 13:49:10 +01002386 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002387 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002388 return;
2389
Ville Syrjälä8553c182013-12-05 15:51:39 +02002390 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002391
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002392 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002393 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002394 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002395 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002396 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002397 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2398
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002399 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002400 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002401 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002402 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002403 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002404 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2405
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002406 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002407 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002408 val = I915_READ(WM_MISC);
2409 if (results->partitioning == INTEL_DDB_PART_1_2)
2410 val &= ~WM_MISC_DATA_PARTITION_5_6;
2411 else
2412 val |= WM_MISC_DATA_PARTITION_5_6;
2413 I915_WRITE(WM_MISC, val);
2414 } else {
2415 val = I915_READ(DISP_ARB_CTL2);
2416 if (results->partitioning == INTEL_DDB_PART_1_2)
2417 val &= ~DISP_DATA_PARTITION_5_6;
2418 else
2419 val |= DISP_DATA_PARTITION_5_6;
2420 I915_WRITE(DISP_ARB_CTL2, val);
2421 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002422 }
2423
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002424 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002425 val = I915_READ(DISP_ARB_CTL);
2426 if (results->enable_fbc_wm)
2427 val &= ~DISP_FBC_WM_DIS;
2428 else
2429 val |= DISP_FBC_WM_DIS;
2430 I915_WRITE(DISP_ARB_CTL, val);
2431 }
2432
Imre Deak954911e2013-12-17 14:46:34 +02002433 if (dirty & WM_DIRTY_LP(1) &&
2434 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2435 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2436
2437 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002438 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2439 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2440 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2441 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2442 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002443
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002444 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002445 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002446 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002447 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02002448 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002449 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002450
2451 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002452}
2453
Ville Syrjälä8553c182013-12-05 15:51:39 +02002454static bool ilk_disable_lp_wm(struct drm_device *dev)
2455{
2456 struct drm_i915_private *dev_priv = dev->dev_private;
2457
2458 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2459}
2460
Damien Lespiaub9cec072014-11-04 17:06:43 +00002461/*
2462 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2463 * different active planes.
2464 */
2465
2466#define SKL_DDB_SIZE 896 /* in blocks */
2467
2468static void
2469skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2470 struct drm_crtc *for_crtc,
2471 const struct intel_wm_config *config,
2472 const struct skl_pipe_wm_parameters *params,
2473 struct skl_ddb_entry *alloc /* out */)
2474{
2475 struct drm_crtc *crtc;
2476 unsigned int pipe_size, ddb_size;
2477 int nth_active_pipe;
2478
2479 if (!params->active) {
2480 alloc->start = 0;
2481 alloc->end = 0;
2482 return;
2483 }
2484
2485 ddb_size = SKL_DDB_SIZE;
2486
2487 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2488
2489 nth_active_pipe = 0;
2490 for_each_crtc(dev, crtc) {
Matt Roper3ef00282015-03-09 10:19:24 -07002491 if (!to_intel_crtc(crtc)->active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00002492 continue;
2493
2494 if (crtc == for_crtc)
2495 break;
2496
2497 nth_active_pipe++;
2498 }
2499
2500 pipe_size = ddb_size / config->num_pipes_active;
2501 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002502 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002503}
2504
2505static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2506{
2507 if (config->num_pipes_active == 1)
2508 return 32;
2509
2510 return 8;
2511}
2512
Damien Lespiaua269c582014-11-04 17:06:49 +00002513static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2514{
2515 entry->start = reg & 0x3ff;
2516 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002517 if (entry->end)
2518 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002519}
2520
Damien Lespiau08db6652014-11-04 17:06:52 +00002521void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2522 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002523{
Damien Lespiaua269c582014-11-04 17:06:49 +00002524 enum pipe pipe;
2525 int plane;
2526 u32 val;
2527
2528 for_each_pipe(dev_priv, pipe) {
Damien Lespiaudd740782015-02-28 14:54:08 +00002529 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiaua269c582014-11-04 17:06:49 +00002530 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2531 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2532 val);
2533 }
2534
2535 val = I915_READ(CUR_BUF_CFG(pipe));
2536 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2537 }
2538}
2539
Damien Lespiaub9cec072014-11-04 17:06:43 +00002540static unsigned int
2541skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2542{
2543 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2544}
2545
2546/*
2547 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2548 * a 8192x4096@32bpp framebuffer:
2549 * 3 * 4096 * 8192 * 4 < 2^32
2550 */
2551static unsigned int
2552skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2553 const struct skl_pipe_wm_parameters *params)
2554{
2555 unsigned int total_data_rate = 0;
2556 int plane;
2557
2558 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2559 const struct intel_plane_wm_parameters *p;
2560
2561 p = &params->plane[plane];
2562 if (!p->enabled)
2563 continue;
2564
2565 total_data_rate += skl_plane_relative_data_rate(p);
2566 }
2567
2568 return total_data_rate;
2569}
2570
2571static void
2572skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2573 const struct intel_wm_config *config,
2574 const struct skl_pipe_wm_parameters *params,
2575 struct skl_ddb_allocation *ddb /* out */)
2576{
2577 struct drm_device *dev = crtc->dev;
Damien Lespiaudd740782015-02-28 14:54:08 +00002578 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2580 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002581 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002582 uint16_t alloc_size, start, cursor_blocks;
Damien Lespiau80958152015-02-09 13:35:10 +00002583 uint16_t minimum[I915_MAX_PLANES];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002584 unsigned int total_data_rate;
2585 int plane;
2586
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002587 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2588 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002589 if (alloc_size == 0) {
2590 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2591 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2592 return;
2593 }
2594
2595 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002596 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2597 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002598
2599 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002600 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002601
Damien Lespiau80958152015-02-09 13:35:10 +00002602 /* 1. Allocate the mininum required blocks for each active plane */
Damien Lespiaudd740782015-02-28 14:54:08 +00002603 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau80958152015-02-09 13:35:10 +00002604 const struct intel_plane_wm_parameters *p;
2605
2606 p = &params->plane[plane];
2607 if (!p->enabled)
2608 continue;
2609
2610 minimum[plane] = 8;
2611 alloc_size -= minimum[plane];
2612 }
2613
Damien Lespiaub9cec072014-11-04 17:06:43 +00002614 /*
Damien Lespiau80958152015-02-09 13:35:10 +00002615 * 2. Distribute the remaining space in proportion to the amount of
2616 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00002617 *
2618 * FIXME: we may not allocate every single block here.
2619 */
2620 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2621
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002622 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002623 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2624 const struct intel_plane_wm_parameters *p;
2625 unsigned int data_rate;
2626 uint16_t plane_blocks;
2627
2628 p = &params->plane[plane];
2629 if (!p->enabled)
2630 continue;
2631
2632 data_rate = skl_plane_relative_data_rate(p);
2633
2634 /*
2635 * promote the expression to 64 bits to avoid overflowing, the
2636 * result is < available as data_rate / total_data_rate < 1
2637 */
Damien Lespiau80958152015-02-09 13:35:10 +00002638 plane_blocks = minimum[plane];
2639 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
2640 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002641
2642 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002643 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002644
2645 start += plane_blocks;
2646 }
2647
2648}
2649
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002650static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002651{
2652 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002653 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002654}
2655
2656/*
2657 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2658 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2659 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2660 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2661*/
2662static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2663 uint32_t latency)
2664{
2665 uint32_t wm_intermediate_val, ret;
2666
2667 if (latency == 0)
2668 return UINT_MAX;
2669
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002670 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel / 512;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002671 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2672
2673 return ret;
2674}
2675
2676static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2677 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002678 uint64_t tiling, uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002679{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002680 uint32_t ret;
2681 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2682 uint32_t wm_intermediate_val;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002683
2684 if (latency == 0)
2685 return UINT_MAX;
2686
2687 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002688
2689 if (tiling == I915_FORMAT_MOD_Y_TILED ||
2690 tiling == I915_FORMAT_MOD_Yf_TILED) {
2691 plane_bytes_per_line *= 4;
2692 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2693 plane_blocks_per_line /= 4;
2694 } else {
2695 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2696 }
2697
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002698 wm_intermediate_val = latency * pixel_rate;
2699 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002700 plane_blocks_per_line;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002701
2702 return ret;
2703}
2704
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002705static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2706 const struct intel_crtc *intel_crtc)
2707{
2708 struct drm_device *dev = intel_crtc->base.dev;
2709 struct drm_i915_private *dev_priv = dev->dev_private;
2710 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2711 enum pipe pipe = intel_crtc->pipe;
2712
2713 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2714 sizeof(new_ddb->plane[pipe])))
2715 return true;
2716
2717 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2718 sizeof(new_ddb->cursor[pipe])))
2719 return true;
2720
2721 return false;
2722}
2723
2724static void skl_compute_wm_global_parameters(struct drm_device *dev,
2725 struct intel_wm_config *config)
2726{
2727 struct drm_crtc *crtc;
2728 struct drm_plane *plane;
2729
2730 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
Matt Roper3ef00282015-03-09 10:19:24 -07002731 config->num_pipes_active += to_intel_crtc(crtc)->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002732
2733 /* FIXME: I don't think we need those two global parameters on SKL */
2734 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2735 struct intel_plane *intel_plane = to_intel_plane(plane);
2736
2737 config->sprites_enabled |= intel_plane->wm.enabled;
2738 config->sprites_scaled |= intel_plane->wm.scaled;
2739 }
2740}
2741
2742static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2743 struct skl_pipe_wm_parameters *p)
2744{
2745 struct drm_device *dev = crtc->dev;
2746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2747 enum pipe pipe = intel_crtc->pipe;
2748 struct drm_plane *plane;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002749 struct drm_framebuffer *fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002750 int i = 1; /* Index for sprite planes start */
2751
Matt Roper3ef00282015-03-09 10:19:24 -07002752 p->active = intel_crtc->active;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002753 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002754 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2755 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002756
2757 /*
2758 * For now, assume primary and cursor planes are always enabled.
2759 */
2760 p->plane[0].enabled = true;
2761 p->plane[0].bytes_per_pixel =
Matt Roper59bea882015-02-27 10:12:01 -08002762 crtc->primary->state->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002763 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2764 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002765 p->plane[0].tiling = DRM_FORMAT_MOD_NONE;
2766 fb = crtc->primary->state->fb;
2767 /*
2768 * Framebuffer can be NULL on plane disable, but it does not
2769 * matter for watermarks if we assume no tiling in that case.
2770 */
2771 if (fb)
2772 p->plane[0].tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002773
2774 p->cursor.enabled = true;
2775 p->cursor.bytes_per_pixel = 4;
Matt Roper3dd512f2015-02-27 10:12:00 -08002776 p->cursor.horiz_pixels = intel_crtc->base.cursor->state->crtc_w ?
2777 intel_crtc->base.cursor->state->crtc_w : 64;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002778 }
2779
2780 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2781 struct intel_plane *intel_plane = to_intel_plane(plane);
2782
Sonika Jindala712f8e2014-12-09 10:59:15 +05302783 if (intel_plane->pipe == pipe &&
2784 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002785 p->plane[i++] = intel_plane->wm;
2786 }
2787}
2788
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002789static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
2790 struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002791 struct intel_plane_wm_parameters *p_params,
2792 uint16_t ddb_allocation,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002793 int level,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002794 uint16_t *out_blocks, /* out */
2795 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002796{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002797 uint32_t latency = dev_priv->wm.skl_latency[level];
2798 uint32_t method1, method2;
2799 uint32_t plane_bytes_per_line, plane_blocks_per_line;
2800 uint32_t res_blocks, res_lines;
2801 uint32_t selected_result;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002802
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002803 if (latency == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002804 return false;
2805
2806 method1 = skl_wm_method1(p->pixel_rate,
2807 p_params->bytes_per_pixel,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002808 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002809 method2 = skl_wm_method2(p->pixel_rate,
2810 p->pipe_htotal,
2811 p_params->horiz_pixels,
2812 p_params->bytes_per_pixel,
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002813 p_params->tiling,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002814 latency);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002815
2816 plane_bytes_per_line = p_params->horiz_pixels *
2817 p_params->bytes_per_pixel;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002818 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002819
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002820 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2821 p_params->tiling == I915_FORMAT_MOD_Yf_TILED) {
2822 uint32_t y_tile_minimum = plane_blocks_per_line * 4;
2823 selected_result = max(method2, y_tile_minimum);
2824 } else {
2825 if ((ddb_allocation / plane_blocks_per_line) >= 1)
2826 selected_result = min(method1, method2);
2827 else
2828 selected_result = method1;
2829 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002830
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002831 res_blocks = selected_result + 1;
2832 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
Damien Lespiaue6d66172014-11-04 17:06:55 +00002833
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00002834 if (level >= 1 && level <= 7) {
2835 if (p_params->tiling == I915_FORMAT_MOD_Y_TILED ||
2836 p_params->tiling == I915_FORMAT_MOD_Yf_TILED)
2837 res_lines += 4;
2838 else
2839 res_blocks++;
2840 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002841
2842 if (res_blocks >= ddb_allocation || res_lines > 31)
Damien Lespiaue6d66172014-11-04 17:06:55 +00002843 return false;
2844
2845 *out_blocks = res_blocks;
2846 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002847
2848 return true;
2849}
2850
2851static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2852 struct skl_ddb_allocation *ddb,
2853 struct skl_pipe_wm_parameters *p,
2854 enum pipe pipe,
2855 int level,
2856 int num_planes,
2857 struct skl_wm_level *result)
2858{
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002859 uint16_t ddb_blocks;
2860 int i;
2861
2862 for (i = 0; i < num_planes; i++) {
2863 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2864
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002865 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
2866 p, &p->plane[i],
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002867 ddb_blocks,
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002868 level,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002869 &result->plane_res_b[i],
2870 &result->plane_res_l[i]);
2871 }
2872
2873 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00002874 result->cursor_en = skl_compute_plane_wm(dev_priv, p, &p->cursor,
2875 ddb_blocks, level,
2876 &result->cursor_res_b,
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002877 &result->cursor_res_l);
2878}
2879
Damien Lespiau407b50f2014-11-04 17:06:57 +00002880static uint32_t
2881skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2882{
Matt Roper3ef00282015-03-09 10:19:24 -07002883 if (!to_intel_crtc(crtc)->active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002884 return 0;
2885
2886 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2887
2888}
2889
2890static void skl_compute_transition_wm(struct drm_crtc *crtc,
2891 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002892 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002893{
Damien Lespiau9414f562014-11-04 17:06:58 +00002894 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2895 int i;
2896
Damien Lespiau407b50f2014-11-04 17:06:57 +00002897 if (!params->active)
2898 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002899
2900 /* Until we know more, just disable transition WMs */
2901 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2902 trans_wm->plane_en[i] = false;
2903 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002904}
2905
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002906static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2907 struct skl_ddb_allocation *ddb,
2908 struct skl_pipe_wm_parameters *params,
2909 struct skl_pipe_wm *pipe_wm)
2910{
2911 struct drm_device *dev = crtc->dev;
2912 const struct drm_i915_private *dev_priv = dev->dev_private;
2913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2914 int level, max_level = ilk_wm_max_level(dev);
2915
2916 for (level = 0; level <= max_level; level++) {
2917 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2918 level, intel_num_planes(intel_crtc),
2919 &pipe_wm->wm[level]);
2920 }
2921 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2922
Damien Lespiau9414f562014-11-04 17:06:58 +00002923 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002924}
2925
2926static void skl_compute_wm_results(struct drm_device *dev,
2927 struct skl_pipe_wm_parameters *p,
2928 struct skl_pipe_wm *p_wm,
2929 struct skl_wm_values *r,
2930 struct intel_crtc *intel_crtc)
2931{
2932 int level, max_level = ilk_wm_max_level(dev);
2933 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002934 uint32_t temp;
2935 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002936
2937 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002938 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2939 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002940
2941 temp |= p_wm->wm[level].plane_res_l[i] <<
2942 PLANE_WM_LINES_SHIFT;
2943 temp |= p_wm->wm[level].plane_res_b[i];
2944 if (p_wm->wm[level].plane_en[i])
2945 temp |= PLANE_WM_EN;
2946
2947 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002948 }
2949
2950 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002951
2952 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2953 temp |= p_wm->wm[level].cursor_res_b;
2954
2955 if (p_wm->wm[level].cursor_en)
2956 temp |= PLANE_WM_EN;
2957
2958 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002959
2960 }
2961
Damien Lespiau9414f562014-11-04 17:06:58 +00002962 /* transition WMs */
2963 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2964 temp = 0;
2965 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2966 temp |= p_wm->trans_wm.plane_res_b[i];
2967 if (p_wm->trans_wm.plane_en[i])
2968 temp |= PLANE_WM_EN;
2969
2970 r->plane_trans[pipe][i] = temp;
2971 }
2972
2973 temp = 0;
2974 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2975 temp |= p_wm->trans_wm.cursor_res_b;
2976 if (p_wm->trans_wm.cursor_en)
2977 temp |= PLANE_WM_EN;
2978
2979 r->cursor_trans[pipe] = temp;
2980
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002981 r->wm_linetime[pipe] = p_wm->linetime;
2982}
2983
Damien Lespiau16160e32014-11-04 17:06:53 +00002984static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2985 const struct skl_ddb_entry *entry)
2986{
2987 if (entry->end)
2988 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2989 else
2990 I915_WRITE(reg, 0);
2991}
2992
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002993static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2994 const struct skl_wm_values *new)
2995{
2996 struct drm_device *dev = dev_priv->dev;
2997 struct intel_crtc *crtc;
2998
2999 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3000 int i, level, max_level = ilk_wm_max_level(dev);
3001 enum pipe pipe = crtc->pipe;
3002
Damien Lespiau5d374d92014-11-04 17:07:00 +00003003 if (!new->dirty[pipe])
3004 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003005
Damien Lespiau5d374d92014-11-04 17:07:00 +00003006 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3007
3008 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003009 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00003010 I915_WRITE(PLANE_WM(pipe, i, level),
3011 new->plane[pipe][i][level]);
3012 I915_WRITE(CUR_WM(pipe, level),
3013 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003014 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00003015 for (i = 0; i < intel_num_planes(crtc); i++)
3016 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3017 new->plane_trans[pipe][i]);
3018 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
3019
3020 for (i = 0; i < intel_num_planes(crtc); i++)
3021 skl_ddb_entry_write(dev_priv,
3022 PLANE_BUF_CFG(pipe, i),
3023 &new->ddb.plane[pipe][i]);
3024
3025 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3026 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003027 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003028}
3029
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003030/*
3031 * When setting up a new DDB allocation arrangement, we need to correctly
3032 * sequence the times at which the new allocations for the pipes are taken into
3033 * account or we'll have pipes fetching from space previously allocated to
3034 * another pipe.
3035 *
3036 * Roughly the sequence looks like:
3037 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3038 * overlapping with a previous light-up pipe (another way to put it is:
3039 * pipes with their new allocation strickly included into their old ones).
3040 * 2. re-allocate the other pipes that get their allocation reduced
3041 * 3. allocate the pipes having their allocation increased
3042 *
3043 * Steps 1. and 2. are here to take care of the following case:
3044 * - Initially DDB looks like this:
3045 * | B | C |
3046 * - enable pipe A.
3047 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3048 * allocation
3049 * | A | B | C |
3050 *
3051 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3052 */
3053
Damien Lespiaud21b7952014-11-04 17:07:03 +00003054static void
3055skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003056{
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003057 int plane;
3058
Damien Lespiaud21b7952014-11-04 17:07:03 +00003059 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3060
Damien Lespiaudd740782015-02-28 14:54:08 +00003061 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003062 I915_WRITE(PLANE_SURF(pipe, plane),
3063 I915_READ(PLANE_SURF(pipe, plane)));
3064 }
3065 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3066}
3067
3068static bool
3069skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3070 const struct skl_ddb_allocation *new,
3071 enum pipe pipe)
3072{
3073 uint16_t old_size, new_size;
3074
3075 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3076 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3077
3078 return old_size != new_size &&
3079 new->pipe[pipe].start >= old->pipe[pipe].start &&
3080 new->pipe[pipe].end <= old->pipe[pipe].end;
3081}
3082
3083static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3084 struct skl_wm_values *new_values)
3085{
3086 struct drm_device *dev = dev_priv->dev;
3087 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3088 bool reallocated[I915_MAX_PIPES] = {false, false, false};
3089 struct intel_crtc *crtc;
3090 enum pipe pipe;
3091
3092 new_ddb = &new_values->ddb;
3093 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3094
3095 /*
3096 * First pass: flush the pipes with the new allocation contained into
3097 * the old space.
3098 *
3099 * We'll wait for the vblank on those pipes to ensure we can safely
3100 * re-allocate the freed space without this pipe fetching from it.
3101 */
3102 for_each_intel_crtc(dev, crtc) {
3103 if (!crtc->active)
3104 continue;
3105
3106 pipe = crtc->pipe;
3107
3108 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3109 continue;
3110
Damien Lespiaud21b7952014-11-04 17:07:03 +00003111 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003112 intel_wait_for_vblank(dev, pipe);
3113
3114 reallocated[pipe] = true;
3115 }
3116
3117
3118 /*
3119 * Second pass: flush the pipes that are having their allocation
3120 * reduced, but overlapping with a previous allocation.
3121 *
3122 * Here as well we need to wait for the vblank to make sure the freed
3123 * space is not used anymore.
3124 */
3125 for_each_intel_crtc(dev, crtc) {
3126 if (!crtc->active)
3127 continue;
3128
3129 pipe = crtc->pipe;
3130
3131 if (reallocated[pipe])
3132 continue;
3133
3134 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3135 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003136 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003137 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303138 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003139 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003140 }
3141
3142 /*
3143 * Third pass: flush the pipes that got more space allocated.
3144 *
3145 * We don't need to actively wait for the update here, next vblank
3146 * will just get more DDB space with the correct WM values.
3147 */
3148 for_each_intel_crtc(dev, crtc) {
3149 if (!crtc->active)
3150 continue;
3151
3152 pipe = crtc->pipe;
3153
3154 /*
3155 * At this point, only the pipes more space than before are
3156 * left to re-allocate.
3157 */
3158 if (reallocated[pipe])
3159 continue;
3160
Damien Lespiaud21b7952014-11-04 17:07:03 +00003161 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003162 }
3163}
3164
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003165static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3166 struct skl_pipe_wm_parameters *params,
3167 struct intel_wm_config *config,
3168 struct skl_ddb_allocation *ddb, /* out */
3169 struct skl_pipe_wm *pipe_wm /* out */)
3170{
3171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3172
3173 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003174 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003175 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3176
3177 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3178 return false;
3179
3180 intel_crtc->wm.skl_active = *pipe_wm;
3181 return true;
3182}
3183
3184static void skl_update_other_pipe_wm(struct drm_device *dev,
3185 struct drm_crtc *crtc,
3186 struct intel_wm_config *config,
3187 struct skl_wm_values *r)
3188{
3189 struct intel_crtc *intel_crtc;
3190 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3191
3192 /*
3193 * If the WM update hasn't changed the allocation for this_crtc (the
3194 * crtc we are currently computing the new WM values for), other
3195 * enabled crtcs will keep the same allocation and we don't need to
3196 * recompute anything for them.
3197 */
3198 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3199 return;
3200
3201 /*
3202 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3203 * other active pipes need new DDB allocation and WM values.
3204 */
3205 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3206 base.head) {
3207 struct skl_pipe_wm_parameters params = {};
3208 struct skl_pipe_wm pipe_wm = {};
3209 bool wm_changed;
3210
3211 if (this_crtc->pipe == intel_crtc->pipe)
3212 continue;
3213
3214 if (!intel_crtc->active)
3215 continue;
3216
3217 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3218 &params, config,
3219 &r->ddb, &pipe_wm);
3220
3221 /*
3222 * If we end up re-computing the other pipe WM values, it's
3223 * because it was really needed, so we expect the WM values to
3224 * be different.
3225 */
3226 WARN_ON(!wm_changed);
3227
3228 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3229 r->dirty[intel_crtc->pipe] = true;
3230 }
3231}
3232
3233static void skl_update_wm(struct drm_crtc *crtc)
3234{
3235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3236 struct drm_device *dev = crtc->dev;
3237 struct drm_i915_private *dev_priv = dev->dev_private;
3238 struct skl_pipe_wm_parameters params = {};
3239 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3240 struct skl_pipe_wm pipe_wm = {};
3241 struct intel_wm_config config = {};
3242
3243 memset(results, 0, sizeof(*results));
3244
3245 skl_compute_wm_global_parameters(dev, &config);
3246
3247 if (!skl_update_pipe_wm(crtc, &params, &config,
3248 &results->ddb, &pipe_wm))
3249 return;
3250
3251 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3252 results->dirty[intel_crtc->pipe] = true;
3253
3254 skl_update_other_pipe_wm(dev, crtc, &config, results);
3255 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003256 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003257
3258 /* store the new configuration */
3259 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003260}
3261
3262static void
3263skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3264 uint32_t sprite_width, uint32_t sprite_height,
3265 int pixel_size, bool enabled, bool scaled)
3266{
3267 struct intel_plane *intel_plane = to_intel_plane(plane);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003268 struct drm_framebuffer *fb = plane->state->fb;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003269
3270 intel_plane->wm.enabled = enabled;
3271 intel_plane->wm.scaled = scaled;
3272 intel_plane->wm.horiz_pixels = sprite_width;
3273 intel_plane->wm.vert_pixels = sprite_height;
3274 intel_plane->wm.bytes_per_pixel = pixel_size;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003275 intel_plane->wm.tiling = DRM_FORMAT_MOD_NONE;
3276 /*
3277 * Framebuffer can be NULL on plane disable, but it does not
3278 * matter for watermarks if we assume no tiling in that case.
3279 */
3280 if (fb)
3281 intel_plane->wm.tiling = fb->modifier[0];
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003282
3283 skl_update_wm(crtc);
3284}
3285
Imre Deak820c1982013-12-17 14:46:36 +02003286static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003287{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003289 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003290 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003291 struct ilk_wm_maximums max;
3292 struct ilk_pipe_wm_parameters params = {};
3293 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003294 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003295 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003296 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003297 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003298
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003299 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003300
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003301 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3302
3303 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3304 return;
3305
3306 intel_crtc->wm.active = pipe_wm;
3307
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003308 ilk_compute_wm_config(dev, &config);
3309
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003310 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003311 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003312
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003313 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003314 if (INTEL_INFO(dev)->gen >= 7 &&
3315 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003316 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003317 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003318
Imre Deak820c1982013-12-17 14:46:36 +02003319 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003320 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003321 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003322 }
3323
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003324 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003325 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003326
Imre Deak820c1982013-12-17 14:46:36 +02003327 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003328
Imre Deak820c1982013-12-17 14:46:36 +02003329 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003330}
3331
Damien Lespiaued57cb82014-07-15 09:21:24 +02003332static void
3333ilk_update_sprite_wm(struct drm_plane *plane,
3334 struct drm_crtc *crtc,
3335 uint32_t sprite_width, uint32_t sprite_height,
3336 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003337{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003338 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003339 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003340
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003341 intel_plane->wm.enabled = enabled;
3342 intel_plane->wm.scaled = scaled;
3343 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003344 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003345 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003346
Ville Syrjälä8553c182013-12-05 15:51:39 +02003347 /*
3348 * IVB workaround: must disable low power watermarks for at least
3349 * one frame before enabling scaling. LP watermarks can be re-enabled
3350 * when scaling is disabled.
3351 *
3352 * WaCxSRDisabledForSpriteScaling:ivb
3353 */
3354 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3355 intel_wait_for_vblank(dev, intel_plane->pipe);
3356
Imre Deak820c1982013-12-17 14:46:36 +02003357 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003358}
3359
Pradeep Bhat30789992014-11-04 17:06:45 +00003360static void skl_pipe_wm_active_state(uint32_t val,
3361 struct skl_pipe_wm *active,
3362 bool is_transwm,
3363 bool is_cursor,
3364 int i,
3365 int level)
3366{
3367 bool is_enabled = (val & PLANE_WM_EN) != 0;
3368
3369 if (!is_transwm) {
3370 if (!is_cursor) {
3371 active->wm[level].plane_en[i] = is_enabled;
3372 active->wm[level].plane_res_b[i] =
3373 val & PLANE_WM_BLOCKS_MASK;
3374 active->wm[level].plane_res_l[i] =
3375 (val >> PLANE_WM_LINES_SHIFT) &
3376 PLANE_WM_LINES_MASK;
3377 } else {
3378 active->wm[level].cursor_en = is_enabled;
3379 active->wm[level].cursor_res_b =
3380 val & PLANE_WM_BLOCKS_MASK;
3381 active->wm[level].cursor_res_l =
3382 (val >> PLANE_WM_LINES_SHIFT) &
3383 PLANE_WM_LINES_MASK;
3384 }
3385 } else {
3386 if (!is_cursor) {
3387 active->trans_wm.plane_en[i] = is_enabled;
3388 active->trans_wm.plane_res_b[i] =
3389 val & PLANE_WM_BLOCKS_MASK;
3390 active->trans_wm.plane_res_l[i] =
3391 (val >> PLANE_WM_LINES_SHIFT) &
3392 PLANE_WM_LINES_MASK;
3393 } else {
3394 active->trans_wm.cursor_en = is_enabled;
3395 active->trans_wm.cursor_res_b =
3396 val & PLANE_WM_BLOCKS_MASK;
3397 active->trans_wm.cursor_res_l =
3398 (val >> PLANE_WM_LINES_SHIFT) &
3399 PLANE_WM_LINES_MASK;
3400 }
3401 }
3402}
3403
3404static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3405{
3406 struct drm_device *dev = crtc->dev;
3407 struct drm_i915_private *dev_priv = dev->dev_private;
3408 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3409 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3410 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3411 enum pipe pipe = intel_crtc->pipe;
3412 int level, i, max_level;
3413 uint32_t temp;
3414
3415 max_level = ilk_wm_max_level(dev);
3416
3417 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3418
3419 for (level = 0; level <= max_level; level++) {
3420 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3421 hw->plane[pipe][i][level] =
3422 I915_READ(PLANE_WM(pipe, i, level));
3423 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3424 }
3425
3426 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3427 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3428 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3429
Matt Roper3ef00282015-03-09 10:19:24 -07003430 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00003431 return;
3432
3433 hw->dirty[pipe] = true;
3434
3435 active->linetime = hw->wm_linetime[pipe];
3436
3437 for (level = 0; level <= max_level; level++) {
3438 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3439 temp = hw->plane[pipe][i][level];
3440 skl_pipe_wm_active_state(temp, active, false,
3441 false, i, level);
3442 }
3443 temp = hw->cursor[pipe][level];
3444 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3445 }
3446
3447 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3448 temp = hw->plane_trans[pipe][i];
3449 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3450 }
3451
3452 temp = hw->cursor_trans[pipe];
3453 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3454}
3455
3456void skl_wm_get_hw_state(struct drm_device *dev)
3457{
Damien Lespiaua269c582014-11-04 17:06:49 +00003458 struct drm_i915_private *dev_priv = dev->dev_private;
3459 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003460 struct drm_crtc *crtc;
3461
Damien Lespiaua269c582014-11-04 17:06:49 +00003462 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003463 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3464 skl_pipe_wm_get_hw_state(crtc);
3465}
3466
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003467static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3468{
3469 struct drm_device *dev = crtc->dev;
3470 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003471 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003472 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3473 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3474 enum pipe pipe = intel_crtc->pipe;
3475 static const unsigned int wm0_pipe_reg[] = {
3476 [PIPE_A] = WM0_PIPEA_ILK,
3477 [PIPE_B] = WM0_PIPEB_ILK,
3478 [PIPE_C] = WM0_PIPEC_IVB,
3479 };
3480
3481 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003482 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003483 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003484
Matt Roper3ef00282015-03-09 10:19:24 -07003485 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003486
3487 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003488 u32 tmp = hw->wm_pipe[pipe];
3489
3490 /*
3491 * For active pipes LP0 watermark is marked as
3492 * enabled, and LP1+ watermaks as disabled since
3493 * we can't really reverse compute them in case
3494 * multiple pipes are active.
3495 */
3496 active->wm[0].enable = true;
3497 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3498 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3499 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3500 active->linetime = hw->wm_linetime[pipe];
3501 } else {
3502 int level, max_level = ilk_wm_max_level(dev);
3503
3504 /*
3505 * For inactive pipes, all watermark levels
3506 * should be marked as enabled but zeroed,
3507 * which is what we'd compute them to.
3508 */
3509 for (level = 0; level <= max_level; level++)
3510 active->wm[level].enable = true;
3511 }
3512}
3513
3514void ilk_wm_get_hw_state(struct drm_device *dev)
3515{
3516 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003517 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003518 struct drm_crtc *crtc;
3519
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003520 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003521 ilk_pipe_wm_get_hw_state(crtc);
3522
3523 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3524 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3525 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3526
3527 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003528 if (INTEL_INFO(dev)->gen >= 7) {
3529 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3530 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3531 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003532
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003533 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003534 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3535 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3536 else if (IS_IVYBRIDGE(dev))
3537 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3538 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003539
3540 hw->enable_fbc_wm =
3541 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3542}
3543
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003544/**
3545 * intel_update_watermarks - update FIFO watermark values based on current modes
3546 *
3547 * Calculate watermark values for the various WM regs based on current mode
3548 * and plane configuration.
3549 *
3550 * There are several cases to deal with here:
3551 * - normal (i.e. non-self-refresh)
3552 * - self-refresh (SR) mode
3553 * - lines are large relative to FIFO size (buffer can hold up to 2)
3554 * - lines are small relative to FIFO size (buffer can hold more than 2
3555 * lines), so need to account for TLB latency
3556 *
3557 * The normal calculation is:
3558 * watermark = dotclock * bytes per pixel * latency
3559 * where latency is platform & configuration dependent (we assume pessimal
3560 * values here).
3561 *
3562 * The SR calculation is:
3563 * watermark = (trunc(latency/line time)+1) * surface width *
3564 * bytes per pixel
3565 * where
3566 * line time = htotal / dotclock
3567 * surface width = hdisplay for normal plane and 64 for cursor
3568 * and latency is assumed to be high, as above.
3569 *
3570 * The final value programmed to the register should always be rounded up,
3571 * and include an extra 2 entries to account for clock crossings.
3572 *
3573 * We don't use the sprite, so we can ignore that. And on Crestline we have
3574 * to set the non-SR watermarks to 8.
3575 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003576void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003577{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003578 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003579
3580 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003581 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003582}
3583
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003584void intel_update_sprite_watermarks(struct drm_plane *plane,
3585 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003586 uint32_t sprite_width,
3587 uint32_t sprite_height,
3588 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003589 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003590{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003591 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003592
3593 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003594 dev_priv->display.update_sprite_wm(plane, crtc,
3595 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003596 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003597}
3598
Daniel Vetter92703882012-08-09 16:46:01 +02003599/**
3600 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003601 */
3602DEFINE_SPINLOCK(mchdev_lock);
3603
3604/* Global for IPS driver to get at the current i915 device. Protected by
3605 * mchdev_lock. */
3606static struct drm_i915_private *i915_mch_dev;
3607
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003608bool ironlake_set_drps(struct drm_device *dev, u8 val)
3609{
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611 u16 rgvswctl;
3612
Daniel Vetter92703882012-08-09 16:46:01 +02003613 assert_spin_locked(&mchdev_lock);
3614
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003615 rgvswctl = I915_READ16(MEMSWCTL);
3616 if (rgvswctl & MEMCTL_CMD_STS) {
3617 DRM_DEBUG("gpu busy, RCS change rejected\n");
3618 return false; /* still busy with another command */
3619 }
3620
3621 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3622 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3623 I915_WRITE16(MEMSWCTL, rgvswctl);
3624 POSTING_READ16(MEMSWCTL);
3625
3626 rgvswctl |= MEMCTL_CMD_STS;
3627 I915_WRITE16(MEMSWCTL, rgvswctl);
3628
3629 return true;
3630}
3631
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003632static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003633{
3634 struct drm_i915_private *dev_priv = dev->dev_private;
3635 u32 rgvmodectl = I915_READ(MEMMODECTL);
3636 u8 fmax, fmin, fstart, vstart;
3637
Daniel Vetter92703882012-08-09 16:46:01 +02003638 spin_lock_irq(&mchdev_lock);
3639
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003640 /* Enable temp reporting */
3641 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3642 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3643
3644 /* 100ms RC evaluation intervals */
3645 I915_WRITE(RCUPEI, 100000);
3646 I915_WRITE(RCDNEI, 100000);
3647
3648 /* Set max/min thresholds to 90ms and 80ms respectively */
3649 I915_WRITE(RCBMAXAVG, 90000);
3650 I915_WRITE(RCBMINAVG, 80000);
3651
3652 I915_WRITE(MEMIHYST, 1);
3653
3654 /* Set up min, max, and cur for interrupt handling */
3655 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3656 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3657 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3658 MEMMODE_FSTART_SHIFT;
3659
3660 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3661 PXVFREQ_PX_SHIFT;
3662
Daniel Vetter20e4d402012-08-08 23:35:39 +02003663 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3664 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003665
Daniel Vetter20e4d402012-08-08 23:35:39 +02003666 dev_priv->ips.max_delay = fstart;
3667 dev_priv->ips.min_delay = fmin;
3668 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003669
3670 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3671 fmax, fmin, fstart);
3672
3673 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3674
3675 /*
3676 * Interrupts will be enabled in ironlake_irq_postinstall
3677 */
3678
3679 I915_WRITE(VIDSTART, vstart);
3680 POSTING_READ(VIDSTART);
3681
3682 rgvmodectl |= MEMMODE_SWMODE_EN;
3683 I915_WRITE(MEMMODECTL, rgvmodectl);
3684
Daniel Vetter92703882012-08-09 16:46:01 +02003685 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003686 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003687 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003688
3689 ironlake_set_drps(dev, fstart);
3690
Daniel Vetter20e4d402012-08-08 23:35:39 +02003691 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003692 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003693 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3694 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003695 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003696
3697 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003698}
3699
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003700static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003701{
3702 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003703 u16 rgvswctl;
3704
3705 spin_lock_irq(&mchdev_lock);
3706
3707 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003708
3709 /* Ack interrupts, disable EFC interrupt */
3710 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3711 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3712 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3713 I915_WRITE(DEIIR, DE_PCU_EVENT);
3714 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3715
3716 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003717 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003718 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003719 rgvswctl |= MEMCTL_CMD_STS;
3720 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003721 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003722
Daniel Vetter92703882012-08-09 16:46:01 +02003723 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003724}
3725
Daniel Vetteracbe9472012-07-26 11:50:05 +02003726/* There's a funny hw issue where the hw returns all 0 when reading from
3727 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3728 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3729 * all limits and the gpu stuck at whatever frequency it is at atm).
3730 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003731static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003732{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003733 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003734
Daniel Vetter20b46e52012-07-26 11:16:14 +02003735 /* Only set the down limit when we've reached the lowest level to avoid
3736 * getting more interrupts, otherwise leave this clear. This prevents a
3737 * race in the hw when coming out of rc6: There's a tiny window where
3738 * the hw runs at the minimal clock before selecting the desired
3739 * frequency, if the down threshold expires in that window we will not
3740 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003741 limits = dev_priv->rps.max_freq_softlimit << 24;
3742 if (val <= dev_priv->rps.min_freq_softlimit)
3743 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003744
3745 return limits;
3746}
3747
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003748static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3749{
3750 int new_power;
3751
3752 new_power = dev_priv->rps.power;
3753 switch (dev_priv->rps.power) {
3754 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003755 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003756 new_power = BETWEEN;
3757 break;
3758
3759 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003760 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003761 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003762 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003763 new_power = HIGH_POWER;
3764 break;
3765
3766 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003767 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 +01003768 new_power = BETWEEN;
3769 break;
3770 }
3771 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003772 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003773 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003774 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003775 new_power = HIGH_POWER;
3776 if (new_power == dev_priv->rps.power)
3777 return;
3778
3779 /* Note the units here are not exactly 1us, but 1280ns. */
3780 switch (new_power) {
3781 case LOW_POWER:
3782 /* Upclock if more than 95% busy over 16ms */
3783 I915_WRITE(GEN6_RP_UP_EI, 12500);
3784 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3785
3786 /* Downclock if less than 85% busy over 32ms */
3787 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3788 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3789
3790 I915_WRITE(GEN6_RP_CONTROL,
3791 GEN6_RP_MEDIA_TURBO |
3792 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3793 GEN6_RP_MEDIA_IS_GFX |
3794 GEN6_RP_ENABLE |
3795 GEN6_RP_UP_BUSY_AVG |
3796 GEN6_RP_DOWN_IDLE_AVG);
3797 break;
3798
3799 case BETWEEN:
3800 /* Upclock if more than 90% busy over 13ms */
3801 I915_WRITE(GEN6_RP_UP_EI, 10250);
3802 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3803
3804 /* Downclock if less than 75% busy over 32ms */
3805 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3806 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3807
3808 I915_WRITE(GEN6_RP_CONTROL,
3809 GEN6_RP_MEDIA_TURBO |
3810 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3811 GEN6_RP_MEDIA_IS_GFX |
3812 GEN6_RP_ENABLE |
3813 GEN6_RP_UP_BUSY_AVG |
3814 GEN6_RP_DOWN_IDLE_AVG);
3815 break;
3816
3817 case HIGH_POWER:
3818 /* Upclock if more than 85% busy over 10ms */
3819 I915_WRITE(GEN6_RP_UP_EI, 8000);
3820 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3821
3822 /* Downclock if less than 60% busy over 32ms */
3823 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3824 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3825
3826 I915_WRITE(GEN6_RP_CONTROL,
3827 GEN6_RP_MEDIA_TURBO |
3828 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3829 GEN6_RP_MEDIA_IS_GFX |
3830 GEN6_RP_ENABLE |
3831 GEN6_RP_UP_BUSY_AVG |
3832 GEN6_RP_DOWN_IDLE_AVG);
3833 break;
3834 }
3835
3836 dev_priv->rps.power = new_power;
3837 dev_priv->rps.last_adj = 0;
3838}
3839
Chris Wilson2876ce72014-03-28 08:03:34 +00003840static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3841{
3842 u32 mask = 0;
3843
3844 if (val > dev_priv->rps.min_freq_softlimit)
3845 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3846 if (val < dev_priv->rps.max_freq_softlimit)
3847 mask |= GEN6_PM_RP_UP_THRESHOLD;
3848
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003849 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3850 mask &= dev_priv->pm_rps_events;
3851
Imre Deak59d02a12014-12-19 19:33:26 +02003852 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003853}
3854
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003855/* gen6_set_rps is called to update the frequency request, but should also be
3856 * called when the range (min_delay and max_delay) is modified so that we can
3857 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003858static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003859{
3860 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003861
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003862 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003863 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3864 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003865
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003866 /* min/max delay may still have been modified so be sure to
3867 * write the limits value.
3868 */
3869 if (val != dev_priv->rps.cur_freq) {
3870 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003871
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003872 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003873 I915_WRITE(GEN6_RPNSWREQ,
3874 HSW_FREQUENCY(val));
3875 else
3876 I915_WRITE(GEN6_RPNSWREQ,
3877 GEN6_FREQUENCY(val) |
3878 GEN6_OFFSET(0) |
3879 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003880 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003881
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003882 /* Make sure we continue to get interrupts
3883 * until we hit the minimum or maximum frequencies.
3884 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003885 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003886 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003887
Ben Widawskyd5570a72012-09-07 19:43:41 -07003888 POSTING_READ(GEN6_RPNSWREQ);
3889
Ben Widawskyb39fb292014-03-19 18:31:11 -07003890 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde9a2012-08-30 13:26:48 +02003891 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003892}
3893
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003894static void valleyview_set_rps(struct drm_device *dev, u8 val)
3895{
3896 struct drm_i915_private *dev_priv = dev->dev_private;
3897
3898 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3899 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3900 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3901
3902 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3903 "Odd GPU freq value\n"))
3904 val &= ~1;
3905
3906 if (val != dev_priv->rps.cur_freq)
3907 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3908
3909 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3910
3911 dev_priv->rps.cur_freq = val;
3912 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3913}
3914
Deepak S76c3552f2014-01-30 23:08:16 +05303915/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3916 *
3917 * * If Gfx is Idle, then
3918 * 1. Mask Turbo interrupts
3919 * 2. Bring up Gfx clock
3920 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3921 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3922 * 5. Unmask Turbo interrupts
3923*/
3924static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3925{
Deepak S5549d252014-06-28 11:26:11 +05303926 struct drm_device *dev = dev_priv->dev;
3927
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003928 /* CHV and latest VLV don't need to force the gfx clock */
3929 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303930 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3931 return;
3932 }
3933
Deepak S76c3552f2014-01-30 23:08:16 +05303934 /*
3935 * When we are idle. Drop to min voltage state.
3936 */
3937
Ben Widawskyb39fb292014-03-19 18:31:11 -07003938 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303939 return;
3940
3941 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003942 I915_WRITE(GEN6_PMINTRMSK,
3943 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303944
Imre Deak650ad972014-04-18 16:35:02 +03003945 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303946
Ben Widawskyb39fb292014-03-19 18:31:11 -07003947 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303948
3949 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003950 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303951
3952 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003953 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303954 DRM_ERROR("timed out waiting for Punit\n");
3955
Imre Deak650ad972014-04-18 16:35:02 +03003956 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303957
Chris Wilson2876ce72014-03-28 08:03:34 +00003958 I915_WRITE(GEN6_PMINTRMSK,
3959 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303960}
3961
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003962void gen6_rps_idle(struct drm_i915_private *dev_priv)
3963{
Damien Lespiau691bb712013-12-12 14:36:36 +00003964 struct drm_device *dev = dev_priv->dev;
3965
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003966 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003967 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003968 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303969 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003970 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003971 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003972 dev_priv->rps.last_adj = 0;
3973 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003974 mutex_unlock(&dev_priv->rps.hw_lock);
3975}
3976
3977void gen6_rps_boost(struct drm_i915_private *dev_priv)
3978{
3979 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003980 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003981 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003982 dev_priv->rps.last_adj = 0;
3983 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003984 mutex_unlock(&dev_priv->rps.hw_lock);
3985}
3986
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003987void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003988{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003989 if (IS_VALLEYVIEW(dev))
3990 valleyview_set_rps(dev, val);
3991 else
3992 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003993}
3994
Zhe Wang20e49362014-11-04 17:07:05 +00003995static void gen9_disable_rps(struct drm_device *dev)
3996{
3997 struct drm_i915_private *dev_priv = dev->dev_private;
3998
3999 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00004000 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00004001}
4002
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004003static void gen6_disable_rps(struct drm_device *dev)
4004{
4005 struct drm_i915_private *dev_priv = dev->dev_private;
4006
4007 I915_WRITE(GEN6_RC_CONTROL, 0);
4008 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02004009}
4010
Deepak S38807742014-05-23 21:00:15 +05304011static void cherryview_disable_rps(struct drm_device *dev)
4012{
4013 struct drm_i915_private *dev_priv = dev->dev_private;
4014
4015 I915_WRITE(GEN6_RC_CONTROL, 0);
4016}
4017
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004018static void valleyview_disable_rps(struct drm_device *dev)
4019{
4020 struct drm_i915_private *dev_priv = dev->dev_private;
4021
Deepak S98a2e5f2014-08-18 10:35:27 -07004022 /* we're doing forcewake before Disabling RC6,
4023 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004024 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07004025
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004026 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004027
Mika Kuoppala59bad942015-01-16 11:34:40 +02004028 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07004029}
4030
Ben Widawskydc39fff2013-10-18 12:32:07 -07004031static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4032{
Imre Deak91ca6892014-04-14 20:24:25 +03004033 if (IS_VALLEYVIEW(dev)) {
4034 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4035 mode = GEN6_RC_CTL_RC6_ENABLE;
4036 else
4037 mode = 0;
4038 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004039 if (HAS_RC6p(dev))
4040 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4041 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
4042 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
4043 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
4044
4045 else
4046 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4047 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07004048}
4049
Imre Deake6069ca2014-04-18 16:01:02 +03004050static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004051{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01004052 /* No RC6 before Ironlake */
4053 if (INTEL_INFO(dev)->gen < 5)
4054 return 0;
4055
Imre Deake6069ca2014-04-18 16:01:02 +03004056 /* RC6 is only on Ironlake mobile not on desktop */
4057 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
4058 return 0;
4059
Daniel Vetter456470e2012-08-08 23:35:40 +02004060 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03004061 if (enable_rc6 >= 0) {
4062 int mask;
4063
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07004064 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03004065 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4066 INTEL_RC6pp_ENABLE;
4067 else
4068 mask = INTEL_RC6_ENABLE;
4069
4070 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02004071 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4072 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03004073
4074 return enable_rc6 & mask;
4075 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004076
Chris Wilson6567d742012-11-10 10:00:06 +00004077 /* Disable RC6 on Ironlake */
4078 if (INTEL_INFO(dev)->gen == 5)
4079 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004080
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004081 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08004082 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08004083
4084 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004085}
4086
Imre Deake6069ca2014-04-18 16:01:02 +03004087int intel_enable_rc6(const struct drm_device *dev)
4088{
4089 return i915.enable_rc6;
4090}
4091
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004092static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004093{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004094 struct drm_i915_private *dev_priv = dev->dev_private;
4095 uint32_t rp_state_cap;
4096 u32 ddcc_status = 0;
4097 int ret;
4098
4099 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004100 /* All of these values are in units of 50MHz */
4101 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004102 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004103 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004104 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004105 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004106 /* hw_max = RP0 until we check for overclocking */
4107 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4108
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004109 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4110 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4111 ret = sandybridge_pcode_read(dev_priv,
4112 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4113 &ddcc_status);
4114 if (0 == ret)
4115 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08004116 clamp_t(u8,
4117 ((ddcc_status >> 8) & 0xff),
4118 dev_priv->rps.min_freq,
4119 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004120 }
4121
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004122 /* Preserve min/max settings in case of re-init */
4123 if (dev_priv->rps.max_freq_softlimit == 0)
4124 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4125
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004126 if (dev_priv->rps.min_freq_softlimit == 0) {
4127 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4128 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004129 /* max(RPe, 450 MHz) */
4130 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004131 else
4132 dev_priv->rps.min_freq_softlimit =
4133 dev_priv->rps.min_freq;
4134 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004135}
4136
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004137/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004138static void gen9_enable_rps(struct drm_device *dev)
4139{
4140 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004141
4142 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4143
Damien Lespiauba1c5542015-01-16 18:07:26 +00004144 gen6_init_rps_frequencies(dev);
4145
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004146 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4147 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4148
4149 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4150 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4151 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4152 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4153 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4154 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4155 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4156 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4157 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4158 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4159 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4160 GEN6_RP_DOWN_IDLE_AVG);
4161
4162 gen6_enable_rps_interrupts(dev);
4163
4164 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4165}
4166
4167static void gen9_enable_rc6(struct drm_device *dev)
4168{
4169 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004170 struct intel_engine_cs *ring;
4171 uint32_t rc6_mask = 0;
4172 int unused;
4173
4174 /* 1a: Software RC state - RC0 */
4175 I915_WRITE(GEN6_RC_STATE, 0);
4176
4177 /* 1b: Get forcewake during program sequence. Although the driver
4178 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004179 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004180
4181 /* 2a: Disable RC states. */
4182 I915_WRITE(GEN6_RC_CONTROL, 0);
4183
4184 /* 2b: Program RC6 thresholds.*/
4185 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4186 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4187 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4188 for_each_ring(ring, dev_priv, unused)
4189 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4190 I915_WRITE(GEN6_RC_SLEEP, 0);
4191 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4192
Zhe Wang38c23522015-01-20 12:23:04 +00004193 /* 2c: Program Coarse Power Gating Policies. */
4194 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4195 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4196
Zhe Wang20e49362014-11-04 17:07:05 +00004197 /* 3a: Enable RC6 */
4198 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4199 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4200 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4201 "on" : "off");
4202 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4203 GEN6_RC_CTL_EI_MODE(1) |
4204 rc6_mask);
4205
Zhe Wang38c23522015-01-20 12:23:04 +00004206 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4207 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4208
Mika Kuoppala59bad942015-01-16 11:34:40 +02004209 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004210
4211}
4212
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004213static void gen8_enable_rps(struct drm_device *dev)
4214{
4215 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004216 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004217 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004218 int unused;
4219
4220 /* 1a: Software RC state - RC0 */
4221 I915_WRITE(GEN6_RC_STATE, 0);
4222
4223 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4224 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004225 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004226
4227 /* 2a: Disable RC states. */
4228 I915_WRITE(GEN6_RC_CONTROL, 0);
4229
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004230 /* Initialize rps frequencies */
4231 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004232
4233 /* 2b: Program RC6 thresholds.*/
4234 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4235 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4236 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4237 for_each_ring(ring, dev_priv, unused)
4238 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4239 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004240 if (IS_BROADWELL(dev))
4241 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4242 else
4243 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004244
4245 /* 3: Enable RC6 */
4246 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4247 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004248 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004249 if (IS_BROADWELL(dev))
4250 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4251 GEN7_RC_CTL_TO_MODE |
4252 rc6_mask);
4253 else
4254 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4255 GEN6_RC_CTL_EI_MODE(1) |
4256 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004257
4258 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004259 I915_WRITE(GEN6_RPNSWREQ,
4260 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4261 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4262 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004263 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4264 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004265
Daniel Vetter7526ed72014-09-29 15:07:19 +02004266 /* Docs recommend 900MHz, and 300 MHz respectively */
4267 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4268 dev_priv->rps.max_freq_softlimit << 24 |
4269 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004270
Daniel Vetter7526ed72014-09-29 15:07:19 +02004271 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4272 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4273 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4274 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004275
Daniel Vetter7526ed72014-09-29 15:07:19 +02004276 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004277
4278 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004279 I915_WRITE(GEN6_RP_CONTROL,
4280 GEN6_RP_MEDIA_TURBO |
4281 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4282 GEN6_RP_MEDIA_IS_GFX |
4283 GEN6_RP_ENABLE |
4284 GEN6_RP_UP_BUSY_AVG |
4285 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004286
Daniel Vetter7526ed72014-09-29 15:07:19 +02004287 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004288
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004289 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4290 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004291
Mika Kuoppala59bad942015-01-16 11:34:40 +02004292 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004293}
4294
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004295static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004296{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004297 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004298 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004299 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004300 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004301 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004302 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004303
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004304 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004305
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004306 /* Here begins a magic sequence of register writes to enable
4307 * auto-downclocking.
4308 *
4309 * Perhaps there might be some value in exposing these to
4310 * userspace...
4311 */
4312 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004313
4314 /* Clear the DBG now so we don't confuse earlier errors */
4315 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4316 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4317 I915_WRITE(GTFIFODBG, gtfifodbg);
4318 }
4319
Mika Kuoppala59bad942015-01-16 11:34:40 +02004320 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004321
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004322 /* Initialize rps frequencies */
4323 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004324
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004325 /* disable the counters and set deterministic thresholds */
4326 I915_WRITE(GEN6_RC_CONTROL, 0);
4327
4328 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4329 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4330 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4331 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4332 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4333
Chris Wilsonb4519512012-05-11 14:29:30 +01004334 for_each_ring(ring, dev_priv, i)
4335 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004336
4337 I915_WRITE(GEN6_RC_SLEEP, 0);
4338 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004339 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004340 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4341 else
4342 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004343 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004344 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4345
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004346 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004347 rc6_mode = intel_enable_rc6(dev_priv->dev);
4348 if (rc6_mode & INTEL_RC6_ENABLE)
4349 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4350
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004351 /* We don't use those on Haswell */
4352 if (!IS_HASWELL(dev)) {
4353 if (rc6_mode & INTEL_RC6p_ENABLE)
4354 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004355
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004356 if (rc6_mode & INTEL_RC6pp_ENABLE)
4357 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4358 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004359
Ben Widawskydc39fff2013-10-18 12:32:07 -07004360 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004361
4362 I915_WRITE(GEN6_RC_CONTROL,
4363 rc6_mask |
4364 GEN6_RC_CTL_EI_MODE(1) |
4365 GEN6_RC_CTL_HW_ENABLE);
4366
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004367 /* Power down if completely idle for over 50ms */
4368 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004369 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004370
Ben Widawsky42c05262012-09-26 10:34:00 -07004371 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004372 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004373 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004374
4375 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4376 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4377 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004378 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004379 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004380 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004381 }
4382
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004383 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004384 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004385
Ben Widawsky31643d52012-09-26 10:34:01 -07004386 rc6vids = 0;
4387 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4388 if (IS_GEN6(dev) && ret) {
4389 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4390 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4391 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4392 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4393 rc6vids &= 0xffff00;
4394 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4395 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4396 if (ret)
4397 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4398 }
4399
Mika Kuoppala59bad942015-01-16 11:34:40 +02004400 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004401}
4402
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004403static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004404{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004405 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004406 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004407 unsigned int gpu_freq;
4408 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004409 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004410 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004411
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004412 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004413
Ben Widawskyeda79642013-10-07 17:15:48 -03004414 policy = cpufreq_cpu_get(0);
4415 if (policy) {
4416 max_ia_freq = policy->cpuinfo.max_freq;
4417 cpufreq_cpu_put(policy);
4418 } else {
4419 /*
4420 * Default to measured freq if none found, PCU will ensure we
4421 * don't go over
4422 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004423 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004424 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004425
4426 /* Convert from kHz to MHz */
4427 max_ia_freq /= 1000;
4428
Ben Widawsky153b4b952013-10-22 22:05:09 -07004429 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004430 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4431 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004432
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004433 /*
4434 * For each potential GPU frequency, load a ring frequency we'd like
4435 * to use for memory access. We do this by specifying the IA frequency
4436 * the PCU should use as a reference to determine the ring frequency.
4437 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004438 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004439 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004440 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004441 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004442
Ben Widawsky46c764d2013-11-02 21:07:49 -07004443 if (INTEL_INFO(dev)->gen >= 8) {
4444 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4445 ring_freq = max(min_ring_freq, gpu_freq);
4446 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004447 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004448 ring_freq = max(min_ring_freq, ring_freq);
4449 /* leave ia_freq as the default, chosen by cpufreq */
4450 } else {
4451 /* On older processors, there is no separate ring
4452 * clock domain, so in order to boost the bandwidth
4453 * of the ring, we need to upclock the CPU (ia_freq).
4454 *
4455 * For GPU frequencies less than 750MHz,
4456 * just use the lowest ring freq.
4457 */
4458 if (gpu_freq < min_freq)
4459 ia_freq = 800;
4460 else
4461 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4462 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4463 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004464
Ben Widawsky42c05262012-09-26 10:34:00 -07004465 sandybridge_pcode_write(dev_priv,
4466 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004467 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4468 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4469 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004470 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004471}
4472
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004473void gen6_update_ring_freq(struct drm_device *dev)
4474{
4475 struct drm_i915_private *dev_priv = dev->dev_private;
4476
4477 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4478 return;
4479
4480 mutex_lock(&dev_priv->rps.hw_lock);
4481 __gen6_update_ring_freq(dev);
4482 mutex_unlock(&dev_priv->rps.hw_lock);
4483}
4484
Ville Syrjälä03af2042014-06-28 02:03:53 +03004485static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304486{
Deepak S095acd52015-01-17 11:05:59 +05304487 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304488 u32 val, rp0;
4489
Deepak S095acd52015-01-17 11:05:59 +05304490 if (dev->pdev->revision >= 0x20) {
4491 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304492
Deepak S095acd52015-01-17 11:05:59 +05304493 switch (INTEL_INFO(dev)->eu_total) {
4494 case 8:
4495 /* (2 * 4) config */
4496 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4497 break;
4498 case 12:
4499 /* (2 * 6) config */
4500 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4501 break;
4502 case 16:
4503 /* (2 * 8) config */
4504 default:
4505 /* Setting (2 * 8) Min RP0 for any other combination */
4506 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4507 break;
4508 }
4509 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4510 } else {
4511 /* For pre-production hardware */
4512 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4513 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4514 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4515 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304516 return rp0;
4517}
4518
4519static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4520{
4521 u32 val, rpe;
4522
4523 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4524 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4525
4526 return rpe;
4527}
4528
Deepak S7707df42014-07-12 18:46:14 +05304529static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4530{
Deepak S095acd52015-01-17 11:05:59 +05304531 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304532 u32 val, rp1;
4533
Deepak S095acd52015-01-17 11:05:59 +05304534 if (dev->pdev->revision >= 0x20) {
4535 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4536 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4537 } else {
4538 /* For pre-production hardware */
4539 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4540 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4541 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4542 }
Deepak S7707df42014-07-12 18:46:14 +05304543 return rp1;
4544}
4545
Ville Syrjälä03af2042014-06-28 02:03:53 +03004546static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304547{
Deepak S095acd52015-01-17 11:05:59 +05304548 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304549 u32 val, rpn;
4550
Deepak S095acd52015-01-17 11:05:59 +05304551 if (dev->pdev->revision >= 0x20) {
4552 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4553 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4554 FB_GFX_FREQ_FUSE_MASK);
4555 } else { /* For pre-production hardware */
4556 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4557 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4558 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4559 }
4560
Deepak S2b6b3a02014-05-27 15:59:30 +05304561 return rpn;
4562}
4563
Deepak Sf8f2b002014-07-10 13:16:21 +05304564static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4565{
4566 u32 val, rp1;
4567
4568 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4569
4570 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4571
4572 return rp1;
4573}
4574
Ville Syrjälä03af2042014-06-28 02:03:53 +03004575static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004576{
4577 u32 val, rp0;
4578
Jani Nikula64936252013-05-22 15:36:20 +03004579 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004580
4581 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4582 /* Clamp to max */
4583 rp0 = min_t(u32, rp0, 0xea);
4584
4585 return rp0;
4586}
4587
4588static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4589{
4590 u32 val, rpe;
4591
Jani Nikula64936252013-05-22 15:36:20 +03004592 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004593 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004594 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004595 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4596
4597 return rpe;
4598}
4599
Ville Syrjälä03af2042014-06-28 02:03:53 +03004600static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004601{
Jani Nikula64936252013-05-22 15:36:20 +03004602 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004603}
4604
Imre Deakae484342014-03-31 15:10:44 +03004605/* Check that the pctx buffer wasn't move under us. */
4606static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4607{
4608 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4609
4610 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4611 dev_priv->vlv_pctx->stolen->start);
4612}
4613
Deepak S38807742014-05-23 21:00:15 +05304614
4615/* Check that the pcbr address is not empty. */
4616static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4617{
4618 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4619
4620 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4621}
4622
4623static void cherryview_setup_pctx(struct drm_device *dev)
4624{
4625 struct drm_i915_private *dev_priv = dev->dev_private;
4626 unsigned long pctx_paddr, paddr;
4627 struct i915_gtt *gtt = &dev_priv->gtt;
4628 u32 pcbr;
4629 int pctx_size = 32*1024;
4630
4631 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4632
4633 pcbr = I915_READ(VLV_PCBR);
4634 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004635 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304636 paddr = (dev_priv->mm.stolen_base +
4637 (gtt->stolen_size - pctx_size));
4638
4639 pctx_paddr = (paddr & (~4095));
4640 I915_WRITE(VLV_PCBR, pctx_paddr);
4641 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004642
4643 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304644}
4645
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004646static void valleyview_setup_pctx(struct drm_device *dev)
4647{
4648 struct drm_i915_private *dev_priv = dev->dev_private;
4649 struct drm_i915_gem_object *pctx;
4650 unsigned long pctx_paddr;
4651 u32 pcbr;
4652 int pctx_size = 24*1024;
4653
Imre Deak17b0c1f2014-02-11 21:39:06 +02004654 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4655
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004656 pcbr = I915_READ(VLV_PCBR);
4657 if (pcbr) {
4658 /* BIOS set it up already, grab the pre-alloc'd space */
4659 int pcbr_offset;
4660
4661 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4662 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4663 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004664 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004665 pctx_size);
4666 goto out;
4667 }
4668
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004669 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4670
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004671 /*
4672 * From the Gunit register HAS:
4673 * The Gfx driver is expected to program this register and ensure
4674 * proper allocation within Gfx stolen memory. For example, this
4675 * register should be programmed such than the PCBR range does not
4676 * overlap with other ranges, such as the frame buffer, protected
4677 * memory, or any other relevant ranges.
4678 */
4679 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4680 if (!pctx) {
4681 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4682 return;
4683 }
4684
4685 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4686 I915_WRITE(VLV_PCBR, pctx_paddr);
4687
4688out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004689 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004690 dev_priv->vlv_pctx = pctx;
4691}
4692
Imre Deakae484342014-03-31 15:10:44 +03004693static void valleyview_cleanup_pctx(struct drm_device *dev)
4694{
4695 struct drm_i915_private *dev_priv = dev->dev_private;
4696
4697 if (WARN_ON(!dev_priv->vlv_pctx))
4698 return;
4699
4700 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4701 dev_priv->vlv_pctx = NULL;
4702}
4703
Imre Deak4e805192014-04-14 20:24:41 +03004704static void valleyview_init_gt_powersave(struct drm_device *dev)
4705{
4706 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004707 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004708
4709 valleyview_setup_pctx(dev);
4710
4711 mutex_lock(&dev_priv->rps.hw_lock);
4712
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004713 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4714 switch ((val >> 6) & 3) {
4715 case 0:
4716 case 1:
4717 dev_priv->mem_freq = 800;
4718 break;
4719 case 2:
4720 dev_priv->mem_freq = 1066;
4721 break;
4722 case 3:
4723 dev_priv->mem_freq = 1333;
4724 break;
4725 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004726 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004727
Imre Deak4e805192014-04-14 20:24:41 +03004728 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4729 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4730 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004731 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004732 dev_priv->rps.max_freq);
4733
4734 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4735 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004736 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004737 dev_priv->rps.efficient_freq);
4738
Deepak Sf8f2b002014-07-10 13:16:21 +05304739 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4740 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004741 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304742 dev_priv->rps.rp1_freq);
4743
Imre Deak4e805192014-04-14 20:24:41 +03004744 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4745 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004746 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004747 dev_priv->rps.min_freq);
4748
4749 /* Preserve min/max settings in case of re-init */
4750 if (dev_priv->rps.max_freq_softlimit == 0)
4751 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4752
4753 if (dev_priv->rps.min_freq_softlimit == 0)
4754 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4755
4756 mutex_unlock(&dev_priv->rps.hw_lock);
4757}
4758
Deepak S38807742014-05-23 21:00:15 +05304759static void cherryview_init_gt_powersave(struct drm_device *dev)
4760{
Deepak S2b6b3a02014-05-27 15:59:30 +05304761 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004762 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304763
Deepak S38807742014-05-23 21:00:15 +05304764 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304765
4766 mutex_lock(&dev_priv->rps.hw_lock);
4767
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004768 mutex_lock(&dev_priv->dpio_lock);
4769 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4770 mutex_unlock(&dev_priv->dpio_lock);
4771
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004772 switch ((val >> 2) & 0x7) {
4773 case 0:
4774 case 1:
4775 dev_priv->rps.cz_freq = 200;
4776 dev_priv->mem_freq = 1600;
4777 break;
4778 case 2:
4779 dev_priv->rps.cz_freq = 267;
4780 dev_priv->mem_freq = 1600;
4781 break;
4782 case 3:
4783 dev_priv->rps.cz_freq = 333;
4784 dev_priv->mem_freq = 2000;
4785 break;
4786 case 4:
4787 dev_priv->rps.cz_freq = 320;
4788 dev_priv->mem_freq = 1600;
4789 break;
4790 case 5:
4791 dev_priv->rps.cz_freq = 400;
4792 dev_priv->mem_freq = 1600;
4793 break;
4794 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004795 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004796
Deepak S2b6b3a02014-05-27 15:59:30 +05304797 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4798 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4799 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004800 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304801 dev_priv->rps.max_freq);
4802
4803 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4804 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004805 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304806 dev_priv->rps.efficient_freq);
4807
Deepak S7707df42014-07-12 18:46:14 +05304808 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4809 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004810 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304811 dev_priv->rps.rp1_freq);
4812
Deepak S2b6b3a02014-05-27 15:59:30 +05304813 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4814 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004815 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304816 dev_priv->rps.min_freq);
4817
Ville Syrjälä1c147622014-08-18 14:42:43 +03004818 WARN_ONCE((dev_priv->rps.max_freq |
4819 dev_priv->rps.efficient_freq |
4820 dev_priv->rps.rp1_freq |
4821 dev_priv->rps.min_freq) & 1,
4822 "Odd GPU freq values\n");
4823
Deepak S2b6b3a02014-05-27 15:59:30 +05304824 /* Preserve min/max settings in case of re-init */
4825 if (dev_priv->rps.max_freq_softlimit == 0)
4826 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4827
4828 if (dev_priv->rps.min_freq_softlimit == 0)
4829 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4830
4831 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304832}
4833
Imre Deak4e805192014-04-14 20:24:41 +03004834static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4835{
4836 valleyview_cleanup_pctx(dev);
4837}
4838
Deepak S38807742014-05-23 21:00:15 +05304839static void cherryview_enable_rps(struct drm_device *dev)
4840{
4841 struct drm_i915_private *dev_priv = dev->dev_private;
4842 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304843 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304844 int i;
4845
4846 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4847
4848 gtfifodbg = I915_READ(GTFIFODBG);
4849 if (gtfifodbg) {
4850 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4851 gtfifodbg);
4852 I915_WRITE(GTFIFODBG, gtfifodbg);
4853 }
4854
4855 cherryview_check_pctx(dev_priv);
4856
4857 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4858 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004859 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304860
Ville Syrjälä160614a2015-01-19 13:50:47 +02004861 /* Disable RC states. */
4862 I915_WRITE(GEN6_RC_CONTROL, 0);
4863
Deepak S38807742014-05-23 21:00:15 +05304864 /* 2a: Program RC6 thresholds.*/
4865 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4866 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4867 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4868
4869 for_each_ring(ring, dev_priv, i)
4870 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4871 I915_WRITE(GEN6_RC_SLEEP, 0);
4872
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004873 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4874 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304875
4876 /* allows RC6 residency counter to work */
4877 I915_WRITE(VLV_COUNTER_CONTROL,
4878 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4879 VLV_MEDIA_RC6_COUNT_EN |
4880 VLV_RENDER_RC6_COUNT_EN));
4881
4882 /* For now we assume BIOS is allocating and populating the PCBR */
4883 pcbr = I915_READ(VLV_PCBR);
4884
Deepak S38807742014-05-23 21:00:15 +05304885 /* 3: Enable RC6 */
4886 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4887 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004888 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304889
4890 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4891
Deepak S2b6b3a02014-05-27 15:59:30 +05304892 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004893 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304894 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4895 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4896 I915_WRITE(GEN6_RP_UP_EI, 66000);
4897 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4898
4899 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4900
4901 /* 5: Enable RPS */
4902 I915_WRITE(GEN6_RP_CONTROL,
4903 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004904 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304905 GEN6_RP_ENABLE |
4906 GEN6_RP_UP_BUSY_AVG |
4907 GEN6_RP_DOWN_IDLE_AVG);
4908
4909 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4910
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004911 /* RPS code assumes GPLL is used */
4912 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4913
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004914 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304915 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4916
4917 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4918 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004919 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304920 dev_priv->rps.cur_freq);
4921
4922 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004923 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304924 dev_priv->rps.efficient_freq);
4925
4926 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4927
Mika Kuoppala59bad942015-01-16 11:34:40 +02004928 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304929}
4930
Jesse Barnes0a073b82013-04-17 15:54:58 -07004931static void valleyview_enable_rps(struct drm_device *dev)
4932{
4933 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004934 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004935 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004936 int i;
4937
4938 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4939
Imre Deakae484342014-03-31 15:10:44 +03004940 valleyview_check_pctx(dev_priv);
4941
Jesse Barnes0a073b82013-04-17 15:54:58 -07004942 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004943 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4944 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004945 I915_WRITE(GTFIFODBG, gtfifodbg);
4946 }
4947
Deepak Sc8d9a592013-11-23 14:55:42 +05304948 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004949 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004950
Ville Syrjälä160614a2015-01-19 13:50:47 +02004951 /* Disable RC states. */
4952 I915_WRITE(GEN6_RC_CONTROL, 0);
4953
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004954 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004955 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4956 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4957 I915_WRITE(GEN6_RP_UP_EI, 66000);
4958 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4959
4960 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4961
4962 I915_WRITE(GEN6_RP_CONTROL,
4963 GEN6_RP_MEDIA_TURBO |
4964 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4965 GEN6_RP_MEDIA_IS_GFX |
4966 GEN6_RP_ENABLE |
4967 GEN6_RP_UP_BUSY_AVG |
4968 GEN6_RP_DOWN_IDLE_CONT);
4969
4970 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4971 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4972 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4973
4974 for_each_ring(ring, dev_priv, i)
4975 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4976
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08004977 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004978
4979 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004980 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004981 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4982 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004983 VLV_MEDIA_RC6_COUNT_EN |
4984 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004985
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004986 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004987 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004988
4989 intel_print_rc6_info(dev, rc6_mode);
4990
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004991 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004992
Jani Nikula64936252013-05-22 15:36:20 +03004993 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004994
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004995 /* RPS code assumes GPLL is used */
4996 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4997
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004998 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07004999 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5000
Ben Widawskyb39fb292014-03-19 18:31:11 -07005001 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03005002 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005003 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005004 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005005
Ville Syrjälä73008b92013-06-25 19:21:01 +03005006 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02005007 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07005008 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005009
Ben Widawskyb39fb292014-03-19 18:31:11 -07005010 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005011
Mika Kuoppala59bad942015-01-16 11:34:40 +02005012 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005013}
5014
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005015static unsigned long intel_pxfreq(u32 vidfreq)
5016{
5017 unsigned long freq;
5018 int div = (vidfreq & 0x3f0000) >> 16;
5019 int post = (vidfreq & 0x3000) >> 12;
5020 int pre = (vidfreq & 0x7);
5021
5022 if (!pre)
5023 return 0;
5024
5025 freq = ((div * 133333) / ((1<<post) * pre));
5026
5027 return freq;
5028}
5029
Daniel Vettereb48eb02012-04-26 23:28:12 +02005030static const struct cparams {
5031 u16 i;
5032 u16 t;
5033 u16 m;
5034 u16 c;
5035} cparams[] = {
5036 { 1, 1333, 301, 28664 },
5037 { 1, 1066, 294, 24460 },
5038 { 1, 800, 294, 25192 },
5039 { 0, 1333, 276, 27605 },
5040 { 0, 1066, 276, 27605 },
5041 { 0, 800, 231, 23784 },
5042};
5043
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005044static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005045{
5046 u64 total_count, diff, ret;
5047 u32 count1, count2, count3, m = 0, c = 0;
5048 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5049 int i;
5050
Daniel Vetter02d71952012-08-09 16:44:54 +02005051 assert_spin_locked(&mchdev_lock);
5052
Daniel Vetter20e4d402012-08-08 23:35:39 +02005053 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005054
5055 /* Prevent division-by-zero if we are asking too fast.
5056 * Also, we don't get interesting results if we are polling
5057 * faster than once in 10ms, so just return the saved value
5058 * in such cases.
5059 */
5060 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005061 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005062
5063 count1 = I915_READ(DMIEC);
5064 count2 = I915_READ(DDREC);
5065 count3 = I915_READ(CSIEC);
5066
5067 total_count = count1 + count2 + count3;
5068
5069 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005070 if (total_count < dev_priv->ips.last_count1) {
5071 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005072 diff += total_count;
5073 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005074 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005075 }
5076
5077 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005078 if (cparams[i].i == dev_priv->ips.c_m &&
5079 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005080 m = cparams[i].m;
5081 c = cparams[i].c;
5082 break;
5083 }
5084 }
5085
5086 diff = div_u64(diff, diff1);
5087 ret = ((m * diff) + c);
5088 ret = div_u64(ret, 10);
5089
Daniel Vetter20e4d402012-08-08 23:35:39 +02005090 dev_priv->ips.last_count1 = total_count;
5091 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005092
Daniel Vetter20e4d402012-08-08 23:35:39 +02005093 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005094
5095 return ret;
5096}
5097
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005098unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5099{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005100 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005101 unsigned long val;
5102
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005103 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005104 return 0;
5105
5106 spin_lock_irq(&mchdev_lock);
5107
5108 val = __i915_chipset_val(dev_priv);
5109
5110 spin_unlock_irq(&mchdev_lock);
5111
5112 return val;
5113}
5114
Daniel Vettereb48eb02012-04-26 23:28:12 +02005115unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5116{
5117 unsigned long m, x, b;
5118 u32 tsfs;
5119
5120 tsfs = I915_READ(TSFS);
5121
5122 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5123 x = I915_READ8(TR1);
5124
5125 b = tsfs & TSFS_INTR_MASK;
5126
5127 return ((m * x) / 127) - b;
5128}
5129
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005130static int _pxvid_to_vd(u8 pxvid)
5131{
5132 if (pxvid == 0)
5133 return 0;
5134
5135 if (pxvid >= 8 && pxvid < 31)
5136 pxvid = 31;
5137
5138 return (pxvid + 2) * 125;
5139}
5140
5141static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005142{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005143 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005144 const int vd = _pxvid_to_vd(pxvid);
5145 const int vm = vd - 1125;
5146
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005147 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005148 return vm > 0 ? vm : 0;
5149
5150 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005151}
5152
Daniel Vetter02d71952012-08-09 16:44:54 +02005153static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005154{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005155 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005156 u32 count;
5157
Daniel Vetter02d71952012-08-09 16:44:54 +02005158 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005159
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005160 now = ktime_get_raw_ns();
5161 diffms = now - dev_priv->ips.last_time2;
5162 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005163
5164 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005165 if (!diffms)
5166 return;
5167
5168 count = I915_READ(GFXEC);
5169
Daniel Vetter20e4d402012-08-08 23:35:39 +02005170 if (count < dev_priv->ips.last_count2) {
5171 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005172 diff += count;
5173 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005174 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005175 }
5176
Daniel Vetter20e4d402012-08-08 23:35:39 +02005177 dev_priv->ips.last_count2 = count;
5178 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005179
5180 /* More magic constants... */
5181 diff = diff * 1181;
5182 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005183 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005184}
5185
Daniel Vetter02d71952012-08-09 16:44:54 +02005186void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5187{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005188 struct drm_device *dev = dev_priv->dev;
5189
5190 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005191 return;
5192
Daniel Vetter92703882012-08-09 16:46:01 +02005193 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005194
5195 __i915_update_gfx_val(dev_priv);
5196
Daniel Vetter92703882012-08-09 16:46:01 +02005197 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005198}
5199
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005200static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005201{
5202 unsigned long t, corr, state1, corr2, state2;
5203 u32 pxvid, ext_v;
5204
Daniel Vetter02d71952012-08-09 16:44:54 +02005205 assert_spin_locked(&mchdev_lock);
5206
Ben Widawskyb39fb292014-03-19 18:31:11 -07005207 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005208 pxvid = (pxvid >> 24) & 0x7f;
5209 ext_v = pvid_to_extvid(dev_priv, pxvid);
5210
5211 state1 = ext_v;
5212
5213 t = i915_mch_val(dev_priv);
5214
5215 /* Revel in the empirically derived constants */
5216
5217 /* Correction factor in 1/100000 units */
5218 if (t > 80)
5219 corr = ((t * 2349) + 135940);
5220 else if (t >= 50)
5221 corr = ((t * 964) + 29317);
5222 else /* < 50 */
5223 corr = ((t * 301) + 1004);
5224
5225 corr = corr * ((150142 * state1) / 10000 - 78642);
5226 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005227 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005228
5229 state2 = (corr2 * state1) / 10000;
5230 state2 /= 100; /* convert to mW */
5231
Daniel Vetter02d71952012-08-09 16:44:54 +02005232 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005233
Daniel Vetter20e4d402012-08-08 23:35:39 +02005234 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005235}
5236
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005237unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5238{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005239 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005240 unsigned long val;
5241
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005242 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005243 return 0;
5244
5245 spin_lock_irq(&mchdev_lock);
5246
5247 val = __i915_gfx_val(dev_priv);
5248
5249 spin_unlock_irq(&mchdev_lock);
5250
5251 return val;
5252}
5253
Daniel Vettereb48eb02012-04-26 23:28:12 +02005254/**
5255 * i915_read_mch_val - return value for IPS use
5256 *
5257 * Calculate and return a value for the IPS driver to use when deciding whether
5258 * we have thermal and power headroom to increase CPU or GPU power budget.
5259 */
5260unsigned long i915_read_mch_val(void)
5261{
5262 struct drm_i915_private *dev_priv;
5263 unsigned long chipset_val, graphics_val, ret = 0;
5264
Daniel Vetter92703882012-08-09 16:46:01 +02005265 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005266 if (!i915_mch_dev)
5267 goto out_unlock;
5268 dev_priv = i915_mch_dev;
5269
Chris Wilsonf531dcb22012-09-25 10:16:12 +01005270 chipset_val = __i915_chipset_val(dev_priv);
5271 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005272
5273 ret = chipset_val + graphics_val;
5274
5275out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005276 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005277
5278 return ret;
5279}
5280EXPORT_SYMBOL_GPL(i915_read_mch_val);
5281
5282/**
5283 * i915_gpu_raise - raise GPU frequency limit
5284 *
5285 * Raise the limit; IPS indicates we have thermal headroom.
5286 */
5287bool i915_gpu_raise(void)
5288{
5289 struct drm_i915_private *dev_priv;
5290 bool ret = true;
5291
Daniel Vetter92703882012-08-09 16:46:01 +02005292 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005293 if (!i915_mch_dev) {
5294 ret = false;
5295 goto out_unlock;
5296 }
5297 dev_priv = i915_mch_dev;
5298
Daniel Vetter20e4d402012-08-08 23:35:39 +02005299 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5300 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005301
5302out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005303 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005304
5305 return ret;
5306}
5307EXPORT_SYMBOL_GPL(i915_gpu_raise);
5308
5309/**
5310 * i915_gpu_lower - lower GPU frequency limit
5311 *
5312 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5313 * frequency maximum.
5314 */
5315bool i915_gpu_lower(void)
5316{
5317 struct drm_i915_private *dev_priv;
5318 bool ret = true;
5319
Daniel Vetter92703882012-08-09 16:46:01 +02005320 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005321 if (!i915_mch_dev) {
5322 ret = false;
5323 goto out_unlock;
5324 }
5325 dev_priv = i915_mch_dev;
5326
Daniel Vetter20e4d402012-08-08 23:35:39 +02005327 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5328 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005329
5330out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005331 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005332
5333 return ret;
5334}
5335EXPORT_SYMBOL_GPL(i915_gpu_lower);
5336
5337/**
5338 * i915_gpu_busy - indicate GPU business to IPS
5339 *
5340 * Tell the IPS driver whether or not the GPU is busy.
5341 */
5342bool i915_gpu_busy(void)
5343{
5344 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005345 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005346 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005347 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005348
Daniel Vetter92703882012-08-09 16:46:01 +02005349 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005350 if (!i915_mch_dev)
5351 goto out_unlock;
5352 dev_priv = i915_mch_dev;
5353
Chris Wilsonf047e392012-07-21 12:31:41 +01005354 for_each_ring(ring, dev_priv, i)
5355 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005356
5357out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005358 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005359
5360 return ret;
5361}
5362EXPORT_SYMBOL_GPL(i915_gpu_busy);
5363
5364/**
5365 * i915_gpu_turbo_disable - disable graphics turbo
5366 *
5367 * Disable graphics turbo by resetting the max frequency and setting the
5368 * current frequency to the default.
5369 */
5370bool i915_gpu_turbo_disable(void)
5371{
5372 struct drm_i915_private *dev_priv;
5373 bool ret = true;
5374
Daniel Vetter92703882012-08-09 16:46:01 +02005375 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005376 if (!i915_mch_dev) {
5377 ret = false;
5378 goto out_unlock;
5379 }
5380 dev_priv = i915_mch_dev;
5381
Daniel Vetter20e4d402012-08-08 23:35:39 +02005382 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005383
Daniel Vetter20e4d402012-08-08 23:35:39 +02005384 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005385 ret = false;
5386
5387out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005388 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005389
5390 return ret;
5391}
5392EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5393
5394/**
5395 * Tells the intel_ips driver that the i915 driver is now loaded, if
5396 * IPS got loaded first.
5397 *
5398 * This awkward dance is so that neither module has to depend on the
5399 * other in order for IPS to do the appropriate communication of
5400 * GPU turbo limits to i915.
5401 */
5402static void
5403ips_ping_for_i915_load(void)
5404{
5405 void (*link)(void);
5406
5407 link = symbol_get(ips_link_to_i915_driver);
5408 if (link) {
5409 link();
5410 symbol_put(ips_link_to_i915_driver);
5411 }
5412}
5413
5414void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5415{
Daniel Vetter02d71952012-08-09 16:44:54 +02005416 /* We only register the i915 ips part with intel-ips once everything is
5417 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005418 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005419 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005420 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005421
5422 ips_ping_for_i915_load();
5423}
5424
5425void intel_gpu_ips_teardown(void)
5426{
Daniel Vetter92703882012-08-09 16:46:01 +02005427 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005428 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005429 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005430}
Deepak S76c3552f2014-01-30 23:08:16 +05305431
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005432static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005433{
5434 struct drm_i915_private *dev_priv = dev->dev_private;
5435 u32 lcfuse;
5436 u8 pxw[16];
5437 int i;
5438
5439 /* Disable to program */
5440 I915_WRITE(ECR, 0);
5441 POSTING_READ(ECR);
5442
5443 /* Program energy weights for various events */
5444 I915_WRITE(SDEW, 0x15040d00);
5445 I915_WRITE(CSIEW0, 0x007f0000);
5446 I915_WRITE(CSIEW1, 0x1e220004);
5447 I915_WRITE(CSIEW2, 0x04000004);
5448
5449 for (i = 0; i < 5; i++)
5450 I915_WRITE(PEW + (i * 4), 0);
5451 for (i = 0; i < 3; i++)
5452 I915_WRITE(DEW + (i * 4), 0);
5453
5454 /* Program P-state weights to account for frequency power adjustment */
5455 for (i = 0; i < 16; i++) {
5456 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5457 unsigned long freq = intel_pxfreq(pxvidfreq);
5458 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5459 PXVFREQ_PX_SHIFT;
5460 unsigned long val;
5461
5462 val = vid * vid;
5463 val *= (freq / 1000);
5464 val *= 255;
5465 val /= (127*127*900);
5466 if (val > 0xff)
5467 DRM_ERROR("bad pxval: %ld\n", val);
5468 pxw[i] = val;
5469 }
5470 /* Render standby states get 0 weight */
5471 pxw[14] = 0;
5472 pxw[15] = 0;
5473
5474 for (i = 0; i < 4; i++) {
5475 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5476 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5477 I915_WRITE(PXW + (i * 4), val);
5478 }
5479
5480 /* Adjust magic regs to magic values (more experimental results) */
5481 I915_WRITE(OGW0, 0);
5482 I915_WRITE(OGW1, 0);
5483 I915_WRITE(EG0, 0x00007f00);
5484 I915_WRITE(EG1, 0x0000000e);
5485 I915_WRITE(EG2, 0x000e0000);
5486 I915_WRITE(EG3, 0x68000300);
5487 I915_WRITE(EG4, 0x42000000);
5488 I915_WRITE(EG5, 0x00140031);
5489 I915_WRITE(EG6, 0);
5490 I915_WRITE(EG7, 0);
5491
5492 for (i = 0; i < 8; i++)
5493 I915_WRITE(PXWL + (i * 4), 0);
5494
5495 /* Enable PMON + select events */
5496 I915_WRITE(ECR, 0x80000019);
5497
5498 lcfuse = I915_READ(LCFUSE02);
5499
Daniel Vetter20e4d402012-08-08 23:35:39 +02005500 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005501}
5502
Imre Deakae484342014-03-31 15:10:44 +03005503void intel_init_gt_powersave(struct drm_device *dev)
5504{
Imre Deake6069ca2014-04-18 16:01:02 +03005505 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5506
Deepak S38807742014-05-23 21:00:15 +05305507 if (IS_CHERRYVIEW(dev))
5508 cherryview_init_gt_powersave(dev);
5509 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005510 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005511}
5512
5513void intel_cleanup_gt_powersave(struct drm_device *dev)
5514{
Deepak S38807742014-05-23 21:00:15 +05305515 if (IS_CHERRYVIEW(dev))
5516 return;
5517 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005518 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005519}
5520
Imre Deakdbea3ce2014-12-15 18:59:28 +02005521static void gen6_suspend_rps(struct drm_device *dev)
5522{
5523 struct drm_i915_private *dev_priv = dev->dev_private;
5524
5525 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5526
5527 /*
5528 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5529 * is added for it.
5530 */
5531 if (INTEL_INFO(dev)->gen < 9)
5532 gen6_disable_rps_interrupts(dev);
5533}
5534
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005535/**
5536 * intel_suspend_gt_powersave - suspend PM work and helper threads
5537 * @dev: drm device
5538 *
5539 * We don't want to disable RC6 or other features here, we just want
5540 * to make sure any work we've queued has finished and won't bother
5541 * us while we're suspended.
5542 */
5543void intel_suspend_gt_powersave(struct drm_device *dev)
5544{
5545 struct drm_i915_private *dev_priv = dev->dev_private;
5546
Imre Deakd4d70aa2014-11-19 15:30:04 +02005547 if (INTEL_INFO(dev)->gen < 6)
5548 return;
5549
Imre Deakdbea3ce2014-12-15 18:59:28 +02005550 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305551
5552 /* Force GPU to min freq during suspend */
5553 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005554}
5555
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005556void intel_disable_gt_powersave(struct drm_device *dev)
5557{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005558 struct drm_i915_private *dev_priv = dev->dev_private;
5559
Daniel Vetter930ebb42012-06-29 23:32:16 +02005560 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005561 ironlake_disable_drps(dev);
Deepak S38807742014-05-23 21:00:15 +05305562 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005563 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005564
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005565 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005566 if (INTEL_INFO(dev)->gen >= 9)
5567 gen9_disable_rps(dev);
5568 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305569 cherryview_disable_rps(dev);
5570 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005571 valleyview_disable_rps(dev);
5572 else
5573 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005574
Chris Wilsonc0951f02013-10-10 21:58:50 +01005575 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005576 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005577 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005578}
5579
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005580static void intel_gen6_powersave_work(struct work_struct *work)
5581{
5582 struct drm_i915_private *dev_priv =
5583 container_of(work, struct drm_i915_private,
5584 rps.delayed_resume_work.work);
5585 struct drm_device *dev = dev_priv->dev;
5586
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005587 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005588
Imre Deak3cc134e2014-11-19 15:30:03 +02005589 /*
5590 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5591 * added for it.
5592 */
5593 if (INTEL_INFO(dev)->gen < 9)
5594 gen6_reset_rps_interrupts(dev);
5595
Deepak S38807742014-05-23 21:00:15 +05305596 if (IS_CHERRYVIEW(dev)) {
5597 cherryview_enable_rps(dev);
5598 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005599 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005600 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005601 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005602 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005603 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005604 } else if (IS_BROADWELL(dev)) {
5605 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005606 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005607 } else {
5608 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005609 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005610 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005611 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005612
5613 if (INTEL_INFO(dev)->gen < 9)
5614 gen6_enable_rps_interrupts(dev);
5615
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005616 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005617
5618 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005619}
5620
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005621void intel_enable_gt_powersave(struct drm_device *dev)
5622{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005623 struct drm_i915_private *dev_priv = dev->dev_private;
5624
Yu Zhangf61018b2015-02-10 19:05:52 +08005625 /* Powersaving is controlled by the host when inside a VM */
5626 if (intel_vgpu_active(dev))
5627 return;
5628
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005629 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005630 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005631 ironlake_enable_drps(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005632 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005633 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305634 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005635 /*
5636 * PCU communication is slow and this doesn't need to be
5637 * done at any specific time, so do this out of our fast path
5638 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005639 *
5640 * We depend on the HW RC6 power context save/restore
5641 * mechanism when entering D3 through runtime PM suspend. So
5642 * disable RPM until RPS/RC6 is properly setup. We can only
5643 * get here via the driver load/system resume/runtime resume
5644 * paths, so the _noresume version is enough (and in case of
5645 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005646 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005647 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5648 round_jiffies_up_relative(HZ)))
5649 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005650 }
5651}
5652
Imre Deakc6df39b2014-04-14 20:24:29 +03005653void intel_reset_gt_powersave(struct drm_device *dev)
5654{
5655 struct drm_i915_private *dev_priv = dev->dev_private;
5656
Imre Deakdbea3ce2014-12-15 18:59:28 +02005657 if (INTEL_INFO(dev)->gen < 6)
5658 return;
5659
5660 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005661 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005662}
5663
Daniel Vetter3107bd42012-10-31 22:52:31 +01005664static void ibx_init_clock_gating(struct drm_device *dev)
5665{
5666 struct drm_i915_private *dev_priv = dev->dev_private;
5667
5668 /*
5669 * On Ibex Peak and Cougar Point, we need to disable clock
5670 * gating for the panel power sequencer or it will fail to
5671 * start up when no ports are active.
5672 */
5673 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5674}
5675
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005676static void g4x_disable_trickle_feed(struct drm_device *dev)
5677{
5678 struct drm_i915_private *dev_priv = dev->dev_private;
5679 int pipe;
5680
Damien Lespiau055e3932014-08-18 13:49:10 +01005681 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005682 I915_WRITE(DSPCNTR(pipe),
5683 I915_READ(DSPCNTR(pipe)) |
5684 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005685 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005686 }
5687}
5688
Ville Syrjälä017636c2013-12-05 15:51:37 +02005689static void ilk_init_lp_watermarks(struct drm_device *dev)
5690{
5691 struct drm_i915_private *dev_priv = dev->dev_private;
5692
5693 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5694 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5695 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5696
5697 /*
5698 * Don't touch WM1S_LP_EN here.
5699 * Doing so could cause underruns.
5700 */
5701}
5702
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005703static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005704{
5705 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005706 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005707
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005708 /*
5709 * Required for FBC
5710 * WaFbcDisableDpfcClockGating:ilk
5711 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005712 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5713 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5714 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005715
5716 I915_WRITE(PCH_3DCGDIS0,
5717 MARIUNIT_CLOCK_GATE_DISABLE |
5718 SVSMUNIT_CLOCK_GATE_DISABLE);
5719 I915_WRITE(PCH_3DCGDIS1,
5720 VFMUNIT_CLOCK_GATE_DISABLE);
5721
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005722 /*
5723 * According to the spec the following bits should be set in
5724 * order to enable memory self-refresh
5725 * The bit 22/21 of 0x42004
5726 * The bit 5 of 0x42020
5727 * The bit 15 of 0x45000
5728 */
5729 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5730 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5731 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005732 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005733 I915_WRITE(DISP_ARB_CTL,
5734 (I915_READ(DISP_ARB_CTL) |
5735 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005736
5737 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005738
5739 /*
5740 * Based on the document from hardware guys the following bits
5741 * should be set unconditionally in order to enable FBC.
5742 * The bit 22 of 0x42000
5743 * The bit 22 of 0x42004
5744 * The bit 7,8,9 of 0x42020.
5745 */
5746 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005747 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005748 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5749 I915_READ(ILK_DISPLAY_CHICKEN1) |
5750 ILK_FBCQ_DIS);
5751 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5752 I915_READ(ILK_DISPLAY_CHICKEN2) |
5753 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005754 }
5755
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005756 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5757
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005758 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5759 I915_READ(ILK_DISPLAY_CHICKEN2) |
5760 ILK_ELPIN_409_SELECT);
5761 I915_WRITE(_3D_CHICKEN2,
5762 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5763 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005764
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005765 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005766 I915_WRITE(CACHE_MODE_0,
5767 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005768
Akash Goel4e046322014-04-04 17:14:38 +05305769 /* WaDisable_RenderCache_OperationalFlush:ilk */
5770 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5771
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005772 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005773
Daniel Vetter3107bd42012-10-31 22:52:31 +01005774 ibx_init_clock_gating(dev);
5775}
5776
5777static void cpt_init_clock_gating(struct drm_device *dev)
5778{
5779 struct drm_i915_private *dev_priv = dev->dev_private;
5780 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005781 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005782
5783 /*
5784 * On Ibex Peak and Cougar Point, we need to disable clock
5785 * gating for the panel power sequencer or it will fail to
5786 * start up when no ports are active.
5787 */
Jesse Barnescd664072013-10-02 10:34:19 -07005788 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5789 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5790 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005791 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5792 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005793 /* The below fixes the weird display corruption, a few pixels shifted
5794 * downward, on (only) LVDS of some HP laptops with IVY.
5795 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005796 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005797 val = I915_READ(TRANS_CHICKEN2(pipe));
5798 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5799 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005800 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005801 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005802 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5803 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5804 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005805 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5806 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005807 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005808 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005809 I915_WRITE(TRANS_CHICKEN1(pipe),
5810 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5811 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005812}
5813
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005814static void gen6_check_mch_setup(struct drm_device *dev)
5815{
5816 struct drm_i915_private *dev_priv = dev->dev_private;
5817 uint32_t tmp;
5818
5819 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005820 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5821 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5822 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005823}
5824
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005825static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005826{
5827 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005828 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005829
Damien Lespiau231e54f2012-10-19 17:55:41 +01005830 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005831
5832 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5833 I915_READ(ILK_DISPLAY_CHICKEN2) |
5834 ILK_ELPIN_409_SELECT);
5835
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005836 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005837 I915_WRITE(_3D_CHICKEN,
5838 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5839
Akash Goel4e046322014-04-04 17:14:38 +05305840 /* WaDisable_RenderCache_OperationalFlush:snb */
5841 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5842
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005843 /*
5844 * BSpec recoomends 8x4 when MSAA is used,
5845 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005846 *
5847 * Note that PS/WM thread counts depend on the WIZ hashing
5848 * disable bit, which we don't touch here, but it's good
5849 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005850 */
5851 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005852 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005853
Ville Syrjälä017636c2013-12-05 15:51:37 +02005854 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005855
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005856 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005857 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005858
5859 I915_WRITE(GEN6_UCGCTL1,
5860 I915_READ(GEN6_UCGCTL1) |
5861 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5862 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5863
5864 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5865 * gating disable must be set. Failure to set it results in
5866 * flickering pixels due to Z write ordering failures after
5867 * some amount of runtime in the Mesa "fire" demo, and Unigine
5868 * Sanctuary and Tropics, and apparently anything else with
5869 * alpha test or pixel discard.
5870 *
5871 * According to the spec, bit 11 (RCCUNIT) must also be set,
5872 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005873 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005874 * WaDisableRCCUnitClockGating:snb
5875 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005876 */
5877 I915_WRITE(GEN6_UCGCTL2,
5878 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5879 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5880
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005881 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005882 I915_WRITE(_3D_CHICKEN3,
5883 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005884
5885 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005886 * Bspec says:
5887 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5888 * 3DSTATE_SF number of SF output attributes is more than 16."
5889 */
5890 I915_WRITE(_3D_CHICKEN3,
5891 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5892
5893 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005894 * According to the spec the following bits should be
5895 * set in order to enable memory self-refresh and fbc:
5896 * The bit21 and bit22 of 0x42000
5897 * The bit21 and bit22 of 0x42004
5898 * The bit5 and bit7 of 0x42020
5899 * The bit14 of 0x70180
5900 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005901 *
5902 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005903 */
5904 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5905 I915_READ(ILK_DISPLAY_CHICKEN1) |
5906 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5907 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5908 I915_READ(ILK_DISPLAY_CHICKEN2) |
5909 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005910 I915_WRITE(ILK_DSPCLK_GATE_D,
5911 I915_READ(ILK_DSPCLK_GATE_D) |
5912 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5913 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005914
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005915 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005916
Daniel Vetter3107bd42012-10-31 22:52:31 +01005917 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005918
5919 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005920}
5921
5922static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5923{
5924 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5925
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005926 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005927 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005928 *
5929 * This actually overrides the dispatch
5930 * mode for all thread types.
5931 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005932 reg &= ~GEN7_FF_SCHED_MASK;
5933 reg |= GEN7_FF_TS_SCHED_HW;
5934 reg |= GEN7_FF_VS_SCHED_HW;
5935 reg |= GEN7_FF_DS_SCHED_HW;
5936
5937 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5938}
5939
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005940static void lpt_init_clock_gating(struct drm_device *dev)
5941{
5942 struct drm_i915_private *dev_priv = dev->dev_private;
5943
5944 /*
5945 * TODO: this bit should only be enabled when really needed, then
5946 * disabled when not needed anymore in order to save power.
5947 */
5948 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5949 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5950 I915_READ(SOUTH_DSPCLK_GATE_D) |
5951 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005952
5953 /* WADPOClockGatingDisable:hsw */
5954 I915_WRITE(_TRANSA_CHICKEN1,
5955 I915_READ(_TRANSA_CHICKEN1) |
5956 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005957}
5958
Imre Deak7d708ee2013-04-17 14:04:50 +03005959static void lpt_suspend_hw(struct drm_device *dev)
5960{
5961 struct drm_i915_private *dev_priv = dev->dev_private;
5962
5963 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5964 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5965
5966 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5967 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5968 }
5969}
5970
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005971static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005972{
5973 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005974 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005975
5976 I915_WRITE(WM3_LP_ILK, 0);
5977 I915_WRITE(WM2_LP_ILK, 0);
5978 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005979
Ben Widawskyab57fff2013-12-12 15:28:04 -08005980 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005981 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005982
Ben Widawskyab57fff2013-12-12 15:28:04 -08005983 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005984 I915_WRITE(CHICKEN_PAR1_1,
5985 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5986
Ben Widawskyab57fff2013-12-12 15:28:04 -08005987 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005988 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005989 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005990 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005991 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005992 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005993
Ben Widawskyab57fff2013-12-12 15:28:04 -08005994 /* WaVSRefCountFullforceMissDisable:bdw */
5995 /* WaDSRefCountFullforceMissDisable:bdw */
5996 I915_WRITE(GEN7_FF_THREAD_MODE,
5997 I915_READ(GEN7_FF_THREAD_MODE) &
5998 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02005999
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006000 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6001 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006002
6003 /* WaDisableSDEUnitClockGating:bdw */
6004 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6005 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006006
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006007 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006008}
6009
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006010static void haswell_init_clock_gating(struct drm_device *dev)
6011{
6012 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006013
Ville Syrjälä017636c2013-12-05 15:51:37 +02006014 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006015
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006016 /* L3 caching of data atomics doesn't work -- disable it. */
6017 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6018 I915_WRITE(HSW_ROW_CHICKEN3,
6019 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6020
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006021 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006022 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6023 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6024 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6025
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006026 /* WaVSRefCountFullforceMissDisable:hsw */
6027 I915_WRITE(GEN7_FF_THREAD_MODE,
6028 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006029
Akash Goel4e046322014-04-04 17:14:38 +05306030 /* WaDisable_RenderCache_OperationalFlush:hsw */
6031 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6032
Chia-I Wufe27c602014-01-28 13:29:33 +08006033 /* enable HiZ Raw Stall Optimization */
6034 I915_WRITE(CACHE_MODE_0_GEN7,
6035 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6036
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006037 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006038 I915_WRITE(CACHE_MODE_1,
6039 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006040
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006041 /*
6042 * BSpec recommends 8x4 when MSAA is used,
6043 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006044 *
6045 * Note that PS/WM thread counts depend on the WIZ hashing
6046 * disable bit, which we don't touch here, but it's good
6047 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006048 */
6049 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006050 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006051
Kenneth Graunke94411592014-12-31 16:23:00 -08006052 /* WaSampleCChickenBitEnable:hsw */
6053 I915_WRITE(HALF_SLICE_CHICKEN3,
6054 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6055
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006056 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006057 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6058
Paulo Zanoni90a88642013-05-03 17:23:45 -03006059 /* WaRsPkgCStateDisplayPMReq:hsw */
6060 I915_WRITE(CHICKEN_PAR1_1,
6061 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006062
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006063 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006064}
6065
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006066static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006067{
6068 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006069 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006070
Ville Syrjälä017636c2013-12-05 15:51:37 +02006071 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006072
Damien Lespiau231e54f2012-10-19 17:55:41 +01006073 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006074
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006075 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006076 I915_WRITE(_3D_CHICKEN3,
6077 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6078
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006079 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006080 I915_WRITE(IVB_CHICKEN3,
6081 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6082 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6083
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006084 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006085 if (IS_IVB_GT1(dev))
6086 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6087 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006088
Akash Goel4e046322014-04-04 17:14:38 +05306089 /* WaDisable_RenderCache_OperationalFlush:ivb */
6090 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6091
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006092 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006093 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6094 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6095
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006096 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006097 I915_WRITE(GEN7_L3CNTLREG1,
6098 GEN7_WA_FOR_GEN7_L3_CONTROL);
6099 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006100 GEN7_WA_L3_CHICKEN_MODE);
6101 if (IS_IVB_GT1(dev))
6102 I915_WRITE(GEN7_ROW_CHICKEN2,
6103 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006104 else {
6105 /* must write both registers */
6106 I915_WRITE(GEN7_ROW_CHICKEN2,
6107 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006108 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6109 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006110 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006111
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006112 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006113 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6114 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6115
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006116 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006117 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006118 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006119 */
6120 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006121 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006122
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006123 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006124 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6125 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6126 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6127
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006128 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006129
6130 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006131
Chris Wilson22721342014-03-04 09:41:43 +00006132 if (0) { /* causes HiZ corruption on ivb:gt1 */
6133 /* enable HiZ Raw Stall Optimization */
6134 I915_WRITE(CACHE_MODE_0_GEN7,
6135 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6136 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006137
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006138 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006139 I915_WRITE(CACHE_MODE_1,
6140 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006141
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006142 /*
6143 * BSpec recommends 8x4 when MSAA is used,
6144 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006145 *
6146 * Note that PS/WM thread counts depend on the WIZ hashing
6147 * disable bit, which we don't touch here, but it's good
6148 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006149 */
6150 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006151 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006152
Ben Widawsky20848222012-05-04 18:58:59 -07006153 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6154 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6155 snpcr |= GEN6_MBC_SNPCR_MED;
6156 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006157
Ben Widawskyab5c6082013-04-05 13:12:41 -07006158 if (!HAS_PCH_NOP(dev))
6159 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006160
6161 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006162}
6163
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006164static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6165{
6166 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6167
6168 /*
6169 * Disable trickle feed and enable pnd deadline calculation
6170 */
6171 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6172 I915_WRITE(CBR1_VLV, 0);
6173}
6174
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006175static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006176{
6177 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006178
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006179 vlv_init_display_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006180
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006181 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006182 I915_WRITE(_3D_CHICKEN3,
6183 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6184
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006185 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006186 I915_WRITE(IVB_CHICKEN3,
6187 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6188 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6189
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006190 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006191 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006192 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006193 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6194 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006195
Akash Goel4e046322014-04-04 17:14:38 +05306196 /* WaDisable_RenderCache_OperationalFlush:vlv */
6197 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6198
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006199 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006200 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6201 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6202
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006203 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006204 I915_WRITE(GEN7_ROW_CHICKEN2,
6205 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6206
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006207 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006208 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6209 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6210 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6211
Ville Syrjälä46680e02014-01-22 21:33:01 +02006212 gen7_setup_fixed_func_scheduler(dev_priv);
6213
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006214 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006215 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006216 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006217 */
6218 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006219 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006220
Akash Goelc98f5062014-03-24 23:00:07 +05306221 /* WaDisableL3Bank2xClockGate:vlv
6222 * Disabling L3 clock gating- MMIO 940c[25] = 1
6223 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6224 I915_WRITE(GEN7_UCGCTL4,
6225 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006226
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006227 /*
6228 * BSpec says this must be set, even though
6229 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6230 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006231 I915_WRITE(CACHE_MODE_1,
6232 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006233
6234 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006235 * BSpec recommends 8x4 when MSAA is used,
6236 * however in practice 16x4 seems fastest.
6237 *
6238 * Note that PS/WM thread counts depend on the WIZ hashing
6239 * disable bit, which we don't touch here, but it's good
6240 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6241 */
6242 I915_WRITE(GEN7_GT_MODE,
6243 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6244
6245 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006246 * WaIncreaseL3CreditsForVLVB0:vlv
6247 * This is the hardware default actually.
6248 */
6249 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6250
6251 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006252 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006253 * Disable clock gating on th GCFG unit to prevent a delay
6254 * in the reporting of vblank events.
6255 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006256 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006257}
6258
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006259static void cherryview_init_clock_gating(struct drm_device *dev)
6260{
6261 struct drm_i915_private *dev_priv = dev->dev_private;
6262
Ville Syrjäläc6beb132015-03-05 21:19:48 +02006263 vlv_init_display_clock_gating(dev_priv);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006264
Ville Syrjälä232ce332014-04-09 13:28:35 +03006265 /* WaVSRefCountFullforceMissDisable:chv */
6266 /* WaDSRefCountFullforceMissDisable:chv */
6267 I915_WRITE(GEN7_FF_THREAD_MODE,
6268 I915_READ(GEN7_FF_THREAD_MODE) &
6269 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006270
6271 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6272 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6273 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006274
6275 /* WaDisableCSUnitClockGating:chv */
6276 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6277 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006278
6279 /* WaDisableSDEUnitClockGating:chv */
6280 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6281 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006282}
6283
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006284static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006285{
6286 struct drm_i915_private *dev_priv = dev->dev_private;
6287 uint32_t dspclk_gate;
6288
6289 I915_WRITE(RENCLK_GATE_D1, 0);
6290 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6291 GS_UNIT_CLOCK_GATE_DISABLE |
6292 CL_UNIT_CLOCK_GATE_DISABLE);
6293 I915_WRITE(RAMCLK_GATE_D, 0);
6294 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6295 OVRUNIT_CLOCK_GATE_DISABLE |
6296 OVCUNIT_CLOCK_GATE_DISABLE;
6297 if (IS_GM45(dev))
6298 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6299 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006300
6301 /* WaDisableRenderCachePipelinedFlush */
6302 I915_WRITE(CACHE_MODE_0,
6303 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006304
Akash Goel4e046322014-04-04 17:14:38 +05306305 /* WaDisable_RenderCache_OperationalFlush:g4x */
6306 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6307
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006308 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006309}
6310
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006311static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006312{
6313 struct drm_i915_private *dev_priv = dev->dev_private;
6314
6315 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6316 I915_WRITE(RENCLK_GATE_D2, 0);
6317 I915_WRITE(DSPCLK_GATE_D, 0);
6318 I915_WRITE(RAMCLK_GATE_D, 0);
6319 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006320 I915_WRITE(MI_ARB_STATE,
6321 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306322
6323 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6324 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006325}
6326
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006327static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006328{
6329 struct drm_i915_private *dev_priv = dev->dev_private;
6330
6331 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6332 I965_RCC_CLOCK_GATE_DISABLE |
6333 I965_RCPB_CLOCK_GATE_DISABLE |
6334 I965_ISC_CLOCK_GATE_DISABLE |
6335 I965_FBC_CLOCK_GATE_DISABLE);
6336 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006337 I915_WRITE(MI_ARB_STATE,
6338 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306339
6340 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6341 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006342}
6343
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006344static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006345{
6346 struct drm_i915_private *dev_priv = dev->dev_private;
6347 u32 dstate = I915_READ(D_STATE);
6348
6349 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6350 DSTATE_DOT_CLOCK_GATING;
6351 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006352
6353 if (IS_PINEVIEW(dev))
6354 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006355
6356 /* IIR "flip pending" means done if this bit is set */
6357 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006358
6359 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006360 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006361
6362 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6363 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006364
6365 I915_WRITE(MI_ARB_STATE,
6366 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006367}
6368
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006369static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006370{
6371 struct drm_i915_private *dev_priv = dev->dev_private;
6372
6373 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006374
6375 /* interrupts should cause a wake up from C3 */
6376 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6377 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006378
6379 I915_WRITE(MEM_MODE,
6380 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006381}
6382
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006383static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006384{
6385 struct drm_i915_private *dev_priv = dev->dev_private;
6386
6387 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006388
6389 I915_WRITE(MEM_MODE,
6390 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6391 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006392}
6393
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006394void intel_init_clock_gating(struct drm_device *dev)
6395{
6396 struct drm_i915_private *dev_priv = dev->dev_private;
6397
Damien Lespiauc57e3552015-02-09 19:33:05 +00006398 if (dev_priv->display.init_clock_gating)
6399 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006400}
6401
Imre Deak7d708ee2013-04-17 14:04:50 +03006402void intel_suspend_hw(struct drm_device *dev)
6403{
6404 if (HAS_PCH_LPT(dev))
6405 lpt_suspend_hw(dev);
6406}
6407
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006408/* Set up chip specific power management-related functions */
6409void intel_init_pm(struct drm_device *dev)
6410{
6411 struct drm_i915_private *dev_priv = dev->dev_private;
6412
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006413 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006414
Daniel Vetterc921aba2012-04-26 23:28:17 +02006415 /* For cxsr */
6416 if (IS_PINEVIEW(dev))
6417 i915_pineview_get_mem_freq(dev);
6418 else if (IS_GEN5(dev))
6419 i915_ironlake_get_mem_freq(dev);
6420
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006421 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006422 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006423 skl_setup_wm_latency(dev);
6424
Damien Lespiau45db2192015-02-09 19:33:09 +00006425 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006426 dev_priv->display.update_wm = skl_update_wm;
6427 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306428 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006429 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006430
Ville Syrjäläbd602542014-01-07 16:14:10 +02006431 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6432 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6433 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6434 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6435 dev_priv->display.update_wm = ilk_update_wm;
6436 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6437 } else {
6438 DRM_DEBUG_KMS("Failed to read display plane latency. "
6439 "Disable CxSR\n");
6440 }
6441
6442 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006443 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006444 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006445 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006446 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006447 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006448 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006449 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006450 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006451 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006452 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläae801522015-03-05 21:19:49 +02006453 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306454 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006455 dev_priv->display.init_clock_gating =
6456 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006457 } else if (IS_VALLEYVIEW(dev)) {
6458 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306459 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006460 dev_priv->display.init_clock_gating =
6461 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006462 } else if (IS_PINEVIEW(dev)) {
6463 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6464 dev_priv->is_ddr3,
6465 dev_priv->fsb_freq,
6466 dev_priv->mem_freq)) {
6467 DRM_INFO("failed to find known CxSR latency "
6468 "(found ddr%s fsb freq %d, mem freq %d), "
6469 "disabling CxSR\n",
6470 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6471 dev_priv->fsb_freq, dev_priv->mem_freq);
6472 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006473 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006474 dev_priv->display.update_wm = NULL;
6475 } else
6476 dev_priv->display.update_wm = pineview_update_wm;
6477 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6478 } else if (IS_G4X(dev)) {
6479 dev_priv->display.update_wm = g4x_update_wm;
6480 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6481 } else if (IS_GEN4(dev)) {
6482 dev_priv->display.update_wm = i965_update_wm;
6483 if (IS_CRESTLINE(dev))
6484 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6485 else if (IS_BROADWATER(dev))
6486 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6487 } else if (IS_GEN3(dev)) {
6488 dev_priv->display.update_wm = i9xx_update_wm;
6489 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6490 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006491 } else if (IS_GEN2(dev)) {
6492 if (INTEL_INFO(dev)->num_pipes == 1) {
6493 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006494 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006495 } else {
6496 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006497 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006498 }
6499
6500 if (IS_I85X(dev) || IS_I865G(dev))
6501 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6502 else
6503 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6504 } else {
6505 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006506 }
6507}
6508
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006509int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006510{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006511 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006512
6513 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6514 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6515 return -EAGAIN;
6516 }
6517
6518 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006519 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006520 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6521
6522 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6523 500)) {
6524 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6525 return -ETIMEDOUT;
6526 }
6527
6528 *val = I915_READ(GEN6_PCODE_DATA);
6529 I915_WRITE(GEN6_PCODE_DATA, 0);
6530
6531 return 0;
6532}
6533
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006534int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006535{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006536 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006537
6538 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6539 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6540 return -EAGAIN;
6541 }
6542
6543 I915_WRITE(GEN6_PCODE_DATA, val);
6544 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6545
6546 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6547 500)) {
6548 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6549 return -ETIMEDOUT;
6550 }
6551
6552 I915_WRITE(GEN6_PCODE_DATA, 0);
6553
6554 return 0;
6555}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006556
Ville Syrjälädd06f882014-11-10 22:55:12 +02006557static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006558{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006559 switch (czclk_freq) {
6560 case 200:
6561 return 10;
6562 case 267:
6563 return 12;
6564 case 320:
6565 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006566 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006567 case 400:
6568 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006569 default:
6570 return -1;
6571 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006572}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006573
Ville Syrjälädd06f882014-11-10 22:55:12 +02006574static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6575{
6576 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6577
6578 div = vlv_gpu_freq_div(czclk_freq);
6579 if (div < 0)
6580 return div;
6581
6582 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006583}
6584
Fengguang Wub55dd642014-07-12 11:21:39 +02006585static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006586{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006587 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006588
Ville Syrjälädd06f882014-11-10 22:55:12 +02006589 mul = vlv_gpu_freq_div(czclk_freq);
6590 if (mul < 0)
6591 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006592
Ville Syrjälädd06f882014-11-10 22:55:12 +02006593 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006594}
6595
Fengguang Wub55dd642014-07-12 11:21:39 +02006596static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306597{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006598 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306599
Ville Syrjälädd06f882014-11-10 22:55:12 +02006600 div = vlv_gpu_freq_div(czclk_freq) / 2;
6601 if (div < 0)
6602 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306603
Ville Syrjälädd06f882014-11-10 22:55:12 +02006604 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306605}
6606
Fengguang Wub55dd642014-07-12 11:21:39 +02006607static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306608{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006609 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306610
Ville Syrjälädd06f882014-11-10 22:55:12 +02006611 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6612 if (mul < 0)
6613 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306614
Ville Syrjälä1c147622014-08-18 14:42:43 +03006615 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006616 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306617}
6618
Ville Syrjälä616bc822015-01-23 21:04:25 +02006619int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6620{
6621 if (IS_CHERRYVIEW(dev_priv->dev))
6622 return chv_gpu_freq(dev_priv, val);
6623 else if (IS_VALLEYVIEW(dev_priv->dev))
6624 return byt_gpu_freq(dev_priv, val);
6625 else
6626 return val * GT_FREQUENCY_MULTIPLIER;
6627}
6628
Ville Syrjälä616bc822015-01-23 21:04:25 +02006629int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6630{
Deepak S22b1b2f2014-07-12 14:54:33 +05306631 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006632 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306633 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006634 return byt_freq_opcode(dev_priv, val);
6635 else
6636 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306637}
6638
Daniel Vetterf742a552013-12-06 10:17:53 +01006639void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006640{
6641 struct drm_i915_private *dev_priv = dev->dev_private;
6642
Daniel Vetterf742a552013-12-06 10:17:53 +01006643 mutex_init(&dev_priv->rps.hw_lock);
6644
Chris Wilson907b28c2013-07-19 20:36:52 +01006645 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6646 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006647
Paulo Zanoni33688d92014-03-07 20:08:19 -03006648 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006649}