blob: d7750176091b1951fb4f64cce9600ea978f24377 [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 Lespiau45db2192015-02-09 19:33:09 +000055static void skl_init_clock_gating(struct drm_device *dev)
Damien Lespiauda2078c2013-02-13 15:27:27 +000056{
Damien Lespiauacd5c342014-03-26 16:55:46 +000057 struct drm_i915_private *dev_priv = dev->dev_private;
58
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000059 if (INTEL_REVID(dev) == SKL_REVID_A0) {
60 /*
61 * WaDisableSDEUnitClockGating:skl
Damien Lespiau9253c2e2015-02-09 19:33:10 +000062 * WaSetGAPSunitClckGateDisable:skl
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000063 */
64 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Damien Lespiau9253c2e2015-02-09 19:33:10 +000065 GEN8_GAPSUNIT_CLOCK_GATE_DISABLE |
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000066 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
67 }
Damien Lespiau8bc0ccf2015-02-09 19:33:18 +000068
69 if (INTEL_REVID(dev) <= SKL_REVID_E0)
70 /* WaDisableLSQCROPERFforOCL:skl */
71 I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
72 GEN8_LQSC_RO_PERF_DIS);
Damien Lespiauda2078c2013-02-13 15:27:27 +000073}
74
Daniel Vetterc921aba2012-04-26 23:28:17 +020075static void i915_pineview_get_mem_freq(struct drm_device *dev)
76{
Jani Nikula50227e12014-03-31 14:27:21 +030077 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +020078 u32 tmp;
79
80 tmp = I915_READ(CLKCFG);
81
82 switch (tmp & CLKCFG_FSB_MASK) {
83 case CLKCFG_FSB_533:
84 dev_priv->fsb_freq = 533; /* 133*4 */
85 break;
86 case CLKCFG_FSB_800:
87 dev_priv->fsb_freq = 800; /* 200*4 */
88 break;
89 case CLKCFG_FSB_667:
90 dev_priv->fsb_freq = 667; /* 167*4 */
91 break;
92 case CLKCFG_FSB_400:
93 dev_priv->fsb_freq = 400; /* 100*4 */
94 break;
95 }
96
97 switch (tmp & CLKCFG_MEM_MASK) {
98 case CLKCFG_MEM_533:
99 dev_priv->mem_freq = 533;
100 break;
101 case CLKCFG_MEM_667:
102 dev_priv->mem_freq = 667;
103 break;
104 case CLKCFG_MEM_800:
105 dev_priv->mem_freq = 800;
106 break;
107 }
108
109 /* detect pineview DDR3 setting */
110 tmp = I915_READ(CSHRDDR3CTL);
111 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
112}
113
114static void i915_ironlake_get_mem_freq(struct drm_device *dev)
115{
Jani Nikula50227e12014-03-31 14:27:21 +0300116 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200117 u16 ddrpll, csipll;
118
119 ddrpll = I915_READ16(DDRMPLL1);
120 csipll = I915_READ16(CSIPLL0);
121
122 switch (ddrpll & 0xff) {
123 case 0xc:
124 dev_priv->mem_freq = 800;
125 break;
126 case 0x10:
127 dev_priv->mem_freq = 1066;
128 break;
129 case 0x14:
130 dev_priv->mem_freq = 1333;
131 break;
132 case 0x18:
133 dev_priv->mem_freq = 1600;
134 break;
135 default:
136 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
137 ddrpll & 0xff);
138 dev_priv->mem_freq = 0;
139 break;
140 }
141
Daniel Vetter20e4d402012-08-08 23:35:39 +0200142 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200143
144 switch (csipll & 0x3ff) {
145 case 0x00c:
146 dev_priv->fsb_freq = 3200;
147 break;
148 case 0x00e:
149 dev_priv->fsb_freq = 3733;
150 break;
151 case 0x010:
152 dev_priv->fsb_freq = 4266;
153 break;
154 case 0x012:
155 dev_priv->fsb_freq = 4800;
156 break;
157 case 0x014:
158 dev_priv->fsb_freq = 5333;
159 break;
160 case 0x016:
161 dev_priv->fsb_freq = 5866;
162 break;
163 case 0x018:
164 dev_priv->fsb_freq = 6400;
165 break;
166 default:
167 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
168 csipll & 0x3ff);
169 dev_priv->fsb_freq = 0;
170 break;
171 }
172
173 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200174 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200175 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200176 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200177 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200178 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200179 }
180}
181
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300182static const struct cxsr_latency cxsr_latency_table[] = {
183 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
184 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
185 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
186 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
187 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
188
189 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
190 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
191 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
192 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
193 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
194
195 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
196 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
197 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
198 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
199 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
200
201 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
202 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
203 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
204 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
205 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
206
207 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
208 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
209 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
210 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
211 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
212
213 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
214 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
215 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
216 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
217 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
218};
219
Daniel Vetter63c62272012-04-21 23:17:55 +0200220static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300221 int is_ddr3,
222 int fsb,
223 int mem)
224{
225 const struct cxsr_latency *latency;
226 int i;
227
228 if (fsb == 0 || mem == 0)
229 return NULL;
230
231 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
232 latency = &cxsr_latency_table[i];
233 if (is_desktop == latency->is_desktop &&
234 is_ddr3 == latency->is_ddr3 &&
235 fsb == latency->fsb_freq && mem == latency->mem_freq)
236 return latency;
237 }
238
239 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
240
241 return NULL;
242}
243
Imre Deak5209b1f2014-07-01 12:36:17 +0300244void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300245{
Imre Deak5209b1f2014-07-01 12:36:17 +0300246 struct drm_device *dev = dev_priv->dev;
247 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300248
Imre Deak5209b1f2014-07-01 12:36:17 +0300249 if (IS_VALLEYVIEW(dev)) {
250 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
251 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
252 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
253 } else if (IS_PINEVIEW(dev)) {
254 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
255 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
256 I915_WRITE(DSPFW3, val);
257 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
258 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
259 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
260 I915_WRITE(FW_BLC_SELF, val);
261 } else if (IS_I915GM(dev)) {
262 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
263 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
264 I915_WRITE(INSTPM, val);
265 } else {
266 return;
267 }
268
269 DRM_DEBUG_KMS("memory self-refresh is %s\n",
270 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300271}
272
273/*
274 * Latency for FIFO fetches is dependent on several factors:
275 * - memory configuration (speed, channels)
276 * - chipset
277 * - current MCH state
278 * It can be fairly high in some situations, so here we assume a fairly
279 * pessimal value. It's a tradeoff between extra memory fetches (if we
280 * set this value too high, the FIFO will fetch frequently to stay full)
281 * and power consumption (set it too low to save power and we might see
282 * FIFO underruns and display "flicker").
283 *
284 * A value of 5us seems to be a good balance; safe for very low end
285 * platforms but not overly aggressive on lower latency configs.
286 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100287static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300288
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300289static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300290{
291 struct drm_i915_private *dev_priv = dev->dev_private;
292 uint32_t dsparb = I915_READ(DSPARB);
293 int size;
294
295 size = dsparb & 0x7f;
296 if (plane)
297 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
298
299 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
300 plane ? "B" : "A", size);
301
302 return size;
303}
304
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200305static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300306{
307 struct drm_i915_private *dev_priv = dev->dev_private;
308 uint32_t dsparb = I915_READ(DSPARB);
309 int size;
310
311 size = dsparb & 0x1ff;
312 if (plane)
313 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
314 size >>= 1; /* Convert to cachelines */
315
316 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
317 plane ? "B" : "A", size);
318
319 return size;
320}
321
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300322static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300323{
324 struct drm_i915_private *dev_priv = dev->dev_private;
325 uint32_t dsparb = I915_READ(DSPARB);
326 int size;
327
328 size = dsparb & 0x7f;
329 size >>= 2; /* Convert to cachelines */
330
331 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
332 plane ? "B" : "A",
333 size);
334
335 return size;
336}
337
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300338/* Pineview has different values for various configs */
339static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300340 .fifo_size = PINEVIEW_DISPLAY_FIFO,
341 .max_wm = PINEVIEW_MAX_WM,
342 .default_wm = PINEVIEW_DFT_WM,
343 .guard_size = PINEVIEW_GUARD_WM,
344 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300345};
346static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300347 .fifo_size = PINEVIEW_DISPLAY_FIFO,
348 .max_wm = PINEVIEW_MAX_WM,
349 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
350 .guard_size = PINEVIEW_GUARD_WM,
351 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300352};
353static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300354 .fifo_size = PINEVIEW_CURSOR_FIFO,
355 .max_wm = PINEVIEW_CURSOR_MAX_WM,
356 .default_wm = PINEVIEW_CURSOR_DFT_WM,
357 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
358 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300359};
360static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300361 .fifo_size = PINEVIEW_CURSOR_FIFO,
362 .max_wm = PINEVIEW_CURSOR_MAX_WM,
363 .default_wm = PINEVIEW_CURSOR_DFT_WM,
364 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
365 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300366};
367static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300368 .fifo_size = G4X_FIFO_SIZE,
369 .max_wm = G4X_MAX_WM,
370 .default_wm = G4X_MAX_WM,
371 .guard_size = 2,
372 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300373};
374static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300375 .fifo_size = I965_CURSOR_FIFO,
376 .max_wm = I965_CURSOR_MAX_WM,
377 .default_wm = I965_CURSOR_DFT_WM,
378 .guard_size = 2,
379 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300380};
381static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300382 .fifo_size = VALLEYVIEW_FIFO_SIZE,
383 .max_wm = VALLEYVIEW_MAX_WM,
384 .default_wm = VALLEYVIEW_MAX_WM,
385 .guard_size = 2,
386 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300387};
388static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300389 .fifo_size = I965_CURSOR_FIFO,
390 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
391 .default_wm = I965_CURSOR_DFT_WM,
392 .guard_size = 2,
393 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300394};
395static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300396 .fifo_size = I965_CURSOR_FIFO,
397 .max_wm = I965_CURSOR_MAX_WM,
398 .default_wm = I965_CURSOR_DFT_WM,
399 .guard_size = 2,
400 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300401};
402static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300403 .fifo_size = I945_FIFO_SIZE,
404 .max_wm = I915_MAX_WM,
405 .default_wm = 1,
406 .guard_size = 2,
407 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300408};
409static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300410 .fifo_size = I915_FIFO_SIZE,
411 .max_wm = I915_MAX_WM,
412 .default_wm = 1,
413 .guard_size = 2,
414 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300415};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300416static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300417 .fifo_size = I855GM_FIFO_SIZE,
418 .max_wm = I915_MAX_WM,
419 .default_wm = 1,
420 .guard_size = 2,
421 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300422};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300423static const struct intel_watermark_params i830_bc_wm_info = {
424 .fifo_size = I855GM_FIFO_SIZE,
425 .max_wm = I915_MAX_WM/2,
426 .default_wm = 1,
427 .guard_size = 2,
428 .cacheline_size = I830_FIFO_LINE_SIZE,
429};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200430static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300431 .fifo_size = I830_FIFO_SIZE,
432 .max_wm = I915_MAX_WM,
433 .default_wm = 1,
434 .guard_size = 2,
435 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300436};
437
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300438/**
439 * intel_calculate_wm - calculate watermark level
440 * @clock_in_khz: pixel clock
441 * @wm: chip FIFO params
442 * @pixel_size: display pixel size
443 * @latency_ns: memory latency for the platform
444 *
445 * Calculate the watermark level (the level at which the display plane will
446 * start fetching from memory again). Each chip has a different display
447 * FIFO size and allocation, so the caller needs to figure that out and pass
448 * in the correct intel_watermark_params structure.
449 *
450 * As the pixel clock runs, the FIFO will be drained at a rate that depends
451 * on the pixel size. When it reaches the watermark level, it'll start
452 * fetching FIFO line sized based chunks from memory until the FIFO fills
453 * past the watermark point. If the FIFO drains completely, a FIFO underrun
454 * will occur, and a display engine hang could result.
455 */
456static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
457 const struct intel_watermark_params *wm,
458 int fifo_size,
459 int pixel_size,
460 unsigned long latency_ns)
461{
462 long entries_required, wm_size;
463
464 /*
465 * Note: we need to make sure we don't overflow for various clock &
466 * latency values.
467 * clocks go from a few thousand to several hundred thousand.
468 * latency is usually a few thousand
469 */
470 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
471 1000;
472 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
473
474 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
475
476 wm_size = fifo_size - (entries_required + wm->guard_size);
477
478 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
479
480 /* Don't promote wm_size to unsigned... */
481 if (wm_size > (long)wm->max_wm)
482 wm_size = wm->max_wm;
483 if (wm_size <= 0)
484 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300485
486 /*
487 * Bspec seems to indicate that the value shouldn't be lower than
488 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
489 * Lets go for 8 which is the burst size since certain platforms
490 * already use a hardcoded 8 (which is what the spec says should be
491 * done).
492 */
493 if (wm_size <= 8)
494 wm_size = 8;
495
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300496 return wm_size;
497}
498
499static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
500{
501 struct drm_crtc *crtc, *enabled = NULL;
502
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100503 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000504 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300505 if (enabled)
506 return NULL;
507 enabled = crtc;
508 }
509 }
510
511 return enabled;
512}
513
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300514static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300516 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300517 struct drm_i915_private *dev_priv = dev->dev_private;
518 struct drm_crtc *crtc;
519 const struct cxsr_latency *latency;
520 u32 reg;
521 unsigned long wm;
522
523 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
524 dev_priv->fsb_freq, dev_priv->mem_freq);
525 if (!latency) {
526 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300527 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300528 return;
529 }
530
531 crtc = single_enabled_crtc(dev);
532 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100533 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -0700534 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100535 int clock;
536
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200537 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100538 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300539
540 /* Display SR */
541 wm = intel_calculate_wm(clock, &pineview_display_wm,
542 pineview_display_wm.fifo_size,
543 pixel_size, latency->display_sr);
544 reg = I915_READ(DSPFW1);
545 reg &= ~DSPFW_SR_MASK;
546 reg |= wm << DSPFW_SR_SHIFT;
547 I915_WRITE(DSPFW1, reg);
548 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
549
550 /* cursor SR */
551 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
552 pineview_display_wm.fifo_size,
553 pixel_size, latency->cursor_sr);
554 reg = I915_READ(DSPFW3);
555 reg &= ~DSPFW_CURSOR_SR_MASK;
556 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
557 I915_WRITE(DSPFW3, reg);
558
559 /* Display HPLL off SR */
560 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
561 pineview_display_hplloff_wm.fifo_size,
562 pixel_size, latency->display_hpll_disable);
563 reg = I915_READ(DSPFW3);
564 reg &= ~DSPFW_HPLL_SR_MASK;
565 reg |= wm & DSPFW_HPLL_SR_MASK;
566 I915_WRITE(DSPFW3, reg);
567
568 /* cursor HPLL off SR */
569 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
570 pineview_display_hplloff_wm.fifo_size,
571 pixel_size, latency->cursor_hpll_disable);
572 reg = I915_READ(DSPFW3);
573 reg &= ~DSPFW_HPLL_CURSOR_MASK;
574 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
575 I915_WRITE(DSPFW3, reg);
576 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
577
Imre Deak5209b1f2014-07-01 12:36:17 +0300578 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300580 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300581 }
582}
583
584static bool g4x_compute_wm0(struct drm_device *dev,
585 int plane,
586 const struct intel_watermark_params *display,
587 int display_latency_ns,
588 const struct intel_watermark_params *cursor,
589 int cursor_latency_ns,
590 int *plane_wm,
591 int *cursor_wm)
592{
593 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300594 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595 int htotal, hdisplay, clock, pixel_size;
596 int line_time_us, line_count;
597 int entries, tlb_miss;
598
599 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000600 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300601 *cursor_wm = cursor->guard_size;
602 *plane_wm = display->guard_size;
603 return false;
604 }
605
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200606 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100607 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800608 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200609 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -0700610 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300611
612 /* Use the small buffer method to calculate plane watermark */
613 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
614 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
615 if (tlb_miss > 0)
616 entries += tlb_miss;
617 entries = DIV_ROUND_UP(entries, display->cacheline_size);
618 *plane_wm = entries + display->guard_size;
619 if (*plane_wm > (int)display->max_wm)
620 *plane_wm = display->max_wm;
621
622 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200623 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +0000625 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300626 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
627 if (tlb_miss > 0)
628 entries += tlb_miss;
629 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
630 *cursor_wm = entries + cursor->guard_size;
631 if (*cursor_wm > (int)cursor->max_wm)
632 *cursor_wm = (int)cursor->max_wm;
633
634 return true;
635}
636
637/*
638 * Check the wm result.
639 *
640 * If any calculated watermark values is larger than the maximum value that
641 * can be programmed into the associated watermark register, that watermark
642 * must be disabled.
643 */
644static bool g4x_check_srwm(struct drm_device *dev,
645 int display_wm, int cursor_wm,
646 const struct intel_watermark_params *display,
647 const struct intel_watermark_params *cursor)
648{
649 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
650 display_wm, cursor_wm);
651
652 if (display_wm > display->max_wm) {
653 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
654 display_wm, display->max_wm);
655 return false;
656 }
657
658 if (cursor_wm > cursor->max_wm) {
659 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
660 cursor_wm, cursor->max_wm);
661 return false;
662 }
663
664 if (!(display_wm || cursor_wm)) {
665 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
666 return false;
667 }
668
669 return true;
670}
671
672static bool g4x_compute_srwm(struct drm_device *dev,
673 int plane,
674 int latency_ns,
675 const struct intel_watermark_params *display,
676 const struct intel_watermark_params *cursor,
677 int *display_wm, int *cursor_wm)
678{
679 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300680 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300681 int hdisplay, htotal, pixel_size, clock;
682 unsigned long line_time_us;
683 int line_count, line_size;
684 int small, large;
685 int entries;
686
687 if (!latency_ns) {
688 *display_wm = *cursor_wm = 0;
689 return false;
690 }
691
692 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200693 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100694 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800695 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200696 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -0700697 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300698
Ville Syrjälä922044c2014-02-14 14:18:57 +0200699 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300700 line_count = (latency_ns / line_time_us + 1000) / 1000;
701 line_size = hdisplay * pixel_size;
702
703 /* Use the minimum of the small and large buffer method for primary */
704 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
705 large = line_count * line_size;
706
707 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
708 *display_wm = entries + display->guard_size;
709
710 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +0000711 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300712 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
713 *cursor_wm = entries + cursor->guard_size;
714
715 return g4x_check_srwm(dev,
716 *display_wm, *cursor_wm,
717 display, cursor);
718}
719
Gajanan Bhat0948c262014-08-07 01:58:24 +0530720static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
721 int pixel_size,
722 int *prec_mult,
723 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700725 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300726 int entries;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200727 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300728
Gajanan Bhat0948c262014-08-07 01:58:24 +0530729 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300730 return false;
731
Gajanan Bhat0948c262014-08-07 01:58:24 +0530732 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
733 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300734
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530735 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700736 if (IS_CHERRYVIEW(dev))
737 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
738 DRAIN_LATENCY_PRECISION_16;
739 else
740 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
741 DRAIN_LATENCY_PRECISION_32;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530742 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300743
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530744 if (*drain_latency > DRAIN_LATENCY_MASK)
745 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746
747 return true;
748}
749
750/*
751 * Update drain latency registers of memory arbiter
752 *
753 * Valleyview SoC has a new memory arbiter and needs drain latency registers
754 * to be programmed. Each plane has a drain latency multiplier and a drain
755 * latency value.
756 */
757
Gajanan Bhat41aad812014-07-16 18:24:03 +0530758static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300759{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700760 struct drm_device *dev = crtc->dev;
761 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
763 int pixel_size;
764 int drain_latency;
765 enum pipe pipe = intel_crtc->pipe;
766 int plane_prec, prec_mult, plane_dl;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700767 const int high_precision = IS_CHERRYVIEW(dev) ?
768 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300769
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700770 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
771 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
Gajanan Bhat0948c262014-08-07 01:58:24 +0530772 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300773
Gajanan Bhat0948c262014-08-07 01:58:24 +0530774 if (!intel_crtc_active(crtc)) {
775 I915_WRITE(VLV_DDL(pipe), plane_dl);
776 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300777 }
778
Gajanan Bhat0948c262014-08-07 01:58:24 +0530779 /* Primary plane Drain Latency */
780 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
781 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700782 plane_prec = (prec_mult == high_precision) ?
783 DDL_PLANE_PRECISION_HIGH :
784 DDL_PLANE_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530785 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786 }
Gajanan Bhat0948c262014-08-07 01:58:24 +0530787
788 /* Cursor Drain Latency
789 * BPP is always 4 for cursor
790 */
791 pixel_size = 4;
792
793 /* Program cursor DL only if it is enabled */
794 if (intel_crtc->cursor_base &&
795 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700796 plane_prec = (prec_mult == high_precision) ?
797 DDL_CURSOR_PRECISION_HIGH :
798 DDL_CURSOR_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530799 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
800 }
801
802 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300803}
804
805#define single_plane_enabled(mask) is_power_of_2(mask)
806
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300807static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300808{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300809 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300810 static const int sr_latency_ns = 12000;
811 struct drm_i915_private *dev_priv = dev->dev_private;
812 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
813 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000814 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300815 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300816 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300817
Gajanan Bhat41aad812014-07-16 18:24:03 +0530818 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300819
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200820 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100821 &valleyview_wm_info, pessimal_latency_ns,
822 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300823 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200824 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300825
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200826 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100827 &valleyview_wm_info, pessimal_latency_ns,
828 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300829 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200830 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300831
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300832 if (single_plane_enabled(enabled) &&
833 g4x_compute_srwm(dev, ffs(enabled) - 1,
834 sr_latency_ns,
835 &valleyview_wm_info,
836 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000837 &plane_sr, &ignore_cursor_sr) &&
838 g4x_compute_srwm(dev, ffs(enabled) - 1,
839 2*sr_latency_ns,
840 &valleyview_wm_info,
841 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +0000842 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +0300843 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +0000844 } else {
Imre Deak98584252014-06-13 14:54:20 +0300845 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300846 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +0000847 plane_sr = cursor_sr = 0;
848 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300849
Ville Syrjäläa5043452014-06-28 02:04:18 +0300850 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
851 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300852 planea_wm, cursora_wm,
853 planeb_wm, cursorb_wm,
854 plane_sr, cursor_sr);
855
856 I915_WRITE(DSPFW1,
857 (plane_sr << DSPFW_SR_SHIFT) |
858 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
859 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +0300860 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300861 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +0000862 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300863 (cursora_wm << DSPFW_CURSORA_SHIFT));
864 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +0000865 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
866 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +0300867
868 if (cxsr_enabled)
869 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300870}
871
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300872static void cherryview_update_wm(struct drm_crtc *crtc)
873{
874 struct drm_device *dev = crtc->dev;
875 static const int sr_latency_ns = 12000;
876 struct drm_i915_private *dev_priv = dev->dev_private;
877 int planea_wm, planeb_wm, planec_wm;
878 int cursora_wm, cursorb_wm, cursorc_wm;
879 int plane_sr, cursor_sr;
880 int ignore_plane_sr, ignore_cursor_sr;
881 unsigned int enabled = 0;
882 bool cxsr_enabled;
883
884 vlv_update_drain_latency(crtc);
885
886 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100887 &valleyview_wm_info, pessimal_latency_ns,
888 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300889 &planea_wm, &cursora_wm))
890 enabled |= 1 << PIPE_A;
891
892 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100893 &valleyview_wm_info, pessimal_latency_ns,
894 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300895 &planeb_wm, &cursorb_wm))
896 enabled |= 1 << PIPE_B;
897
898 if (g4x_compute_wm0(dev, PIPE_C,
Chris Wilson5aef6002014-09-03 11:56:07 +0100899 &valleyview_wm_info, pessimal_latency_ns,
900 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300901 &planec_wm, &cursorc_wm))
902 enabled |= 1 << PIPE_C;
903
904 if (single_plane_enabled(enabled) &&
905 g4x_compute_srwm(dev, ffs(enabled) - 1,
906 sr_latency_ns,
907 &valleyview_wm_info,
908 &valleyview_cursor_wm_info,
909 &plane_sr, &ignore_cursor_sr) &&
910 g4x_compute_srwm(dev, ffs(enabled) - 1,
911 2*sr_latency_ns,
912 &valleyview_wm_info,
913 &valleyview_cursor_wm_info,
914 &ignore_plane_sr, &cursor_sr)) {
915 cxsr_enabled = true;
916 } else {
917 cxsr_enabled = false;
918 intel_set_memory_cxsr(dev_priv, false);
919 plane_sr = cursor_sr = 0;
920 }
921
922 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
923 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
924 "SR: plane=%d, cursor=%d\n",
925 planea_wm, cursora_wm,
926 planeb_wm, cursorb_wm,
927 planec_wm, cursorc_wm,
928 plane_sr, cursor_sr);
929
930 I915_WRITE(DSPFW1,
931 (plane_sr << DSPFW_SR_SHIFT) |
932 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
933 (planeb_wm << DSPFW_PLANEB_SHIFT) |
934 (planea_wm << DSPFW_PLANEA_SHIFT));
935 I915_WRITE(DSPFW2,
936 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
937 (cursora_wm << DSPFW_CURSORA_SHIFT));
938 I915_WRITE(DSPFW3,
939 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
940 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
941 I915_WRITE(DSPFW9_CHV,
942 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
943 DSPFW_CURSORC_MASK)) |
944 (planec_wm << DSPFW_PLANEC_SHIFT) |
945 (cursorc_wm << DSPFW_CURSORC_SHIFT));
946
947 if (cxsr_enabled)
948 intel_set_memory_cxsr(dev_priv, true);
949}
950
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530951static void valleyview_update_sprite_wm(struct drm_plane *plane,
952 struct drm_crtc *crtc,
953 uint32_t sprite_width,
954 uint32_t sprite_height,
955 int pixel_size,
956 bool enabled, bool scaled)
957{
958 struct drm_device *dev = crtc->dev;
959 struct drm_i915_private *dev_priv = dev->dev_private;
960 int pipe = to_intel_plane(plane)->pipe;
961 int sprite = to_intel_plane(plane)->plane;
962 int drain_latency;
963 int plane_prec;
964 int sprite_dl;
965 int prec_mult;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700966 const int high_precision = IS_CHERRYVIEW(dev) ?
967 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530968
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700969 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530970 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
971
972 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
973 &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700974 plane_prec = (prec_mult == high_precision) ?
975 DDL_SPRITE_PRECISION_HIGH(sprite) :
976 DDL_SPRITE_PRECISION_LOW(sprite);
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530977 sprite_dl |= plane_prec |
978 (drain_latency << DDL_SPRITE_SHIFT(sprite));
979 }
980
981 I915_WRITE(VLV_DDL(pipe), sprite_dl);
982}
983
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300984static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300985{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300986 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300987 static const int sr_latency_ns = 12000;
988 struct drm_i915_private *dev_priv = dev->dev_private;
989 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
990 int plane_sr, cursor_sr;
991 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300992 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300993
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200994 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100995 &g4x_wm_info, pessimal_latency_ns,
996 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300997 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200998 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300999
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001000 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001001 &g4x_wm_info, pessimal_latency_ns,
1002 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001003 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001004 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001005
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001006 if (single_plane_enabled(enabled) &&
1007 g4x_compute_srwm(dev, ffs(enabled) - 1,
1008 sr_latency_ns,
1009 &g4x_wm_info,
1010 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001011 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001012 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001013 } else {
Imre Deak98584252014-06-13 14:54:20 +03001014 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001015 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001016 plane_sr = cursor_sr = 0;
1017 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001018
Ville Syrjäläa5043452014-06-28 02:04:18 +03001019 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1020 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001021 planea_wm, cursora_wm,
1022 planeb_wm, cursorb_wm,
1023 plane_sr, cursor_sr);
1024
1025 I915_WRITE(DSPFW1,
1026 (plane_sr << DSPFW_SR_SHIFT) |
1027 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1028 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001029 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001030 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001031 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001032 (cursora_wm << DSPFW_CURSORA_SHIFT));
1033 /* HPLL off in SR has some issues on G4x... disable it */
1034 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001035 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001036 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001037
1038 if (cxsr_enabled)
1039 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001040}
1041
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001042static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001043{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001044 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001045 struct drm_i915_private *dev_priv = dev->dev_private;
1046 struct drm_crtc *crtc;
1047 int srwm = 1;
1048 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001049 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001050
1051 /* Calc sr entries for one plane configs */
1052 crtc = single_enabled_crtc(dev);
1053 if (crtc) {
1054 /* self-refresh has much higher latency */
1055 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001056 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001057 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001058 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001059 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001060 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001061 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001062 unsigned long line_time_us;
1063 int entries;
1064
Ville Syrjälä922044c2014-02-14 14:18:57 +02001065 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001066
1067 /* Use ns/us then divide to preserve precision */
1068 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1069 pixel_size * hdisplay;
1070 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1071 srwm = I965_FIFO_SIZE - entries;
1072 if (srwm < 0)
1073 srwm = 1;
1074 srwm &= 0x1ff;
1075 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1076 entries, srwm);
1077
1078 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001079 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001080 entries = DIV_ROUND_UP(entries,
1081 i965_cursor_wm_info.cacheline_size);
1082 cursor_sr = i965_cursor_wm_info.fifo_size -
1083 (entries + i965_cursor_wm_info.guard_size);
1084
1085 if (cursor_sr > i965_cursor_wm_info.max_wm)
1086 cursor_sr = i965_cursor_wm_info.max_wm;
1087
1088 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1089 "cursor %d\n", srwm, cursor_sr);
1090
Imre Deak98584252014-06-13 14:54:20 +03001091 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001092 } else {
Imre Deak98584252014-06-13 14:54:20 +03001093 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001094 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001095 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001096 }
1097
1098 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1099 srwm);
1100
1101 /* 965 has limitations... */
1102 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001103 (8 << DSPFW_CURSORB_SHIFT) |
1104 (8 << DSPFW_PLANEB_SHIFT) |
1105 (8 << DSPFW_PLANEA_SHIFT));
1106 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1107 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001108 /* update cursor SR watermark */
1109 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001110
1111 if (cxsr_enabled)
1112 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001113}
1114
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001115static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001116{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001117 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001118 struct drm_i915_private *dev_priv = dev->dev_private;
1119 const struct intel_watermark_params *wm_info;
1120 uint32_t fwater_lo;
1121 uint32_t fwater_hi;
1122 int cwm, srwm = 1;
1123 int fifo_size;
1124 int planea_wm, planeb_wm;
1125 struct drm_crtc *crtc, *enabled = NULL;
1126
1127 if (IS_I945GM(dev))
1128 wm_info = &i945_wm_info;
1129 else if (!IS_GEN2(dev))
1130 wm_info = &i915_wm_info;
1131 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001132 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001133
1134 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1135 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001136 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001137 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001138 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001139 if (IS_GEN2(dev))
1140 cpp = 4;
1141
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001142 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001143 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001144 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001145 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001146 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001147 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001148 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001149 if (planea_wm > (long)wm_info->max_wm)
1150 planea_wm = wm_info->max_wm;
1151 }
1152
1153 if (IS_GEN2(dev))
1154 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001155
1156 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1157 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001158 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001159 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001160 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001161 if (IS_GEN2(dev))
1162 cpp = 4;
1163
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001164 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001165 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001166 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001167 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001168 if (enabled == NULL)
1169 enabled = crtc;
1170 else
1171 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001172 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001173 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001174 if (planeb_wm > (long)wm_info->max_wm)
1175 planeb_wm = wm_info->max_wm;
1176 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001177
1178 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1179
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001180 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001181 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001182
Matt Roper2ff8fde2014-07-08 07:50:07 -07001183 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001184
1185 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001186 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001187 enabled = NULL;
1188 }
1189
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001190 /*
1191 * Overlay gets an aggressive default since video jitter is bad.
1192 */
1193 cwm = 2;
1194
1195 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001196 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001197
1198 /* Calc sr entries for one plane configs */
1199 if (HAS_FW_BLC(dev) && enabled) {
1200 /* self-refresh has much higher latency */
1201 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001202 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001203 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001204 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001205 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001206 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001207 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001208 unsigned long line_time_us;
1209 int entries;
1210
Ville Syrjälä922044c2014-02-14 14:18:57 +02001211 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001212
1213 /* Use ns/us then divide to preserve precision */
1214 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1215 pixel_size * hdisplay;
1216 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1217 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1218 srwm = wm_info->fifo_size - entries;
1219 if (srwm < 0)
1220 srwm = 1;
1221
1222 if (IS_I945G(dev) || IS_I945GM(dev))
1223 I915_WRITE(FW_BLC_SELF,
1224 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1225 else if (IS_I915GM(dev))
1226 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1227 }
1228
1229 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1230 planea_wm, planeb_wm, cwm, srwm);
1231
1232 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1233 fwater_hi = (cwm & 0x1f);
1234
1235 /* Set request length to 8 cachelines per fetch */
1236 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1237 fwater_hi = fwater_hi | (1 << 8);
1238
1239 I915_WRITE(FW_BLC, fwater_lo);
1240 I915_WRITE(FW_BLC2, fwater_hi);
1241
Imre Deak5209b1f2014-07-01 12:36:17 +03001242 if (enabled)
1243 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001244}
1245
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001246static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001247{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001248 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001249 struct drm_i915_private *dev_priv = dev->dev_private;
1250 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001251 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001252 uint32_t fwater_lo;
1253 int planea_wm;
1254
1255 crtc = single_enabled_crtc(dev);
1256 if (crtc == NULL)
1257 return;
1258
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001259 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001260 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001261 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001262 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001263 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001264 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1265 fwater_lo |= (3<<8) | planea_wm;
1266
1267 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1268
1269 I915_WRITE(FW_BLC, fwater_lo);
1270}
1271
Ville Syrjälä36587292013-07-05 11:57:16 +03001272static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1273 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001274{
1275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001276 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001277
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001278 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001279
1280 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1281 * adjust the pixel_rate here. */
1282
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001283 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001284 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001285 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001286
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001287 pipe_w = intel_crtc->config->pipe_src_w;
1288 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001289 pfit_w = (pfit_size >> 16) & 0xFFFF;
1290 pfit_h = pfit_size & 0xFFFF;
1291 if (pipe_w < pfit_w)
1292 pipe_w = pfit_w;
1293 if (pipe_h < pfit_h)
1294 pipe_h = pfit_h;
1295
1296 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1297 pfit_w * pfit_h);
1298 }
1299
1300 return pixel_rate;
1301}
1302
Ville Syrjälä37126462013-08-01 16:18:55 +03001303/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001304static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001305 uint32_t latency)
1306{
1307 uint64_t ret;
1308
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001309 if (WARN(latency == 0, "Latency value missing\n"))
1310 return UINT_MAX;
1311
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001312 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1313 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1314
1315 return ret;
1316}
1317
Ville Syrjälä37126462013-08-01 16:18:55 +03001318/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001319static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001320 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1321 uint32_t latency)
1322{
1323 uint32_t ret;
1324
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001325 if (WARN(latency == 0, "Latency value missing\n"))
1326 return UINT_MAX;
1327
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001328 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1329 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1330 ret = DIV_ROUND_UP(ret, 64) + 2;
1331 return ret;
1332}
1333
Ville Syrjälä23297042013-07-05 11:57:17 +03001334static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001335 uint8_t bytes_per_pixel)
1336{
1337 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1338}
1339
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001340struct skl_pipe_wm_parameters {
1341 bool active;
1342 uint32_t pipe_htotal;
1343 uint32_t pixel_rate; /* in KHz */
1344 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1345 struct intel_plane_wm_parameters cursor;
1346};
1347
Imre Deak820c1982013-12-17 14:46:36 +02001348struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001349 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001350 uint32_t pipe_htotal;
1351 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001352 struct intel_plane_wm_parameters pri;
1353 struct intel_plane_wm_parameters spr;
1354 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001355};
1356
Imre Deak820c1982013-12-17 14:46:36 +02001357struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001358 uint16_t pri;
1359 uint16_t spr;
1360 uint16_t cur;
1361 uint16_t fbc;
1362};
1363
Ville Syrjälä240264f2013-08-07 13:29:12 +03001364/* used in computing the new watermarks state */
1365struct intel_wm_config {
1366 unsigned int num_pipes_active;
1367 bool sprites_enabled;
1368 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001369};
1370
Ville Syrjälä37126462013-08-01 16:18:55 +03001371/*
1372 * For both WM_PIPE and WM_LP.
1373 * mem_value must be in 0.1us units.
1374 */
Imre Deak820c1982013-12-17 14:46:36 +02001375static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001376 uint32_t mem_value,
1377 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001378{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001379 uint32_t method1, method2;
1380
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001381 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001382 return 0;
1383
Ville Syrjälä23297042013-07-05 11:57:17 +03001384 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001385 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001386 mem_value);
1387
1388 if (!is_lp)
1389 return method1;
1390
Ville Syrjälä23297042013-07-05 11:57:17 +03001391 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001392 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001393 params->pri.horiz_pixels,
1394 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001395 mem_value);
1396
1397 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001398}
1399
Ville Syrjälä37126462013-08-01 16:18:55 +03001400/*
1401 * For both WM_PIPE and WM_LP.
1402 * mem_value must be in 0.1us units.
1403 */
Imre Deak820c1982013-12-17 14:46:36 +02001404static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001405 uint32_t mem_value)
1406{
1407 uint32_t method1, method2;
1408
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001409 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001410 return 0;
1411
Ville Syrjälä23297042013-07-05 11:57:17 +03001412 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001413 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001414 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001415 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001416 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001417 params->spr.horiz_pixels,
1418 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001419 mem_value);
1420 return min(method1, method2);
1421}
1422
Ville Syrjälä37126462013-08-01 16:18:55 +03001423/*
1424 * For both WM_PIPE and WM_LP.
1425 * mem_value must be in 0.1us units.
1426 */
Imre Deak820c1982013-12-17 14:46:36 +02001427static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001428 uint32_t mem_value)
1429{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001430 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001431 return 0;
1432
Ville Syrjälä23297042013-07-05 11:57:17 +03001433 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001434 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001435 params->cur.horiz_pixels,
1436 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001437 mem_value);
1438}
1439
Paulo Zanonicca32e92013-05-31 11:45:06 -03001440/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001441static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001442 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001443{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001444 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001445 return 0;
1446
Ville Syrjälä23297042013-07-05 11:57:17 +03001447 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001448 params->pri.horiz_pixels,
1449 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001450}
1451
Ville Syrjälä158ae642013-08-07 13:28:19 +03001452static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1453{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001454 if (INTEL_INFO(dev)->gen >= 8)
1455 return 3072;
1456 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001457 return 768;
1458 else
1459 return 512;
1460}
1461
Ville Syrjälä4e975082014-03-07 18:32:11 +02001462static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1463 int level, bool is_sprite)
1464{
1465 if (INTEL_INFO(dev)->gen >= 8)
1466 /* BDW primary/sprite plane watermarks */
1467 return level == 0 ? 255 : 2047;
1468 else if (INTEL_INFO(dev)->gen >= 7)
1469 /* IVB/HSW primary/sprite plane watermarks */
1470 return level == 0 ? 127 : 1023;
1471 else if (!is_sprite)
1472 /* ILK/SNB primary plane watermarks */
1473 return level == 0 ? 127 : 511;
1474 else
1475 /* ILK/SNB sprite plane watermarks */
1476 return level == 0 ? 63 : 255;
1477}
1478
1479static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1480 int level)
1481{
1482 if (INTEL_INFO(dev)->gen >= 7)
1483 return level == 0 ? 63 : 255;
1484 else
1485 return level == 0 ? 31 : 63;
1486}
1487
1488static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1489{
1490 if (INTEL_INFO(dev)->gen >= 8)
1491 return 31;
1492 else
1493 return 15;
1494}
1495
Ville Syrjälä158ae642013-08-07 13:28:19 +03001496/* Calculate the maximum primary/sprite plane watermark */
1497static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1498 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001499 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001500 enum intel_ddb_partitioning ddb_partitioning,
1501 bool is_sprite)
1502{
1503 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001504
1505 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001506 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001507 return 0;
1508
1509 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001510 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001511 fifo_size /= INTEL_INFO(dev)->num_pipes;
1512
1513 /*
1514 * For some reason the non self refresh
1515 * FIFO size is only half of the self
1516 * refresh FIFO size on ILK/SNB.
1517 */
1518 if (INTEL_INFO(dev)->gen <= 6)
1519 fifo_size /= 2;
1520 }
1521
Ville Syrjälä240264f2013-08-07 13:29:12 +03001522 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001523 /* level 0 is always calculated with 1:1 split */
1524 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1525 if (is_sprite)
1526 fifo_size *= 5;
1527 fifo_size /= 6;
1528 } else {
1529 fifo_size /= 2;
1530 }
1531 }
1532
1533 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001534 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001535}
1536
1537/* Calculate the maximum cursor plane watermark */
1538static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001539 int level,
1540 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001541{
1542 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001543 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001544 return 64;
1545
1546 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001547 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001548}
1549
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001550static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001551 int level,
1552 const struct intel_wm_config *config,
1553 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001554 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001555{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001556 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1557 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1558 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001559 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001560}
1561
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001562static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1563 int level,
1564 struct ilk_wm_maximums *max)
1565{
1566 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1567 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1568 max->cur = ilk_cursor_wm_reg_max(dev, level);
1569 max->fbc = ilk_fbc_wm_reg_max(dev);
1570}
1571
Ville Syrjäläd9395652013-10-09 19:18:10 +03001572static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001573 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001574 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001575{
1576 bool ret;
1577
1578 /* already determined to be invalid? */
1579 if (!result->enable)
1580 return false;
1581
1582 result->enable = result->pri_val <= max->pri &&
1583 result->spr_val <= max->spr &&
1584 result->cur_val <= max->cur;
1585
1586 ret = result->enable;
1587
1588 /*
1589 * HACK until we can pre-compute everything,
1590 * and thus fail gracefully if LP0 watermarks
1591 * are exceeded...
1592 */
1593 if (level == 0 && !result->enable) {
1594 if (result->pri_val > max->pri)
1595 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1596 level, result->pri_val, max->pri);
1597 if (result->spr_val > max->spr)
1598 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1599 level, result->spr_val, max->spr);
1600 if (result->cur_val > max->cur)
1601 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1602 level, result->cur_val, max->cur);
1603
1604 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1605 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1606 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1607 result->enable = true;
1608 }
1609
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001610 return ret;
1611}
1612
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001613static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001614 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001615 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001616 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001617{
1618 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1619 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1620 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1621
1622 /* WM1+ latency values stored in 0.5us units */
1623 if (level > 0) {
1624 pri_latency *= 5;
1625 spr_latency *= 5;
1626 cur_latency *= 5;
1627 }
1628
1629 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1630 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1631 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1632 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1633 result->enable = true;
1634}
1635
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001636static uint32_t
1637hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001638{
1639 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001641 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001642 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001643
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001644 if (!intel_crtc_active(crtc))
1645 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001646
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001647 /* The WM are computed with base on how long it takes to fill a single
1648 * row at the given clock rate, multiplied by 8.
1649 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001650 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1651 mode->crtc_clock);
1652 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001653 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001654
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001655 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1656 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001657}
1658
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001659static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001660{
1661 struct drm_i915_private *dev_priv = dev->dev_private;
1662
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001663 if (IS_GEN9(dev)) {
1664 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001665 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001666 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001667
1668 /* read the first set of memory latencies[0:3] */
1669 val = 0; /* data0 to be programmed to 0 for first set */
1670 mutex_lock(&dev_priv->rps.hw_lock);
1671 ret = sandybridge_pcode_read(dev_priv,
1672 GEN9_PCODE_READ_MEM_LATENCY,
1673 &val);
1674 mutex_unlock(&dev_priv->rps.hw_lock);
1675
1676 if (ret) {
1677 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1678 return;
1679 }
1680
1681 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1682 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1683 GEN9_MEM_LATENCY_LEVEL_MASK;
1684 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1685 GEN9_MEM_LATENCY_LEVEL_MASK;
1686 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1687 GEN9_MEM_LATENCY_LEVEL_MASK;
1688
1689 /* read the second set of memory latencies[4:7] */
1690 val = 1; /* data0 to be programmed to 1 for second set */
1691 mutex_lock(&dev_priv->rps.hw_lock);
1692 ret = sandybridge_pcode_read(dev_priv,
1693 GEN9_PCODE_READ_MEM_LATENCY,
1694 &val);
1695 mutex_unlock(&dev_priv->rps.hw_lock);
1696 if (ret) {
1697 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1698 return;
1699 }
1700
1701 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1702 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1703 GEN9_MEM_LATENCY_LEVEL_MASK;
1704 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1705 GEN9_MEM_LATENCY_LEVEL_MASK;
1706 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1707 GEN9_MEM_LATENCY_LEVEL_MASK;
1708
Vandana Kannan367294b2014-11-04 17:06:46 +00001709 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001710 * WaWmMemoryReadLatency:skl
1711 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001712 * punit doesn't take into account the read latency so we need
1713 * to add 2us to the various latency levels we retrieve from
1714 * the punit.
1715 * - W0 is a bit special in that it's the only level that
1716 * can't be disabled if we want to have display working, so
1717 * we always add 2us there.
1718 * - For levels >=1, punit returns 0us latency when they are
1719 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001720 *
1721 * Additionally, if a level n (n > 1) has a 0us latency, all
1722 * levels m (m >= n) need to be disabled. We make sure to
1723 * sanitize the values out of the punit to satisfy this
1724 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001725 */
1726 wm[0] += 2;
1727 for (level = 1; level <= max_level; level++)
1728 if (wm[level] != 0)
1729 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001730 else {
1731 for (i = level + 1; i <= max_level; i++)
1732 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001733
Vandana Kannan4f947382014-11-04 17:06:47 +00001734 break;
1735 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001736 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001737 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1738
1739 wm[0] = (sskpd >> 56) & 0xFF;
1740 if (wm[0] == 0)
1741 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001742 wm[1] = (sskpd >> 4) & 0xFF;
1743 wm[2] = (sskpd >> 12) & 0xFF;
1744 wm[3] = (sskpd >> 20) & 0x1FF;
1745 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001746 } else if (INTEL_INFO(dev)->gen >= 6) {
1747 uint32_t sskpd = I915_READ(MCH_SSKPD);
1748
1749 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1750 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1751 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1752 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001753 } else if (INTEL_INFO(dev)->gen >= 5) {
1754 uint32_t mltr = I915_READ(MLTR_ILK);
1755
1756 /* ILK primary LP0 latency is 700 ns */
1757 wm[0] = 7;
1758 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1759 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001760 }
1761}
1762
Ville Syrjälä53615a52013-08-01 16:18:50 +03001763static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1764{
1765 /* ILK sprite LP0 latency is 1300 ns */
1766 if (INTEL_INFO(dev)->gen == 5)
1767 wm[0] = 13;
1768}
1769
1770static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1771{
1772 /* ILK cursor LP0 latency is 1300 ns */
1773 if (INTEL_INFO(dev)->gen == 5)
1774 wm[0] = 13;
1775
1776 /* WaDoubleCursorLP3Latency:ivb */
1777 if (IS_IVYBRIDGE(dev))
1778 wm[3] *= 2;
1779}
1780
Damien Lespiau546c81f2014-05-13 15:30:26 +01001781int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001782{
1783 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001784 if (IS_GEN9(dev))
1785 return 7;
1786 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001787 return 4;
1788 else if (INTEL_INFO(dev)->gen >= 6)
1789 return 3;
1790 else
1791 return 2;
1792}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001793
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001794static void intel_print_wm_latency(struct drm_device *dev,
1795 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001796 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001797{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001798 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001799
1800 for (level = 0; level <= max_level; level++) {
1801 unsigned int latency = wm[level];
1802
1803 if (latency == 0) {
1804 DRM_ERROR("%s WM%d latency not provided\n",
1805 name, level);
1806 continue;
1807 }
1808
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001809 /*
1810 * - latencies are in us on gen9.
1811 * - before then, WM1+ latency values are in 0.5us units
1812 */
1813 if (IS_GEN9(dev))
1814 latency *= 10;
1815 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001816 latency *= 5;
1817
1818 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1819 name, level, wm[level],
1820 latency / 10, latency % 10);
1821 }
1822}
1823
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001824static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1825 uint16_t wm[5], uint16_t min)
1826{
1827 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1828
1829 if (wm[0] >= min)
1830 return false;
1831
1832 wm[0] = max(wm[0], min);
1833 for (level = 1; level <= max_level; level++)
1834 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1835
1836 return true;
1837}
1838
1839static void snb_wm_latency_quirk(struct drm_device *dev)
1840{
1841 struct drm_i915_private *dev_priv = dev->dev_private;
1842 bool changed;
1843
1844 /*
1845 * The BIOS provided WM memory latency values are often
1846 * inadequate for high resolution displays. Adjust them.
1847 */
1848 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1849 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1850 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1851
1852 if (!changed)
1853 return;
1854
1855 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1856 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1857 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1858 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1859}
1860
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001861static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001862{
1863 struct drm_i915_private *dev_priv = dev->dev_private;
1864
1865 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1866
1867 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1868 sizeof(dev_priv->wm.pri_latency));
1869 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1870 sizeof(dev_priv->wm.pri_latency));
1871
1872 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1873 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001874
1875 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1876 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1877 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001878
1879 if (IS_GEN6(dev))
1880 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001881}
1882
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001883static void skl_setup_wm_latency(struct drm_device *dev)
1884{
1885 struct drm_i915_private *dev_priv = dev->dev_private;
1886
1887 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1888 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1889}
1890
Imre Deak820c1982013-12-17 14:46:36 +02001891static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001892 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001893{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001894 struct drm_device *dev = crtc->dev;
1895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1896 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001897 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001898
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001899 if (!intel_crtc_active(crtc))
1900 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001901
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001902 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001903 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001904 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
1905 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
1906 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001907 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001908 p->cur.horiz_pixels = intel_crtc->cursor_width;
1909 /* TODO: for now, assume primary and cursor planes are always enabled. */
1910 p->pri.enabled = true;
1911 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001912
Matt Roperaf2b6532014-04-01 15:22:32 -07001913 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001914 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001915
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001916 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001917 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001918 break;
1919 }
1920 }
1921}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001922
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001923static void ilk_compute_wm_config(struct drm_device *dev,
1924 struct intel_wm_config *config)
1925{
1926 struct intel_crtc *intel_crtc;
1927
1928 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01001929 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001930 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
1931
1932 if (!wm->pipe_enabled)
1933 continue;
1934
1935 config->sprites_enabled |= wm->sprites_enabled;
1936 config->sprites_scaled |= wm->sprites_scaled;
1937 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001938 }
1939}
1940
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001941/* Compute new watermarks for the pipe */
1942static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02001943 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001944 struct intel_pipe_wm *pipe_wm)
1945{
1946 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001947 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001948 int level, max_level = ilk_wm_max_level(dev);
1949 /* LP0 watermark maximums depend on this pipe alone */
1950 struct intel_wm_config config = {
1951 .num_pipes_active = 1,
1952 .sprites_enabled = params->spr.enabled,
1953 .sprites_scaled = params->spr.scaled,
1954 };
Imre Deak820c1982013-12-17 14:46:36 +02001955 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001956
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001957 pipe_wm->pipe_enabled = params->active;
1958 pipe_wm->sprites_enabled = params->spr.enabled;
1959 pipe_wm->sprites_scaled = params->spr.scaled;
1960
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02001961 /* ILK/SNB: LP2+ watermarks only w/o sprites */
1962 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
1963 max_level = 1;
1964
1965 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
1966 if (params->spr.scaled)
1967 max_level = 0;
1968
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001969 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001970
Ville Syrjäläa42a5712014-01-07 16:14:08 +02001971 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02001972 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001973
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001974 /* LP0 watermarks always use 1/2 DDB partitioning */
1975 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
1976
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001977 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001978 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
1979 return false;
1980
1981 ilk_compute_wm_reg_maximums(dev, 1, &max);
1982
1983 for (level = 1; level <= max_level; level++) {
1984 struct intel_wm_level wm = {};
1985
1986 ilk_compute_wm_level(dev_priv, level, params, &wm);
1987
1988 /*
1989 * Disable any watermark level that exceeds the
1990 * register maximums since such watermarks are
1991 * always invalid.
1992 */
1993 if (!ilk_validate_wm_level(level, &max, &wm))
1994 break;
1995
1996 pipe_wm->wm[level] = wm;
1997 }
1998
1999 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002000}
2001
2002/*
2003 * Merge the watermarks from all active pipes for a specific level.
2004 */
2005static void ilk_merge_wm_level(struct drm_device *dev,
2006 int level,
2007 struct intel_wm_level *ret_wm)
2008{
2009 const struct intel_crtc *intel_crtc;
2010
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002011 ret_wm->enable = true;
2012
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002013 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002014 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2015 const struct intel_wm_level *wm = &active->wm[level];
2016
2017 if (!active->pipe_enabled)
2018 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002019
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002020 /*
2021 * The watermark values may have been used in the past,
2022 * so we must maintain them in the registers for some
2023 * time even if the level is now disabled.
2024 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002025 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002026 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002027
2028 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2029 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2030 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2031 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2032 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002033}
2034
2035/*
2036 * Merge all low power watermarks for all active pipes.
2037 */
2038static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002039 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002040 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002041 struct intel_pipe_wm *merged)
2042{
2043 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002044 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002045
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002046 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2047 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2048 config->num_pipes_active > 1)
2049 return;
2050
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002051 /* ILK: FBC WM must be disabled always */
2052 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002053
2054 /* merge each WM1+ level */
2055 for (level = 1; level <= max_level; level++) {
2056 struct intel_wm_level *wm = &merged->wm[level];
2057
2058 ilk_merge_wm_level(dev, level, wm);
2059
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002060 if (level > last_enabled_level)
2061 wm->enable = false;
2062 else if (!ilk_validate_wm_level(level, max, wm))
2063 /* make sure all following levels get disabled */
2064 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002065
2066 /*
2067 * The spec says it is preferred to disable
2068 * FBC WMs instead of disabling a WM level.
2069 */
2070 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002071 if (wm->enable)
2072 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002073 wm->fbc_val = 0;
2074 }
2075 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002076
2077 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2078 /*
2079 * FIXME this is racy. FBC might get enabled later.
2080 * What we should check here is whether FBC can be
2081 * enabled sometime later.
2082 */
2083 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2084 for (level = 2; level <= max_level; level++) {
2085 struct intel_wm_level *wm = &merged->wm[level];
2086
2087 wm->enable = false;
2088 }
2089 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002090}
2091
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002092static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2093{
2094 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2095 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2096}
2097
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002098/* The value we need to program into the WM_LPx latency field */
2099static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2100{
2101 struct drm_i915_private *dev_priv = dev->dev_private;
2102
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002103 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002104 return 2 * level;
2105 else
2106 return dev_priv->wm.pri_latency[level];
2107}
2108
Imre Deak820c1982013-12-17 14:46:36 +02002109static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002110 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002111 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002112 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002113{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002114 struct intel_crtc *intel_crtc;
2115 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002116
Ville Syrjälä0362c782013-10-09 19:17:57 +03002117 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002118 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002119
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002120 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002121 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002122 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002123
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002124 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002125
Ville Syrjälä0362c782013-10-09 19:17:57 +03002126 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002127
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002128 /*
2129 * Maintain the watermark values even if the level is
2130 * disabled. Doing otherwise could cause underruns.
2131 */
2132 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002133 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002134 (r->pri_val << WM1_LP_SR_SHIFT) |
2135 r->cur_val;
2136
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002137 if (r->enable)
2138 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2139
Ville Syrjälä416f4722013-11-02 21:07:46 -07002140 if (INTEL_INFO(dev)->gen >= 8)
2141 results->wm_lp[wm_lp - 1] |=
2142 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2143 else
2144 results->wm_lp[wm_lp - 1] |=
2145 r->fbc_val << WM1_LP_FBC_SHIFT;
2146
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002147 /*
2148 * Always set WM1S_LP_EN when spr_val != 0, even if the
2149 * level is disabled. Doing otherwise could cause underruns.
2150 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002151 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2152 WARN_ON(wm_lp != 1);
2153 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2154 } else
2155 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002156 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002157
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002158 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002159 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002160 enum pipe pipe = intel_crtc->pipe;
2161 const struct intel_wm_level *r =
2162 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002163
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002164 if (WARN_ON(!r->enable))
2165 continue;
2166
2167 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2168
2169 results->wm_pipe[pipe] =
2170 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2171 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2172 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002173 }
2174}
2175
Paulo Zanoni861f3382013-05-31 10:19:21 -03002176/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2177 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002178static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002179 struct intel_pipe_wm *r1,
2180 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002181{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002182 int level, max_level = ilk_wm_max_level(dev);
2183 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002184
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002185 for (level = 1; level <= max_level; level++) {
2186 if (r1->wm[level].enable)
2187 level1 = level;
2188 if (r2->wm[level].enable)
2189 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002190 }
2191
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002192 if (level1 == level2) {
2193 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002194 return r2;
2195 else
2196 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002197 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002198 return r1;
2199 } else {
2200 return r2;
2201 }
2202}
2203
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002204/* dirty bits used to track which watermarks need changes */
2205#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2206#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2207#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2208#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2209#define WM_DIRTY_FBC (1 << 24)
2210#define WM_DIRTY_DDB (1 << 25)
2211
Damien Lespiau055e3932014-08-18 13:49:10 +01002212static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002213 const struct ilk_wm_values *old,
2214 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002215{
2216 unsigned int dirty = 0;
2217 enum pipe pipe;
2218 int wm_lp;
2219
Damien Lespiau055e3932014-08-18 13:49:10 +01002220 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002221 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2222 dirty |= WM_DIRTY_LINETIME(pipe);
2223 /* Must disable LP1+ watermarks too */
2224 dirty |= WM_DIRTY_LP_ALL;
2225 }
2226
2227 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2228 dirty |= WM_DIRTY_PIPE(pipe);
2229 /* Must disable LP1+ watermarks too */
2230 dirty |= WM_DIRTY_LP_ALL;
2231 }
2232 }
2233
2234 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2235 dirty |= WM_DIRTY_FBC;
2236 /* Must disable LP1+ watermarks too */
2237 dirty |= WM_DIRTY_LP_ALL;
2238 }
2239
2240 if (old->partitioning != new->partitioning) {
2241 dirty |= WM_DIRTY_DDB;
2242 /* Must disable LP1+ watermarks too */
2243 dirty |= WM_DIRTY_LP_ALL;
2244 }
2245
2246 /* LP1+ watermarks already deemed dirty, no need to continue */
2247 if (dirty & WM_DIRTY_LP_ALL)
2248 return dirty;
2249
2250 /* Find the lowest numbered LP1+ watermark in need of an update... */
2251 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2252 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2253 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2254 break;
2255 }
2256
2257 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2258 for (; wm_lp <= 3; wm_lp++)
2259 dirty |= WM_DIRTY_LP(wm_lp);
2260
2261 return dirty;
2262}
2263
Ville Syrjälä8553c182013-12-05 15:51:39 +02002264static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2265 unsigned int dirty)
2266{
Imre Deak820c1982013-12-17 14:46:36 +02002267 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002268 bool changed = false;
2269
2270 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2271 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2272 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2273 changed = true;
2274 }
2275 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2276 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2277 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2278 changed = true;
2279 }
2280 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2281 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2282 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2283 changed = true;
2284 }
2285
2286 /*
2287 * Don't touch WM1S_LP_EN here.
2288 * Doing so could cause underruns.
2289 */
2290
2291 return changed;
2292}
2293
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002294/*
2295 * The spec says we shouldn't write when we don't need, because every write
2296 * causes WMs to be re-evaluated, expending some power.
2297 */
Imre Deak820c1982013-12-17 14:46:36 +02002298static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2299 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002300{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002301 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002302 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002303 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002304 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002305
Damien Lespiau055e3932014-08-18 13:49:10 +01002306 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002307 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002308 return;
2309
Ville Syrjälä8553c182013-12-05 15:51:39 +02002310 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002311
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002312 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002313 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002314 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002315 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002316 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002317 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2318
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002319 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002320 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002321 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002322 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002323 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002324 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2325
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002326 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002327 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002328 val = I915_READ(WM_MISC);
2329 if (results->partitioning == INTEL_DDB_PART_1_2)
2330 val &= ~WM_MISC_DATA_PARTITION_5_6;
2331 else
2332 val |= WM_MISC_DATA_PARTITION_5_6;
2333 I915_WRITE(WM_MISC, val);
2334 } else {
2335 val = I915_READ(DISP_ARB_CTL2);
2336 if (results->partitioning == INTEL_DDB_PART_1_2)
2337 val &= ~DISP_DATA_PARTITION_5_6;
2338 else
2339 val |= DISP_DATA_PARTITION_5_6;
2340 I915_WRITE(DISP_ARB_CTL2, val);
2341 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002342 }
2343
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002344 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002345 val = I915_READ(DISP_ARB_CTL);
2346 if (results->enable_fbc_wm)
2347 val &= ~DISP_FBC_WM_DIS;
2348 else
2349 val |= DISP_FBC_WM_DIS;
2350 I915_WRITE(DISP_ARB_CTL, val);
2351 }
2352
Imre Deak954911e2013-12-17 14:46:34 +02002353 if (dirty & WM_DIRTY_LP(1) &&
2354 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2355 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2356
2357 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002358 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2359 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2360 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2361 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2362 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002363
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002364 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002365 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002366 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002367 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002368 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002369 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002370
2371 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002372}
2373
Ville Syrjälä8553c182013-12-05 15:51:39 +02002374static bool ilk_disable_lp_wm(struct drm_device *dev)
2375{
2376 struct drm_i915_private *dev_priv = dev->dev_private;
2377
2378 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2379}
2380
Damien Lespiaub9cec072014-11-04 17:06:43 +00002381/*
2382 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2383 * different active planes.
2384 */
2385
2386#define SKL_DDB_SIZE 896 /* in blocks */
2387
2388static void
2389skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2390 struct drm_crtc *for_crtc,
2391 const struct intel_wm_config *config,
2392 const struct skl_pipe_wm_parameters *params,
2393 struct skl_ddb_entry *alloc /* out */)
2394{
2395 struct drm_crtc *crtc;
2396 unsigned int pipe_size, ddb_size;
2397 int nth_active_pipe;
2398
2399 if (!params->active) {
2400 alloc->start = 0;
2401 alloc->end = 0;
2402 return;
2403 }
2404
2405 ddb_size = SKL_DDB_SIZE;
2406
2407 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2408
2409 nth_active_pipe = 0;
2410 for_each_crtc(dev, crtc) {
2411 if (!intel_crtc_active(crtc))
2412 continue;
2413
2414 if (crtc == for_crtc)
2415 break;
2416
2417 nth_active_pipe++;
2418 }
2419
2420 pipe_size = ddb_size / config->num_pipes_active;
2421 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002422 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002423}
2424
2425static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2426{
2427 if (config->num_pipes_active == 1)
2428 return 32;
2429
2430 return 8;
2431}
2432
Damien Lespiaua269c582014-11-04 17:06:49 +00002433static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2434{
2435 entry->start = reg & 0x3ff;
2436 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002437 if (entry->end)
2438 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002439}
2440
Damien Lespiau08db6652014-11-04 17:06:52 +00002441void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2442 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002443{
2444 struct drm_device *dev = dev_priv->dev;
2445 enum pipe pipe;
2446 int plane;
2447 u32 val;
2448
2449 for_each_pipe(dev_priv, pipe) {
2450 for_each_plane(pipe, plane) {
2451 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2452 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2453 val);
2454 }
2455
2456 val = I915_READ(CUR_BUF_CFG(pipe));
2457 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2458 }
2459}
2460
Damien Lespiaub9cec072014-11-04 17:06:43 +00002461static unsigned int
2462skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2463{
2464 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2465}
2466
2467/*
2468 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2469 * a 8192x4096@32bpp framebuffer:
2470 * 3 * 4096 * 8192 * 4 < 2^32
2471 */
2472static unsigned int
2473skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2474 const struct skl_pipe_wm_parameters *params)
2475{
2476 unsigned int total_data_rate = 0;
2477 int plane;
2478
2479 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2480 const struct intel_plane_wm_parameters *p;
2481
2482 p = &params->plane[plane];
2483 if (!p->enabled)
2484 continue;
2485
2486 total_data_rate += skl_plane_relative_data_rate(p);
2487 }
2488
2489 return total_data_rate;
2490}
2491
2492static void
2493skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2494 const struct intel_wm_config *config,
2495 const struct skl_pipe_wm_parameters *params,
2496 struct skl_ddb_allocation *ddb /* out */)
2497{
2498 struct drm_device *dev = crtc->dev;
2499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2500 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002501 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002502 uint16_t alloc_size, start, cursor_blocks;
2503 unsigned int total_data_rate;
2504 int plane;
2505
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002506 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2507 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002508 if (alloc_size == 0) {
2509 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2510 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2511 return;
2512 }
2513
2514 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002515 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2516 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002517
2518 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002519 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002520
2521 /*
2522 * Each active plane get a portion of the remaining space, in
2523 * proportion to the amount of data they need to fetch from memory.
2524 *
2525 * FIXME: we may not allocate every single block here.
2526 */
2527 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2528
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002529 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002530 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2531 const struct intel_plane_wm_parameters *p;
2532 unsigned int data_rate;
2533 uint16_t plane_blocks;
2534
2535 p = &params->plane[plane];
2536 if (!p->enabled)
2537 continue;
2538
2539 data_rate = skl_plane_relative_data_rate(p);
2540
2541 /*
2542 * promote the expression to 64 bits to avoid overflowing, the
2543 * result is < available as data_rate / total_data_rate < 1
2544 */
2545 plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
2546 total_data_rate);
2547
2548 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002549 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002550
2551 start += plane_blocks;
2552 }
2553
2554}
2555
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002556static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002557{
2558 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002559 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002560}
2561
2562/*
2563 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2564 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2565 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2566 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2567*/
2568static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2569 uint32_t latency)
2570{
2571 uint32_t wm_intermediate_val, ret;
2572
2573 if (latency == 0)
2574 return UINT_MAX;
2575
2576 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
2577 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2578
2579 return ret;
2580}
2581
2582static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2583 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
2584 uint32_t latency)
2585{
2586 uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
2587
2588 if (latency == 0)
2589 return UINT_MAX;
2590
2591 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
2592 wm_intermediate_val = latency * pixel_rate;
2593 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
2594 plane_bytes_per_line;
2595
2596 return ret;
2597}
2598
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002599static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2600 const struct intel_crtc *intel_crtc)
2601{
2602 struct drm_device *dev = intel_crtc->base.dev;
2603 struct drm_i915_private *dev_priv = dev->dev_private;
2604 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2605 enum pipe pipe = intel_crtc->pipe;
2606
2607 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2608 sizeof(new_ddb->plane[pipe])))
2609 return true;
2610
2611 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2612 sizeof(new_ddb->cursor[pipe])))
2613 return true;
2614
2615 return false;
2616}
2617
2618static void skl_compute_wm_global_parameters(struct drm_device *dev,
2619 struct intel_wm_config *config)
2620{
2621 struct drm_crtc *crtc;
2622 struct drm_plane *plane;
2623
2624 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2625 config->num_pipes_active += intel_crtc_active(crtc);
2626
2627 /* FIXME: I don't think we need those two global parameters on SKL */
2628 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2629 struct intel_plane *intel_plane = to_intel_plane(plane);
2630
2631 config->sprites_enabled |= intel_plane->wm.enabled;
2632 config->sprites_scaled |= intel_plane->wm.scaled;
2633 }
2634}
2635
2636static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2637 struct skl_pipe_wm_parameters *p)
2638{
2639 struct drm_device *dev = crtc->dev;
2640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2641 enum pipe pipe = intel_crtc->pipe;
2642 struct drm_plane *plane;
2643 int i = 1; /* Index for sprite planes start */
2644
2645 p->active = intel_crtc_active(crtc);
2646 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002647 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2648 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002649
2650 /*
2651 * For now, assume primary and cursor planes are always enabled.
2652 */
2653 p->plane[0].enabled = true;
2654 p->plane[0].bytes_per_pixel =
2655 crtc->primary->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002656 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2657 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002658
2659 p->cursor.enabled = true;
2660 p->cursor.bytes_per_pixel = 4;
2661 p->cursor.horiz_pixels = intel_crtc->cursor_width ?
2662 intel_crtc->cursor_width : 64;
2663 }
2664
2665 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2666 struct intel_plane *intel_plane = to_intel_plane(plane);
2667
Sonika Jindala712f8e2014-12-09 10:59:15 +05302668 if (intel_plane->pipe == pipe &&
2669 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002670 p->plane[i++] = intel_plane->wm;
2671 }
2672}
2673
2674static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002675 struct intel_plane_wm_parameters *p_params,
2676 uint16_t ddb_allocation,
2677 uint32_t mem_value,
2678 uint16_t *out_blocks, /* out */
2679 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002680{
Damien Lespiaue6d66172014-11-04 17:06:55 +00002681 uint32_t method1, method2, plane_bytes_per_line, res_blocks, res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002682 uint32_t result_bytes;
2683
Vandana Kannan4f947382014-11-04 17:06:47 +00002684 if (mem_value == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002685 return false;
2686
2687 method1 = skl_wm_method1(p->pixel_rate,
2688 p_params->bytes_per_pixel,
2689 mem_value);
2690 method2 = skl_wm_method2(p->pixel_rate,
2691 p->pipe_htotal,
2692 p_params->horiz_pixels,
2693 p_params->bytes_per_pixel,
2694 mem_value);
2695
2696 plane_bytes_per_line = p_params->horiz_pixels *
2697 p_params->bytes_per_pixel;
2698
2699 /* For now xtile and linear */
Damien Lespiau21fca252014-11-04 17:06:54 +00002700 if (((ddb_allocation * 512) / plane_bytes_per_line) >= 1)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002701 result_bytes = min(method1, method2);
2702 else
2703 result_bytes = method1;
2704
Damien Lespiaue6d66172014-11-04 17:06:55 +00002705 res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
2706 res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
2707
2708 if (res_blocks > ddb_allocation || res_lines > 31)
2709 return false;
2710
2711 *out_blocks = res_blocks;
2712 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002713
2714 return true;
2715}
2716
2717static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2718 struct skl_ddb_allocation *ddb,
2719 struct skl_pipe_wm_parameters *p,
2720 enum pipe pipe,
2721 int level,
2722 int num_planes,
2723 struct skl_wm_level *result)
2724{
2725 uint16_t latency = dev_priv->wm.skl_latency[level];
2726 uint16_t ddb_blocks;
2727 int i;
2728
2729 for (i = 0; i < num_planes; i++) {
2730 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2731
2732 result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
2733 ddb_blocks,
2734 latency,
2735 &result->plane_res_b[i],
2736 &result->plane_res_l[i]);
2737 }
2738
2739 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
2740 result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
2741 latency, &result->cursor_res_b,
2742 &result->cursor_res_l);
2743}
2744
Damien Lespiau407b50f2014-11-04 17:06:57 +00002745static uint32_t
2746skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2747{
2748 if (!intel_crtc_active(crtc))
2749 return 0;
2750
2751 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2752
2753}
2754
2755static void skl_compute_transition_wm(struct drm_crtc *crtc,
2756 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002757 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002758{
Damien Lespiau9414f562014-11-04 17:06:58 +00002759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2760 int i;
2761
Damien Lespiau407b50f2014-11-04 17:06:57 +00002762 if (!params->active)
2763 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002764
2765 /* Until we know more, just disable transition WMs */
2766 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2767 trans_wm->plane_en[i] = false;
2768 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002769}
2770
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002771static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2772 struct skl_ddb_allocation *ddb,
2773 struct skl_pipe_wm_parameters *params,
2774 struct skl_pipe_wm *pipe_wm)
2775{
2776 struct drm_device *dev = crtc->dev;
2777 const struct drm_i915_private *dev_priv = dev->dev_private;
2778 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2779 int level, max_level = ilk_wm_max_level(dev);
2780
2781 for (level = 0; level <= max_level; level++) {
2782 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2783 level, intel_num_planes(intel_crtc),
2784 &pipe_wm->wm[level]);
2785 }
2786 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2787
Damien Lespiau9414f562014-11-04 17:06:58 +00002788 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002789}
2790
2791static void skl_compute_wm_results(struct drm_device *dev,
2792 struct skl_pipe_wm_parameters *p,
2793 struct skl_pipe_wm *p_wm,
2794 struct skl_wm_values *r,
2795 struct intel_crtc *intel_crtc)
2796{
2797 int level, max_level = ilk_wm_max_level(dev);
2798 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002799 uint32_t temp;
2800 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002801
2802 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002803 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2804 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002805
2806 temp |= p_wm->wm[level].plane_res_l[i] <<
2807 PLANE_WM_LINES_SHIFT;
2808 temp |= p_wm->wm[level].plane_res_b[i];
2809 if (p_wm->wm[level].plane_en[i])
2810 temp |= PLANE_WM_EN;
2811
2812 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002813 }
2814
2815 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002816
2817 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2818 temp |= p_wm->wm[level].cursor_res_b;
2819
2820 if (p_wm->wm[level].cursor_en)
2821 temp |= PLANE_WM_EN;
2822
2823 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002824
2825 }
2826
Damien Lespiau9414f562014-11-04 17:06:58 +00002827 /* transition WMs */
2828 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2829 temp = 0;
2830 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2831 temp |= p_wm->trans_wm.plane_res_b[i];
2832 if (p_wm->trans_wm.plane_en[i])
2833 temp |= PLANE_WM_EN;
2834
2835 r->plane_trans[pipe][i] = temp;
2836 }
2837
2838 temp = 0;
2839 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2840 temp |= p_wm->trans_wm.cursor_res_b;
2841 if (p_wm->trans_wm.cursor_en)
2842 temp |= PLANE_WM_EN;
2843
2844 r->cursor_trans[pipe] = temp;
2845
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002846 r->wm_linetime[pipe] = p_wm->linetime;
2847}
2848
Damien Lespiau16160e32014-11-04 17:06:53 +00002849static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2850 const struct skl_ddb_entry *entry)
2851{
2852 if (entry->end)
2853 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2854 else
2855 I915_WRITE(reg, 0);
2856}
2857
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002858static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2859 const struct skl_wm_values *new)
2860{
2861 struct drm_device *dev = dev_priv->dev;
2862 struct intel_crtc *crtc;
2863
2864 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
2865 int i, level, max_level = ilk_wm_max_level(dev);
2866 enum pipe pipe = crtc->pipe;
2867
Damien Lespiau5d374d92014-11-04 17:07:00 +00002868 if (!new->dirty[pipe])
2869 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002870
Damien Lespiau5d374d92014-11-04 17:07:00 +00002871 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
2872
2873 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002874 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00002875 I915_WRITE(PLANE_WM(pipe, i, level),
2876 new->plane[pipe][i][level]);
2877 I915_WRITE(CUR_WM(pipe, level),
2878 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002879 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00002880 for (i = 0; i < intel_num_planes(crtc); i++)
2881 I915_WRITE(PLANE_WM_TRANS(pipe, i),
2882 new->plane_trans[pipe][i]);
2883 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
2884
2885 for (i = 0; i < intel_num_planes(crtc); i++)
2886 skl_ddb_entry_write(dev_priv,
2887 PLANE_BUF_CFG(pipe, i),
2888 &new->ddb.plane[pipe][i]);
2889
2890 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
2891 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002892 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002893}
2894
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002895/*
2896 * When setting up a new DDB allocation arrangement, we need to correctly
2897 * sequence the times at which the new allocations for the pipes are taken into
2898 * account or we'll have pipes fetching from space previously allocated to
2899 * another pipe.
2900 *
2901 * Roughly the sequence looks like:
2902 * 1. re-allocate the pipe(s) with the allocation being reduced and not
2903 * overlapping with a previous light-up pipe (another way to put it is:
2904 * pipes with their new allocation strickly included into their old ones).
2905 * 2. re-allocate the other pipes that get their allocation reduced
2906 * 3. allocate the pipes having their allocation increased
2907 *
2908 * Steps 1. and 2. are here to take care of the following case:
2909 * - Initially DDB looks like this:
2910 * | B | C |
2911 * - enable pipe A.
2912 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
2913 * allocation
2914 * | A | B | C |
2915 *
2916 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
2917 */
2918
Damien Lespiaud21b7952014-11-04 17:07:03 +00002919static void
2920skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002921{
2922 struct drm_device *dev = dev_priv->dev;
2923 int plane;
2924
Damien Lespiaud21b7952014-11-04 17:07:03 +00002925 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
2926
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002927 for_each_plane(pipe, plane) {
2928 I915_WRITE(PLANE_SURF(pipe, plane),
2929 I915_READ(PLANE_SURF(pipe, plane)));
2930 }
2931 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
2932}
2933
2934static bool
2935skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
2936 const struct skl_ddb_allocation *new,
2937 enum pipe pipe)
2938{
2939 uint16_t old_size, new_size;
2940
2941 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
2942 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
2943
2944 return old_size != new_size &&
2945 new->pipe[pipe].start >= old->pipe[pipe].start &&
2946 new->pipe[pipe].end <= old->pipe[pipe].end;
2947}
2948
2949static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
2950 struct skl_wm_values *new_values)
2951{
2952 struct drm_device *dev = dev_priv->dev;
2953 struct skl_ddb_allocation *cur_ddb, *new_ddb;
2954 bool reallocated[I915_MAX_PIPES] = {false, false, false};
2955 struct intel_crtc *crtc;
2956 enum pipe pipe;
2957
2958 new_ddb = &new_values->ddb;
2959 cur_ddb = &dev_priv->wm.skl_hw.ddb;
2960
2961 /*
2962 * First pass: flush the pipes with the new allocation contained into
2963 * the old space.
2964 *
2965 * We'll wait for the vblank on those pipes to ensure we can safely
2966 * re-allocate the freed space without this pipe fetching from it.
2967 */
2968 for_each_intel_crtc(dev, crtc) {
2969 if (!crtc->active)
2970 continue;
2971
2972 pipe = crtc->pipe;
2973
2974 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
2975 continue;
2976
Damien Lespiaud21b7952014-11-04 17:07:03 +00002977 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002978 intel_wait_for_vblank(dev, pipe);
2979
2980 reallocated[pipe] = true;
2981 }
2982
2983
2984 /*
2985 * Second pass: flush the pipes that are having their allocation
2986 * reduced, but overlapping with a previous allocation.
2987 *
2988 * Here as well we need to wait for the vblank to make sure the freed
2989 * space is not used anymore.
2990 */
2991 for_each_intel_crtc(dev, crtc) {
2992 if (!crtc->active)
2993 continue;
2994
2995 pipe = crtc->pipe;
2996
2997 if (reallocated[pipe])
2998 continue;
2999
3000 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3001 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00003002 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003003 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303004 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003005 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003006 }
3007
3008 /*
3009 * Third pass: flush the pipes that got more space allocated.
3010 *
3011 * We don't need to actively wait for the update here, next vblank
3012 * will just get more DDB space with the correct WM values.
3013 */
3014 for_each_intel_crtc(dev, crtc) {
3015 if (!crtc->active)
3016 continue;
3017
3018 pipe = crtc->pipe;
3019
3020 /*
3021 * At this point, only the pipes more space than before are
3022 * left to re-allocate.
3023 */
3024 if (reallocated[pipe])
3025 continue;
3026
Damien Lespiaud21b7952014-11-04 17:07:03 +00003027 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003028 }
3029}
3030
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003031static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3032 struct skl_pipe_wm_parameters *params,
3033 struct intel_wm_config *config,
3034 struct skl_ddb_allocation *ddb, /* out */
3035 struct skl_pipe_wm *pipe_wm /* out */)
3036{
3037 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3038
3039 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003040 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003041 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3042
3043 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3044 return false;
3045
3046 intel_crtc->wm.skl_active = *pipe_wm;
3047 return true;
3048}
3049
3050static void skl_update_other_pipe_wm(struct drm_device *dev,
3051 struct drm_crtc *crtc,
3052 struct intel_wm_config *config,
3053 struct skl_wm_values *r)
3054{
3055 struct intel_crtc *intel_crtc;
3056 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3057
3058 /*
3059 * If the WM update hasn't changed the allocation for this_crtc (the
3060 * crtc we are currently computing the new WM values for), other
3061 * enabled crtcs will keep the same allocation and we don't need to
3062 * recompute anything for them.
3063 */
3064 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3065 return;
3066
3067 /*
3068 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3069 * other active pipes need new DDB allocation and WM values.
3070 */
3071 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3072 base.head) {
3073 struct skl_pipe_wm_parameters params = {};
3074 struct skl_pipe_wm pipe_wm = {};
3075 bool wm_changed;
3076
3077 if (this_crtc->pipe == intel_crtc->pipe)
3078 continue;
3079
3080 if (!intel_crtc->active)
3081 continue;
3082
3083 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3084 &params, config,
3085 &r->ddb, &pipe_wm);
3086
3087 /*
3088 * If we end up re-computing the other pipe WM values, it's
3089 * because it was really needed, so we expect the WM values to
3090 * be different.
3091 */
3092 WARN_ON(!wm_changed);
3093
3094 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3095 r->dirty[intel_crtc->pipe] = true;
3096 }
3097}
3098
3099static void skl_update_wm(struct drm_crtc *crtc)
3100{
3101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3102 struct drm_device *dev = crtc->dev;
3103 struct drm_i915_private *dev_priv = dev->dev_private;
3104 struct skl_pipe_wm_parameters params = {};
3105 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3106 struct skl_pipe_wm pipe_wm = {};
3107 struct intel_wm_config config = {};
3108
3109 memset(results, 0, sizeof(*results));
3110
3111 skl_compute_wm_global_parameters(dev, &config);
3112
3113 if (!skl_update_pipe_wm(crtc, &params, &config,
3114 &results->ddb, &pipe_wm))
3115 return;
3116
3117 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3118 results->dirty[intel_crtc->pipe] = true;
3119
3120 skl_update_other_pipe_wm(dev, crtc, &config, results);
3121 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003122 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003123
3124 /* store the new configuration */
3125 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003126}
3127
3128static void
3129skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3130 uint32_t sprite_width, uint32_t sprite_height,
3131 int pixel_size, bool enabled, bool scaled)
3132{
3133 struct intel_plane *intel_plane = to_intel_plane(plane);
3134
3135 intel_plane->wm.enabled = enabled;
3136 intel_plane->wm.scaled = scaled;
3137 intel_plane->wm.horiz_pixels = sprite_width;
3138 intel_plane->wm.vert_pixels = sprite_height;
3139 intel_plane->wm.bytes_per_pixel = pixel_size;
3140
3141 skl_update_wm(crtc);
3142}
3143
Imre Deak820c1982013-12-17 14:46:36 +02003144static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003145{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003147 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003148 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003149 struct ilk_wm_maximums max;
3150 struct ilk_pipe_wm_parameters params = {};
3151 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003152 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003153 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003154 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003155 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003156
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003157 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003158
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003159 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3160
3161 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3162 return;
3163
3164 intel_crtc->wm.active = pipe_wm;
3165
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003166 ilk_compute_wm_config(dev, &config);
3167
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003168 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003169 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003170
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003171 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003172 if (INTEL_INFO(dev)->gen >= 7 &&
3173 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003174 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003175 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003176
Imre Deak820c1982013-12-17 14:46:36 +02003177 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003178 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003179 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003180 }
3181
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003182 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003183 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003184
Imre Deak820c1982013-12-17 14:46:36 +02003185 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003186
Imre Deak820c1982013-12-17 14:46:36 +02003187 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003188}
3189
Damien Lespiaued57cb82014-07-15 09:21:24 +02003190static void
3191ilk_update_sprite_wm(struct drm_plane *plane,
3192 struct drm_crtc *crtc,
3193 uint32_t sprite_width, uint32_t sprite_height,
3194 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003195{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003196 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003197 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003198
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003199 intel_plane->wm.enabled = enabled;
3200 intel_plane->wm.scaled = scaled;
3201 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003202 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003203 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003204
Ville Syrjälä8553c182013-12-05 15:51:39 +02003205 /*
3206 * IVB workaround: must disable low power watermarks for at least
3207 * one frame before enabling scaling. LP watermarks can be re-enabled
3208 * when scaling is disabled.
3209 *
3210 * WaCxSRDisabledForSpriteScaling:ivb
3211 */
3212 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3213 intel_wait_for_vblank(dev, intel_plane->pipe);
3214
Imre Deak820c1982013-12-17 14:46:36 +02003215 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003216}
3217
Pradeep Bhat30789992014-11-04 17:06:45 +00003218static void skl_pipe_wm_active_state(uint32_t val,
3219 struct skl_pipe_wm *active,
3220 bool is_transwm,
3221 bool is_cursor,
3222 int i,
3223 int level)
3224{
3225 bool is_enabled = (val & PLANE_WM_EN) != 0;
3226
3227 if (!is_transwm) {
3228 if (!is_cursor) {
3229 active->wm[level].plane_en[i] = is_enabled;
3230 active->wm[level].plane_res_b[i] =
3231 val & PLANE_WM_BLOCKS_MASK;
3232 active->wm[level].plane_res_l[i] =
3233 (val >> PLANE_WM_LINES_SHIFT) &
3234 PLANE_WM_LINES_MASK;
3235 } else {
3236 active->wm[level].cursor_en = is_enabled;
3237 active->wm[level].cursor_res_b =
3238 val & PLANE_WM_BLOCKS_MASK;
3239 active->wm[level].cursor_res_l =
3240 (val >> PLANE_WM_LINES_SHIFT) &
3241 PLANE_WM_LINES_MASK;
3242 }
3243 } else {
3244 if (!is_cursor) {
3245 active->trans_wm.plane_en[i] = is_enabled;
3246 active->trans_wm.plane_res_b[i] =
3247 val & PLANE_WM_BLOCKS_MASK;
3248 active->trans_wm.plane_res_l[i] =
3249 (val >> PLANE_WM_LINES_SHIFT) &
3250 PLANE_WM_LINES_MASK;
3251 } else {
3252 active->trans_wm.cursor_en = is_enabled;
3253 active->trans_wm.cursor_res_b =
3254 val & PLANE_WM_BLOCKS_MASK;
3255 active->trans_wm.cursor_res_l =
3256 (val >> PLANE_WM_LINES_SHIFT) &
3257 PLANE_WM_LINES_MASK;
3258 }
3259 }
3260}
3261
3262static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3263{
3264 struct drm_device *dev = crtc->dev;
3265 struct drm_i915_private *dev_priv = dev->dev_private;
3266 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3268 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3269 enum pipe pipe = intel_crtc->pipe;
3270 int level, i, max_level;
3271 uint32_t temp;
3272
3273 max_level = ilk_wm_max_level(dev);
3274
3275 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3276
3277 for (level = 0; level <= max_level; level++) {
3278 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3279 hw->plane[pipe][i][level] =
3280 I915_READ(PLANE_WM(pipe, i, level));
3281 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3282 }
3283
3284 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3285 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3286 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3287
3288 if (!intel_crtc_active(crtc))
3289 return;
3290
3291 hw->dirty[pipe] = true;
3292
3293 active->linetime = hw->wm_linetime[pipe];
3294
3295 for (level = 0; level <= max_level; level++) {
3296 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3297 temp = hw->plane[pipe][i][level];
3298 skl_pipe_wm_active_state(temp, active, false,
3299 false, i, level);
3300 }
3301 temp = hw->cursor[pipe][level];
3302 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3303 }
3304
3305 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3306 temp = hw->plane_trans[pipe][i];
3307 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3308 }
3309
3310 temp = hw->cursor_trans[pipe];
3311 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3312}
3313
3314void skl_wm_get_hw_state(struct drm_device *dev)
3315{
Damien Lespiaua269c582014-11-04 17:06:49 +00003316 struct drm_i915_private *dev_priv = dev->dev_private;
3317 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003318 struct drm_crtc *crtc;
3319
Damien Lespiaua269c582014-11-04 17:06:49 +00003320 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003321 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3322 skl_pipe_wm_get_hw_state(crtc);
3323}
3324
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003325static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3326{
3327 struct drm_device *dev = crtc->dev;
3328 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003329 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3331 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3332 enum pipe pipe = intel_crtc->pipe;
3333 static const unsigned int wm0_pipe_reg[] = {
3334 [PIPE_A] = WM0_PIPEA_ILK,
3335 [PIPE_B] = WM0_PIPEB_ILK,
3336 [PIPE_C] = WM0_PIPEC_IVB,
3337 };
3338
3339 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003340 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003341 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003342
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003343 active->pipe_enabled = intel_crtc_active(crtc);
3344
3345 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003346 u32 tmp = hw->wm_pipe[pipe];
3347
3348 /*
3349 * For active pipes LP0 watermark is marked as
3350 * enabled, and LP1+ watermaks as disabled since
3351 * we can't really reverse compute them in case
3352 * multiple pipes are active.
3353 */
3354 active->wm[0].enable = true;
3355 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3356 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3357 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3358 active->linetime = hw->wm_linetime[pipe];
3359 } else {
3360 int level, max_level = ilk_wm_max_level(dev);
3361
3362 /*
3363 * For inactive pipes, all watermark levels
3364 * should be marked as enabled but zeroed,
3365 * which is what we'd compute them to.
3366 */
3367 for (level = 0; level <= max_level; level++)
3368 active->wm[level].enable = true;
3369 }
3370}
3371
3372void ilk_wm_get_hw_state(struct drm_device *dev)
3373{
3374 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003375 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003376 struct drm_crtc *crtc;
3377
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003378 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003379 ilk_pipe_wm_get_hw_state(crtc);
3380
3381 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3382 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3383 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3384
3385 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003386 if (INTEL_INFO(dev)->gen >= 7) {
3387 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3388 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3389 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003390
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003391 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003392 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3393 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3394 else if (IS_IVYBRIDGE(dev))
3395 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3396 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003397
3398 hw->enable_fbc_wm =
3399 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3400}
3401
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003402/**
3403 * intel_update_watermarks - update FIFO watermark values based on current modes
3404 *
3405 * Calculate watermark values for the various WM regs based on current mode
3406 * and plane configuration.
3407 *
3408 * There are several cases to deal with here:
3409 * - normal (i.e. non-self-refresh)
3410 * - self-refresh (SR) mode
3411 * - lines are large relative to FIFO size (buffer can hold up to 2)
3412 * - lines are small relative to FIFO size (buffer can hold more than 2
3413 * lines), so need to account for TLB latency
3414 *
3415 * The normal calculation is:
3416 * watermark = dotclock * bytes per pixel * latency
3417 * where latency is platform & configuration dependent (we assume pessimal
3418 * values here).
3419 *
3420 * The SR calculation is:
3421 * watermark = (trunc(latency/line time)+1) * surface width *
3422 * bytes per pixel
3423 * where
3424 * line time = htotal / dotclock
3425 * surface width = hdisplay for normal plane and 64 for cursor
3426 * and latency is assumed to be high, as above.
3427 *
3428 * The final value programmed to the register should always be rounded up,
3429 * and include an extra 2 entries to account for clock crossings.
3430 *
3431 * We don't use the sprite, so we can ignore that. And on Crestline we have
3432 * to set the non-SR watermarks to 8.
3433 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003434void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003435{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003436 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003437
3438 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003439 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003440}
3441
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003442void intel_update_sprite_watermarks(struct drm_plane *plane,
3443 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003444 uint32_t sprite_width,
3445 uint32_t sprite_height,
3446 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003447 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003448{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003449 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003450
3451 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003452 dev_priv->display.update_sprite_wm(plane, crtc,
3453 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003454 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003455}
3456
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003457static struct drm_i915_gem_object *
3458intel_alloc_context_page(struct drm_device *dev)
3459{
3460 struct drm_i915_gem_object *ctx;
3461 int ret;
3462
3463 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3464
3465 ctx = i915_gem_alloc_object(dev, 4096);
3466 if (!ctx) {
3467 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3468 return NULL;
3469 }
3470
Daniel Vetterc69766f2014-02-14 14:01:17 +01003471 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003472 if (ret) {
3473 DRM_ERROR("failed to pin power context: %d\n", ret);
3474 goto err_unref;
3475 }
3476
3477 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3478 if (ret) {
3479 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3480 goto err_unpin;
3481 }
3482
3483 return ctx;
3484
3485err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003486 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003487err_unref:
3488 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003489 return NULL;
3490}
3491
Daniel Vetter92703882012-08-09 16:46:01 +02003492/**
3493 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003494 */
3495DEFINE_SPINLOCK(mchdev_lock);
3496
3497/* Global for IPS driver to get at the current i915 device. Protected by
3498 * mchdev_lock. */
3499static struct drm_i915_private *i915_mch_dev;
3500
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003501bool ironlake_set_drps(struct drm_device *dev, u8 val)
3502{
3503 struct drm_i915_private *dev_priv = dev->dev_private;
3504 u16 rgvswctl;
3505
Daniel Vetter92703882012-08-09 16:46:01 +02003506 assert_spin_locked(&mchdev_lock);
3507
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003508 rgvswctl = I915_READ16(MEMSWCTL);
3509 if (rgvswctl & MEMCTL_CMD_STS) {
3510 DRM_DEBUG("gpu busy, RCS change rejected\n");
3511 return false; /* still busy with another command */
3512 }
3513
3514 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3515 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3516 I915_WRITE16(MEMSWCTL, rgvswctl);
3517 POSTING_READ16(MEMSWCTL);
3518
3519 rgvswctl |= MEMCTL_CMD_STS;
3520 I915_WRITE16(MEMSWCTL, rgvswctl);
3521
3522 return true;
3523}
3524
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003525static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003526{
3527 struct drm_i915_private *dev_priv = dev->dev_private;
3528 u32 rgvmodectl = I915_READ(MEMMODECTL);
3529 u8 fmax, fmin, fstart, vstart;
3530
Daniel Vetter92703882012-08-09 16:46:01 +02003531 spin_lock_irq(&mchdev_lock);
3532
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003533 /* Enable temp reporting */
3534 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3535 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3536
3537 /* 100ms RC evaluation intervals */
3538 I915_WRITE(RCUPEI, 100000);
3539 I915_WRITE(RCDNEI, 100000);
3540
3541 /* Set max/min thresholds to 90ms and 80ms respectively */
3542 I915_WRITE(RCBMAXAVG, 90000);
3543 I915_WRITE(RCBMINAVG, 80000);
3544
3545 I915_WRITE(MEMIHYST, 1);
3546
3547 /* Set up min, max, and cur for interrupt handling */
3548 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3549 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3550 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3551 MEMMODE_FSTART_SHIFT;
3552
3553 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3554 PXVFREQ_PX_SHIFT;
3555
Daniel Vetter20e4d402012-08-08 23:35:39 +02003556 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3557 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003558
Daniel Vetter20e4d402012-08-08 23:35:39 +02003559 dev_priv->ips.max_delay = fstart;
3560 dev_priv->ips.min_delay = fmin;
3561 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003562
3563 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3564 fmax, fmin, fstart);
3565
3566 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3567
3568 /*
3569 * Interrupts will be enabled in ironlake_irq_postinstall
3570 */
3571
3572 I915_WRITE(VIDSTART, vstart);
3573 POSTING_READ(VIDSTART);
3574
3575 rgvmodectl |= MEMMODE_SWMODE_EN;
3576 I915_WRITE(MEMMODECTL, rgvmodectl);
3577
Daniel Vetter92703882012-08-09 16:46:01 +02003578 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003579 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003580 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003581
3582 ironlake_set_drps(dev, fstart);
3583
Daniel Vetter20e4d402012-08-08 23:35:39 +02003584 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003585 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003586 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3587 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003588 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003589
3590 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003591}
3592
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003593static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003594{
3595 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003596 u16 rgvswctl;
3597
3598 spin_lock_irq(&mchdev_lock);
3599
3600 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003601
3602 /* Ack interrupts, disable EFC interrupt */
3603 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3604 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3605 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3606 I915_WRITE(DEIIR, DE_PCU_EVENT);
3607 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3608
3609 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003610 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003611 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003612 rgvswctl |= MEMCTL_CMD_STS;
3613 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003614 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003615
Daniel Vetter92703882012-08-09 16:46:01 +02003616 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003617}
3618
Daniel Vetteracbe9472012-07-26 11:50:05 +02003619/* There's a funny hw issue where the hw returns all 0 when reading from
3620 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3621 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3622 * all limits and the gpu stuck at whatever frequency it is at atm).
3623 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003624static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003625{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003626 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003627
Daniel Vetter20b46e52012-07-26 11:16:14 +02003628 /* Only set the down limit when we've reached the lowest level to avoid
3629 * getting more interrupts, otherwise leave this clear. This prevents a
3630 * race in the hw when coming out of rc6: There's a tiny window where
3631 * the hw runs at the minimal clock before selecting the desired
3632 * frequency, if the down threshold expires in that window we will not
3633 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003634 limits = dev_priv->rps.max_freq_softlimit << 24;
3635 if (val <= dev_priv->rps.min_freq_softlimit)
3636 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003637
3638 return limits;
3639}
3640
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003641static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3642{
3643 int new_power;
3644
3645 new_power = dev_priv->rps.power;
3646 switch (dev_priv->rps.power) {
3647 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003648 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003649 new_power = BETWEEN;
3650 break;
3651
3652 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003653 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003654 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003655 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003656 new_power = HIGH_POWER;
3657 break;
3658
3659 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003660 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 +01003661 new_power = BETWEEN;
3662 break;
3663 }
3664 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003665 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003666 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003667 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003668 new_power = HIGH_POWER;
3669 if (new_power == dev_priv->rps.power)
3670 return;
3671
3672 /* Note the units here are not exactly 1us, but 1280ns. */
3673 switch (new_power) {
3674 case LOW_POWER:
3675 /* Upclock if more than 95% busy over 16ms */
3676 I915_WRITE(GEN6_RP_UP_EI, 12500);
3677 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3678
3679 /* Downclock if less than 85% busy over 32ms */
3680 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3681 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3682
3683 I915_WRITE(GEN6_RP_CONTROL,
3684 GEN6_RP_MEDIA_TURBO |
3685 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3686 GEN6_RP_MEDIA_IS_GFX |
3687 GEN6_RP_ENABLE |
3688 GEN6_RP_UP_BUSY_AVG |
3689 GEN6_RP_DOWN_IDLE_AVG);
3690 break;
3691
3692 case BETWEEN:
3693 /* Upclock if more than 90% busy over 13ms */
3694 I915_WRITE(GEN6_RP_UP_EI, 10250);
3695 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3696
3697 /* Downclock if less than 75% busy over 32ms */
3698 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3699 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3700
3701 I915_WRITE(GEN6_RP_CONTROL,
3702 GEN6_RP_MEDIA_TURBO |
3703 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3704 GEN6_RP_MEDIA_IS_GFX |
3705 GEN6_RP_ENABLE |
3706 GEN6_RP_UP_BUSY_AVG |
3707 GEN6_RP_DOWN_IDLE_AVG);
3708 break;
3709
3710 case HIGH_POWER:
3711 /* Upclock if more than 85% busy over 10ms */
3712 I915_WRITE(GEN6_RP_UP_EI, 8000);
3713 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3714
3715 /* Downclock if less than 60% busy over 32ms */
3716 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3717 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3718
3719 I915_WRITE(GEN6_RP_CONTROL,
3720 GEN6_RP_MEDIA_TURBO |
3721 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3722 GEN6_RP_MEDIA_IS_GFX |
3723 GEN6_RP_ENABLE |
3724 GEN6_RP_UP_BUSY_AVG |
3725 GEN6_RP_DOWN_IDLE_AVG);
3726 break;
3727 }
3728
3729 dev_priv->rps.power = new_power;
3730 dev_priv->rps.last_adj = 0;
3731}
3732
Chris Wilson2876ce72014-03-28 08:03:34 +00003733static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3734{
3735 u32 mask = 0;
3736
3737 if (val > dev_priv->rps.min_freq_softlimit)
3738 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3739 if (val < dev_priv->rps.max_freq_softlimit)
3740 mask |= GEN6_PM_RP_UP_THRESHOLD;
3741
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003742 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3743 mask &= dev_priv->pm_rps_events;
3744
Imre Deak59d02a12014-12-19 19:33:26 +02003745 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003746}
3747
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003748/* gen6_set_rps is called to update the frequency request, but should also be
3749 * called when the range (min_delay and max_delay) is modified so that we can
3750 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003751static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003752{
3753 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003754
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003755 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003756 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3757 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003758
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003759 /* min/max delay may still have been modified so be sure to
3760 * write the limits value.
3761 */
3762 if (val != dev_priv->rps.cur_freq) {
3763 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003764
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003765 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003766 I915_WRITE(GEN6_RPNSWREQ,
3767 HSW_FREQUENCY(val));
3768 else
3769 I915_WRITE(GEN6_RPNSWREQ,
3770 GEN6_FREQUENCY(val) |
3771 GEN6_OFFSET(0) |
3772 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003773 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003774
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003775 /* Make sure we continue to get interrupts
3776 * until we hit the minimum or maximum frequencies.
3777 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003778 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003779 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003780
Ben Widawskyd5570a72012-09-07 19:43:41 -07003781 POSTING_READ(GEN6_RPNSWREQ);
3782
Ben Widawskyb39fb292014-03-19 18:31:11 -07003783 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003784 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003785}
3786
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003787static void valleyview_set_rps(struct drm_device *dev, u8 val)
3788{
3789 struct drm_i915_private *dev_priv = dev->dev_private;
3790
3791 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3792 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3793 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3794
3795 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3796 "Odd GPU freq value\n"))
3797 val &= ~1;
3798
3799 if (val != dev_priv->rps.cur_freq)
3800 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3801
3802 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3803
3804 dev_priv->rps.cur_freq = val;
3805 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3806}
3807
Deepak S76c3552f2014-01-30 23:08:16 +05303808/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3809 *
3810 * * If Gfx is Idle, then
3811 * 1. Mask Turbo interrupts
3812 * 2. Bring up Gfx clock
3813 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3814 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3815 * 5. Unmask Turbo interrupts
3816*/
3817static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3818{
Deepak S5549d252014-06-28 11:26:11 +05303819 struct drm_device *dev = dev_priv->dev;
3820
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003821 /* CHV and latest VLV don't need to force the gfx clock */
3822 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303823 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3824 return;
3825 }
3826
Deepak S76c3552f2014-01-30 23:08:16 +05303827 /*
3828 * When we are idle. Drop to min voltage state.
3829 */
3830
Ben Widawskyb39fb292014-03-19 18:31:11 -07003831 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303832 return;
3833
3834 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003835 I915_WRITE(GEN6_PMINTRMSK,
3836 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303837
Imre Deak650ad972014-04-18 16:35:02 +03003838 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303839
Ben Widawskyb39fb292014-03-19 18:31:11 -07003840 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303841
3842 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003843 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303844
3845 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003846 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303847 DRM_ERROR("timed out waiting for Punit\n");
3848
Imre Deak650ad972014-04-18 16:35:02 +03003849 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303850
Chris Wilson2876ce72014-03-28 08:03:34 +00003851 I915_WRITE(GEN6_PMINTRMSK,
3852 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303853}
3854
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003855void gen6_rps_idle(struct drm_i915_private *dev_priv)
3856{
Damien Lespiau691bb712013-12-12 14:36:36 +00003857 struct drm_device *dev = dev_priv->dev;
3858
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003859 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003860 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003861 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303862 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003863 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003864 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003865 dev_priv->rps.last_adj = 0;
3866 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003867 mutex_unlock(&dev_priv->rps.hw_lock);
3868}
3869
3870void gen6_rps_boost(struct drm_i915_private *dev_priv)
3871{
3872 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003873 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003874 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003875 dev_priv->rps.last_adj = 0;
3876 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003877 mutex_unlock(&dev_priv->rps.hw_lock);
3878}
3879
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003880void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003881{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003882 if (IS_VALLEYVIEW(dev))
3883 valleyview_set_rps(dev, val);
3884 else
3885 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003886}
3887
Zhe Wang20e49362014-11-04 17:07:05 +00003888static void gen9_disable_rps(struct drm_device *dev)
3889{
3890 struct drm_i915_private *dev_priv = dev->dev_private;
3891
3892 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00003893 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00003894}
3895
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003896static void gen6_disable_rps(struct drm_device *dev)
3897{
3898 struct drm_i915_private *dev_priv = dev->dev_private;
3899
3900 I915_WRITE(GEN6_RC_CONTROL, 0);
3901 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003902}
3903
Deepak S38807742014-05-23 21:00:15 +05303904static void cherryview_disable_rps(struct drm_device *dev)
3905{
3906 struct drm_i915_private *dev_priv = dev->dev_private;
3907
3908 I915_WRITE(GEN6_RC_CONTROL, 0);
3909}
3910
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003911static void valleyview_disable_rps(struct drm_device *dev)
3912{
3913 struct drm_i915_private *dev_priv = dev->dev_private;
3914
Deepak S98a2e5f2014-08-18 10:35:27 -07003915 /* we're doing forcewake before Disabling RC6,
3916 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02003917 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07003918
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003919 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003920
Mika Kuoppala59bad942015-01-16 11:34:40 +02003921 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003922}
3923
Ben Widawskydc39fff2013-10-18 12:32:07 -07003924static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3925{
Imre Deak91ca6892014-04-14 20:24:25 +03003926 if (IS_VALLEYVIEW(dev)) {
3927 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3928 mode = GEN6_RC_CTL_RC6_ENABLE;
3929 else
3930 mode = 0;
3931 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003932 if (HAS_RC6p(dev))
3933 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
3934 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3935 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3936 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3937
3938 else
3939 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
3940 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003941}
3942
Imre Deake6069ca2014-04-18 16:01:02 +03003943static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003944{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003945 /* No RC6 before Ironlake */
3946 if (INTEL_INFO(dev)->gen < 5)
3947 return 0;
3948
Imre Deake6069ca2014-04-18 16:01:02 +03003949 /* RC6 is only on Ironlake mobile not on desktop */
3950 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3951 return 0;
3952
Daniel Vetter456470e2012-08-08 23:35:40 +02003953 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003954 if (enable_rc6 >= 0) {
3955 int mask;
3956
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003957 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03003958 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3959 INTEL_RC6pp_ENABLE;
3960 else
3961 mask = INTEL_RC6_ENABLE;
3962
3963 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003964 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3965 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003966
3967 return enable_rc6 & mask;
3968 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003969
Chris Wilson6567d742012-11-10 10:00:06 +00003970 /* Disable RC6 on Ironlake */
3971 if (INTEL_INFO(dev)->gen == 5)
3972 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003973
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003974 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003975 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003976
3977 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003978}
3979
Imre Deake6069ca2014-04-18 16:01:02 +03003980int intel_enable_rc6(const struct drm_device *dev)
3981{
3982 return i915.enable_rc6;
3983}
3984
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003985static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003986{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003987 struct drm_i915_private *dev_priv = dev->dev_private;
3988 uint32_t rp_state_cap;
3989 u32 ddcc_status = 0;
3990 int ret;
3991
3992 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003993 /* All of these values are in units of 50MHz */
3994 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003995 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003996 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003997 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003998 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003999 /* hw_max = RP0 until we check for overclocking */
4000 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4001
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004002 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4003 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4004 ret = sandybridge_pcode_read(dev_priv,
4005 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4006 &ddcc_status);
4007 if (0 == ret)
4008 dev_priv->rps.efficient_freq =
4009 (ddcc_status >> 8) & 0xff;
4010 }
4011
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004012 /* Preserve min/max settings in case of re-init */
4013 if (dev_priv->rps.max_freq_softlimit == 0)
4014 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4015
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004016 if (dev_priv->rps.min_freq_softlimit == 0) {
4017 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4018 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004019 /* max(RPe, 450 MHz) */
4020 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004021 else
4022 dev_priv->rps.min_freq_softlimit =
4023 dev_priv->rps.min_freq;
4024 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004025}
4026
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004027/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004028static void gen9_enable_rps(struct drm_device *dev)
4029{
4030 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004031
4032 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4033
Damien Lespiauba1c5542015-01-16 18:07:26 +00004034 gen6_init_rps_frequencies(dev);
4035
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004036 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4037 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4038
4039 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4040 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4041 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4042 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4043 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4044 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4045 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4046 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4047 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4048 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4049 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4050 GEN6_RP_DOWN_IDLE_AVG);
4051
4052 gen6_enable_rps_interrupts(dev);
4053
4054 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4055}
4056
4057static void gen9_enable_rc6(struct drm_device *dev)
4058{
4059 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004060 struct intel_engine_cs *ring;
4061 uint32_t rc6_mask = 0;
4062 int unused;
4063
4064 /* 1a: Software RC state - RC0 */
4065 I915_WRITE(GEN6_RC_STATE, 0);
4066
4067 /* 1b: Get forcewake during program sequence. Although the driver
4068 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004069 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004070
4071 /* 2a: Disable RC states. */
4072 I915_WRITE(GEN6_RC_CONTROL, 0);
4073
4074 /* 2b: Program RC6 thresholds.*/
4075 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4076 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4077 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4078 for_each_ring(ring, dev_priv, unused)
4079 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4080 I915_WRITE(GEN6_RC_SLEEP, 0);
4081 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4082
Zhe Wang38c23522015-01-20 12:23:04 +00004083 /* 2c: Program Coarse Power Gating Policies. */
4084 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4085 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4086
Zhe Wang20e49362014-11-04 17:07:05 +00004087 /* 3a: Enable RC6 */
4088 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4089 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4090 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4091 "on" : "off");
4092 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4093 GEN6_RC_CTL_EI_MODE(1) |
4094 rc6_mask);
4095
Zhe Wang38c23522015-01-20 12:23:04 +00004096 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4097 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4098
Mika Kuoppala59bad942015-01-16 11:34:40 +02004099 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004100
4101}
4102
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004103static void gen8_enable_rps(struct drm_device *dev)
4104{
4105 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004106 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004107 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004108 int unused;
4109
4110 /* 1a: Software RC state - RC0 */
4111 I915_WRITE(GEN6_RC_STATE, 0);
4112
4113 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4114 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004115 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004116
4117 /* 2a: Disable RC states. */
4118 I915_WRITE(GEN6_RC_CONTROL, 0);
4119
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004120 /* Initialize rps frequencies */
4121 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004122
4123 /* 2b: Program RC6 thresholds.*/
4124 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4125 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4126 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4127 for_each_ring(ring, dev_priv, unused)
4128 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4129 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004130 if (IS_BROADWELL(dev))
4131 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4132 else
4133 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004134
4135 /* 3: Enable RC6 */
4136 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4137 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004138 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004139 if (IS_BROADWELL(dev))
4140 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4141 GEN7_RC_CTL_TO_MODE |
4142 rc6_mask);
4143 else
4144 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4145 GEN6_RC_CTL_EI_MODE(1) |
4146 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004147
4148 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004149 I915_WRITE(GEN6_RPNSWREQ,
4150 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4151 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4152 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004153 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4154 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004155
Daniel Vetter7526ed72014-09-29 15:07:19 +02004156 /* Docs recommend 900MHz, and 300 MHz respectively */
4157 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4158 dev_priv->rps.max_freq_softlimit << 24 |
4159 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004160
Daniel Vetter7526ed72014-09-29 15:07:19 +02004161 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4162 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4163 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4164 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004165
Daniel Vetter7526ed72014-09-29 15:07:19 +02004166 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004167
4168 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004169 I915_WRITE(GEN6_RP_CONTROL,
4170 GEN6_RP_MEDIA_TURBO |
4171 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4172 GEN6_RP_MEDIA_IS_GFX |
4173 GEN6_RP_ENABLE |
4174 GEN6_RP_UP_BUSY_AVG |
4175 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004176
Daniel Vetter7526ed72014-09-29 15:07:19 +02004177 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004178
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004179 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4180 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004181
Mika Kuoppala59bad942015-01-16 11:34:40 +02004182 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004183}
4184
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004185static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004186{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004187 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004188 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004189 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004190 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004191 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004192 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004193
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004194 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004195
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004196 /* Here begins a magic sequence of register writes to enable
4197 * auto-downclocking.
4198 *
4199 * Perhaps there might be some value in exposing these to
4200 * userspace...
4201 */
4202 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004203
4204 /* Clear the DBG now so we don't confuse earlier errors */
4205 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4206 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4207 I915_WRITE(GTFIFODBG, gtfifodbg);
4208 }
4209
Mika Kuoppala59bad942015-01-16 11:34:40 +02004210 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004211
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004212 /* Initialize rps frequencies */
4213 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004214
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004215 /* disable the counters and set deterministic thresholds */
4216 I915_WRITE(GEN6_RC_CONTROL, 0);
4217
4218 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4219 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4220 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4221 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4222 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4223
Chris Wilsonb4519512012-05-11 14:29:30 +01004224 for_each_ring(ring, dev_priv, i)
4225 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004226
4227 I915_WRITE(GEN6_RC_SLEEP, 0);
4228 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004229 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004230 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4231 else
4232 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004233 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004234 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4235
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004236 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004237 rc6_mode = intel_enable_rc6(dev_priv->dev);
4238 if (rc6_mode & INTEL_RC6_ENABLE)
4239 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4240
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004241 /* We don't use those on Haswell */
4242 if (!IS_HASWELL(dev)) {
4243 if (rc6_mode & INTEL_RC6p_ENABLE)
4244 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004245
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004246 if (rc6_mode & INTEL_RC6pp_ENABLE)
4247 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4248 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004249
Ben Widawskydc39fff2013-10-18 12:32:07 -07004250 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004251
4252 I915_WRITE(GEN6_RC_CONTROL,
4253 rc6_mask |
4254 GEN6_RC_CTL_EI_MODE(1) |
4255 GEN6_RC_CTL_HW_ENABLE);
4256
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004257 /* Power down if completely idle for over 50ms */
4258 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004259 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004260
Ben Widawsky42c05262012-09-26 10:34:00 -07004261 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004262 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004263 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004264
4265 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4266 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4267 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004268 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004269 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004270 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004271 }
4272
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004273 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004274 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004275
Ben Widawsky31643d52012-09-26 10:34:01 -07004276 rc6vids = 0;
4277 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4278 if (IS_GEN6(dev) && ret) {
4279 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4280 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4281 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4282 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4283 rc6vids &= 0xffff00;
4284 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4285 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4286 if (ret)
4287 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4288 }
4289
Mika Kuoppala59bad942015-01-16 11:34:40 +02004290 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004291}
4292
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004293static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004294{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004295 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004296 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004297 unsigned int gpu_freq;
4298 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004299 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004300 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004301
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004302 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004303
Ben Widawskyeda79642013-10-07 17:15:48 -03004304 policy = cpufreq_cpu_get(0);
4305 if (policy) {
4306 max_ia_freq = policy->cpuinfo.max_freq;
4307 cpufreq_cpu_put(policy);
4308 } else {
4309 /*
4310 * Default to measured freq if none found, PCU will ensure we
4311 * don't go over
4312 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004313 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004314 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004315
4316 /* Convert from kHz to MHz */
4317 max_ia_freq /= 1000;
4318
Ben Widawsky153b4b952013-10-22 22:05:09 -07004319 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004320 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4321 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004322
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004323 /*
4324 * For each potential GPU frequency, load a ring frequency we'd like
4325 * to use for memory access. We do this by specifying the IA frequency
4326 * the PCU should use as a reference to determine the ring frequency.
4327 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004328 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004329 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004330 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004331 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004332
Ben Widawsky46c764d2013-11-02 21:07:49 -07004333 if (INTEL_INFO(dev)->gen >= 8) {
4334 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4335 ring_freq = max(min_ring_freq, gpu_freq);
4336 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004337 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004338 ring_freq = max(min_ring_freq, ring_freq);
4339 /* leave ia_freq as the default, chosen by cpufreq */
4340 } else {
4341 /* On older processors, there is no separate ring
4342 * clock domain, so in order to boost the bandwidth
4343 * of the ring, we need to upclock the CPU (ia_freq).
4344 *
4345 * For GPU frequencies less than 750MHz,
4346 * just use the lowest ring freq.
4347 */
4348 if (gpu_freq < min_freq)
4349 ia_freq = 800;
4350 else
4351 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4352 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4353 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004354
Ben Widawsky42c05262012-09-26 10:34:00 -07004355 sandybridge_pcode_write(dev_priv,
4356 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004357 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4358 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4359 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004360 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004361}
4362
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004363void gen6_update_ring_freq(struct drm_device *dev)
4364{
4365 struct drm_i915_private *dev_priv = dev->dev_private;
4366
4367 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4368 return;
4369
4370 mutex_lock(&dev_priv->rps.hw_lock);
4371 __gen6_update_ring_freq(dev);
4372 mutex_unlock(&dev_priv->rps.hw_lock);
4373}
4374
Ville Syrjälä03af2042014-06-28 02:03:53 +03004375static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304376{
Deepak S095acd52015-01-17 11:05:59 +05304377 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304378 u32 val, rp0;
4379
Deepak S095acd52015-01-17 11:05:59 +05304380 if (dev->pdev->revision >= 0x20) {
4381 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304382
Deepak S095acd52015-01-17 11:05:59 +05304383 switch (INTEL_INFO(dev)->eu_total) {
4384 case 8:
4385 /* (2 * 4) config */
4386 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4387 break;
4388 case 12:
4389 /* (2 * 6) config */
4390 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4391 break;
4392 case 16:
4393 /* (2 * 8) config */
4394 default:
4395 /* Setting (2 * 8) Min RP0 for any other combination */
4396 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4397 break;
4398 }
4399 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4400 } else {
4401 /* For pre-production hardware */
4402 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4403 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4404 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4405 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304406 return rp0;
4407}
4408
4409static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4410{
4411 u32 val, rpe;
4412
4413 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4414 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4415
4416 return rpe;
4417}
4418
Deepak S7707df42014-07-12 18:46:14 +05304419static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4420{
Deepak S095acd52015-01-17 11:05:59 +05304421 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304422 u32 val, rp1;
4423
Deepak S095acd52015-01-17 11:05:59 +05304424 if (dev->pdev->revision >= 0x20) {
4425 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4426 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4427 } else {
4428 /* For pre-production hardware */
4429 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4430 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4431 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4432 }
Deepak S7707df42014-07-12 18:46:14 +05304433 return rp1;
4434}
4435
Ville Syrjälä03af2042014-06-28 02:03:53 +03004436static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304437{
Deepak S095acd52015-01-17 11:05:59 +05304438 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304439 u32 val, rpn;
4440
Deepak S095acd52015-01-17 11:05:59 +05304441 if (dev->pdev->revision >= 0x20) {
4442 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4443 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4444 FB_GFX_FREQ_FUSE_MASK);
4445 } else { /* For pre-production hardware */
4446 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4447 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4448 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4449 }
4450
Deepak S2b6b3a02014-05-27 15:59:30 +05304451 return rpn;
4452}
4453
Deepak Sf8f2b002014-07-10 13:16:21 +05304454static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4455{
4456 u32 val, rp1;
4457
4458 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4459
4460 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4461
4462 return rp1;
4463}
4464
Ville Syrjälä03af2042014-06-28 02:03:53 +03004465static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004466{
4467 u32 val, rp0;
4468
Jani Nikula64936252013-05-22 15:36:20 +03004469 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004470
4471 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4472 /* Clamp to max */
4473 rp0 = min_t(u32, rp0, 0xea);
4474
4475 return rp0;
4476}
4477
4478static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4479{
4480 u32 val, rpe;
4481
Jani Nikula64936252013-05-22 15:36:20 +03004482 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004483 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004484 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004485 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4486
4487 return rpe;
4488}
4489
Ville Syrjälä03af2042014-06-28 02:03:53 +03004490static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004491{
Jani Nikula64936252013-05-22 15:36:20 +03004492 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004493}
4494
Imre Deakae484342014-03-31 15:10:44 +03004495/* Check that the pctx buffer wasn't move under us. */
4496static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4497{
4498 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4499
4500 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4501 dev_priv->vlv_pctx->stolen->start);
4502}
4503
Deepak S38807742014-05-23 21:00:15 +05304504
4505/* Check that the pcbr address is not empty. */
4506static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4507{
4508 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4509
4510 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4511}
4512
4513static void cherryview_setup_pctx(struct drm_device *dev)
4514{
4515 struct drm_i915_private *dev_priv = dev->dev_private;
4516 unsigned long pctx_paddr, paddr;
4517 struct i915_gtt *gtt = &dev_priv->gtt;
4518 u32 pcbr;
4519 int pctx_size = 32*1024;
4520
4521 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4522
4523 pcbr = I915_READ(VLV_PCBR);
4524 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004525 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304526 paddr = (dev_priv->mm.stolen_base +
4527 (gtt->stolen_size - pctx_size));
4528
4529 pctx_paddr = (paddr & (~4095));
4530 I915_WRITE(VLV_PCBR, pctx_paddr);
4531 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004532
4533 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304534}
4535
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004536static void valleyview_setup_pctx(struct drm_device *dev)
4537{
4538 struct drm_i915_private *dev_priv = dev->dev_private;
4539 struct drm_i915_gem_object *pctx;
4540 unsigned long pctx_paddr;
4541 u32 pcbr;
4542 int pctx_size = 24*1024;
4543
Imre Deak17b0c1f2014-02-11 21:39:06 +02004544 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4545
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004546 pcbr = I915_READ(VLV_PCBR);
4547 if (pcbr) {
4548 /* BIOS set it up already, grab the pre-alloc'd space */
4549 int pcbr_offset;
4550
4551 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4552 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4553 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004554 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004555 pctx_size);
4556 goto out;
4557 }
4558
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004559 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4560
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004561 /*
4562 * From the Gunit register HAS:
4563 * The Gfx driver is expected to program this register and ensure
4564 * proper allocation within Gfx stolen memory. For example, this
4565 * register should be programmed such than the PCBR range does not
4566 * overlap with other ranges, such as the frame buffer, protected
4567 * memory, or any other relevant ranges.
4568 */
4569 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4570 if (!pctx) {
4571 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4572 return;
4573 }
4574
4575 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4576 I915_WRITE(VLV_PCBR, pctx_paddr);
4577
4578out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004579 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004580 dev_priv->vlv_pctx = pctx;
4581}
4582
Imre Deakae484342014-03-31 15:10:44 +03004583static void valleyview_cleanup_pctx(struct drm_device *dev)
4584{
4585 struct drm_i915_private *dev_priv = dev->dev_private;
4586
4587 if (WARN_ON(!dev_priv->vlv_pctx))
4588 return;
4589
4590 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4591 dev_priv->vlv_pctx = NULL;
4592}
4593
Imre Deak4e805192014-04-14 20:24:41 +03004594static void valleyview_init_gt_powersave(struct drm_device *dev)
4595{
4596 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004597 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004598
4599 valleyview_setup_pctx(dev);
4600
4601 mutex_lock(&dev_priv->rps.hw_lock);
4602
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004603 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4604 switch ((val >> 6) & 3) {
4605 case 0:
4606 case 1:
4607 dev_priv->mem_freq = 800;
4608 break;
4609 case 2:
4610 dev_priv->mem_freq = 1066;
4611 break;
4612 case 3:
4613 dev_priv->mem_freq = 1333;
4614 break;
4615 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004616 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004617
Imre Deak4e805192014-04-14 20:24:41 +03004618 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4619 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4620 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004621 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004622 dev_priv->rps.max_freq);
4623
4624 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4625 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004626 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004627 dev_priv->rps.efficient_freq);
4628
Deepak Sf8f2b002014-07-10 13:16:21 +05304629 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4630 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004631 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304632 dev_priv->rps.rp1_freq);
4633
Imre Deak4e805192014-04-14 20:24:41 +03004634 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4635 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004636 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004637 dev_priv->rps.min_freq);
4638
4639 /* Preserve min/max settings in case of re-init */
4640 if (dev_priv->rps.max_freq_softlimit == 0)
4641 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4642
4643 if (dev_priv->rps.min_freq_softlimit == 0)
4644 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4645
4646 mutex_unlock(&dev_priv->rps.hw_lock);
4647}
4648
Deepak S38807742014-05-23 21:00:15 +05304649static void cherryview_init_gt_powersave(struct drm_device *dev)
4650{
Deepak S2b6b3a02014-05-27 15:59:30 +05304651 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004652 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304653
Deepak S38807742014-05-23 21:00:15 +05304654 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304655
4656 mutex_lock(&dev_priv->rps.hw_lock);
4657
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004658 mutex_lock(&dev_priv->dpio_lock);
4659 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4660 mutex_unlock(&dev_priv->dpio_lock);
4661
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004662 switch ((val >> 2) & 0x7) {
4663 case 0:
4664 case 1:
4665 dev_priv->rps.cz_freq = 200;
4666 dev_priv->mem_freq = 1600;
4667 break;
4668 case 2:
4669 dev_priv->rps.cz_freq = 267;
4670 dev_priv->mem_freq = 1600;
4671 break;
4672 case 3:
4673 dev_priv->rps.cz_freq = 333;
4674 dev_priv->mem_freq = 2000;
4675 break;
4676 case 4:
4677 dev_priv->rps.cz_freq = 320;
4678 dev_priv->mem_freq = 1600;
4679 break;
4680 case 5:
4681 dev_priv->rps.cz_freq = 400;
4682 dev_priv->mem_freq = 1600;
4683 break;
4684 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004685 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004686
Deepak S2b6b3a02014-05-27 15:59:30 +05304687 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4688 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4689 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004690 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304691 dev_priv->rps.max_freq);
4692
4693 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4694 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004695 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304696 dev_priv->rps.efficient_freq);
4697
Deepak S7707df42014-07-12 18:46:14 +05304698 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4699 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004700 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304701 dev_priv->rps.rp1_freq);
4702
Deepak S2b6b3a02014-05-27 15:59:30 +05304703 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4704 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004705 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304706 dev_priv->rps.min_freq);
4707
Ville Syrjälä1c147622014-08-18 14:42:43 +03004708 WARN_ONCE((dev_priv->rps.max_freq |
4709 dev_priv->rps.efficient_freq |
4710 dev_priv->rps.rp1_freq |
4711 dev_priv->rps.min_freq) & 1,
4712 "Odd GPU freq values\n");
4713
Deepak S2b6b3a02014-05-27 15:59:30 +05304714 /* Preserve min/max settings in case of re-init */
4715 if (dev_priv->rps.max_freq_softlimit == 0)
4716 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4717
4718 if (dev_priv->rps.min_freq_softlimit == 0)
4719 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4720
4721 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304722}
4723
Imre Deak4e805192014-04-14 20:24:41 +03004724static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4725{
4726 valleyview_cleanup_pctx(dev);
4727}
4728
Deepak S38807742014-05-23 21:00:15 +05304729static void cherryview_enable_rps(struct drm_device *dev)
4730{
4731 struct drm_i915_private *dev_priv = dev->dev_private;
4732 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304733 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304734 int i;
4735
4736 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4737
4738 gtfifodbg = I915_READ(GTFIFODBG);
4739 if (gtfifodbg) {
4740 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4741 gtfifodbg);
4742 I915_WRITE(GTFIFODBG, gtfifodbg);
4743 }
4744
4745 cherryview_check_pctx(dev_priv);
4746
4747 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4748 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004749 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304750
Ville Syrjälä160614a2015-01-19 13:50:47 +02004751 /* Disable RC states. */
4752 I915_WRITE(GEN6_RC_CONTROL, 0);
4753
Deepak S38807742014-05-23 21:00:15 +05304754 /* 2a: Program RC6 thresholds.*/
4755 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4756 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4757 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4758
4759 for_each_ring(ring, dev_priv, i)
4760 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4761 I915_WRITE(GEN6_RC_SLEEP, 0);
4762
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004763 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4764 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304765
4766 /* allows RC6 residency counter to work */
4767 I915_WRITE(VLV_COUNTER_CONTROL,
4768 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4769 VLV_MEDIA_RC6_COUNT_EN |
4770 VLV_RENDER_RC6_COUNT_EN));
4771
4772 /* For now we assume BIOS is allocating and populating the PCBR */
4773 pcbr = I915_READ(VLV_PCBR);
4774
Deepak S38807742014-05-23 21:00:15 +05304775 /* 3: Enable RC6 */
4776 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4777 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004778 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304779
4780 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4781
Deepak S2b6b3a02014-05-27 15:59:30 +05304782 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004783 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304784 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4785 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4786 I915_WRITE(GEN6_RP_UP_EI, 66000);
4787 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4788
4789 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4790
4791 /* 5: Enable RPS */
4792 I915_WRITE(GEN6_RP_CONTROL,
4793 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004794 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304795 GEN6_RP_ENABLE |
4796 GEN6_RP_UP_BUSY_AVG |
4797 GEN6_RP_DOWN_IDLE_AVG);
4798
4799 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4800
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004801 /* RPS code assumes GPLL is used */
4802 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4803
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004804 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304805 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4806
4807 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4808 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004809 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304810 dev_priv->rps.cur_freq);
4811
4812 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004813 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304814 dev_priv->rps.efficient_freq);
4815
4816 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4817
Mika Kuoppala59bad942015-01-16 11:34:40 +02004818 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304819}
4820
Jesse Barnes0a073b82013-04-17 15:54:58 -07004821static void valleyview_enable_rps(struct drm_device *dev)
4822{
4823 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004824 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004825 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004826 int i;
4827
4828 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4829
Imre Deakae484342014-03-31 15:10:44 +03004830 valleyview_check_pctx(dev_priv);
4831
Jesse Barnes0a073b82013-04-17 15:54:58 -07004832 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004833 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4834 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004835 I915_WRITE(GTFIFODBG, gtfifodbg);
4836 }
4837
Deepak Sc8d9a592013-11-23 14:55:42 +05304838 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004839 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004840
Ville Syrjälä160614a2015-01-19 13:50:47 +02004841 /* Disable RC states. */
4842 I915_WRITE(GEN6_RC_CONTROL, 0);
4843
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004844 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004845 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4846 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4847 I915_WRITE(GEN6_RP_UP_EI, 66000);
4848 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4849
4850 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4851
4852 I915_WRITE(GEN6_RP_CONTROL,
4853 GEN6_RP_MEDIA_TURBO |
4854 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4855 GEN6_RP_MEDIA_IS_GFX |
4856 GEN6_RP_ENABLE |
4857 GEN6_RP_UP_BUSY_AVG |
4858 GEN6_RP_DOWN_IDLE_CONT);
4859
4860 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4861 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4862 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4863
4864 for_each_ring(ring, dev_priv, i)
4865 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4866
Jesse Barnes2f0aa302013-11-15 09:32:11 -08004867 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004868
4869 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004870 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004871 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4872 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004873 VLV_MEDIA_RC6_COUNT_EN |
4874 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004875
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004876 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004877 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004878
4879 intel_print_rc6_info(dev, rc6_mode);
4880
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004881 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004882
Jani Nikula64936252013-05-22 15:36:20 +03004883 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004884
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004885 /* RPS code assumes GPLL is used */
4886 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4887
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004888 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07004889 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4890
Ben Widawskyb39fb292014-03-19 18:31:11 -07004891 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004892 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004893 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004894 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004895
Ville Syrjälä73008b92013-06-25 19:21:01 +03004896 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004897 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004898 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004899
Ben Widawskyb39fb292014-03-19 18:31:11 -07004900 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004901
Mika Kuoppala59bad942015-01-16 11:34:40 +02004902 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004903}
4904
Daniel Vetter930ebb42012-06-29 23:32:16 +02004905void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004906{
4907 struct drm_i915_private *dev_priv = dev->dev_private;
4908
Daniel Vetter3e373942012-11-02 19:55:04 +01004909 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004910 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004911 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4912 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004913 }
4914
Daniel Vetter3e373942012-11-02 19:55:04 +01004915 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004916 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004917 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4918 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004919 }
4920}
4921
Daniel Vetter930ebb42012-06-29 23:32:16 +02004922static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004923{
4924 struct drm_i915_private *dev_priv = dev->dev_private;
4925
4926 if (I915_READ(PWRCTXA)) {
4927 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4928 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4929 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4930 50);
4931
4932 I915_WRITE(PWRCTXA, 0);
4933 POSTING_READ(PWRCTXA);
4934
4935 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4936 POSTING_READ(RSTDBYCTL);
4937 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004938}
4939
4940static int ironlake_setup_rc6(struct drm_device *dev)
4941{
4942 struct drm_i915_private *dev_priv = dev->dev_private;
4943
Daniel Vetter3e373942012-11-02 19:55:04 +01004944 if (dev_priv->ips.renderctx == NULL)
4945 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4946 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004947 return -ENOMEM;
4948
Daniel Vetter3e373942012-11-02 19:55:04 +01004949 if (dev_priv->ips.pwrctx == NULL)
4950 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4951 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004952 ironlake_teardown_rc6(dev);
4953 return -ENOMEM;
4954 }
4955
4956 return 0;
4957}
4958
Daniel Vetter930ebb42012-06-29 23:32:16 +02004959static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004960{
4961 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004962 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004963 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004964 int ret;
4965
4966 /* rc6 disabled by default due to repeated reports of hanging during
4967 * boot and resume.
4968 */
4969 if (!intel_enable_rc6(dev))
4970 return;
4971
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004972 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4973
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004974 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004975 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004976 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004977
Chris Wilson3e960502012-11-27 16:22:54 +00004978 was_interruptible = dev_priv->mm.interruptible;
4979 dev_priv->mm.interruptible = false;
4980
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004981 /*
4982 * GPU can automatically power down the render unit if given a page
4983 * to save state.
4984 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004985 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004986 if (ret) {
4987 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004988 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004989 return;
4990 }
4991
Daniel Vetter6d90c952012-04-26 23:28:05 +02004992 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4993 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004994 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004995 MI_MM_SPACE_GTT |
4996 MI_SAVE_EXT_STATE_EN |
4997 MI_RESTORE_EXT_STATE_EN |
4998 MI_RESTORE_INHIBIT);
4999 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
5000 intel_ring_emit(ring, MI_NOOP);
5001 intel_ring_emit(ring, MI_FLUSH);
5002 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005003
5004 /*
5005 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
5006 * does an implicit flush, combined with MI_FLUSH above, it should be
5007 * safe to assume that renderctx is valid
5008 */
Chris Wilson3e960502012-11-27 16:22:54 +00005009 ret = intel_ring_idle(ring);
5010 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005011 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02005012 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005013 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005014 return;
5015 }
5016
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005017 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005018 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005019
Imre Deak91ca6892014-04-14 20:24:25 +03005020 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005021}
5022
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005023static unsigned long intel_pxfreq(u32 vidfreq)
5024{
5025 unsigned long freq;
5026 int div = (vidfreq & 0x3f0000) >> 16;
5027 int post = (vidfreq & 0x3000) >> 12;
5028 int pre = (vidfreq & 0x7);
5029
5030 if (!pre)
5031 return 0;
5032
5033 freq = ((div * 133333) / ((1<<post) * pre));
5034
5035 return freq;
5036}
5037
Daniel Vettereb48eb02012-04-26 23:28:12 +02005038static const struct cparams {
5039 u16 i;
5040 u16 t;
5041 u16 m;
5042 u16 c;
5043} cparams[] = {
5044 { 1, 1333, 301, 28664 },
5045 { 1, 1066, 294, 24460 },
5046 { 1, 800, 294, 25192 },
5047 { 0, 1333, 276, 27605 },
5048 { 0, 1066, 276, 27605 },
5049 { 0, 800, 231, 23784 },
5050};
5051
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005052static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005053{
5054 u64 total_count, diff, ret;
5055 u32 count1, count2, count3, m = 0, c = 0;
5056 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5057 int i;
5058
Daniel Vetter02d71952012-08-09 16:44:54 +02005059 assert_spin_locked(&mchdev_lock);
5060
Daniel Vetter20e4d402012-08-08 23:35:39 +02005061 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005062
5063 /* Prevent division-by-zero if we are asking too fast.
5064 * Also, we don't get interesting results if we are polling
5065 * faster than once in 10ms, so just return the saved value
5066 * in such cases.
5067 */
5068 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005069 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005070
5071 count1 = I915_READ(DMIEC);
5072 count2 = I915_READ(DDREC);
5073 count3 = I915_READ(CSIEC);
5074
5075 total_count = count1 + count2 + count3;
5076
5077 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005078 if (total_count < dev_priv->ips.last_count1) {
5079 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005080 diff += total_count;
5081 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005082 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005083 }
5084
5085 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005086 if (cparams[i].i == dev_priv->ips.c_m &&
5087 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005088 m = cparams[i].m;
5089 c = cparams[i].c;
5090 break;
5091 }
5092 }
5093
5094 diff = div_u64(diff, diff1);
5095 ret = ((m * diff) + c);
5096 ret = div_u64(ret, 10);
5097
Daniel Vetter20e4d402012-08-08 23:35:39 +02005098 dev_priv->ips.last_count1 = total_count;
5099 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005100
Daniel Vetter20e4d402012-08-08 23:35:39 +02005101 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005102
5103 return ret;
5104}
5105
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005106unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5107{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005108 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005109 unsigned long val;
5110
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005111 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005112 return 0;
5113
5114 spin_lock_irq(&mchdev_lock);
5115
5116 val = __i915_chipset_val(dev_priv);
5117
5118 spin_unlock_irq(&mchdev_lock);
5119
5120 return val;
5121}
5122
Daniel Vettereb48eb02012-04-26 23:28:12 +02005123unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5124{
5125 unsigned long m, x, b;
5126 u32 tsfs;
5127
5128 tsfs = I915_READ(TSFS);
5129
5130 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5131 x = I915_READ8(TR1);
5132
5133 b = tsfs & TSFS_INTR_MASK;
5134
5135 return ((m * x) / 127) - b;
5136}
5137
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005138static int _pxvid_to_vd(u8 pxvid)
5139{
5140 if (pxvid == 0)
5141 return 0;
5142
5143 if (pxvid >= 8 && pxvid < 31)
5144 pxvid = 31;
5145
5146 return (pxvid + 2) * 125;
5147}
5148
5149static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005150{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005151 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005152 const int vd = _pxvid_to_vd(pxvid);
5153 const int vm = vd - 1125;
5154
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005155 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005156 return vm > 0 ? vm : 0;
5157
5158 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005159}
5160
Daniel Vetter02d71952012-08-09 16:44:54 +02005161static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005162{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005163 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005164 u32 count;
5165
Daniel Vetter02d71952012-08-09 16:44:54 +02005166 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005167
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005168 now = ktime_get_raw_ns();
5169 diffms = now - dev_priv->ips.last_time2;
5170 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005171
5172 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005173 if (!diffms)
5174 return;
5175
5176 count = I915_READ(GFXEC);
5177
Daniel Vetter20e4d402012-08-08 23:35:39 +02005178 if (count < dev_priv->ips.last_count2) {
5179 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005180 diff += count;
5181 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005182 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005183 }
5184
Daniel Vetter20e4d402012-08-08 23:35:39 +02005185 dev_priv->ips.last_count2 = count;
5186 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005187
5188 /* More magic constants... */
5189 diff = diff * 1181;
5190 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005191 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005192}
5193
Daniel Vetter02d71952012-08-09 16:44:54 +02005194void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5195{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005196 struct drm_device *dev = dev_priv->dev;
5197
5198 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005199 return;
5200
Daniel Vetter92703882012-08-09 16:46:01 +02005201 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005202
5203 __i915_update_gfx_val(dev_priv);
5204
Daniel Vetter92703882012-08-09 16:46:01 +02005205 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005206}
5207
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005208static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005209{
5210 unsigned long t, corr, state1, corr2, state2;
5211 u32 pxvid, ext_v;
5212
Daniel Vetter02d71952012-08-09 16:44:54 +02005213 assert_spin_locked(&mchdev_lock);
5214
Ben Widawskyb39fb292014-03-19 18:31:11 -07005215 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005216 pxvid = (pxvid >> 24) & 0x7f;
5217 ext_v = pvid_to_extvid(dev_priv, pxvid);
5218
5219 state1 = ext_v;
5220
5221 t = i915_mch_val(dev_priv);
5222
5223 /* Revel in the empirically derived constants */
5224
5225 /* Correction factor in 1/100000 units */
5226 if (t > 80)
5227 corr = ((t * 2349) + 135940);
5228 else if (t >= 50)
5229 corr = ((t * 964) + 29317);
5230 else /* < 50 */
5231 corr = ((t * 301) + 1004);
5232
5233 corr = corr * ((150142 * state1) / 10000 - 78642);
5234 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005235 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005236
5237 state2 = (corr2 * state1) / 10000;
5238 state2 /= 100; /* convert to mW */
5239
Daniel Vetter02d71952012-08-09 16:44:54 +02005240 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005241
Daniel Vetter20e4d402012-08-08 23:35:39 +02005242 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005243}
5244
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005245unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5246{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005247 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005248 unsigned long val;
5249
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005250 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005251 return 0;
5252
5253 spin_lock_irq(&mchdev_lock);
5254
5255 val = __i915_gfx_val(dev_priv);
5256
5257 spin_unlock_irq(&mchdev_lock);
5258
5259 return val;
5260}
5261
Daniel Vettereb48eb02012-04-26 23:28:12 +02005262/**
5263 * i915_read_mch_val - return value for IPS use
5264 *
5265 * Calculate and return a value for the IPS driver to use when deciding whether
5266 * we have thermal and power headroom to increase CPU or GPU power budget.
5267 */
5268unsigned long i915_read_mch_val(void)
5269{
5270 struct drm_i915_private *dev_priv;
5271 unsigned long chipset_val, graphics_val, ret = 0;
5272
Daniel Vetter92703882012-08-09 16:46:01 +02005273 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005274 if (!i915_mch_dev)
5275 goto out_unlock;
5276 dev_priv = i915_mch_dev;
5277
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005278 chipset_val = __i915_chipset_val(dev_priv);
5279 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005280
5281 ret = chipset_val + graphics_val;
5282
5283out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005284 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005285
5286 return ret;
5287}
5288EXPORT_SYMBOL_GPL(i915_read_mch_val);
5289
5290/**
5291 * i915_gpu_raise - raise GPU frequency limit
5292 *
5293 * Raise the limit; IPS indicates we have thermal headroom.
5294 */
5295bool i915_gpu_raise(void)
5296{
5297 struct drm_i915_private *dev_priv;
5298 bool ret = true;
5299
Daniel Vetter92703882012-08-09 16:46:01 +02005300 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005301 if (!i915_mch_dev) {
5302 ret = false;
5303 goto out_unlock;
5304 }
5305 dev_priv = i915_mch_dev;
5306
Daniel Vetter20e4d402012-08-08 23:35:39 +02005307 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5308 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005309
5310out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005311 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005312
5313 return ret;
5314}
5315EXPORT_SYMBOL_GPL(i915_gpu_raise);
5316
5317/**
5318 * i915_gpu_lower - lower GPU frequency limit
5319 *
5320 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5321 * frequency maximum.
5322 */
5323bool i915_gpu_lower(void)
5324{
5325 struct drm_i915_private *dev_priv;
5326 bool ret = true;
5327
Daniel Vetter92703882012-08-09 16:46:01 +02005328 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005329 if (!i915_mch_dev) {
5330 ret = false;
5331 goto out_unlock;
5332 }
5333 dev_priv = i915_mch_dev;
5334
Daniel Vetter20e4d402012-08-08 23:35:39 +02005335 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5336 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005337
5338out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005339 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005340
5341 return ret;
5342}
5343EXPORT_SYMBOL_GPL(i915_gpu_lower);
5344
5345/**
5346 * i915_gpu_busy - indicate GPU business to IPS
5347 *
5348 * Tell the IPS driver whether or not the GPU is busy.
5349 */
5350bool i915_gpu_busy(void)
5351{
5352 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005353 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005354 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005355 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005356
Daniel Vetter92703882012-08-09 16:46:01 +02005357 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005358 if (!i915_mch_dev)
5359 goto out_unlock;
5360 dev_priv = i915_mch_dev;
5361
Chris Wilsonf047e392012-07-21 12:31:41 +01005362 for_each_ring(ring, dev_priv, i)
5363 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005364
5365out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005366 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005367
5368 return ret;
5369}
5370EXPORT_SYMBOL_GPL(i915_gpu_busy);
5371
5372/**
5373 * i915_gpu_turbo_disable - disable graphics turbo
5374 *
5375 * Disable graphics turbo by resetting the max frequency and setting the
5376 * current frequency to the default.
5377 */
5378bool i915_gpu_turbo_disable(void)
5379{
5380 struct drm_i915_private *dev_priv;
5381 bool ret = true;
5382
Daniel Vetter92703882012-08-09 16:46:01 +02005383 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005384 if (!i915_mch_dev) {
5385 ret = false;
5386 goto out_unlock;
5387 }
5388 dev_priv = i915_mch_dev;
5389
Daniel Vetter20e4d402012-08-08 23:35:39 +02005390 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005391
Daniel Vetter20e4d402012-08-08 23:35:39 +02005392 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005393 ret = false;
5394
5395out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005396 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005397
5398 return ret;
5399}
5400EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5401
5402/**
5403 * Tells the intel_ips driver that the i915 driver is now loaded, if
5404 * IPS got loaded first.
5405 *
5406 * This awkward dance is so that neither module has to depend on the
5407 * other in order for IPS to do the appropriate communication of
5408 * GPU turbo limits to i915.
5409 */
5410static void
5411ips_ping_for_i915_load(void)
5412{
5413 void (*link)(void);
5414
5415 link = symbol_get(ips_link_to_i915_driver);
5416 if (link) {
5417 link();
5418 symbol_put(ips_link_to_i915_driver);
5419 }
5420}
5421
5422void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5423{
Daniel Vetter02d71952012-08-09 16:44:54 +02005424 /* We only register the i915 ips part with intel-ips once everything is
5425 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005426 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005427 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005428 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005429
5430 ips_ping_for_i915_load();
5431}
5432
5433void intel_gpu_ips_teardown(void)
5434{
Daniel Vetter92703882012-08-09 16:46:01 +02005435 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005436 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005437 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005438}
Deepak S76c3552f2014-01-30 23:08:16 +05305439
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005440static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005441{
5442 struct drm_i915_private *dev_priv = dev->dev_private;
5443 u32 lcfuse;
5444 u8 pxw[16];
5445 int i;
5446
5447 /* Disable to program */
5448 I915_WRITE(ECR, 0);
5449 POSTING_READ(ECR);
5450
5451 /* Program energy weights for various events */
5452 I915_WRITE(SDEW, 0x15040d00);
5453 I915_WRITE(CSIEW0, 0x007f0000);
5454 I915_WRITE(CSIEW1, 0x1e220004);
5455 I915_WRITE(CSIEW2, 0x04000004);
5456
5457 for (i = 0; i < 5; i++)
5458 I915_WRITE(PEW + (i * 4), 0);
5459 for (i = 0; i < 3; i++)
5460 I915_WRITE(DEW + (i * 4), 0);
5461
5462 /* Program P-state weights to account for frequency power adjustment */
5463 for (i = 0; i < 16; i++) {
5464 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5465 unsigned long freq = intel_pxfreq(pxvidfreq);
5466 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5467 PXVFREQ_PX_SHIFT;
5468 unsigned long val;
5469
5470 val = vid * vid;
5471 val *= (freq / 1000);
5472 val *= 255;
5473 val /= (127*127*900);
5474 if (val > 0xff)
5475 DRM_ERROR("bad pxval: %ld\n", val);
5476 pxw[i] = val;
5477 }
5478 /* Render standby states get 0 weight */
5479 pxw[14] = 0;
5480 pxw[15] = 0;
5481
5482 for (i = 0; i < 4; i++) {
5483 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5484 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5485 I915_WRITE(PXW + (i * 4), val);
5486 }
5487
5488 /* Adjust magic regs to magic values (more experimental results) */
5489 I915_WRITE(OGW0, 0);
5490 I915_WRITE(OGW1, 0);
5491 I915_WRITE(EG0, 0x00007f00);
5492 I915_WRITE(EG1, 0x0000000e);
5493 I915_WRITE(EG2, 0x000e0000);
5494 I915_WRITE(EG3, 0x68000300);
5495 I915_WRITE(EG4, 0x42000000);
5496 I915_WRITE(EG5, 0x00140031);
5497 I915_WRITE(EG6, 0);
5498 I915_WRITE(EG7, 0);
5499
5500 for (i = 0; i < 8; i++)
5501 I915_WRITE(PXWL + (i * 4), 0);
5502
5503 /* Enable PMON + select events */
5504 I915_WRITE(ECR, 0x80000019);
5505
5506 lcfuse = I915_READ(LCFUSE02);
5507
Daniel Vetter20e4d402012-08-08 23:35:39 +02005508 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005509}
5510
Imre Deakae484342014-03-31 15:10:44 +03005511void intel_init_gt_powersave(struct drm_device *dev)
5512{
Imre Deake6069ca2014-04-18 16:01:02 +03005513 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5514
Deepak S38807742014-05-23 21:00:15 +05305515 if (IS_CHERRYVIEW(dev))
5516 cherryview_init_gt_powersave(dev);
5517 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005518 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005519}
5520
5521void intel_cleanup_gt_powersave(struct drm_device *dev)
5522{
Deepak S38807742014-05-23 21:00:15 +05305523 if (IS_CHERRYVIEW(dev))
5524 return;
5525 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005526 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005527}
5528
Imre Deakdbea3ce2014-12-15 18:59:28 +02005529static void gen6_suspend_rps(struct drm_device *dev)
5530{
5531 struct drm_i915_private *dev_priv = dev->dev_private;
5532
5533 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5534
5535 /*
5536 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5537 * is added for it.
5538 */
5539 if (INTEL_INFO(dev)->gen < 9)
5540 gen6_disable_rps_interrupts(dev);
5541}
5542
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005543/**
5544 * intel_suspend_gt_powersave - suspend PM work and helper threads
5545 * @dev: drm device
5546 *
5547 * We don't want to disable RC6 or other features here, we just want
5548 * to make sure any work we've queued has finished and won't bother
5549 * us while we're suspended.
5550 */
5551void intel_suspend_gt_powersave(struct drm_device *dev)
5552{
5553 struct drm_i915_private *dev_priv = dev->dev_private;
5554
Imre Deakd4d70aa2014-11-19 15:30:04 +02005555 if (INTEL_INFO(dev)->gen < 6)
5556 return;
5557
Imre Deakdbea3ce2014-12-15 18:59:28 +02005558 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305559
5560 /* Force GPU to min freq during suspend */
5561 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005562}
5563
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005564void intel_disable_gt_powersave(struct drm_device *dev)
5565{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005566 struct drm_i915_private *dev_priv = dev->dev_private;
5567
Daniel Vetter930ebb42012-06-29 23:32:16 +02005568 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005569 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005570 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305571 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005572 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005573
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005574 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005575 if (INTEL_INFO(dev)->gen >= 9)
5576 gen9_disable_rps(dev);
5577 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305578 cherryview_disable_rps(dev);
5579 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005580 valleyview_disable_rps(dev);
5581 else
5582 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005583
Chris Wilsonc0951f02013-10-10 21:58:50 +01005584 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005585 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005586 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005587}
5588
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005589static void intel_gen6_powersave_work(struct work_struct *work)
5590{
5591 struct drm_i915_private *dev_priv =
5592 container_of(work, struct drm_i915_private,
5593 rps.delayed_resume_work.work);
5594 struct drm_device *dev = dev_priv->dev;
5595
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005596 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005597
Imre Deak3cc134e2014-11-19 15:30:03 +02005598 /*
5599 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5600 * added for it.
5601 */
5602 if (INTEL_INFO(dev)->gen < 9)
5603 gen6_reset_rps_interrupts(dev);
5604
Deepak S38807742014-05-23 21:00:15 +05305605 if (IS_CHERRYVIEW(dev)) {
5606 cherryview_enable_rps(dev);
5607 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005608 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005609 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005610 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005611 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005612 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005613 } else if (IS_BROADWELL(dev)) {
5614 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005615 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005616 } else {
5617 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005618 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005619 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005620 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005621
5622 if (INTEL_INFO(dev)->gen < 9)
5623 gen6_enable_rps_interrupts(dev);
5624
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005625 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005626
5627 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005628}
5629
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005630void intel_enable_gt_powersave(struct drm_device *dev)
5631{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005632 struct drm_i915_private *dev_priv = dev->dev_private;
5633
Yu Zhangf61018b2015-02-10 19:05:52 +08005634 /* Powersaving is controlled by the host when inside a VM */
5635 if (intel_vgpu_active(dev))
5636 return;
5637
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005638 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005639 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005640 ironlake_enable_drps(dev);
5641 ironlake_enable_rc6(dev);
5642 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005643 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305644 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005645 /*
5646 * PCU communication is slow and this doesn't need to be
5647 * done at any specific time, so do this out of our fast path
5648 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005649 *
5650 * We depend on the HW RC6 power context save/restore
5651 * mechanism when entering D3 through runtime PM suspend. So
5652 * disable RPM until RPS/RC6 is properly setup. We can only
5653 * get here via the driver load/system resume/runtime resume
5654 * paths, so the _noresume version is enough (and in case of
5655 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005656 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005657 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5658 round_jiffies_up_relative(HZ)))
5659 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005660 }
5661}
5662
Imre Deakc6df39b2014-04-14 20:24:29 +03005663void intel_reset_gt_powersave(struct drm_device *dev)
5664{
5665 struct drm_i915_private *dev_priv = dev->dev_private;
5666
Imre Deakdbea3ce2014-12-15 18:59:28 +02005667 if (INTEL_INFO(dev)->gen < 6)
5668 return;
5669
5670 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005671 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005672}
5673
Daniel Vetter3107bd42012-10-31 22:52:31 +01005674static void ibx_init_clock_gating(struct drm_device *dev)
5675{
5676 struct drm_i915_private *dev_priv = dev->dev_private;
5677
5678 /*
5679 * On Ibex Peak and Cougar Point, we need to disable clock
5680 * gating for the panel power sequencer or it will fail to
5681 * start up when no ports are active.
5682 */
5683 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5684}
5685
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005686static void g4x_disable_trickle_feed(struct drm_device *dev)
5687{
5688 struct drm_i915_private *dev_priv = dev->dev_private;
5689 int pipe;
5690
Damien Lespiau055e3932014-08-18 13:49:10 +01005691 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005692 I915_WRITE(DSPCNTR(pipe),
5693 I915_READ(DSPCNTR(pipe)) |
5694 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005695 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005696 }
5697}
5698
Ville Syrjälä017636c2013-12-05 15:51:37 +02005699static void ilk_init_lp_watermarks(struct drm_device *dev)
5700{
5701 struct drm_i915_private *dev_priv = dev->dev_private;
5702
5703 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5704 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5705 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5706
5707 /*
5708 * Don't touch WM1S_LP_EN here.
5709 * Doing so could cause underruns.
5710 */
5711}
5712
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005713static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005714{
5715 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005716 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005717
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005718 /*
5719 * Required for FBC
5720 * WaFbcDisableDpfcClockGating:ilk
5721 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005722 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5723 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5724 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005725
5726 I915_WRITE(PCH_3DCGDIS0,
5727 MARIUNIT_CLOCK_GATE_DISABLE |
5728 SVSMUNIT_CLOCK_GATE_DISABLE);
5729 I915_WRITE(PCH_3DCGDIS1,
5730 VFMUNIT_CLOCK_GATE_DISABLE);
5731
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005732 /*
5733 * According to the spec the following bits should be set in
5734 * order to enable memory self-refresh
5735 * The bit 22/21 of 0x42004
5736 * The bit 5 of 0x42020
5737 * The bit 15 of 0x45000
5738 */
5739 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5740 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5741 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005742 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005743 I915_WRITE(DISP_ARB_CTL,
5744 (I915_READ(DISP_ARB_CTL) |
5745 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005746
5747 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005748
5749 /*
5750 * Based on the document from hardware guys the following bits
5751 * should be set unconditionally in order to enable FBC.
5752 * The bit 22 of 0x42000
5753 * The bit 22 of 0x42004
5754 * The bit 7,8,9 of 0x42020.
5755 */
5756 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005757 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005758 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5759 I915_READ(ILK_DISPLAY_CHICKEN1) |
5760 ILK_FBCQ_DIS);
5761 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5762 I915_READ(ILK_DISPLAY_CHICKEN2) |
5763 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005764 }
5765
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005766 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5767
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005768 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5769 I915_READ(ILK_DISPLAY_CHICKEN2) |
5770 ILK_ELPIN_409_SELECT);
5771 I915_WRITE(_3D_CHICKEN2,
5772 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5773 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005774
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005775 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005776 I915_WRITE(CACHE_MODE_0,
5777 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005778
Akash Goel4e046322014-04-04 17:14:38 +05305779 /* WaDisable_RenderCache_OperationalFlush:ilk */
5780 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5781
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005782 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005783
Daniel Vetter3107bd42012-10-31 22:52:31 +01005784 ibx_init_clock_gating(dev);
5785}
5786
5787static void cpt_init_clock_gating(struct drm_device *dev)
5788{
5789 struct drm_i915_private *dev_priv = dev->dev_private;
5790 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005791 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005792
5793 /*
5794 * On Ibex Peak and Cougar Point, we need to disable clock
5795 * gating for the panel power sequencer or it will fail to
5796 * start up when no ports are active.
5797 */
Jesse Barnescd664072013-10-02 10:34:19 -07005798 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5799 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5800 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005801 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5802 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005803 /* The below fixes the weird display corruption, a few pixels shifted
5804 * downward, on (only) LVDS of some HP laptops with IVY.
5805 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005806 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005807 val = I915_READ(TRANS_CHICKEN2(pipe));
5808 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5809 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005810 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005811 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005812 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5813 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5814 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005815 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5816 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005817 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005818 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005819 I915_WRITE(TRANS_CHICKEN1(pipe),
5820 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5821 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005822}
5823
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005824static void gen6_check_mch_setup(struct drm_device *dev)
5825{
5826 struct drm_i915_private *dev_priv = dev->dev_private;
5827 uint32_t tmp;
5828
5829 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005830 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5831 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5832 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005833}
5834
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005835static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005836{
5837 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005838 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005839
Damien Lespiau231e54f2012-10-19 17:55:41 +01005840 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005841
5842 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5843 I915_READ(ILK_DISPLAY_CHICKEN2) |
5844 ILK_ELPIN_409_SELECT);
5845
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005846 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005847 I915_WRITE(_3D_CHICKEN,
5848 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5849
Akash Goel4e046322014-04-04 17:14:38 +05305850 /* WaDisable_RenderCache_OperationalFlush:snb */
5851 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5852
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005853 /*
5854 * BSpec recoomends 8x4 when MSAA is used,
5855 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005856 *
5857 * Note that PS/WM thread counts depend on the WIZ hashing
5858 * disable bit, which we don't touch here, but it's good
5859 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005860 */
5861 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005862 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005863
Ville Syrjälä017636c2013-12-05 15:51:37 +02005864 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005865
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005866 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005867 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005868
5869 I915_WRITE(GEN6_UCGCTL1,
5870 I915_READ(GEN6_UCGCTL1) |
5871 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5872 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5873
5874 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5875 * gating disable must be set. Failure to set it results in
5876 * flickering pixels due to Z write ordering failures after
5877 * some amount of runtime in the Mesa "fire" demo, and Unigine
5878 * Sanctuary and Tropics, and apparently anything else with
5879 * alpha test or pixel discard.
5880 *
5881 * According to the spec, bit 11 (RCCUNIT) must also be set,
5882 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005883 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005884 * WaDisableRCCUnitClockGating:snb
5885 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005886 */
5887 I915_WRITE(GEN6_UCGCTL2,
5888 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5889 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5890
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005891 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005892 I915_WRITE(_3D_CHICKEN3,
5893 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005894
5895 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005896 * Bspec says:
5897 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5898 * 3DSTATE_SF number of SF output attributes is more than 16."
5899 */
5900 I915_WRITE(_3D_CHICKEN3,
5901 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5902
5903 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005904 * According to the spec the following bits should be
5905 * set in order to enable memory self-refresh and fbc:
5906 * The bit21 and bit22 of 0x42000
5907 * The bit21 and bit22 of 0x42004
5908 * The bit5 and bit7 of 0x42020
5909 * The bit14 of 0x70180
5910 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005911 *
5912 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005913 */
5914 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5915 I915_READ(ILK_DISPLAY_CHICKEN1) |
5916 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5917 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5918 I915_READ(ILK_DISPLAY_CHICKEN2) |
5919 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005920 I915_WRITE(ILK_DSPCLK_GATE_D,
5921 I915_READ(ILK_DSPCLK_GATE_D) |
5922 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5923 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005924
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005925 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005926
Daniel Vetter3107bd42012-10-31 22:52:31 +01005927 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005928
5929 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005930}
5931
5932static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5933{
5934 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5935
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005936 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005937 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005938 *
5939 * This actually overrides the dispatch
5940 * mode for all thread types.
5941 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005942 reg &= ~GEN7_FF_SCHED_MASK;
5943 reg |= GEN7_FF_TS_SCHED_HW;
5944 reg |= GEN7_FF_VS_SCHED_HW;
5945 reg |= GEN7_FF_DS_SCHED_HW;
5946
5947 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5948}
5949
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005950static void lpt_init_clock_gating(struct drm_device *dev)
5951{
5952 struct drm_i915_private *dev_priv = dev->dev_private;
5953
5954 /*
5955 * TODO: this bit should only be enabled when really needed, then
5956 * disabled when not needed anymore in order to save power.
5957 */
5958 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5959 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5960 I915_READ(SOUTH_DSPCLK_GATE_D) |
5961 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005962
5963 /* WADPOClockGatingDisable:hsw */
5964 I915_WRITE(_TRANSA_CHICKEN1,
5965 I915_READ(_TRANSA_CHICKEN1) |
5966 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005967}
5968
Imre Deak7d708ee2013-04-17 14:04:50 +03005969static void lpt_suspend_hw(struct drm_device *dev)
5970{
5971 struct drm_i915_private *dev_priv = dev->dev_private;
5972
5973 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5974 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5975
5976 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5977 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5978 }
5979}
5980
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005981static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005982{
5983 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005984 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005985
5986 I915_WRITE(WM3_LP_ILK, 0);
5987 I915_WRITE(WM2_LP_ILK, 0);
5988 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005989
Ben Widawskyab57fff2013-12-12 15:28:04 -08005990 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005991 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005992
Ben Widawskyab57fff2013-12-12 15:28:04 -08005993 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005994 I915_WRITE(CHICKEN_PAR1_1,
5995 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5996
Ben Widawskyab57fff2013-12-12 15:28:04 -08005997 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005998 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005999 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02006000 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02006001 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07006002 }
Ben Widawsky63801f22013-12-12 17:26:03 -08006003
Ben Widawskyab57fff2013-12-12 15:28:04 -08006004 /* WaVSRefCountFullforceMissDisable:bdw */
6005 /* WaDSRefCountFullforceMissDisable:bdw */
6006 I915_WRITE(GEN7_FF_THREAD_MODE,
6007 I915_READ(GEN7_FF_THREAD_MODE) &
6008 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006009
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006010 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6011 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006012
6013 /* WaDisableSDEUnitClockGating:bdw */
6014 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6015 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006016
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006017 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006018}
6019
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006020static void haswell_init_clock_gating(struct drm_device *dev)
6021{
6022 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006023
Ville Syrjälä017636c2013-12-05 15:51:37 +02006024 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006025
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006026 /* L3 caching of data atomics doesn't work -- disable it. */
6027 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6028 I915_WRITE(HSW_ROW_CHICKEN3,
6029 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6030
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006031 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006032 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6033 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6034 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6035
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006036 /* WaVSRefCountFullforceMissDisable:hsw */
6037 I915_WRITE(GEN7_FF_THREAD_MODE,
6038 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006039
Akash Goel4e046322014-04-04 17:14:38 +05306040 /* WaDisable_RenderCache_OperationalFlush:hsw */
6041 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6042
Chia-I Wufe27c602014-01-28 13:29:33 +08006043 /* enable HiZ Raw Stall Optimization */
6044 I915_WRITE(CACHE_MODE_0_GEN7,
6045 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6046
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006047 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006048 I915_WRITE(CACHE_MODE_1,
6049 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006050
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006051 /*
6052 * BSpec recommends 8x4 when MSAA is used,
6053 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006054 *
6055 * Note that PS/WM thread counts depend on the WIZ hashing
6056 * disable bit, which we don't touch here, but it's good
6057 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006058 */
6059 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006060 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006061
Kenneth Graunke94411592014-12-31 16:23:00 -08006062 /* WaSampleCChickenBitEnable:hsw */
6063 I915_WRITE(HALF_SLICE_CHICKEN3,
6064 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6065
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006066 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006067 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6068
Paulo Zanoni90a88642013-05-03 17:23:45 -03006069 /* WaRsPkgCStateDisplayPMReq:hsw */
6070 I915_WRITE(CHICKEN_PAR1_1,
6071 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006072
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006073 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006074}
6075
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006076static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006077{
6078 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006079 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006080
Ville Syrjälä017636c2013-12-05 15:51:37 +02006081 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006082
Damien Lespiau231e54f2012-10-19 17:55:41 +01006083 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006084
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006085 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006086 I915_WRITE(_3D_CHICKEN3,
6087 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6088
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006089 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006090 I915_WRITE(IVB_CHICKEN3,
6091 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6092 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6093
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006094 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006095 if (IS_IVB_GT1(dev))
6096 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6097 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006098
Akash Goel4e046322014-04-04 17:14:38 +05306099 /* WaDisable_RenderCache_OperationalFlush:ivb */
6100 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6101
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006102 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006103 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6104 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6105
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006106 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006107 I915_WRITE(GEN7_L3CNTLREG1,
6108 GEN7_WA_FOR_GEN7_L3_CONTROL);
6109 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006110 GEN7_WA_L3_CHICKEN_MODE);
6111 if (IS_IVB_GT1(dev))
6112 I915_WRITE(GEN7_ROW_CHICKEN2,
6113 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006114 else {
6115 /* must write both registers */
6116 I915_WRITE(GEN7_ROW_CHICKEN2,
6117 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006118 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6119 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006120 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006121
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006122 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006123 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6124 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6125
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006126 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006127 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006128 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006129 */
6130 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006131 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006132
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006133 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006134 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6135 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6136 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6137
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006138 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006139
6140 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006141
Chris Wilson22721342014-03-04 09:41:43 +00006142 if (0) { /* causes HiZ corruption on ivb:gt1 */
6143 /* enable HiZ Raw Stall Optimization */
6144 I915_WRITE(CACHE_MODE_0_GEN7,
6145 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6146 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006147
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006148 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006149 I915_WRITE(CACHE_MODE_1,
6150 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006151
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006152 /*
6153 * BSpec recommends 8x4 when MSAA is used,
6154 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006155 *
6156 * Note that PS/WM thread counts depend on the WIZ hashing
6157 * disable bit, which we don't touch here, but it's good
6158 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006159 */
6160 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006161 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006162
Ben Widawsky20848222012-05-04 18:58:59 -07006163 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6164 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6165 snpcr |= GEN6_MBC_SNPCR_MED;
6166 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006167
Ben Widawskyab5c6082013-04-05 13:12:41 -07006168 if (!HAS_PCH_NOP(dev))
6169 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006170
6171 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006172}
6173
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006174static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006175{
6176 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006177
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03006178 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006179
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006180 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006181 I915_WRITE(_3D_CHICKEN3,
6182 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6183
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006184 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006185 I915_WRITE(IVB_CHICKEN3,
6186 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6187 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6188
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006189 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006190 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006191 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006192 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6193 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006194
Akash Goel4e046322014-04-04 17:14:38 +05306195 /* WaDisable_RenderCache_OperationalFlush:vlv */
6196 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6197
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006198 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006199 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6200 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6201
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006202 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006203 I915_WRITE(GEN7_ROW_CHICKEN2,
6204 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6205
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006206 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006207 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6208 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6209 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6210
Ville Syrjälä46680e02014-01-22 21:33:01 +02006211 gen7_setup_fixed_func_scheduler(dev_priv);
6212
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006213 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006214 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006215 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006216 */
6217 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006218 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006219
Akash Goelc98f5062014-03-24 23:00:07 +05306220 /* WaDisableL3Bank2xClockGate:vlv
6221 * Disabling L3 clock gating- MMIO 940c[25] = 1
6222 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6223 I915_WRITE(GEN7_UCGCTL4,
6224 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006225
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006226 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006227
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006228 /*
6229 * BSpec says this must be set, even though
6230 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6231 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006232 I915_WRITE(CACHE_MODE_1,
6233 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006234
6235 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006236 * BSpec recommends 8x4 when MSAA is used,
6237 * however in practice 16x4 seems fastest.
6238 *
6239 * Note that PS/WM thread counts depend on the WIZ hashing
6240 * disable bit, which we don't touch here, but it's good
6241 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6242 */
6243 I915_WRITE(GEN7_GT_MODE,
6244 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6245
6246 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006247 * WaIncreaseL3CreditsForVLVB0:vlv
6248 * This is the hardware default actually.
6249 */
6250 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6251
6252 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006253 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006254 * Disable clock gating on th GCFG unit to prevent a delay
6255 * in the reporting of vblank events.
6256 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006257 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006258}
6259
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006260static void cherryview_init_clock_gating(struct drm_device *dev)
6261{
6262 struct drm_i915_private *dev_priv = dev->dev_private;
6263
6264 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6265
6266 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006267
Ville Syrjälä232ce332014-04-09 13:28:35 +03006268 /* WaVSRefCountFullforceMissDisable:chv */
6269 /* WaDSRefCountFullforceMissDisable:chv */
6270 I915_WRITE(GEN7_FF_THREAD_MODE,
6271 I915_READ(GEN7_FF_THREAD_MODE) &
6272 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006273
6274 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6275 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6276 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006277
6278 /* WaDisableCSUnitClockGating:chv */
6279 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6280 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006281
6282 /* WaDisableSDEUnitClockGating:chv */
6283 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6284 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006285}
6286
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006287static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006288{
6289 struct drm_i915_private *dev_priv = dev->dev_private;
6290 uint32_t dspclk_gate;
6291
6292 I915_WRITE(RENCLK_GATE_D1, 0);
6293 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6294 GS_UNIT_CLOCK_GATE_DISABLE |
6295 CL_UNIT_CLOCK_GATE_DISABLE);
6296 I915_WRITE(RAMCLK_GATE_D, 0);
6297 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6298 OVRUNIT_CLOCK_GATE_DISABLE |
6299 OVCUNIT_CLOCK_GATE_DISABLE;
6300 if (IS_GM45(dev))
6301 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6302 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006303
6304 /* WaDisableRenderCachePipelinedFlush */
6305 I915_WRITE(CACHE_MODE_0,
6306 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006307
Akash Goel4e046322014-04-04 17:14:38 +05306308 /* WaDisable_RenderCache_OperationalFlush:g4x */
6309 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6310
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006311 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006312}
6313
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006314static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006315{
6316 struct drm_i915_private *dev_priv = dev->dev_private;
6317
6318 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6319 I915_WRITE(RENCLK_GATE_D2, 0);
6320 I915_WRITE(DSPCLK_GATE_D, 0);
6321 I915_WRITE(RAMCLK_GATE_D, 0);
6322 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006323 I915_WRITE(MI_ARB_STATE,
6324 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306325
6326 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6327 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006328}
6329
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006330static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006331{
6332 struct drm_i915_private *dev_priv = dev->dev_private;
6333
6334 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6335 I965_RCC_CLOCK_GATE_DISABLE |
6336 I965_RCPB_CLOCK_GATE_DISABLE |
6337 I965_ISC_CLOCK_GATE_DISABLE |
6338 I965_FBC_CLOCK_GATE_DISABLE);
6339 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006340 I915_WRITE(MI_ARB_STATE,
6341 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306342
6343 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6344 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006345}
6346
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006347static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006348{
6349 struct drm_i915_private *dev_priv = dev->dev_private;
6350 u32 dstate = I915_READ(D_STATE);
6351
6352 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6353 DSTATE_DOT_CLOCK_GATING;
6354 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006355
6356 if (IS_PINEVIEW(dev))
6357 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006358
6359 /* IIR "flip pending" means done if this bit is set */
6360 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006361
6362 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006363 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006364
6365 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6366 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006367
6368 I915_WRITE(MI_ARB_STATE,
6369 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006370}
6371
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006372static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006373{
6374 struct drm_i915_private *dev_priv = dev->dev_private;
6375
6376 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006377
6378 /* interrupts should cause a wake up from C3 */
6379 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6380 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006381
6382 I915_WRITE(MEM_MODE,
6383 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006384}
6385
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006386static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006387{
6388 struct drm_i915_private *dev_priv = dev->dev_private;
6389
6390 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006391
6392 I915_WRITE(MEM_MODE,
6393 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6394 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006395}
6396
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006397void intel_init_clock_gating(struct drm_device *dev)
6398{
6399 struct drm_i915_private *dev_priv = dev->dev_private;
6400
Damien Lespiauc57e3552015-02-09 19:33:05 +00006401 if (dev_priv->display.init_clock_gating)
6402 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006403}
6404
Imre Deak7d708ee2013-04-17 14:04:50 +03006405void intel_suspend_hw(struct drm_device *dev)
6406{
6407 if (HAS_PCH_LPT(dev))
6408 lpt_suspend_hw(dev);
6409}
6410
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006411/* Set up chip specific power management-related functions */
6412void intel_init_pm(struct drm_device *dev)
6413{
6414 struct drm_i915_private *dev_priv = dev->dev_private;
6415
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006416 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006417
Daniel Vetterc921aba2012-04-26 23:28:17 +02006418 /* For cxsr */
6419 if (IS_PINEVIEW(dev))
6420 i915_pineview_get_mem_freq(dev);
6421 else if (IS_GEN5(dev))
6422 i915_ironlake_get_mem_freq(dev);
6423
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006424 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006425 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006426 skl_setup_wm_latency(dev);
6427
Damien Lespiau45db2192015-02-09 19:33:09 +00006428 dev_priv->display.init_clock_gating = skl_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006429 dev_priv->display.update_wm = skl_update_wm;
6430 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306431 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006432 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006433
Ville Syrjäläbd602542014-01-07 16:14:10 +02006434 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6435 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6436 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6437 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6438 dev_priv->display.update_wm = ilk_update_wm;
6439 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6440 } else {
6441 DRM_DEBUG_KMS("Failed to read display plane latency. "
6442 "Disable CxSR\n");
6443 }
6444
6445 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006446 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006447 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006448 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006449 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006450 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006451 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006452 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006453 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006454 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006455 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03006456 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306457 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006458 dev_priv->display.init_clock_gating =
6459 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006460 } else if (IS_VALLEYVIEW(dev)) {
6461 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306462 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006463 dev_priv->display.init_clock_gating =
6464 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006465 } else if (IS_PINEVIEW(dev)) {
6466 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6467 dev_priv->is_ddr3,
6468 dev_priv->fsb_freq,
6469 dev_priv->mem_freq)) {
6470 DRM_INFO("failed to find known CxSR latency "
6471 "(found ddr%s fsb freq %d, mem freq %d), "
6472 "disabling CxSR\n",
6473 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6474 dev_priv->fsb_freq, dev_priv->mem_freq);
6475 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006476 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006477 dev_priv->display.update_wm = NULL;
6478 } else
6479 dev_priv->display.update_wm = pineview_update_wm;
6480 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6481 } else if (IS_G4X(dev)) {
6482 dev_priv->display.update_wm = g4x_update_wm;
6483 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6484 } else if (IS_GEN4(dev)) {
6485 dev_priv->display.update_wm = i965_update_wm;
6486 if (IS_CRESTLINE(dev))
6487 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6488 else if (IS_BROADWATER(dev))
6489 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6490 } else if (IS_GEN3(dev)) {
6491 dev_priv->display.update_wm = i9xx_update_wm;
6492 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6493 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006494 } else if (IS_GEN2(dev)) {
6495 if (INTEL_INFO(dev)->num_pipes == 1) {
6496 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006497 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006498 } else {
6499 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006500 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006501 }
6502
6503 if (IS_I85X(dev) || IS_I865G(dev))
6504 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6505 else
6506 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6507 } else {
6508 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006509 }
6510}
6511
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006512int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006513{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006514 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006515
6516 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6517 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6518 return -EAGAIN;
6519 }
6520
6521 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006522 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006523 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6524
6525 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6526 500)) {
6527 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6528 return -ETIMEDOUT;
6529 }
6530
6531 *val = I915_READ(GEN6_PCODE_DATA);
6532 I915_WRITE(GEN6_PCODE_DATA, 0);
6533
6534 return 0;
6535}
6536
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006537int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006538{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006539 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006540
6541 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6542 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6543 return -EAGAIN;
6544 }
6545
6546 I915_WRITE(GEN6_PCODE_DATA, val);
6547 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6548
6549 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6550 500)) {
6551 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6552 return -ETIMEDOUT;
6553 }
6554
6555 I915_WRITE(GEN6_PCODE_DATA, 0);
6556
6557 return 0;
6558}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006559
Ville Syrjälädd06f882014-11-10 22:55:12 +02006560static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006561{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006562 switch (czclk_freq) {
6563 case 200:
6564 return 10;
6565 case 267:
6566 return 12;
6567 case 320:
6568 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006569 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006570 case 400:
6571 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006572 default:
6573 return -1;
6574 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006575}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006576
Ville Syrjälädd06f882014-11-10 22:55:12 +02006577static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6578{
6579 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6580
6581 div = vlv_gpu_freq_div(czclk_freq);
6582 if (div < 0)
6583 return div;
6584
6585 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006586}
6587
Fengguang Wub55dd642014-07-12 11:21:39 +02006588static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006589{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006590 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006591
Ville Syrjälädd06f882014-11-10 22:55:12 +02006592 mul = vlv_gpu_freq_div(czclk_freq);
6593 if (mul < 0)
6594 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006595
Ville Syrjälädd06f882014-11-10 22:55:12 +02006596 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006597}
6598
Fengguang Wub55dd642014-07-12 11:21:39 +02006599static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306600{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006601 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306602
Ville Syrjälädd06f882014-11-10 22:55:12 +02006603 div = vlv_gpu_freq_div(czclk_freq) / 2;
6604 if (div < 0)
6605 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306606
Ville Syrjälädd06f882014-11-10 22:55:12 +02006607 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306608}
6609
Fengguang Wub55dd642014-07-12 11:21:39 +02006610static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306611{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006612 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306613
Ville Syrjälädd06f882014-11-10 22:55:12 +02006614 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6615 if (mul < 0)
6616 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306617
Ville Syrjälä1c147622014-08-18 14:42:43 +03006618 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006619 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306620}
6621
Ville Syrjälä616bc822015-01-23 21:04:25 +02006622int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6623{
6624 if (IS_CHERRYVIEW(dev_priv->dev))
6625 return chv_gpu_freq(dev_priv, val);
6626 else if (IS_VALLEYVIEW(dev_priv->dev))
6627 return byt_gpu_freq(dev_priv, val);
6628 else
6629 return val * GT_FREQUENCY_MULTIPLIER;
6630}
6631
Ville Syrjälä616bc822015-01-23 21:04:25 +02006632int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6633{
Deepak S22b1b2f2014-07-12 14:54:33 +05306634 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006635 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306636 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006637 return byt_freq_opcode(dev_priv, val);
6638 else
6639 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306640}
6641
Daniel Vetterf742a552013-12-06 10:17:53 +01006642void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006643{
6644 struct drm_i915_private *dev_priv = dev->dev_private;
6645
Daniel Vetterf742a552013-12-06 10:17:53 +01006646 mutex_init(&dev_priv->rps.hw_lock);
6647
Chris Wilson907b28c2013-07-19 20:36:52 +01006648 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6649 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006650
Paulo Zanoni33688d92014-03-07 20:08:19 -03006651 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006652}