blob: 613827596aad17358c24a2e2314065153050df6f [file] [log] [blame]
Eugeni Dodonov85208be2012-04-16 22:20:34 -03001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -030028#include <linux/cpufreq.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030029#include "i915_drv.h"
30#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020031#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030033
Ben Widawskydc39fff2013-10-18 12:32:07 -070034/**
35 * RC6 is a special power stage which allows the GPU to enter an very
36 * low-voltage mode when idle, using down to 0V while at this stage. This
37 * stage is entered automatically when the GPU is idle when RC6 support is
38 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
39 *
40 * There are different RC6 modes available in Intel GPU, which differentiate
41 * among each other with the latency required to enter and leave RC6 and
42 * voltage consumed by the GPU in different states.
43 *
44 * The combination of the following flags define which states GPU is allowed
45 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
46 * RC6pp is deepest RC6. Their support by hardware varies according to the
47 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
48 * which brings the most power savings; deeper states save more power, but
49 * require higher latency to switch to and wake up.
50 */
51#define INTEL_RC6_ENABLE (1<<0)
52#define INTEL_RC6p_ENABLE (1<<1)
53#define INTEL_RC6pp_ENABLE (1<<2)
54
Damien Lespiauda2078c2013-02-13 15:27:27 +000055static void gen9_init_clock_gating(struct drm_device *dev)
56{
Damien Lespiauacd5c342014-03-26 16:55:46 +000057 struct drm_i915_private *dev_priv = dev->dev_private;
58
Hoath, Nicholas3dcd0202015-02-05 10:47:21 +000059 if (INTEL_REVID(dev) == SKL_REVID_A0) {
60 /*
61 * WaDisableSDEUnitClockGating:skl
62 * This seems to be a pre-production w/a.
63 */
64 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
65 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
66 }
Damien Lespiauda2078c2013-02-13 15:27:27 +000067}
68
Daniel Vetterc921aba2012-04-26 23:28:17 +020069static void i915_pineview_get_mem_freq(struct drm_device *dev)
70{
Jani Nikula50227e12014-03-31 14:27:21 +030071 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +020072 u32 tmp;
73
74 tmp = I915_READ(CLKCFG);
75
76 switch (tmp & CLKCFG_FSB_MASK) {
77 case CLKCFG_FSB_533:
78 dev_priv->fsb_freq = 533; /* 133*4 */
79 break;
80 case CLKCFG_FSB_800:
81 dev_priv->fsb_freq = 800; /* 200*4 */
82 break;
83 case CLKCFG_FSB_667:
84 dev_priv->fsb_freq = 667; /* 167*4 */
85 break;
86 case CLKCFG_FSB_400:
87 dev_priv->fsb_freq = 400; /* 100*4 */
88 break;
89 }
90
91 switch (tmp & CLKCFG_MEM_MASK) {
92 case CLKCFG_MEM_533:
93 dev_priv->mem_freq = 533;
94 break;
95 case CLKCFG_MEM_667:
96 dev_priv->mem_freq = 667;
97 break;
98 case CLKCFG_MEM_800:
99 dev_priv->mem_freq = 800;
100 break;
101 }
102
103 /* detect pineview DDR3 setting */
104 tmp = I915_READ(CSHRDDR3CTL);
105 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
106}
107
108static void i915_ironlake_get_mem_freq(struct drm_device *dev)
109{
Jani Nikula50227e12014-03-31 14:27:21 +0300110 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200111 u16 ddrpll, csipll;
112
113 ddrpll = I915_READ16(DDRMPLL1);
114 csipll = I915_READ16(CSIPLL0);
115
116 switch (ddrpll & 0xff) {
117 case 0xc:
118 dev_priv->mem_freq = 800;
119 break;
120 case 0x10:
121 dev_priv->mem_freq = 1066;
122 break;
123 case 0x14:
124 dev_priv->mem_freq = 1333;
125 break;
126 case 0x18:
127 dev_priv->mem_freq = 1600;
128 break;
129 default:
130 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
131 ddrpll & 0xff);
132 dev_priv->mem_freq = 0;
133 break;
134 }
135
Daniel Vetter20e4d402012-08-08 23:35:39 +0200136 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200137
138 switch (csipll & 0x3ff) {
139 case 0x00c:
140 dev_priv->fsb_freq = 3200;
141 break;
142 case 0x00e:
143 dev_priv->fsb_freq = 3733;
144 break;
145 case 0x010:
146 dev_priv->fsb_freq = 4266;
147 break;
148 case 0x012:
149 dev_priv->fsb_freq = 4800;
150 break;
151 case 0x014:
152 dev_priv->fsb_freq = 5333;
153 break;
154 case 0x016:
155 dev_priv->fsb_freq = 5866;
156 break;
157 case 0x018:
158 dev_priv->fsb_freq = 6400;
159 break;
160 default:
161 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
162 csipll & 0x3ff);
163 dev_priv->fsb_freq = 0;
164 break;
165 }
166
167 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200168 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200170 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200171 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200172 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200173 }
174}
175
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300176static const struct cxsr_latency cxsr_latency_table[] = {
177 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
178 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
179 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
180 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
181 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
182
183 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
184 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
185 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
186 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
187 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
188
189 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
190 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
191 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
192 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
193 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
194
195 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
196 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
197 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
198 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
199 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
200
201 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
202 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
203 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
204 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
205 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
206
207 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
208 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
209 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
210 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
211 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
212};
213
Daniel Vetter63c62272012-04-21 23:17:55 +0200214static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300215 int is_ddr3,
216 int fsb,
217 int mem)
218{
219 const struct cxsr_latency *latency;
220 int i;
221
222 if (fsb == 0 || mem == 0)
223 return NULL;
224
225 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
226 latency = &cxsr_latency_table[i];
227 if (is_desktop == latency->is_desktop &&
228 is_ddr3 == latency->is_ddr3 &&
229 fsb == latency->fsb_freq && mem == latency->mem_freq)
230 return latency;
231 }
232
233 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
234
235 return NULL;
236}
237
Imre Deak5209b1f2014-07-01 12:36:17 +0300238void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300239{
Imre Deak5209b1f2014-07-01 12:36:17 +0300240 struct drm_device *dev = dev_priv->dev;
241 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300242
Imre Deak5209b1f2014-07-01 12:36:17 +0300243 if (IS_VALLEYVIEW(dev)) {
244 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
245 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
246 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
247 } else if (IS_PINEVIEW(dev)) {
248 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
249 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
250 I915_WRITE(DSPFW3, val);
251 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
252 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
253 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
254 I915_WRITE(FW_BLC_SELF, val);
255 } else if (IS_I915GM(dev)) {
256 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
257 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
258 I915_WRITE(INSTPM, val);
259 } else {
260 return;
261 }
262
263 DRM_DEBUG_KMS("memory self-refresh is %s\n",
264 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300265}
266
267/*
268 * Latency for FIFO fetches is dependent on several factors:
269 * - memory configuration (speed, channels)
270 * - chipset
271 * - current MCH state
272 * It can be fairly high in some situations, so here we assume a fairly
273 * pessimal value. It's a tradeoff between extra memory fetches (if we
274 * set this value too high, the FIFO will fetch frequently to stay full)
275 * and power consumption (set it too low to save power and we might see
276 * FIFO underruns and display "flicker").
277 *
278 * A value of 5us seems to be a good balance; safe for very low end
279 * platforms but not overly aggressive on lower latency configs.
280 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100281static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300282
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300283static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300284{
285 struct drm_i915_private *dev_priv = dev->dev_private;
286 uint32_t dsparb = I915_READ(DSPARB);
287 int size;
288
289 size = dsparb & 0x7f;
290 if (plane)
291 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
292
293 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
294 plane ? "B" : "A", size);
295
296 return size;
297}
298
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200299static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300300{
301 struct drm_i915_private *dev_priv = dev->dev_private;
302 uint32_t dsparb = I915_READ(DSPARB);
303 int size;
304
305 size = dsparb & 0x1ff;
306 if (plane)
307 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
308 size >>= 1; /* Convert to cachelines */
309
310 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
311 plane ? "B" : "A", size);
312
313 return size;
314}
315
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300316static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300317{
318 struct drm_i915_private *dev_priv = dev->dev_private;
319 uint32_t dsparb = I915_READ(DSPARB);
320 int size;
321
322 size = dsparb & 0x7f;
323 size >>= 2; /* Convert to cachelines */
324
325 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
326 plane ? "B" : "A",
327 size);
328
329 return size;
330}
331
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300332/* Pineview has different values for various configs */
333static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300334 .fifo_size = PINEVIEW_DISPLAY_FIFO,
335 .max_wm = PINEVIEW_MAX_WM,
336 .default_wm = PINEVIEW_DFT_WM,
337 .guard_size = PINEVIEW_GUARD_WM,
338 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300339};
340static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300341 .fifo_size = PINEVIEW_DISPLAY_FIFO,
342 .max_wm = PINEVIEW_MAX_WM,
343 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
344 .guard_size = PINEVIEW_GUARD_WM,
345 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300346};
347static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300348 .fifo_size = PINEVIEW_CURSOR_FIFO,
349 .max_wm = PINEVIEW_CURSOR_MAX_WM,
350 .default_wm = PINEVIEW_CURSOR_DFT_WM,
351 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
352 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300353};
354static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300355 .fifo_size = PINEVIEW_CURSOR_FIFO,
356 .max_wm = PINEVIEW_CURSOR_MAX_WM,
357 .default_wm = PINEVIEW_CURSOR_DFT_WM,
358 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
359 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300360};
361static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300362 .fifo_size = G4X_FIFO_SIZE,
363 .max_wm = G4X_MAX_WM,
364 .default_wm = G4X_MAX_WM,
365 .guard_size = 2,
366 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300367};
368static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300369 .fifo_size = I965_CURSOR_FIFO,
370 .max_wm = I965_CURSOR_MAX_WM,
371 .default_wm = I965_CURSOR_DFT_WM,
372 .guard_size = 2,
373 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300374};
375static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300376 .fifo_size = VALLEYVIEW_FIFO_SIZE,
377 .max_wm = VALLEYVIEW_MAX_WM,
378 .default_wm = VALLEYVIEW_MAX_WM,
379 .guard_size = 2,
380 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300381};
382static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300383 .fifo_size = I965_CURSOR_FIFO,
384 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
385 .default_wm = I965_CURSOR_DFT_WM,
386 .guard_size = 2,
387 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300388};
389static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300390 .fifo_size = I965_CURSOR_FIFO,
391 .max_wm = I965_CURSOR_MAX_WM,
392 .default_wm = I965_CURSOR_DFT_WM,
393 .guard_size = 2,
394 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300395};
396static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300397 .fifo_size = I945_FIFO_SIZE,
398 .max_wm = I915_MAX_WM,
399 .default_wm = 1,
400 .guard_size = 2,
401 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300402};
403static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300404 .fifo_size = I915_FIFO_SIZE,
405 .max_wm = I915_MAX_WM,
406 .default_wm = 1,
407 .guard_size = 2,
408 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300409};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300410static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300411 .fifo_size = I855GM_FIFO_SIZE,
412 .max_wm = I915_MAX_WM,
413 .default_wm = 1,
414 .guard_size = 2,
415 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300416};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300417static const struct intel_watermark_params i830_bc_wm_info = {
418 .fifo_size = I855GM_FIFO_SIZE,
419 .max_wm = I915_MAX_WM/2,
420 .default_wm = 1,
421 .guard_size = 2,
422 .cacheline_size = I830_FIFO_LINE_SIZE,
423};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200424static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300425 .fifo_size = I830_FIFO_SIZE,
426 .max_wm = I915_MAX_WM,
427 .default_wm = 1,
428 .guard_size = 2,
429 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300430};
431
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300432/**
433 * intel_calculate_wm - calculate watermark level
434 * @clock_in_khz: pixel clock
435 * @wm: chip FIFO params
436 * @pixel_size: display pixel size
437 * @latency_ns: memory latency for the platform
438 *
439 * Calculate the watermark level (the level at which the display plane will
440 * start fetching from memory again). Each chip has a different display
441 * FIFO size and allocation, so the caller needs to figure that out and pass
442 * in the correct intel_watermark_params structure.
443 *
444 * As the pixel clock runs, the FIFO will be drained at a rate that depends
445 * on the pixel size. When it reaches the watermark level, it'll start
446 * fetching FIFO line sized based chunks from memory until the FIFO fills
447 * past the watermark point. If the FIFO drains completely, a FIFO underrun
448 * will occur, and a display engine hang could result.
449 */
450static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
451 const struct intel_watermark_params *wm,
452 int fifo_size,
453 int pixel_size,
454 unsigned long latency_ns)
455{
456 long entries_required, wm_size;
457
458 /*
459 * Note: we need to make sure we don't overflow for various clock &
460 * latency values.
461 * clocks go from a few thousand to several hundred thousand.
462 * latency is usually a few thousand
463 */
464 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
465 1000;
466 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
467
468 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
469
470 wm_size = fifo_size - (entries_required + wm->guard_size);
471
472 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
473
474 /* Don't promote wm_size to unsigned... */
475 if (wm_size > (long)wm->max_wm)
476 wm_size = wm->max_wm;
477 if (wm_size <= 0)
478 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300479
480 /*
481 * Bspec seems to indicate that the value shouldn't be lower than
482 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
483 * Lets go for 8 which is the burst size since certain platforms
484 * already use a hardcoded 8 (which is what the spec says should be
485 * done).
486 */
487 if (wm_size <= 8)
488 wm_size = 8;
489
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300490 return wm_size;
491}
492
493static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
494{
495 struct drm_crtc *crtc, *enabled = NULL;
496
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100497 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000498 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300499 if (enabled)
500 return NULL;
501 enabled = crtc;
502 }
503 }
504
505 return enabled;
506}
507
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300508static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300509{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300510 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300511 struct drm_i915_private *dev_priv = dev->dev_private;
512 struct drm_crtc *crtc;
513 const struct cxsr_latency *latency;
514 u32 reg;
515 unsigned long wm;
516
517 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
518 dev_priv->fsb_freq, dev_priv->mem_freq);
519 if (!latency) {
520 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300521 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300522 return;
523 }
524
525 crtc = single_enabled_crtc(dev);
526 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100527 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -0700528 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100529 int clock;
530
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200531 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100532 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533
534 /* Display SR */
535 wm = intel_calculate_wm(clock, &pineview_display_wm,
536 pineview_display_wm.fifo_size,
537 pixel_size, latency->display_sr);
538 reg = I915_READ(DSPFW1);
539 reg &= ~DSPFW_SR_MASK;
540 reg |= wm << DSPFW_SR_SHIFT;
541 I915_WRITE(DSPFW1, reg);
542 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
543
544 /* cursor SR */
545 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
546 pineview_display_wm.fifo_size,
547 pixel_size, latency->cursor_sr);
548 reg = I915_READ(DSPFW3);
549 reg &= ~DSPFW_CURSOR_SR_MASK;
550 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
551 I915_WRITE(DSPFW3, reg);
552
553 /* Display HPLL off SR */
554 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
555 pineview_display_hplloff_wm.fifo_size,
556 pixel_size, latency->display_hpll_disable);
557 reg = I915_READ(DSPFW3);
558 reg &= ~DSPFW_HPLL_SR_MASK;
559 reg |= wm & DSPFW_HPLL_SR_MASK;
560 I915_WRITE(DSPFW3, reg);
561
562 /* cursor HPLL off SR */
563 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
564 pineview_display_hplloff_wm.fifo_size,
565 pixel_size, latency->cursor_hpll_disable);
566 reg = I915_READ(DSPFW3);
567 reg &= ~DSPFW_HPLL_CURSOR_MASK;
568 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
569 I915_WRITE(DSPFW3, reg);
570 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
571
Imre Deak5209b1f2014-07-01 12:36:17 +0300572 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300573 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300574 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300575 }
576}
577
578static bool g4x_compute_wm0(struct drm_device *dev,
579 int plane,
580 const struct intel_watermark_params *display,
581 int display_latency_ns,
582 const struct intel_watermark_params *cursor,
583 int cursor_latency_ns,
584 int *plane_wm,
585 int *cursor_wm)
586{
587 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300588 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300589 int htotal, hdisplay, clock, pixel_size;
590 int line_time_us, line_count;
591 int entries, tlb_miss;
592
593 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000594 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300595 *cursor_wm = cursor->guard_size;
596 *plane_wm = display->guard_size;
597 return false;
598 }
599
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200600 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100601 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800602 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200603 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -0700604 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300605
606 /* Use the small buffer method to calculate plane watermark */
607 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
608 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
609 if (tlb_miss > 0)
610 entries += tlb_miss;
611 entries = DIV_ROUND_UP(entries, display->cacheline_size);
612 *plane_wm = entries + display->guard_size;
613 if (*plane_wm > (int)display->max_wm)
614 *plane_wm = display->max_wm;
615
616 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200617 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300618 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +0000619 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300620 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
621 if (tlb_miss > 0)
622 entries += tlb_miss;
623 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
624 *cursor_wm = entries + cursor->guard_size;
625 if (*cursor_wm > (int)cursor->max_wm)
626 *cursor_wm = (int)cursor->max_wm;
627
628 return true;
629}
630
631/*
632 * Check the wm result.
633 *
634 * If any calculated watermark values is larger than the maximum value that
635 * can be programmed into the associated watermark register, that watermark
636 * must be disabled.
637 */
638static bool g4x_check_srwm(struct drm_device *dev,
639 int display_wm, int cursor_wm,
640 const struct intel_watermark_params *display,
641 const struct intel_watermark_params *cursor)
642{
643 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
644 display_wm, cursor_wm);
645
646 if (display_wm > display->max_wm) {
647 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
648 display_wm, display->max_wm);
649 return false;
650 }
651
652 if (cursor_wm > cursor->max_wm) {
653 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
654 cursor_wm, cursor->max_wm);
655 return false;
656 }
657
658 if (!(display_wm || cursor_wm)) {
659 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
660 return false;
661 }
662
663 return true;
664}
665
666static bool g4x_compute_srwm(struct drm_device *dev,
667 int plane,
668 int latency_ns,
669 const struct intel_watermark_params *display,
670 const struct intel_watermark_params *cursor,
671 int *display_wm, int *cursor_wm)
672{
673 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300674 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300675 int hdisplay, htotal, pixel_size, clock;
676 unsigned long line_time_us;
677 int line_count, line_size;
678 int small, large;
679 int entries;
680
681 if (!latency_ns) {
682 *display_wm = *cursor_wm = 0;
683 return false;
684 }
685
686 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200687 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100688 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800689 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200690 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -0700691 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300692
Ville Syrjälä922044c2014-02-14 14:18:57 +0200693 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300694 line_count = (latency_ns / line_time_us + 1000) / 1000;
695 line_size = hdisplay * pixel_size;
696
697 /* Use the minimum of the small and large buffer method for primary */
698 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
699 large = line_count * line_size;
700
701 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
702 *display_wm = entries + display->guard_size;
703
704 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +0000705 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300706 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
707 *cursor_wm = entries + cursor->guard_size;
708
709 return g4x_check_srwm(dev,
710 *display_wm, *cursor_wm,
711 display, cursor);
712}
713
Gajanan Bhat0948c262014-08-07 01:58:24 +0530714static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
715 int pixel_size,
716 int *prec_mult,
717 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300718{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700719 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300720 int entries;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200721 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722
Gajanan Bhat0948c262014-08-07 01:58:24 +0530723 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724 return false;
725
Gajanan Bhat0948c262014-08-07 01:58:24 +0530726 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
727 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300728
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530729 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700730 if (IS_CHERRYVIEW(dev))
731 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
732 DRAIN_LATENCY_PRECISION_16;
733 else
734 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
735 DRAIN_LATENCY_PRECISION_32;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530736 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300737
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530738 if (*drain_latency > DRAIN_LATENCY_MASK)
739 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300740
741 return true;
742}
743
744/*
745 * Update drain latency registers of memory arbiter
746 *
747 * Valleyview SoC has a new memory arbiter and needs drain latency registers
748 * to be programmed. Each plane has a drain latency multiplier and a drain
749 * latency value.
750 */
751
Gajanan Bhat41aad812014-07-16 18:24:03 +0530752static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300753{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700754 struct drm_device *dev = crtc->dev;
755 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
757 int pixel_size;
758 int drain_latency;
759 enum pipe pipe = intel_crtc->pipe;
760 int plane_prec, prec_mult, plane_dl;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700761 const int high_precision = IS_CHERRYVIEW(dev) ?
762 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300763
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700764 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
765 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
Gajanan Bhat0948c262014-08-07 01:58:24 +0530766 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300767
Gajanan Bhat0948c262014-08-07 01:58:24 +0530768 if (!intel_crtc_active(crtc)) {
769 I915_WRITE(VLV_DDL(pipe), plane_dl);
770 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771 }
772
Gajanan Bhat0948c262014-08-07 01:58:24 +0530773 /* Primary plane Drain Latency */
774 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
775 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700776 plane_prec = (prec_mult == high_precision) ?
777 DDL_PLANE_PRECISION_HIGH :
778 DDL_PLANE_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530779 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300780 }
Gajanan Bhat0948c262014-08-07 01:58:24 +0530781
782 /* Cursor Drain Latency
783 * BPP is always 4 for cursor
784 */
785 pixel_size = 4;
786
787 /* Program cursor DL only if it is enabled */
788 if (intel_crtc->cursor_base &&
789 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700790 plane_prec = (prec_mult == high_precision) ?
791 DDL_CURSOR_PRECISION_HIGH :
792 DDL_CURSOR_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530793 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
794 }
795
796 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300797}
798
799#define single_plane_enabled(mask) is_power_of_2(mask)
800
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300801static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300802{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300803 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300804 static const int sr_latency_ns = 12000;
805 struct drm_i915_private *dev_priv = dev->dev_private;
806 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
807 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000808 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300809 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300810 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300811
Gajanan Bhat41aad812014-07-16 18:24:03 +0530812 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300813
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200814 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100815 &valleyview_wm_info, pessimal_latency_ns,
816 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300817 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200818 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300819
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200820 if (g4x_compute_wm0(dev, PIPE_B,
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 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200824 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300825
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300826 if (single_plane_enabled(enabled) &&
827 g4x_compute_srwm(dev, ffs(enabled) - 1,
828 sr_latency_ns,
829 &valleyview_wm_info,
830 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000831 &plane_sr, &ignore_cursor_sr) &&
832 g4x_compute_srwm(dev, ffs(enabled) - 1,
833 2*sr_latency_ns,
834 &valleyview_wm_info,
835 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +0000836 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +0300837 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +0000838 } else {
Imre Deak98584252014-06-13 14:54:20 +0300839 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300840 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +0000841 plane_sr = cursor_sr = 0;
842 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300843
Ville Syrjäläa5043452014-06-28 02:04:18 +0300844 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
845 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300846 planea_wm, cursora_wm,
847 planeb_wm, cursorb_wm,
848 plane_sr, cursor_sr);
849
850 I915_WRITE(DSPFW1,
851 (plane_sr << DSPFW_SR_SHIFT) |
852 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
853 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +0300854 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300855 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +0000856 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300857 (cursora_wm << DSPFW_CURSORA_SHIFT));
858 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +0000859 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
860 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +0300861
862 if (cxsr_enabled)
863 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300864}
865
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300866static void cherryview_update_wm(struct drm_crtc *crtc)
867{
868 struct drm_device *dev = crtc->dev;
869 static const int sr_latency_ns = 12000;
870 struct drm_i915_private *dev_priv = dev->dev_private;
871 int planea_wm, planeb_wm, planec_wm;
872 int cursora_wm, cursorb_wm, cursorc_wm;
873 int plane_sr, cursor_sr;
874 int ignore_plane_sr, ignore_cursor_sr;
875 unsigned int enabled = 0;
876 bool cxsr_enabled;
877
878 vlv_update_drain_latency(crtc);
879
880 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100881 &valleyview_wm_info, pessimal_latency_ns,
882 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300883 &planea_wm, &cursora_wm))
884 enabled |= 1 << PIPE_A;
885
886 if (g4x_compute_wm0(dev, PIPE_B,
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 &planeb_wm, &cursorb_wm))
890 enabled |= 1 << PIPE_B;
891
892 if (g4x_compute_wm0(dev, PIPE_C,
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 &planec_wm, &cursorc_wm))
896 enabled |= 1 << PIPE_C;
897
898 if (single_plane_enabled(enabled) &&
899 g4x_compute_srwm(dev, ffs(enabled) - 1,
900 sr_latency_ns,
901 &valleyview_wm_info,
902 &valleyview_cursor_wm_info,
903 &plane_sr, &ignore_cursor_sr) &&
904 g4x_compute_srwm(dev, ffs(enabled) - 1,
905 2*sr_latency_ns,
906 &valleyview_wm_info,
907 &valleyview_cursor_wm_info,
908 &ignore_plane_sr, &cursor_sr)) {
909 cxsr_enabled = true;
910 } else {
911 cxsr_enabled = false;
912 intel_set_memory_cxsr(dev_priv, false);
913 plane_sr = cursor_sr = 0;
914 }
915
916 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
917 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
918 "SR: plane=%d, cursor=%d\n",
919 planea_wm, cursora_wm,
920 planeb_wm, cursorb_wm,
921 planec_wm, cursorc_wm,
922 plane_sr, cursor_sr);
923
924 I915_WRITE(DSPFW1,
925 (plane_sr << DSPFW_SR_SHIFT) |
926 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
927 (planeb_wm << DSPFW_PLANEB_SHIFT) |
928 (planea_wm << DSPFW_PLANEA_SHIFT));
929 I915_WRITE(DSPFW2,
930 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
931 (cursora_wm << DSPFW_CURSORA_SHIFT));
932 I915_WRITE(DSPFW3,
933 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
934 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
935 I915_WRITE(DSPFW9_CHV,
936 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
937 DSPFW_CURSORC_MASK)) |
938 (planec_wm << DSPFW_PLANEC_SHIFT) |
939 (cursorc_wm << DSPFW_CURSORC_SHIFT));
940
941 if (cxsr_enabled)
942 intel_set_memory_cxsr(dev_priv, true);
943}
944
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530945static void valleyview_update_sprite_wm(struct drm_plane *plane,
946 struct drm_crtc *crtc,
947 uint32_t sprite_width,
948 uint32_t sprite_height,
949 int pixel_size,
950 bool enabled, bool scaled)
951{
952 struct drm_device *dev = crtc->dev;
953 struct drm_i915_private *dev_priv = dev->dev_private;
954 int pipe = to_intel_plane(plane)->pipe;
955 int sprite = to_intel_plane(plane)->plane;
956 int drain_latency;
957 int plane_prec;
958 int sprite_dl;
959 int prec_mult;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700960 const int high_precision = IS_CHERRYVIEW(dev) ?
961 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530962
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700963 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530964 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
965
966 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
967 &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700968 plane_prec = (prec_mult == high_precision) ?
969 DDL_SPRITE_PRECISION_HIGH(sprite) :
970 DDL_SPRITE_PRECISION_LOW(sprite);
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530971 sprite_dl |= plane_prec |
972 (drain_latency << DDL_SPRITE_SHIFT(sprite));
973 }
974
975 I915_WRITE(VLV_DDL(pipe), sprite_dl);
976}
977
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300978static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300979{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300980 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300981 static const int sr_latency_ns = 12000;
982 struct drm_i915_private *dev_priv = dev->dev_private;
983 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
984 int plane_sr, cursor_sr;
985 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300986 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300987
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200988 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100989 &g4x_wm_info, pessimal_latency_ns,
990 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300991 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200992 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300993
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200994 if (g4x_compute_wm0(dev, PIPE_B,
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 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200998 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300999
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001000 if (single_plane_enabled(enabled) &&
1001 g4x_compute_srwm(dev, ffs(enabled) - 1,
1002 sr_latency_ns,
1003 &g4x_wm_info,
1004 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001005 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001006 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001007 } else {
Imre Deak98584252014-06-13 14:54:20 +03001008 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001009 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001010 plane_sr = cursor_sr = 0;
1011 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001012
Ville Syrjäläa5043452014-06-28 02:04:18 +03001013 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1014 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001015 planea_wm, cursora_wm,
1016 planeb_wm, cursorb_wm,
1017 plane_sr, cursor_sr);
1018
1019 I915_WRITE(DSPFW1,
1020 (plane_sr << DSPFW_SR_SHIFT) |
1021 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1022 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001023 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001024 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001025 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001026 (cursora_wm << DSPFW_CURSORA_SHIFT));
1027 /* HPLL off in SR has some issues on G4x... disable it */
1028 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001029 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001030 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001031
1032 if (cxsr_enabled)
1033 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001034}
1035
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001036static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001037{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001038 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001039 struct drm_i915_private *dev_priv = dev->dev_private;
1040 struct drm_crtc *crtc;
1041 int srwm = 1;
1042 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001043 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001044
1045 /* Calc sr entries for one plane configs */
1046 crtc = single_enabled_crtc(dev);
1047 if (crtc) {
1048 /* self-refresh has much higher latency */
1049 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001050 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001051 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001052 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001053 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001054 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001055 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001056 unsigned long line_time_us;
1057 int entries;
1058
Ville Syrjälä922044c2014-02-14 14:18:57 +02001059 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001060
1061 /* Use ns/us then divide to preserve precision */
1062 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1063 pixel_size * hdisplay;
1064 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1065 srwm = I965_FIFO_SIZE - entries;
1066 if (srwm < 0)
1067 srwm = 1;
1068 srwm &= 0x1ff;
1069 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1070 entries, srwm);
1071
1072 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001073 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001074 entries = DIV_ROUND_UP(entries,
1075 i965_cursor_wm_info.cacheline_size);
1076 cursor_sr = i965_cursor_wm_info.fifo_size -
1077 (entries + i965_cursor_wm_info.guard_size);
1078
1079 if (cursor_sr > i965_cursor_wm_info.max_wm)
1080 cursor_sr = i965_cursor_wm_info.max_wm;
1081
1082 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1083 "cursor %d\n", srwm, cursor_sr);
1084
Imre Deak98584252014-06-13 14:54:20 +03001085 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001086 } else {
Imre Deak98584252014-06-13 14:54:20 +03001087 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001088 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001089 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001090 }
1091
1092 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1093 srwm);
1094
1095 /* 965 has limitations... */
1096 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001097 (8 << DSPFW_CURSORB_SHIFT) |
1098 (8 << DSPFW_PLANEB_SHIFT) |
1099 (8 << DSPFW_PLANEA_SHIFT));
1100 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1101 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001102 /* update cursor SR watermark */
1103 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001104
1105 if (cxsr_enabled)
1106 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001107}
1108
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001109static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001110{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001111 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001112 struct drm_i915_private *dev_priv = dev->dev_private;
1113 const struct intel_watermark_params *wm_info;
1114 uint32_t fwater_lo;
1115 uint32_t fwater_hi;
1116 int cwm, srwm = 1;
1117 int fifo_size;
1118 int planea_wm, planeb_wm;
1119 struct drm_crtc *crtc, *enabled = NULL;
1120
1121 if (IS_I945GM(dev))
1122 wm_info = &i945_wm_info;
1123 else if (!IS_GEN2(dev))
1124 wm_info = &i915_wm_info;
1125 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001126 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001127
1128 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1129 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001130 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001131 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001132 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001133 if (IS_GEN2(dev))
1134 cpp = 4;
1135
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001136 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001137 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001138 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001139 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001140 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001141 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001142 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001143 if (planea_wm > (long)wm_info->max_wm)
1144 planea_wm = wm_info->max_wm;
1145 }
1146
1147 if (IS_GEN2(dev))
1148 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001149
1150 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1151 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001152 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001153 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001154 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001155 if (IS_GEN2(dev))
1156 cpp = 4;
1157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001158 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001159 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001160 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001161 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001162 if (enabled == NULL)
1163 enabled = crtc;
1164 else
1165 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001166 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001167 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001168 if (planeb_wm > (long)wm_info->max_wm)
1169 planeb_wm = wm_info->max_wm;
1170 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001171
1172 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1173
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001174 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001175 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001176
Matt Roper2ff8fde2014-07-08 07:50:07 -07001177 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001178
1179 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001180 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001181 enabled = NULL;
1182 }
1183
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001184 /*
1185 * Overlay gets an aggressive default since video jitter is bad.
1186 */
1187 cwm = 2;
1188
1189 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001190 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001191
1192 /* Calc sr entries for one plane configs */
1193 if (HAS_FW_BLC(dev) && enabled) {
1194 /* self-refresh has much higher latency */
1195 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001196 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001197 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001198 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001199 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001200 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001201 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001202 unsigned long line_time_us;
1203 int entries;
1204
Ville Syrjälä922044c2014-02-14 14:18:57 +02001205 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001206
1207 /* Use ns/us then divide to preserve precision */
1208 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1209 pixel_size * hdisplay;
1210 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1211 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1212 srwm = wm_info->fifo_size - entries;
1213 if (srwm < 0)
1214 srwm = 1;
1215
1216 if (IS_I945G(dev) || IS_I945GM(dev))
1217 I915_WRITE(FW_BLC_SELF,
1218 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1219 else if (IS_I915GM(dev))
1220 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1221 }
1222
1223 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1224 planea_wm, planeb_wm, cwm, srwm);
1225
1226 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1227 fwater_hi = (cwm & 0x1f);
1228
1229 /* Set request length to 8 cachelines per fetch */
1230 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1231 fwater_hi = fwater_hi | (1 << 8);
1232
1233 I915_WRITE(FW_BLC, fwater_lo);
1234 I915_WRITE(FW_BLC2, fwater_hi);
1235
Imre Deak5209b1f2014-07-01 12:36:17 +03001236 if (enabled)
1237 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001238}
1239
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001240static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001241{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001242 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001243 struct drm_i915_private *dev_priv = dev->dev_private;
1244 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001245 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001246 uint32_t fwater_lo;
1247 int planea_wm;
1248
1249 crtc = single_enabled_crtc(dev);
1250 if (crtc == NULL)
1251 return;
1252
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001253 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001254 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001255 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001256 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001257 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001258 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1259 fwater_lo |= (3<<8) | planea_wm;
1260
1261 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1262
1263 I915_WRITE(FW_BLC, fwater_lo);
1264}
1265
Ville Syrjälä36587292013-07-05 11:57:16 +03001266static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1267 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001268{
1269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001270 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001271
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001272 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001273
1274 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1275 * adjust the pixel_rate here. */
1276
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001277 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001278 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001279 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001280
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001281 pipe_w = intel_crtc->config->pipe_src_w;
1282 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001283 pfit_w = (pfit_size >> 16) & 0xFFFF;
1284 pfit_h = pfit_size & 0xFFFF;
1285 if (pipe_w < pfit_w)
1286 pipe_w = pfit_w;
1287 if (pipe_h < pfit_h)
1288 pipe_h = pfit_h;
1289
1290 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1291 pfit_w * pfit_h);
1292 }
1293
1294 return pixel_rate;
1295}
1296
Ville Syrjälä37126462013-08-01 16:18:55 +03001297/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001298static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001299 uint32_t latency)
1300{
1301 uint64_t ret;
1302
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001303 if (WARN(latency == 0, "Latency value missing\n"))
1304 return UINT_MAX;
1305
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001306 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1307 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1308
1309 return ret;
1310}
1311
Ville Syrjälä37126462013-08-01 16:18:55 +03001312/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001313static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001314 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1315 uint32_t latency)
1316{
1317 uint32_t ret;
1318
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001319 if (WARN(latency == 0, "Latency value missing\n"))
1320 return UINT_MAX;
1321
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001322 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1323 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1324 ret = DIV_ROUND_UP(ret, 64) + 2;
1325 return ret;
1326}
1327
Ville Syrjälä23297042013-07-05 11:57:17 +03001328static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001329 uint8_t bytes_per_pixel)
1330{
1331 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1332}
1333
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001334struct skl_pipe_wm_parameters {
1335 bool active;
1336 uint32_t pipe_htotal;
1337 uint32_t pixel_rate; /* in KHz */
1338 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1339 struct intel_plane_wm_parameters cursor;
1340};
1341
Imre Deak820c1982013-12-17 14:46:36 +02001342struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001343 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001344 uint32_t pipe_htotal;
1345 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001346 struct intel_plane_wm_parameters pri;
1347 struct intel_plane_wm_parameters spr;
1348 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001349};
1350
Imre Deak820c1982013-12-17 14:46:36 +02001351struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001352 uint16_t pri;
1353 uint16_t spr;
1354 uint16_t cur;
1355 uint16_t fbc;
1356};
1357
Ville Syrjälä240264f2013-08-07 13:29:12 +03001358/* used in computing the new watermarks state */
1359struct intel_wm_config {
1360 unsigned int num_pipes_active;
1361 bool sprites_enabled;
1362 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001363};
1364
Ville Syrjälä37126462013-08-01 16:18:55 +03001365/*
1366 * For both WM_PIPE and WM_LP.
1367 * mem_value must be in 0.1us units.
1368 */
Imre Deak820c1982013-12-17 14:46:36 +02001369static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001370 uint32_t mem_value,
1371 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001372{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001373 uint32_t method1, method2;
1374
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001375 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001376 return 0;
1377
Ville Syrjälä23297042013-07-05 11:57:17 +03001378 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001379 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001380 mem_value);
1381
1382 if (!is_lp)
1383 return method1;
1384
Ville Syrjälä23297042013-07-05 11:57:17 +03001385 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001386 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001387 params->pri.horiz_pixels,
1388 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001389 mem_value);
1390
1391 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001392}
1393
Ville Syrjälä37126462013-08-01 16:18:55 +03001394/*
1395 * For both WM_PIPE and WM_LP.
1396 * mem_value must be in 0.1us units.
1397 */
Imre Deak820c1982013-12-17 14:46:36 +02001398static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001399 uint32_t mem_value)
1400{
1401 uint32_t method1, method2;
1402
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001403 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001404 return 0;
1405
Ville Syrjälä23297042013-07-05 11:57:17 +03001406 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001407 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001408 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001409 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001410 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001411 params->spr.horiz_pixels,
1412 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001413 mem_value);
1414 return min(method1, method2);
1415}
1416
Ville Syrjälä37126462013-08-01 16:18:55 +03001417/*
1418 * For both WM_PIPE and WM_LP.
1419 * mem_value must be in 0.1us units.
1420 */
Imre Deak820c1982013-12-17 14:46:36 +02001421static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001422 uint32_t mem_value)
1423{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001424 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001425 return 0;
1426
Ville Syrjälä23297042013-07-05 11:57:17 +03001427 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001428 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001429 params->cur.horiz_pixels,
1430 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001431 mem_value);
1432}
1433
Paulo Zanonicca32e92013-05-31 11:45:06 -03001434/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001435static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001436 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001437{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001438 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001439 return 0;
1440
Ville Syrjälä23297042013-07-05 11:57:17 +03001441 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001442 params->pri.horiz_pixels,
1443 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001444}
1445
Ville Syrjälä158ae642013-08-07 13:28:19 +03001446static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1447{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001448 if (INTEL_INFO(dev)->gen >= 8)
1449 return 3072;
1450 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001451 return 768;
1452 else
1453 return 512;
1454}
1455
Ville Syrjälä4e975082014-03-07 18:32:11 +02001456static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1457 int level, bool is_sprite)
1458{
1459 if (INTEL_INFO(dev)->gen >= 8)
1460 /* BDW primary/sprite plane watermarks */
1461 return level == 0 ? 255 : 2047;
1462 else if (INTEL_INFO(dev)->gen >= 7)
1463 /* IVB/HSW primary/sprite plane watermarks */
1464 return level == 0 ? 127 : 1023;
1465 else if (!is_sprite)
1466 /* ILK/SNB primary plane watermarks */
1467 return level == 0 ? 127 : 511;
1468 else
1469 /* ILK/SNB sprite plane watermarks */
1470 return level == 0 ? 63 : 255;
1471}
1472
1473static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1474 int level)
1475{
1476 if (INTEL_INFO(dev)->gen >= 7)
1477 return level == 0 ? 63 : 255;
1478 else
1479 return level == 0 ? 31 : 63;
1480}
1481
1482static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1483{
1484 if (INTEL_INFO(dev)->gen >= 8)
1485 return 31;
1486 else
1487 return 15;
1488}
1489
Ville Syrjälä158ae642013-08-07 13:28:19 +03001490/* Calculate the maximum primary/sprite plane watermark */
1491static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1492 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001493 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001494 enum intel_ddb_partitioning ddb_partitioning,
1495 bool is_sprite)
1496{
1497 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001498
1499 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001500 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001501 return 0;
1502
1503 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001504 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001505 fifo_size /= INTEL_INFO(dev)->num_pipes;
1506
1507 /*
1508 * For some reason the non self refresh
1509 * FIFO size is only half of the self
1510 * refresh FIFO size on ILK/SNB.
1511 */
1512 if (INTEL_INFO(dev)->gen <= 6)
1513 fifo_size /= 2;
1514 }
1515
Ville Syrjälä240264f2013-08-07 13:29:12 +03001516 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001517 /* level 0 is always calculated with 1:1 split */
1518 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1519 if (is_sprite)
1520 fifo_size *= 5;
1521 fifo_size /= 6;
1522 } else {
1523 fifo_size /= 2;
1524 }
1525 }
1526
1527 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001528 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001529}
1530
1531/* Calculate the maximum cursor plane watermark */
1532static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001533 int level,
1534 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001535{
1536 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001537 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001538 return 64;
1539
1540 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001541 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001542}
1543
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001544static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001545 int level,
1546 const struct intel_wm_config *config,
1547 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001548 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001549{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001550 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1551 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1552 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001553 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001554}
1555
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001556static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1557 int level,
1558 struct ilk_wm_maximums *max)
1559{
1560 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1561 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1562 max->cur = ilk_cursor_wm_reg_max(dev, level);
1563 max->fbc = ilk_fbc_wm_reg_max(dev);
1564}
1565
Ville Syrjäläd9395652013-10-09 19:18:10 +03001566static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001567 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001568 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001569{
1570 bool ret;
1571
1572 /* already determined to be invalid? */
1573 if (!result->enable)
1574 return false;
1575
1576 result->enable = result->pri_val <= max->pri &&
1577 result->spr_val <= max->spr &&
1578 result->cur_val <= max->cur;
1579
1580 ret = result->enable;
1581
1582 /*
1583 * HACK until we can pre-compute everything,
1584 * and thus fail gracefully if LP0 watermarks
1585 * are exceeded...
1586 */
1587 if (level == 0 && !result->enable) {
1588 if (result->pri_val > max->pri)
1589 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1590 level, result->pri_val, max->pri);
1591 if (result->spr_val > max->spr)
1592 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1593 level, result->spr_val, max->spr);
1594 if (result->cur_val > max->cur)
1595 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1596 level, result->cur_val, max->cur);
1597
1598 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1599 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1600 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1601 result->enable = true;
1602 }
1603
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001604 return ret;
1605}
1606
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001607static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001608 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001609 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001610 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001611{
1612 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1613 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1614 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1615
1616 /* WM1+ latency values stored in 0.5us units */
1617 if (level > 0) {
1618 pri_latency *= 5;
1619 spr_latency *= 5;
1620 cur_latency *= 5;
1621 }
1622
1623 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1624 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1625 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1626 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1627 result->enable = true;
1628}
1629
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001630static uint32_t
1631hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001632{
1633 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001635 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001636 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001637
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001638 if (!intel_crtc_active(crtc))
1639 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001640
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001641 /* The WM are computed with base on how long it takes to fill a single
1642 * row at the given clock rate, multiplied by 8.
1643 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001644 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1645 mode->crtc_clock);
1646 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001647 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001648
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001649 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1650 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001651}
1652
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001653static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001654{
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001657 if (IS_GEN9(dev)) {
1658 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001659 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001660 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001661
1662 /* read the first set of memory latencies[0:3] */
1663 val = 0; /* data0 to be programmed to 0 for first set */
1664 mutex_lock(&dev_priv->rps.hw_lock);
1665 ret = sandybridge_pcode_read(dev_priv,
1666 GEN9_PCODE_READ_MEM_LATENCY,
1667 &val);
1668 mutex_unlock(&dev_priv->rps.hw_lock);
1669
1670 if (ret) {
1671 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1672 return;
1673 }
1674
1675 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1676 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1677 GEN9_MEM_LATENCY_LEVEL_MASK;
1678 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1679 GEN9_MEM_LATENCY_LEVEL_MASK;
1680 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1681 GEN9_MEM_LATENCY_LEVEL_MASK;
1682
1683 /* read the second set of memory latencies[4:7] */
1684 val = 1; /* data0 to be programmed to 1 for second set */
1685 mutex_lock(&dev_priv->rps.hw_lock);
1686 ret = sandybridge_pcode_read(dev_priv,
1687 GEN9_PCODE_READ_MEM_LATENCY,
1688 &val);
1689 mutex_unlock(&dev_priv->rps.hw_lock);
1690 if (ret) {
1691 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1692 return;
1693 }
1694
1695 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1696 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1697 GEN9_MEM_LATENCY_LEVEL_MASK;
1698 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1699 GEN9_MEM_LATENCY_LEVEL_MASK;
1700 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1701 GEN9_MEM_LATENCY_LEVEL_MASK;
1702
Vandana Kannan367294b2014-11-04 17:06:46 +00001703 /*
Damien Lespiau6f972352015-02-09 19:33:07 +00001704 * WaWmMemoryReadLatency:skl
1705 *
Vandana Kannan367294b2014-11-04 17:06:46 +00001706 * punit doesn't take into account the read latency so we need
1707 * to add 2us to the various latency levels we retrieve from
1708 * the punit.
1709 * - W0 is a bit special in that it's the only level that
1710 * can't be disabled if we want to have display working, so
1711 * we always add 2us there.
1712 * - For levels >=1, punit returns 0us latency when they are
1713 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001714 *
1715 * Additionally, if a level n (n > 1) has a 0us latency, all
1716 * levels m (m >= n) need to be disabled. We make sure to
1717 * sanitize the values out of the punit to satisfy this
1718 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001719 */
1720 wm[0] += 2;
1721 for (level = 1; level <= max_level; level++)
1722 if (wm[level] != 0)
1723 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001724 else {
1725 for (i = level + 1; i <= max_level; i++)
1726 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001727
Vandana Kannan4f947382014-11-04 17:06:47 +00001728 break;
1729 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001730 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001731 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1732
1733 wm[0] = (sskpd >> 56) & 0xFF;
1734 if (wm[0] == 0)
1735 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001736 wm[1] = (sskpd >> 4) & 0xFF;
1737 wm[2] = (sskpd >> 12) & 0xFF;
1738 wm[3] = (sskpd >> 20) & 0x1FF;
1739 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001740 } else if (INTEL_INFO(dev)->gen >= 6) {
1741 uint32_t sskpd = I915_READ(MCH_SSKPD);
1742
1743 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1744 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1745 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1746 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001747 } else if (INTEL_INFO(dev)->gen >= 5) {
1748 uint32_t mltr = I915_READ(MLTR_ILK);
1749
1750 /* ILK primary LP0 latency is 700 ns */
1751 wm[0] = 7;
1752 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1753 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001754 }
1755}
1756
Ville Syrjälä53615a52013-08-01 16:18:50 +03001757static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1758{
1759 /* ILK sprite LP0 latency is 1300 ns */
1760 if (INTEL_INFO(dev)->gen == 5)
1761 wm[0] = 13;
1762}
1763
1764static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1765{
1766 /* ILK cursor LP0 latency is 1300 ns */
1767 if (INTEL_INFO(dev)->gen == 5)
1768 wm[0] = 13;
1769
1770 /* WaDoubleCursorLP3Latency:ivb */
1771 if (IS_IVYBRIDGE(dev))
1772 wm[3] *= 2;
1773}
1774
Damien Lespiau546c81f2014-05-13 15:30:26 +01001775int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001776{
1777 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001778 if (IS_GEN9(dev))
1779 return 7;
1780 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001781 return 4;
1782 else if (INTEL_INFO(dev)->gen >= 6)
1783 return 3;
1784 else
1785 return 2;
1786}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001787
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001788static void intel_print_wm_latency(struct drm_device *dev,
1789 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001790 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001791{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001792 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001793
1794 for (level = 0; level <= max_level; level++) {
1795 unsigned int latency = wm[level];
1796
1797 if (latency == 0) {
1798 DRM_ERROR("%s WM%d latency not provided\n",
1799 name, level);
1800 continue;
1801 }
1802
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001803 /*
1804 * - latencies are in us on gen9.
1805 * - before then, WM1+ latency values are in 0.5us units
1806 */
1807 if (IS_GEN9(dev))
1808 latency *= 10;
1809 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001810 latency *= 5;
1811
1812 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1813 name, level, wm[level],
1814 latency / 10, latency % 10);
1815 }
1816}
1817
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001818static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1819 uint16_t wm[5], uint16_t min)
1820{
1821 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1822
1823 if (wm[0] >= min)
1824 return false;
1825
1826 wm[0] = max(wm[0], min);
1827 for (level = 1; level <= max_level; level++)
1828 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1829
1830 return true;
1831}
1832
1833static void snb_wm_latency_quirk(struct drm_device *dev)
1834{
1835 struct drm_i915_private *dev_priv = dev->dev_private;
1836 bool changed;
1837
1838 /*
1839 * The BIOS provided WM memory latency values are often
1840 * inadequate for high resolution displays. Adjust them.
1841 */
1842 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1843 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1844 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1845
1846 if (!changed)
1847 return;
1848
1849 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1850 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1851 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1852 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1853}
1854
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001855static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001856{
1857 struct drm_i915_private *dev_priv = dev->dev_private;
1858
1859 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1860
1861 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1862 sizeof(dev_priv->wm.pri_latency));
1863 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1864 sizeof(dev_priv->wm.pri_latency));
1865
1866 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1867 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001868
1869 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1870 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1871 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001872
1873 if (IS_GEN6(dev))
1874 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001875}
1876
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001877static void skl_setup_wm_latency(struct drm_device *dev)
1878{
1879 struct drm_i915_private *dev_priv = dev->dev_private;
1880
1881 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1882 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1883}
1884
Imre Deak820c1982013-12-17 14:46:36 +02001885static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001886 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001887{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001888 struct drm_device *dev = crtc->dev;
1889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1890 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001891 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001892
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001893 if (!intel_crtc_active(crtc))
1894 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001895
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001896 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001897 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001898 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
1899 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
1900 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001901 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001902 p->cur.horiz_pixels = intel_crtc->cursor_width;
1903 /* TODO: for now, assume primary and cursor planes are always enabled. */
1904 p->pri.enabled = true;
1905 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001906
Matt Roperaf2b6532014-04-01 15:22:32 -07001907 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001908 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001909
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001910 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001911 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001912 break;
1913 }
1914 }
1915}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001916
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001917static void ilk_compute_wm_config(struct drm_device *dev,
1918 struct intel_wm_config *config)
1919{
1920 struct intel_crtc *intel_crtc;
1921
1922 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01001923 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001924 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
1925
1926 if (!wm->pipe_enabled)
1927 continue;
1928
1929 config->sprites_enabled |= wm->sprites_enabled;
1930 config->sprites_scaled |= wm->sprites_scaled;
1931 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001932 }
1933}
1934
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001935/* Compute new watermarks for the pipe */
1936static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02001937 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001938 struct intel_pipe_wm *pipe_wm)
1939{
1940 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001941 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001942 int level, max_level = ilk_wm_max_level(dev);
1943 /* LP0 watermark maximums depend on this pipe alone */
1944 struct intel_wm_config config = {
1945 .num_pipes_active = 1,
1946 .sprites_enabled = params->spr.enabled,
1947 .sprites_scaled = params->spr.scaled,
1948 };
Imre Deak820c1982013-12-17 14:46:36 +02001949 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001950
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001951 pipe_wm->pipe_enabled = params->active;
1952 pipe_wm->sprites_enabled = params->spr.enabled;
1953 pipe_wm->sprites_scaled = params->spr.scaled;
1954
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02001955 /* ILK/SNB: LP2+ watermarks only w/o sprites */
1956 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
1957 max_level = 1;
1958
1959 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
1960 if (params->spr.scaled)
1961 max_level = 0;
1962
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001963 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001964
Ville Syrjäläa42a5712014-01-07 16:14:08 +02001965 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02001966 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001967
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001968 /* LP0 watermarks always use 1/2 DDB partitioning */
1969 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
1970
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001971 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001972 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
1973 return false;
1974
1975 ilk_compute_wm_reg_maximums(dev, 1, &max);
1976
1977 for (level = 1; level <= max_level; level++) {
1978 struct intel_wm_level wm = {};
1979
1980 ilk_compute_wm_level(dev_priv, level, params, &wm);
1981
1982 /*
1983 * Disable any watermark level that exceeds the
1984 * register maximums since such watermarks are
1985 * always invalid.
1986 */
1987 if (!ilk_validate_wm_level(level, &max, &wm))
1988 break;
1989
1990 pipe_wm->wm[level] = wm;
1991 }
1992
1993 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001994}
1995
1996/*
1997 * Merge the watermarks from all active pipes for a specific level.
1998 */
1999static void ilk_merge_wm_level(struct drm_device *dev,
2000 int level,
2001 struct intel_wm_level *ret_wm)
2002{
2003 const struct intel_crtc *intel_crtc;
2004
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002005 ret_wm->enable = true;
2006
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002007 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002008 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2009 const struct intel_wm_level *wm = &active->wm[level];
2010
2011 if (!active->pipe_enabled)
2012 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002013
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002014 /*
2015 * The watermark values may have been used in the past,
2016 * so we must maintain them in the registers for some
2017 * time even if the level is now disabled.
2018 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002019 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002020 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002021
2022 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2023 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2024 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2025 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2026 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002027}
2028
2029/*
2030 * Merge all low power watermarks for all active pipes.
2031 */
2032static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002033 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002034 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002035 struct intel_pipe_wm *merged)
2036{
2037 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002038 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002039
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002040 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2041 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2042 config->num_pipes_active > 1)
2043 return;
2044
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002045 /* ILK: FBC WM must be disabled always */
2046 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002047
2048 /* merge each WM1+ level */
2049 for (level = 1; level <= max_level; level++) {
2050 struct intel_wm_level *wm = &merged->wm[level];
2051
2052 ilk_merge_wm_level(dev, level, wm);
2053
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002054 if (level > last_enabled_level)
2055 wm->enable = false;
2056 else if (!ilk_validate_wm_level(level, max, wm))
2057 /* make sure all following levels get disabled */
2058 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002059
2060 /*
2061 * The spec says it is preferred to disable
2062 * FBC WMs instead of disabling a WM level.
2063 */
2064 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002065 if (wm->enable)
2066 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002067 wm->fbc_val = 0;
2068 }
2069 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002070
2071 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2072 /*
2073 * FIXME this is racy. FBC might get enabled later.
2074 * What we should check here is whether FBC can be
2075 * enabled sometime later.
2076 */
2077 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2078 for (level = 2; level <= max_level; level++) {
2079 struct intel_wm_level *wm = &merged->wm[level];
2080
2081 wm->enable = false;
2082 }
2083 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002084}
2085
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002086static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2087{
2088 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2089 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2090}
2091
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002092/* The value we need to program into the WM_LPx latency field */
2093static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2094{
2095 struct drm_i915_private *dev_priv = dev->dev_private;
2096
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002097 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002098 return 2 * level;
2099 else
2100 return dev_priv->wm.pri_latency[level];
2101}
2102
Imre Deak820c1982013-12-17 14:46:36 +02002103static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002104 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002105 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002106 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002107{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002108 struct intel_crtc *intel_crtc;
2109 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002110
Ville Syrjälä0362c782013-10-09 19:17:57 +03002111 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002112 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002113
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002114 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002115 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002116 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002117
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002118 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002119
Ville Syrjälä0362c782013-10-09 19:17:57 +03002120 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002121
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002122 /*
2123 * Maintain the watermark values even if the level is
2124 * disabled. Doing otherwise could cause underruns.
2125 */
2126 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002127 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002128 (r->pri_val << WM1_LP_SR_SHIFT) |
2129 r->cur_val;
2130
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002131 if (r->enable)
2132 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2133
Ville Syrjälä416f4722013-11-02 21:07:46 -07002134 if (INTEL_INFO(dev)->gen >= 8)
2135 results->wm_lp[wm_lp - 1] |=
2136 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2137 else
2138 results->wm_lp[wm_lp - 1] |=
2139 r->fbc_val << WM1_LP_FBC_SHIFT;
2140
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002141 /*
2142 * Always set WM1S_LP_EN when spr_val != 0, even if the
2143 * level is disabled. Doing otherwise could cause underruns.
2144 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002145 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2146 WARN_ON(wm_lp != 1);
2147 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2148 } else
2149 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002150 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002151
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002152 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002153 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002154 enum pipe pipe = intel_crtc->pipe;
2155 const struct intel_wm_level *r =
2156 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002157
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002158 if (WARN_ON(!r->enable))
2159 continue;
2160
2161 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2162
2163 results->wm_pipe[pipe] =
2164 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2165 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2166 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002167 }
2168}
2169
Paulo Zanoni861f3382013-05-31 10:19:21 -03002170/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2171 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002172static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002173 struct intel_pipe_wm *r1,
2174 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002175{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002176 int level, max_level = ilk_wm_max_level(dev);
2177 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002178
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002179 for (level = 1; level <= max_level; level++) {
2180 if (r1->wm[level].enable)
2181 level1 = level;
2182 if (r2->wm[level].enable)
2183 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002184 }
2185
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002186 if (level1 == level2) {
2187 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002188 return r2;
2189 else
2190 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002191 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002192 return r1;
2193 } else {
2194 return r2;
2195 }
2196}
2197
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002198/* dirty bits used to track which watermarks need changes */
2199#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2200#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2201#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2202#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2203#define WM_DIRTY_FBC (1 << 24)
2204#define WM_DIRTY_DDB (1 << 25)
2205
Damien Lespiau055e3932014-08-18 13:49:10 +01002206static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002207 const struct ilk_wm_values *old,
2208 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002209{
2210 unsigned int dirty = 0;
2211 enum pipe pipe;
2212 int wm_lp;
2213
Damien Lespiau055e3932014-08-18 13:49:10 +01002214 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002215 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2216 dirty |= WM_DIRTY_LINETIME(pipe);
2217 /* Must disable LP1+ watermarks too */
2218 dirty |= WM_DIRTY_LP_ALL;
2219 }
2220
2221 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2222 dirty |= WM_DIRTY_PIPE(pipe);
2223 /* Must disable LP1+ watermarks too */
2224 dirty |= WM_DIRTY_LP_ALL;
2225 }
2226 }
2227
2228 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2229 dirty |= WM_DIRTY_FBC;
2230 /* Must disable LP1+ watermarks too */
2231 dirty |= WM_DIRTY_LP_ALL;
2232 }
2233
2234 if (old->partitioning != new->partitioning) {
2235 dirty |= WM_DIRTY_DDB;
2236 /* Must disable LP1+ watermarks too */
2237 dirty |= WM_DIRTY_LP_ALL;
2238 }
2239
2240 /* LP1+ watermarks already deemed dirty, no need to continue */
2241 if (dirty & WM_DIRTY_LP_ALL)
2242 return dirty;
2243
2244 /* Find the lowest numbered LP1+ watermark in need of an update... */
2245 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2246 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2247 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2248 break;
2249 }
2250
2251 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2252 for (; wm_lp <= 3; wm_lp++)
2253 dirty |= WM_DIRTY_LP(wm_lp);
2254
2255 return dirty;
2256}
2257
Ville Syrjälä8553c182013-12-05 15:51:39 +02002258static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2259 unsigned int dirty)
2260{
Imre Deak820c1982013-12-17 14:46:36 +02002261 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002262 bool changed = false;
2263
2264 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2265 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2266 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2267 changed = true;
2268 }
2269 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2270 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2271 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2272 changed = true;
2273 }
2274 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2275 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2276 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2277 changed = true;
2278 }
2279
2280 /*
2281 * Don't touch WM1S_LP_EN here.
2282 * Doing so could cause underruns.
2283 */
2284
2285 return changed;
2286}
2287
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002288/*
2289 * The spec says we shouldn't write when we don't need, because every write
2290 * causes WMs to be re-evaluated, expending some power.
2291 */
Imre Deak820c1982013-12-17 14:46:36 +02002292static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2293 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002294{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002295 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002296 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002297 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002298 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002299
Damien Lespiau055e3932014-08-18 13:49:10 +01002300 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002301 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002302 return;
2303
Ville Syrjälä8553c182013-12-05 15:51:39 +02002304 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002305
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002306 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002307 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002308 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002309 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002310 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002311 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2312
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002313 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002314 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002315 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002316 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002317 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002318 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2319
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002320 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002321 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002322 val = I915_READ(WM_MISC);
2323 if (results->partitioning == INTEL_DDB_PART_1_2)
2324 val &= ~WM_MISC_DATA_PARTITION_5_6;
2325 else
2326 val |= WM_MISC_DATA_PARTITION_5_6;
2327 I915_WRITE(WM_MISC, val);
2328 } else {
2329 val = I915_READ(DISP_ARB_CTL2);
2330 if (results->partitioning == INTEL_DDB_PART_1_2)
2331 val &= ~DISP_DATA_PARTITION_5_6;
2332 else
2333 val |= DISP_DATA_PARTITION_5_6;
2334 I915_WRITE(DISP_ARB_CTL2, val);
2335 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002336 }
2337
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002338 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002339 val = I915_READ(DISP_ARB_CTL);
2340 if (results->enable_fbc_wm)
2341 val &= ~DISP_FBC_WM_DIS;
2342 else
2343 val |= DISP_FBC_WM_DIS;
2344 I915_WRITE(DISP_ARB_CTL, val);
2345 }
2346
Imre Deak954911e2013-12-17 14:46:34 +02002347 if (dirty & WM_DIRTY_LP(1) &&
2348 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2349 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2350
2351 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002352 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2353 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2354 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2355 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2356 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002357
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002358 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002359 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002360 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002361 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002362 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002363 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002364
2365 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002366}
2367
Ville Syrjälä8553c182013-12-05 15:51:39 +02002368static bool ilk_disable_lp_wm(struct drm_device *dev)
2369{
2370 struct drm_i915_private *dev_priv = dev->dev_private;
2371
2372 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2373}
2374
Damien Lespiaub9cec072014-11-04 17:06:43 +00002375/*
2376 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2377 * different active planes.
2378 */
2379
2380#define SKL_DDB_SIZE 896 /* in blocks */
2381
2382static void
2383skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2384 struct drm_crtc *for_crtc,
2385 const struct intel_wm_config *config,
2386 const struct skl_pipe_wm_parameters *params,
2387 struct skl_ddb_entry *alloc /* out */)
2388{
2389 struct drm_crtc *crtc;
2390 unsigned int pipe_size, ddb_size;
2391 int nth_active_pipe;
2392
2393 if (!params->active) {
2394 alloc->start = 0;
2395 alloc->end = 0;
2396 return;
2397 }
2398
2399 ddb_size = SKL_DDB_SIZE;
2400
2401 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2402
2403 nth_active_pipe = 0;
2404 for_each_crtc(dev, crtc) {
2405 if (!intel_crtc_active(crtc))
2406 continue;
2407
2408 if (crtc == for_crtc)
2409 break;
2410
2411 nth_active_pipe++;
2412 }
2413
2414 pipe_size = ddb_size / config->num_pipes_active;
2415 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002416 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002417}
2418
2419static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2420{
2421 if (config->num_pipes_active == 1)
2422 return 32;
2423
2424 return 8;
2425}
2426
Damien Lespiaua269c582014-11-04 17:06:49 +00002427static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2428{
2429 entry->start = reg & 0x3ff;
2430 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002431 if (entry->end)
2432 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002433}
2434
Damien Lespiau08db6652014-11-04 17:06:52 +00002435void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2436 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002437{
2438 struct drm_device *dev = dev_priv->dev;
2439 enum pipe pipe;
2440 int plane;
2441 u32 val;
2442
2443 for_each_pipe(dev_priv, pipe) {
2444 for_each_plane(pipe, plane) {
2445 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2446 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2447 val);
2448 }
2449
2450 val = I915_READ(CUR_BUF_CFG(pipe));
2451 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2452 }
2453}
2454
Damien Lespiaub9cec072014-11-04 17:06:43 +00002455static unsigned int
2456skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2457{
2458 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2459}
2460
2461/*
2462 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2463 * a 8192x4096@32bpp framebuffer:
2464 * 3 * 4096 * 8192 * 4 < 2^32
2465 */
2466static unsigned int
2467skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2468 const struct skl_pipe_wm_parameters *params)
2469{
2470 unsigned int total_data_rate = 0;
2471 int plane;
2472
2473 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2474 const struct intel_plane_wm_parameters *p;
2475
2476 p = &params->plane[plane];
2477 if (!p->enabled)
2478 continue;
2479
2480 total_data_rate += skl_plane_relative_data_rate(p);
2481 }
2482
2483 return total_data_rate;
2484}
2485
2486static void
2487skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2488 const struct intel_wm_config *config,
2489 const struct skl_pipe_wm_parameters *params,
2490 struct skl_ddb_allocation *ddb /* out */)
2491{
2492 struct drm_device *dev = crtc->dev;
2493 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2494 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002495 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002496 uint16_t alloc_size, start, cursor_blocks;
2497 unsigned int total_data_rate;
2498 int plane;
2499
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002500 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2501 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002502 if (alloc_size == 0) {
2503 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2504 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2505 return;
2506 }
2507
2508 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002509 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2510 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002511
2512 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002513 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002514
2515 /*
2516 * Each active plane get a portion of the remaining space, in
2517 * proportion to the amount of data they need to fetch from memory.
2518 *
2519 * FIXME: we may not allocate every single block here.
2520 */
2521 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2522
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002523 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002524 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2525 const struct intel_plane_wm_parameters *p;
2526 unsigned int data_rate;
2527 uint16_t plane_blocks;
2528
2529 p = &params->plane[plane];
2530 if (!p->enabled)
2531 continue;
2532
2533 data_rate = skl_plane_relative_data_rate(p);
2534
2535 /*
2536 * promote the expression to 64 bits to avoid overflowing, the
2537 * result is < available as data_rate / total_data_rate < 1
2538 */
2539 plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
2540 total_data_rate);
2541
2542 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002543 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002544
2545 start += plane_blocks;
2546 }
2547
2548}
2549
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002550static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002551{
2552 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002553 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002554}
2555
2556/*
2557 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2558 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2559 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2560 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2561*/
2562static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2563 uint32_t latency)
2564{
2565 uint32_t wm_intermediate_val, ret;
2566
2567 if (latency == 0)
2568 return UINT_MAX;
2569
2570 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
2571 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2572
2573 return ret;
2574}
2575
2576static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2577 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
2578 uint32_t latency)
2579{
2580 uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
2581
2582 if (latency == 0)
2583 return UINT_MAX;
2584
2585 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
2586 wm_intermediate_val = latency * pixel_rate;
2587 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
2588 plane_bytes_per_line;
2589
2590 return ret;
2591}
2592
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002593static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2594 const struct intel_crtc *intel_crtc)
2595{
2596 struct drm_device *dev = intel_crtc->base.dev;
2597 struct drm_i915_private *dev_priv = dev->dev_private;
2598 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2599 enum pipe pipe = intel_crtc->pipe;
2600
2601 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2602 sizeof(new_ddb->plane[pipe])))
2603 return true;
2604
2605 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2606 sizeof(new_ddb->cursor[pipe])))
2607 return true;
2608
2609 return false;
2610}
2611
2612static void skl_compute_wm_global_parameters(struct drm_device *dev,
2613 struct intel_wm_config *config)
2614{
2615 struct drm_crtc *crtc;
2616 struct drm_plane *plane;
2617
2618 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2619 config->num_pipes_active += intel_crtc_active(crtc);
2620
2621 /* FIXME: I don't think we need those two global parameters on SKL */
2622 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2623 struct intel_plane *intel_plane = to_intel_plane(plane);
2624
2625 config->sprites_enabled |= intel_plane->wm.enabled;
2626 config->sprites_scaled |= intel_plane->wm.scaled;
2627 }
2628}
2629
2630static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2631 struct skl_pipe_wm_parameters *p)
2632{
2633 struct drm_device *dev = crtc->dev;
2634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2635 enum pipe pipe = intel_crtc->pipe;
2636 struct drm_plane *plane;
2637 int i = 1; /* Index for sprite planes start */
2638
2639 p->active = intel_crtc_active(crtc);
2640 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002641 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2642 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002643
2644 /*
2645 * For now, assume primary and cursor planes are always enabled.
2646 */
2647 p->plane[0].enabled = true;
2648 p->plane[0].bytes_per_pixel =
2649 crtc->primary->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002650 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2651 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002652
2653 p->cursor.enabled = true;
2654 p->cursor.bytes_per_pixel = 4;
2655 p->cursor.horiz_pixels = intel_crtc->cursor_width ?
2656 intel_crtc->cursor_width : 64;
2657 }
2658
2659 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2660 struct intel_plane *intel_plane = to_intel_plane(plane);
2661
Sonika Jindala712f8e2014-12-09 10:59:15 +05302662 if (intel_plane->pipe == pipe &&
2663 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002664 p->plane[i++] = intel_plane->wm;
2665 }
2666}
2667
2668static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002669 struct intel_plane_wm_parameters *p_params,
2670 uint16_t ddb_allocation,
2671 uint32_t mem_value,
2672 uint16_t *out_blocks, /* out */
2673 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002674{
Damien Lespiaue6d66172014-11-04 17:06:55 +00002675 uint32_t method1, method2, plane_bytes_per_line, res_blocks, res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002676 uint32_t result_bytes;
2677
Vandana Kannan4f947382014-11-04 17:06:47 +00002678 if (mem_value == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002679 return false;
2680
2681 method1 = skl_wm_method1(p->pixel_rate,
2682 p_params->bytes_per_pixel,
2683 mem_value);
2684 method2 = skl_wm_method2(p->pixel_rate,
2685 p->pipe_htotal,
2686 p_params->horiz_pixels,
2687 p_params->bytes_per_pixel,
2688 mem_value);
2689
2690 plane_bytes_per_line = p_params->horiz_pixels *
2691 p_params->bytes_per_pixel;
2692
2693 /* For now xtile and linear */
Damien Lespiau21fca252014-11-04 17:06:54 +00002694 if (((ddb_allocation * 512) / plane_bytes_per_line) >= 1)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002695 result_bytes = min(method1, method2);
2696 else
2697 result_bytes = method1;
2698
Damien Lespiaue6d66172014-11-04 17:06:55 +00002699 res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
2700 res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
2701
2702 if (res_blocks > ddb_allocation || res_lines > 31)
2703 return false;
2704
2705 *out_blocks = res_blocks;
2706 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002707
2708 return true;
2709}
2710
2711static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2712 struct skl_ddb_allocation *ddb,
2713 struct skl_pipe_wm_parameters *p,
2714 enum pipe pipe,
2715 int level,
2716 int num_planes,
2717 struct skl_wm_level *result)
2718{
2719 uint16_t latency = dev_priv->wm.skl_latency[level];
2720 uint16_t ddb_blocks;
2721 int i;
2722
2723 for (i = 0; i < num_planes; i++) {
2724 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2725
2726 result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
2727 ddb_blocks,
2728 latency,
2729 &result->plane_res_b[i],
2730 &result->plane_res_l[i]);
2731 }
2732
2733 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
2734 result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
2735 latency, &result->cursor_res_b,
2736 &result->cursor_res_l);
2737}
2738
Damien Lespiau407b50f2014-11-04 17:06:57 +00002739static uint32_t
2740skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2741{
2742 if (!intel_crtc_active(crtc))
2743 return 0;
2744
2745 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2746
2747}
2748
2749static void skl_compute_transition_wm(struct drm_crtc *crtc,
2750 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002751 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002752{
Damien Lespiau9414f562014-11-04 17:06:58 +00002753 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2754 int i;
2755
Damien Lespiau407b50f2014-11-04 17:06:57 +00002756 if (!params->active)
2757 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002758
2759 /* Until we know more, just disable transition WMs */
2760 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2761 trans_wm->plane_en[i] = false;
2762 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002763}
2764
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002765static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2766 struct skl_ddb_allocation *ddb,
2767 struct skl_pipe_wm_parameters *params,
2768 struct skl_pipe_wm *pipe_wm)
2769{
2770 struct drm_device *dev = crtc->dev;
2771 const struct drm_i915_private *dev_priv = dev->dev_private;
2772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2773 int level, max_level = ilk_wm_max_level(dev);
2774
2775 for (level = 0; level <= max_level; level++) {
2776 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2777 level, intel_num_planes(intel_crtc),
2778 &pipe_wm->wm[level]);
2779 }
2780 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2781
Damien Lespiau9414f562014-11-04 17:06:58 +00002782 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002783}
2784
2785static void skl_compute_wm_results(struct drm_device *dev,
2786 struct skl_pipe_wm_parameters *p,
2787 struct skl_pipe_wm *p_wm,
2788 struct skl_wm_values *r,
2789 struct intel_crtc *intel_crtc)
2790{
2791 int level, max_level = ilk_wm_max_level(dev);
2792 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002793 uint32_t temp;
2794 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002795
2796 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002797 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2798 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002799
2800 temp |= p_wm->wm[level].plane_res_l[i] <<
2801 PLANE_WM_LINES_SHIFT;
2802 temp |= p_wm->wm[level].plane_res_b[i];
2803 if (p_wm->wm[level].plane_en[i])
2804 temp |= PLANE_WM_EN;
2805
2806 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002807 }
2808
2809 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002810
2811 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2812 temp |= p_wm->wm[level].cursor_res_b;
2813
2814 if (p_wm->wm[level].cursor_en)
2815 temp |= PLANE_WM_EN;
2816
2817 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002818
2819 }
2820
Damien Lespiau9414f562014-11-04 17:06:58 +00002821 /* transition WMs */
2822 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2823 temp = 0;
2824 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2825 temp |= p_wm->trans_wm.plane_res_b[i];
2826 if (p_wm->trans_wm.plane_en[i])
2827 temp |= PLANE_WM_EN;
2828
2829 r->plane_trans[pipe][i] = temp;
2830 }
2831
2832 temp = 0;
2833 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2834 temp |= p_wm->trans_wm.cursor_res_b;
2835 if (p_wm->trans_wm.cursor_en)
2836 temp |= PLANE_WM_EN;
2837
2838 r->cursor_trans[pipe] = temp;
2839
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002840 r->wm_linetime[pipe] = p_wm->linetime;
2841}
2842
Damien Lespiau16160e32014-11-04 17:06:53 +00002843static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2844 const struct skl_ddb_entry *entry)
2845{
2846 if (entry->end)
2847 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2848 else
2849 I915_WRITE(reg, 0);
2850}
2851
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002852static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2853 const struct skl_wm_values *new)
2854{
2855 struct drm_device *dev = dev_priv->dev;
2856 struct intel_crtc *crtc;
2857
2858 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
2859 int i, level, max_level = ilk_wm_max_level(dev);
2860 enum pipe pipe = crtc->pipe;
2861
Damien Lespiau5d374d92014-11-04 17:07:00 +00002862 if (!new->dirty[pipe])
2863 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002864
Damien Lespiau5d374d92014-11-04 17:07:00 +00002865 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
2866
2867 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002868 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00002869 I915_WRITE(PLANE_WM(pipe, i, level),
2870 new->plane[pipe][i][level]);
2871 I915_WRITE(CUR_WM(pipe, level),
2872 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002873 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00002874 for (i = 0; i < intel_num_planes(crtc); i++)
2875 I915_WRITE(PLANE_WM_TRANS(pipe, i),
2876 new->plane_trans[pipe][i]);
2877 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
2878
2879 for (i = 0; i < intel_num_planes(crtc); i++)
2880 skl_ddb_entry_write(dev_priv,
2881 PLANE_BUF_CFG(pipe, i),
2882 &new->ddb.plane[pipe][i]);
2883
2884 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
2885 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002886 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002887}
2888
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002889/*
2890 * When setting up a new DDB allocation arrangement, we need to correctly
2891 * sequence the times at which the new allocations for the pipes are taken into
2892 * account or we'll have pipes fetching from space previously allocated to
2893 * another pipe.
2894 *
2895 * Roughly the sequence looks like:
2896 * 1. re-allocate the pipe(s) with the allocation being reduced and not
2897 * overlapping with a previous light-up pipe (another way to put it is:
2898 * pipes with their new allocation strickly included into their old ones).
2899 * 2. re-allocate the other pipes that get their allocation reduced
2900 * 3. allocate the pipes having their allocation increased
2901 *
2902 * Steps 1. and 2. are here to take care of the following case:
2903 * - Initially DDB looks like this:
2904 * | B | C |
2905 * - enable pipe A.
2906 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
2907 * allocation
2908 * | A | B | C |
2909 *
2910 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
2911 */
2912
Damien Lespiaud21b7952014-11-04 17:07:03 +00002913static void
2914skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002915{
2916 struct drm_device *dev = dev_priv->dev;
2917 int plane;
2918
Damien Lespiaud21b7952014-11-04 17:07:03 +00002919 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
2920
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002921 for_each_plane(pipe, plane) {
2922 I915_WRITE(PLANE_SURF(pipe, plane),
2923 I915_READ(PLANE_SURF(pipe, plane)));
2924 }
2925 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
2926}
2927
2928static bool
2929skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
2930 const struct skl_ddb_allocation *new,
2931 enum pipe pipe)
2932{
2933 uint16_t old_size, new_size;
2934
2935 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
2936 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
2937
2938 return old_size != new_size &&
2939 new->pipe[pipe].start >= old->pipe[pipe].start &&
2940 new->pipe[pipe].end <= old->pipe[pipe].end;
2941}
2942
2943static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
2944 struct skl_wm_values *new_values)
2945{
2946 struct drm_device *dev = dev_priv->dev;
2947 struct skl_ddb_allocation *cur_ddb, *new_ddb;
2948 bool reallocated[I915_MAX_PIPES] = {false, false, false};
2949 struct intel_crtc *crtc;
2950 enum pipe pipe;
2951
2952 new_ddb = &new_values->ddb;
2953 cur_ddb = &dev_priv->wm.skl_hw.ddb;
2954
2955 /*
2956 * First pass: flush the pipes with the new allocation contained into
2957 * the old space.
2958 *
2959 * We'll wait for the vblank on those pipes to ensure we can safely
2960 * re-allocate the freed space without this pipe fetching from it.
2961 */
2962 for_each_intel_crtc(dev, crtc) {
2963 if (!crtc->active)
2964 continue;
2965
2966 pipe = crtc->pipe;
2967
2968 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
2969 continue;
2970
Damien Lespiaud21b7952014-11-04 17:07:03 +00002971 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002972 intel_wait_for_vblank(dev, pipe);
2973
2974 reallocated[pipe] = true;
2975 }
2976
2977
2978 /*
2979 * Second pass: flush the pipes that are having their allocation
2980 * reduced, but overlapping with a previous allocation.
2981 *
2982 * Here as well we need to wait for the vblank to make sure the freed
2983 * space is not used anymore.
2984 */
2985 for_each_intel_crtc(dev, crtc) {
2986 if (!crtc->active)
2987 continue;
2988
2989 pipe = crtc->pipe;
2990
2991 if (reallocated[pipe])
2992 continue;
2993
2994 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
2995 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00002996 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002997 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05302998 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002999 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003000 }
3001
3002 /*
3003 * Third pass: flush the pipes that got more space allocated.
3004 *
3005 * We don't need to actively wait for the update here, next vblank
3006 * will just get more DDB space with the correct WM values.
3007 */
3008 for_each_intel_crtc(dev, crtc) {
3009 if (!crtc->active)
3010 continue;
3011
3012 pipe = crtc->pipe;
3013
3014 /*
3015 * At this point, only the pipes more space than before are
3016 * left to re-allocate.
3017 */
3018 if (reallocated[pipe])
3019 continue;
3020
Damien Lespiaud21b7952014-11-04 17:07:03 +00003021 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003022 }
3023}
3024
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003025static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3026 struct skl_pipe_wm_parameters *params,
3027 struct intel_wm_config *config,
3028 struct skl_ddb_allocation *ddb, /* out */
3029 struct skl_pipe_wm *pipe_wm /* out */)
3030{
3031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3032
3033 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003034 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003035 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3036
3037 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3038 return false;
3039
3040 intel_crtc->wm.skl_active = *pipe_wm;
3041 return true;
3042}
3043
3044static void skl_update_other_pipe_wm(struct drm_device *dev,
3045 struct drm_crtc *crtc,
3046 struct intel_wm_config *config,
3047 struct skl_wm_values *r)
3048{
3049 struct intel_crtc *intel_crtc;
3050 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3051
3052 /*
3053 * If the WM update hasn't changed the allocation for this_crtc (the
3054 * crtc we are currently computing the new WM values for), other
3055 * enabled crtcs will keep the same allocation and we don't need to
3056 * recompute anything for them.
3057 */
3058 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3059 return;
3060
3061 /*
3062 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3063 * other active pipes need new DDB allocation and WM values.
3064 */
3065 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3066 base.head) {
3067 struct skl_pipe_wm_parameters params = {};
3068 struct skl_pipe_wm pipe_wm = {};
3069 bool wm_changed;
3070
3071 if (this_crtc->pipe == intel_crtc->pipe)
3072 continue;
3073
3074 if (!intel_crtc->active)
3075 continue;
3076
3077 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3078 &params, config,
3079 &r->ddb, &pipe_wm);
3080
3081 /*
3082 * If we end up re-computing the other pipe WM values, it's
3083 * because it was really needed, so we expect the WM values to
3084 * be different.
3085 */
3086 WARN_ON(!wm_changed);
3087
3088 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3089 r->dirty[intel_crtc->pipe] = true;
3090 }
3091}
3092
3093static void skl_update_wm(struct drm_crtc *crtc)
3094{
3095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3096 struct drm_device *dev = crtc->dev;
3097 struct drm_i915_private *dev_priv = dev->dev_private;
3098 struct skl_pipe_wm_parameters params = {};
3099 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3100 struct skl_pipe_wm pipe_wm = {};
3101 struct intel_wm_config config = {};
3102
3103 memset(results, 0, sizeof(*results));
3104
3105 skl_compute_wm_global_parameters(dev, &config);
3106
3107 if (!skl_update_pipe_wm(crtc, &params, &config,
3108 &results->ddb, &pipe_wm))
3109 return;
3110
3111 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3112 results->dirty[intel_crtc->pipe] = true;
3113
3114 skl_update_other_pipe_wm(dev, crtc, &config, results);
3115 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003116 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003117
3118 /* store the new configuration */
3119 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003120}
3121
3122static void
3123skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3124 uint32_t sprite_width, uint32_t sprite_height,
3125 int pixel_size, bool enabled, bool scaled)
3126{
3127 struct intel_plane *intel_plane = to_intel_plane(plane);
3128
3129 intel_plane->wm.enabled = enabled;
3130 intel_plane->wm.scaled = scaled;
3131 intel_plane->wm.horiz_pixels = sprite_width;
3132 intel_plane->wm.vert_pixels = sprite_height;
3133 intel_plane->wm.bytes_per_pixel = pixel_size;
3134
3135 skl_update_wm(crtc);
3136}
3137
Imre Deak820c1982013-12-17 14:46:36 +02003138static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003139{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003141 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003142 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003143 struct ilk_wm_maximums max;
3144 struct ilk_pipe_wm_parameters params = {};
3145 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003146 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003147 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003148 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003149 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003150
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003151 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003152
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003153 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3154
3155 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3156 return;
3157
3158 intel_crtc->wm.active = pipe_wm;
3159
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003160 ilk_compute_wm_config(dev, &config);
3161
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003162 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003163 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003164
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003165 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003166 if (INTEL_INFO(dev)->gen >= 7 &&
3167 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003168 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003169 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003170
Imre Deak820c1982013-12-17 14:46:36 +02003171 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003172 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003173 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003174 }
3175
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003176 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003177 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003178
Imre Deak820c1982013-12-17 14:46:36 +02003179 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003180
Imre Deak820c1982013-12-17 14:46:36 +02003181 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003182}
3183
Damien Lespiaued57cb82014-07-15 09:21:24 +02003184static void
3185ilk_update_sprite_wm(struct drm_plane *plane,
3186 struct drm_crtc *crtc,
3187 uint32_t sprite_width, uint32_t sprite_height,
3188 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003189{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003190 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003191 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003192
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003193 intel_plane->wm.enabled = enabled;
3194 intel_plane->wm.scaled = scaled;
3195 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003196 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003197 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003198
Ville Syrjälä8553c182013-12-05 15:51:39 +02003199 /*
3200 * IVB workaround: must disable low power watermarks for at least
3201 * one frame before enabling scaling. LP watermarks can be re-enabled
3202 * when scaling is disabled.
3203 *
3204 * WaCxSRDisabledForSpriteScaling:ivb
3205 */
3206 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3207 intel_wait_for_vblank(dev, intel_plane->pipe);
3208
Imre Deak820c1982013-12-17 14:46:36 +02003209 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003210}
3211
Pradeep Bhat30789992014-11-04 17:06:45 +00003212static void skl_pipe_wm_active_state(uint32_t val,
3213 struct skl_pipe_wm *active,
3214 bool is_transwm,
3215 bool is_cursor,
3216 int i,
3217 int level)
3218{
3219 bool is_enabled = (val & PLANE_WM_EN) != 0;
3220
3221 if (!is_transwm) {
3222 if (!is_cursor) {
3223 active->wm[level].plane_en[i] = is_enabled;
3224 active->wm[level].plane_res_b[i] =
3225 val & PLANE_WM_BLOCKS_MASK;
3226 active->wm[level].plane_res_l[i] =
3227 (val >> PLANE_WM_LINES_SHIFT) &
3228 PLANE_WM_LINES_MASK;
3229 } else {
3230 active->wm[level].cursor_en = is_enabled;
3231 active->wm[level].cursor_res_b =
3232 val & PLANE_WM_BLOCKS_MASK;
3233 active->wm[level].cursor_res_l =
3234 (val >> PLANE_WM_LINES_SHIFT) &
3235 PLANE_WM_LINES_MASK;
3236 }
3237 } else {
3238 if (!is_cursor) {
3239 active->trans_wm.plane_en[i] = is_enabled;
3240 active->trans_wm.plane_res_b[i] =
3241 val & PLANE_WM_BLOCKS_MASK;
3242 active->trans_wm.plane_res_l[i] =
3243 (val >> PLANE_WM_LINES_SHIFT) &
3244 PLANE_WM_LINES_MASK;
3245 } else {
3246 active->trans_wm.cursor_en = is_enabled;
3247 active->trans_wm.cursor_res_b =
3248 val & PLANE_WM_BLOCKS_MASK;
3249 active->trans_wm.cursor_res_l =
3250 (val >> PLANE_WM_LINES_SHIFT) &
3251 PLANE_WM_LINES_MASK;
3252 }
3253 }
3254}
3255
3256static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3257{
3258 struct drm_device *dev = crtc->dev;
3259 struct drm_i915_private *dev_priv = dev->dev_private;
3260 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3262 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3263 enum pipe pipe = intel_crtc->pipe;
3264 int level, i, max_level;
3265 uint32_t temp;
3266
3267 max_level = ilk_wm_max_level(dev);
3268
3269 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3270
3271 for (level = 0; level <= max_level; level++) {
3272 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3273 hw->plane[pipe][i][level] =
3274 I915_READ(PLANE_WM(pipe, i, level));
3275 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3276 }
3277
3278 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3279 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3280 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3281
3282 if (!intel_crtc_active(crtc))
3283 return;
3284
3285 hw->dirty[pipe] = true;
3286
3287 active->linetime = hw->wm_linetime[pipe];
3288
3289 for (level = 0; level <= max_level; level++) {
3290 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3291 temp = hw->plane[pipe][i][level];
3292 skl_pipe_wm_active_state(temp, active, false,
3293 false, i, level);
3294 }
3295 temp = hw->cursor[pipe][level];
3296 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3297 }
3298
3299 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3300 temp = hw->plane_trans[pipe][i];
3301 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3302 }
3303
3304 temp = hw->cursor_trans[pipe];
3305 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3306}
3307
3308void skl_wm_get_hw_state(struct drm_device *dev)
3309{
Damien Lespiaua269c582014-11-04 17:06:49 +00003310 struct drm_i915_private *dev_priv = dev->dev_private;
3311 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003312 struct drm_crtc *crtc;
3313
Damien Lespiaua269c582014-11-04 17:06:49 +00003314 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003315 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3316 skl_pipe_wm_get_hw_state(crtc);
3317}
3318
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003319static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3320{
3321 struct drm_device *dev = crtc->dev;
3322 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003323 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003324 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3325 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3326 enum pipe pipe = intel_crtc->pipe;
3327 static const unsigned int wm0_pipe_reg[] = {
3328 [PIPE_A] = WM0_PIPEA_ILK,
3329 [PIPE_B] = WM0_PIPEB_ILK,
3330 [PIPE_C] = WM0_PIPEC_IVB,
3331 };
3332
3333 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003334 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003335 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003336
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003337 active->pipe_enabled = intel_crtc_active(crtc);
3338
3339 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003340 u32 tmp = hw->wm_pipe[pipe];
3341
3342 /*
3343 * For active pipes LP0 watermark is marked as
3344 * enabled, and LP1+ watermaks as disabled since
3345 * we can't really reverse compute them in case
3346 * multiple pipes are active.
3347 */
3348 active->wm[0].enable = true;
3349 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3350 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3351 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3352 active->linetime = hw->wm_linetime[pipe];
3353 } else {
3354 int level, max_level = ilk_wm_max_level(dev);
3355
3356 /*
3357 * For inactive pipes, all watermark levels
3358 * should be marked as enabled but zeroed,
3359 * which is what we'd compute them to.
3360 */
3361 for (level = 0; level <= max_level; level++)
3362 active->wm[level].enable = true;
3363 }
3364}
3365
3366void ilk_wm_get_hw_state(struct drm_device *dev)
3367{
3368 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003369 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003370 struct drm_crtc *crtc;
3371
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003372 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003373 ilk_pipe_wm_get_hw_state(crtc);
3374
3375 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3376 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3377 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3378
3379 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003380 if (INTEL_INFO(dev)->gen >= 7) {
3381 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3382 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3383 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003384
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003385 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003386 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3387 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3388 else if (IS_IVYBRIDGE(dev))
3389 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3390 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003391
3392 hw->enable_fbc_wm =
3393 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3394}
3395
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003396/**
3397 * intel_update_watermarks - update FIFO watermark values based on current modes
3398 *
3399 * Calculate watermark values for the various WM regs based on current mode
3400 * and plane configuration.
3401 *
3402 * There are several cases to deal with here:
3403 * - normal (i.e. non-self-refresh)
3404 * - self-refresh (SR) mode
3405 * - lines are large relative to FIFO size (buffer can hold up to 2)
3406 * - lines are small relative to FIFO size (buffer can hold more than 2
3407 * lines), so need to account for TLB latency
3408 *
3409 * The normal calculation is:
3410 * watermark = dotclock * bytes per pixel * latency
3411 * where latency is platform & configuration dependent (we assume pessimal
3412 * values here).
3413 *
3414 * The SR calculation is:
3415 * watermark = (trunc(latency/line time)+1) * surface width *
3416 * bytes per pixel
3417 * where
3418 * line time = htotal / dotclock
3419 * surface width = hdisplay for normal plane and 64 for cursor
3420 * and latency is assumed to be high, as above.
3421 *
3422 * The final value programmed to the register should always be rounded up,
3423 * and include an extra 2 entries to account for clock crossings.
3424 *
3425 * We don't use the sprite, so we can ignore that. And on Crestline we have
3426 * to set the non-SR watermarks to 8.
3427 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003428void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003429{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003430 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003431
3432 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003433 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003434}
3435
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003436void intel_update_sprite_watermarks(struct drm_plane *plane,
3437 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003438 uint32_t sprite_width,
3439 uint32_t sprite_height,
3440 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003441 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003442{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003443 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003444
3445 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003446 dev_priv->display.update_sprite_wm(plane, crtc,
3447 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003448 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003449}
3450
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003451static struct drm_i915_gem_object *
3452intel_alloc_context_page(struct drm_device *dev)
3453{
3454 struct drm_i915_gem_object *ctx;
3455 int ret;
3456
3457 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3458
3459 ctx = i915_gem_alloc_object(dev, 4096);
3460 if (!ctx) {
3461 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3462 return NULL;
3463 }
3464
Daniel Vetterc69766f2014-02-14 14:01:17 +01003465 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003466 if (ret) {
3467 DRM_ERROR("failed to pin power context: %d\n", ret);
3468 goto err_unref;
3469 }
3470
3471 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3472 if (ret) {
3473 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3474 goto err_unpin;
3475 }
3476
3477 return ctx;
3478
3479err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003480 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003481err_unref:
3482 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003483 return NULL;
3484}
3485
Daniel Vetter92703882012-08-09 16:46:01 +02003486/**
3487 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003488 */
3489DEFINE_SPINLOCK(mchdev_lock);
3490
3491/* Global for IPS driver to get at the current i915 device. Protected by
3492 * mchdev_lock. */
3493static struct drm_i915_private *i915_mch_dev;
3494
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003495bool ironlake_set_drps(struct drm_device *dev, u8 val)
3496{
3497 struct drm_i915_private *dev_priv = dev->dev_private;
3498 u16 rgvswctl;
3499
Daniel Vetter92703882012-08-09 16:46:01 +02003500 assert_spin_locked(&mchdev_lock);
3501
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003502 rgvswctl = I915_READ16(MEMSWCTL);
3503 if (rgvswctl & MEMCTL_CMD_STS) {
3504 DRM_DEBUG("gpu busy, RCS change rejected\n");
3505 return false; /* still busy with another command */
3506 }
3507
3508 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3509 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3510 I915_WRITE16(MEMSWCTL, rgvswctl);
3511 POSTING_READ16(MEMSWCTL);
3512
3513 rgvswctl |= MEMCTL_CMD_STS;
3514 I915_WRITE16(MEMSWCTL, rgvswctl);
3515
3516 return true;
3517}
3518
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003519static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003520{
3521 struct drm_i915_private *dev_priv = dev->dev_private;
3522 u32 rgvmodectl = I915_READ(MEMMODECTL);
3523 u8 fmax, fmin, fstart, vstart;
3524
Daniel Vetter92703882012-08-09 16:46:01 +02003525 spin_lock_irq(&mchdev_lock);
3526
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003527 /* Enable temp reporting */
3528 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3529 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3530
3531 /* 100ms RC evaluation intervals */
3532 I915_WRITE(RCUPEI, 100000);
3533 I915_WRITE(RCDNEI, 100000);
3534
3535 /* Set max/min thresholds to 90ms and 80ms respectively */
3536 I915_WRITE(RCBMAXAVG, 90000);
3537 I915_WRITE(RCBMINAVG, 80000);
3538
3539 I915_WRITE(MEMIHYST, 1);
3540
3541 /* Set up min, max, and cur for interrupt handling */
3542 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3543 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3544 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3545 MEMMODE_FSTART_SHIFT;
3546
3547 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3548 PXVFREQ_PX_SHIFT;
3549
Daniel Vetter20e4d402012-08-08 23:35:39 +02003550 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3551 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003552
Daniel Vetter20e4d402012-08-08 23:35:39 +02003553 dev_priv->ips.max_delay = fstart;
3554 dev_priv->ips.min_delay = fmin;
3555 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003556
3557 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3558 fmax, fmin, fstart);
3559
3560 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3561
3562 /*
3563 * Interrupts will be enabled in ironlake_irq_postinstall
3564 */
3565
3566 I915_WRITE(VIDSTART, vstart);
3567 POSTING_READ(VIDSTART);
3568
3569 rgvmodectl |= MEMMODE_SWMODE_EN;
3570 I915_WRITE(MEMMODECTL, rgvmodectl);
3571
Daniel Vetter92703882012-08-09 16:46:01 +02003572 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003573 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003574 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003575
3576 ironlake_set_drps(dev, fstart);
3577
Daniel Vetter20e4d402012-08-08 23:35:39 +02003578 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003579 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003580 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3581 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003582 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003583
3584 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003585}
3586
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003587static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003588{
3589 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003590 u16 rgvswctl;
3591
3592 spin_lock_irq(&mchdev_lock);
3593
3594 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003595
3596 /* Ack interrupts, disable EFC interrupt */
3597 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3598 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3599 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3600 I915_WRITE(DEIIR, DE_PCU_EVENT);
3601 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3602
3603 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003604 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003605 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003606 rgvswctl |= MEMCTL_CMD_STS;
3607 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003608 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003609
Daniel Vetter92703882012-08-09 16:46:01 +02003610 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003611}
3612
Daniel Vetteracbe9472012-07-26 11:50:05 +02003613/* There's a funny hw issue where the hw returns all 0 when reading from
3614 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3615 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3616 * all limits and the gpu stuck at whatever frequency it is at atm).
3617 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003618static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003619{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003620 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003621
Daniel Vetter20b46e52012-07-26 11:16:14 +02003622 /* Only set the down limit when we've reached the lowest level to avoid
3623 * getting more interrupts, otherwise leave this clear. This prevents a
3624 * race in the hw when coming out of rc6: There's a tiny window where
3625 * the hw runs at the minimal clock before selecting the desired
3626 * frequency, if the down threshold expires in that window we will not
3627 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003628 limits = dev_priv->rps.max_freq_softlimit << 24;
3629 if (val <= dev_priv->rps.min_freq_softlimit)
3630 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003631
3632 return limits;
3633}
3634
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003635static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3636{
3637 int new_power;
3638
3639 new_power = dev_priv->rps.power;
3640 switch (dev_priv->rps.power) {
3641 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003642 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003643 new_power = BETWEEN;
3644 break;
3645
3646 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003647 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003648 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003649 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003650 new_power = HIGH_POWER;
3651 break;
3652
3653 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003654 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 +01003655 new_power = BETWEEN;
3656 break;
3657 }
3658 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003659 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003660 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003661 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003662 new_power = HIGH_POWER;
3663 if (new_power == dev_priv->rps.power)
3664 return;
3665
3666 /* Note the units here are not exactly 1us, but 1280ns. */
3667 switch (new_power) {
3668 case LOW_POWER:
3669 /* Upclock if more than 95% busy over 16ms */
3670 I915_WRITE(GEN6_RP_UP_EI, 12500);
3671 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3672
3673 /* Downclock if less than 85% busy over 32ms */
3674 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3675 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3676
3677 I915_WRITE(GEN6_RP_CONTROL,
3678 GEN6_RP_MEDIA_TURBO |
3679 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3680 GEN6_RP_MEDIA_IS_GFX |
3681 GEN6_RP_ENABLE |
3682 GEN6_RP_UP_BUSY_AVG |
3683 GEN6_RP_DOWN_IDLE_AVG);
3684 break;
3685
3686 case BETWEEN:
3687 /* Upclock if more than 90% busy over 13ms */
3688 I915_WRITE(GEN6_RP_UP_EI, 10250);
3689 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3690
3691 /* Downclock if less than 75% busy over 32ms */
3692 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3693 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3694
3695 I915_WRITE(GEN6_RP_CONTROL,
3696 GEN6_RP_MEDIA_TURBO |
3697 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3698 GEN6_RP_MEDIA_IS_GFX |
3699 GEN6_RP_ENABLE |
3700 GEN6_RP_UP_BUSY_AVG |
3701 GEN6_RP_DOWN_IDLE_AVG);
3702 break;
3703
3704 case HIGH_POWER:
3705 /* Upclock if more than 85% busy over 10ms */
3706 I915_WRITE(GEN6_RP_UP_EI, 8000);
3707 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3708
3709 /* Downclock if less than 60% busy over 32ms */
3710 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3711 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3712
3713 I915_WRITE(GEN6_RP_CONTROL,
3714 GEN6_RP_MEDIA_TURBO |
3715 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3716 GEN6_RP_MEDIA_IS_GFX |
3717 GEN6_RP_ENABLE |
3718 GEN6_RP_UP_BUSY_AVG |
3719 GEN6_RP_DOWN_IDLE_AVG);
3720 break;
3721 }
3722
3723 dev_priv->rps.power = new_power;
3724 dev_priv->rps.last_adj = 0;
3725}
3726
Chris Wilson2876ce72014-03-28 08:03:34 +00003727static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3728{
3729 u32 mask = 0;
3730
3731 if (val > dev_priv->rps.min_freq_softlimit)
3732 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3733 if (val < dev_priv->rps.max_freq_softlimit)
3734 mask |= GEN6_PM_RP_UP_THRESHOLD;
3735
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003736 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3737 mask &= dev_priv->pm_rps_events;
3738
Imre Deak59d02a12014-12-19 19:33:26 +02003739 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003740}
3741
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003742/* gen6_set_rps is called to update the frequency request, but should also be
3743 * called when the range (min_delay and max_delay) is modified so that we can
3744 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003745static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003746{
3747 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003748
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003749 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003750 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3751 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003752
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003753 /* min/max delay may still have been modified so be sure to
3754 * write the limits value.
3755 */
3756 if (val != dev_priv->rps.cur_freq) {
3757 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003758
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003759 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003760 I915_WRITE(GEN6_RPNSWREQ,
3761 HSW_FREQUENCY(val));
3762 else
3763 I915_WRITE(GEN6_RPNSWREQ,
3764 GEN6_FREQUENCY(val) |
3765 GEN6_OFFSET(0) |
3766 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003767 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003768
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003769 /* Make sure we continue to get interrupts
3770 * until we hit the minimum or maximum frequencies.
3771 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003772 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003773 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003774
Ben Widawskyd5570a72012-09-07 19:43:41 -07003775 POSTING_READ(GEN6_RPNSWREQ);
3776
Ben Widawskyb39fb292014-03-19 18:31:11 -07003777 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003778 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003779}
3780
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003781static void valleyview_set_rps(struct drm_device *dev, u8 val)
3782{
3783 struct drm_i915_private *dev_priv = dev->dev_private;
3784
3785 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3786 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3787 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3788
3789 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3790 "Odd GPU freq value\n"))
3791 val &= ~1;
3792
3793 if (val != dev_priv->rps.cur_freq)
3794 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3795
3796 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3797
3798 dev_priv->rps.cur_freq = val;
3799 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3800}
3801
Deepak S76c3552f2014-01-30 23:08:16 +05303802/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3803 *
3804 * * If Gfx is Idle, then
3805 * 1. Mask Turbo interrupts
3806 * 2. Bring up Gfx clock
3807 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3808 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3809 * 5. Unmask Turbo interrupts
3810*/
3811static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3812{
Deepak S5549d252014-06-28 11:26:11 +05303813 struct drm_device *dev = dev_priv->dev;
3814
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003815 /* CHV and latest VLV don't need to force the gfx clock */
3816 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303817 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3818 return;
3819 }
3820
Deepak S76c3552f2014-01-30 23:08:16 +05303821 /*
3822 * When we are idle. Drop to min voltage state.
3823 */
3824
Ben Widawskyb39fb292014-03-19 18:31:11 -07003825 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303826 return;
3827
3828 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003829 I915_WRITE(GEN6_PMINTRMSK,
3830 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303831
Imre Deak650ad972014-04-18 16:35:02 +03003832 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303833
Ben Widawskyb39fb292014-03-19 18:31:11 -07003834 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303835
3836 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003837 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303838
3839 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003840 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303841 DRM_ERROR("timed out waiting for Punit\n");
3842
Imre Deak650ad972014-04-18 16:35:02 +03003843 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303844
Chris Wilson2876ce72014-03-28 08:03:34 +00003845 I915_WRITE(GEN6_PMINTRMSK,
3846 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303847}
3848
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003849void gen6_rps_idle(struct drm_i915_private *dev_priv)
3850{
Damien Lespiau691bb712013-12-12 14:36:36 +00003851 struct drm_device *dev = dev_priv->dev;
3852
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003853 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003854 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003855 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303856 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003857 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003858 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003859 dev_priv->rps.last_adj = 0;
3860 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003861 mutex_unlock(&dev_priv->rps.hw_lock);
3862}
3863
3864void gen6_rps_boost(struct drm_i915_private *dev_priv)
3865{
3866 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003867 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003868 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003869 dev_priv->rps.last_adj = 0;
3870 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003871 mutex_unlock(&dev_priv->rps.hw_lock);
3872}
3873
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003874void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003875{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003876 if (IS_VALLEYVIEW(dev))
3877 valleyview_set_rps(dev, val);
3878 else
3879 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003880}
3881
Zhe Wang20e49362014-11-04 17:07:05 +00003882static void gen9_disable_rps(struct drm_device *dev)
3883{
3884 struct drm_i915_private *dev_priv = dev->dev_private;
3885
3886 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00003887 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00003888}
3889
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003890static void gen6_disable_rps(struct drm_device *dev)
3891{
3892 struct drm_i915_private *dev_priv = dev->dev_private;
3893
3894 I915_WRITE(GEN6_RC_CONTROL, 0);
3895 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003896}
3897
Deepak S38807742014-05-23 21:00:15 +05303898static void cherryview_disable_rps(struct drm_device *dev)
3899{
3900 struct drm_i915_private *dev_priv = dev->dev_private;
3901
3902 I915_WRITE(GEN6_RC_CONTROL, 0);
3903}
3904
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003905static void valleyview_disable_rps(struct drm_device *dev)
3906{
3907 struct drm_i915_private *dev_priv = dev->dev_private;
3908
Deepak S98a2e5f2014-08-18 10:35:27 -07003909 /* we're doing forcewake before Disabling RC6,
3910 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02003911 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07003912
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003913 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003914
Mika Kuoppala59bad942015-01-16 11:34:40 +02003915 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003916}
3917
Ben Widawskydc39fff2013-10-18 12:32:07 -07003918static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3919{
Imre Deak91ca6892014-04-14 20:24:25 +03003920 if (IS_VALLEYVIEW(dev)) {
3921 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3922 mode = GEN6_RC_CTL_RC6_ENABLE;
3923 else
3924 mode = 0;
3925 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003926 if (HAS_RC6p(dev))
3927 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
3928 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3929 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3930 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3931
3932 else
3933 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
3934 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003935}
3936
Imre Deake6069ca2014-04-18 16:01:02 +03003937static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003938{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003939 /* No RC6 before Ironlake */
3940 if (INTEL_INFO(dev)->gen < 5)
3941 return 0;
3942
Imre Deake6069ca2014-04-18 16:01:02 +03003943 /* RC6 is only on Ironlake mobile not on desktop */
3944 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3945 return 0;
3946
Daniel Vetter456470e2012-08-08 23:35:40 +02003947 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003948 if (enable_rc6 >= 0) {
3949 int mask;
3950
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003951 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03003952 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3953 INTEL_RC6pp_ENABLE;
3954 else
3955 mask = INTEL_RC6_ENABLE;
3956
3957 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003958 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3959 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003960
3961 return enable_rc6 & mask;
3962 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003963
Chris Wilson6567d742012-11-10 10:00:06 +00003964 /* Disable RC6 on Ironlake */
3965 if (INTEL_INFO(dev)->gen == 5)
3966 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003967
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003968 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003969 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003970
3971 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003972}
3973
Imre Deake6069ca2014-04-18 16:01:02 +03003974int intel_enable_rc6(const struct drm_device *dev)
3975{
3976 return i915.enable_rc6;
3977}
3978
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003979static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003980{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003981 struct drm_i915_private *dev_priv = dev->dev_private;
3982 uint32_t rp_state_cap;
3983 u32 ddcc_status = 0;
3984 int ret;
3985
3986 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003987 /* All of these values are in units of 50MHz */
3988 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003989 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003990 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003991 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003992 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003993 /* hw_max = RP0 until we check for overclocking */
3994 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3995
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003996 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3997 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3998 ret = sandybridge_pcode_read(dev_priv,
3999 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4000 &ddcc_status);
4001 if (0 == ret)
4002 dev_priv->rps.efficient_freq =
4003 (ddcc_status >> 8) & 0xff;
4004 }
4005
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004006 /* Preserve min/max settings in case of re-init */
4007 if (dev_priv->rps.max_freq_softlimit == 0)
4008 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4009
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004010 if (dev_priv->rps.min_freq_softlimit == 0) {
4011 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4012 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004013 /* max(RPe, 450 MHz) */
4014 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004015 else
4016 dev_priv->rps.min_freq_softlimit =
4017 dev_priv->rps.min_freq;
4018 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004019}
4020
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004021/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004022static void gen9_enable_rps(struct drm_device *dev)
4023{
4024 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004025
4026 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4027
Damien Lespiauba1c5542015-01-16 18:07:26 +00004028 gen6_init_rps_frequencies(dev);
4029
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004030 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4031 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4032
4033 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4034 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4035 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4036 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4037 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4038 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4039 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4040 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4041 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4042 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4043 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4044 GEN6_RP_DOWN_IDLE_AVG);
4045
4046 gen6_enable_rps_interrupts(dev);
4047
4048 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4049}
4050
4051static void gen9_enable_rc6(struct drm_device *dev)
4052{
4053 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004054 struct intel_engine_cs *ring;
4055 uint32_t rc6_mask = 0;
4056 int unused;
4057
4058 /* 1a: Software RC state - RC0 */
4059 I915_WRITE(GEN6_RC_STATE, 0);
4060
4061 /* 1b: Get forcewake during program sequence. Although the driver
4062 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004063 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004064
4065 /* 2a: Disable RC states. */
4066 I915_WRITE(GEN6_RC_CONTROL, 0);
4067
4068 /* 2b: Program RC6 thresholds.*/
4069 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4070 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4071 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4072 for_each_ring(ring, dev_priv, unused)
4073 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4074 I915_WRITE(GEN6_RC_SLEEP, 0);
4075 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4076
Zhe Wang38c23522015-01-20 12:23:04 +00004077 /* 2c: Program Coarse Power Gating Policies. */
4078 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4079 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4080
Zhe Wang20e49362014-11-04 17:07:05 +00004081 /* 3a: Enable RC6 */
4082 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4083 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4084 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4085 "on" : "off");
4086 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4087 GEN6_RC_CTL_EI_MODE(1) |
4088 rc6_mask);
4089
Zhe Wang38c23522015-01-20 12:23:04 +00004090 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4091 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4092
Mika Kuoppala59bad942015-01-16 11:34:40 +02004093 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004094
4095}
4096
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004097static void gen8_enable_rps(struct drm_device *dev)
4098{
4099 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004100 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004101 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004102 int unused;
4103
4104 /* 1a: Software RC state - RC0 */
4105 I915_WRITE(GEN6_RC_STATE, 0);
4106
4107 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4108 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004109 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004110
4111 /* 2a: Disable RC states. */
4112 I915_WRITE(GEN6_RC_CONTROL, 0);
4113
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004114 /* Initialize rps frequencies */
4115 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004116
4117 /* 2b: Program RC6 thresholds.*/
4118 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4119 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4120 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4121 for_each_ring(ring, dev_priv, unused)
4122 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4123 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004124 if (IS_BROADWELL(dev))
4125 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4126 else
4127 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004128
4129 /* 3: Enable RC6 */
4130 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4131 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004132 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004133 if (IS_BROADWELL(dev))
4134 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4135 GEN7_RC_CTL_TO_MODE |
4136 rc6_mask);
4137 else
4138 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4139 GEN6_RC_CTL_EI_MODE(1) |
4140 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004141
4142 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004143 I915_WRITE(GEN6_RPNSWREQ,
4144 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4145 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4146 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004147 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4148 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004149
Daniel Vetter7526ed72014-09-29 15:07:19 +02004150 /* Docs recommend 900MHz, and 300 MHz respectively */
4151 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4152 dev_priv->rps.max_freq_softlimit << 24 |
4153 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004154
Daniel Vetter7526ed72014-09-29 15:07:19 +02004155 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4156 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4157 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4158 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004159
Daniel Vetter7526ed72014-09-29 15:07:19 +02004160 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004161
4162 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004163 I915_WRITE(GEN6_RP_CONTROL,
4164 GEN6_RP_MEDIA_TURBO |
4165 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4166 GEN6_RP_MEDIA_IS_GFX |
4167 GEN6_RP_ENABLE |
4168 GEN6_RP_UP_BUSY_AVG |
4169 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004170
Daniel Vetter7526ed72014-09-29 15:07:19 +02004171 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004172
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004173 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4174 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004175
Mika Kuoppala59bad942015-01-16 11:34:40 +02004176 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004177}
4178
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004179static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004180{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004181 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004182 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004183 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004184 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004185 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004186 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004187
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004188 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004189
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004190 /* Here begins a magic sequence of register writes to enable
4191 * auto-downclocking.
4192 *
4193 * Perhaps there might be some value in exposing these to
4194 * userspace...
4195 */
4196 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004197
4198 /* Clear the DBG now so we don't confuse earlier errors */
4199 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4200 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4201 I915_WRITE(GTFIFODBG, gtfifodbg);
4202 }
4203
Mika Kuoppala59bad942015-01-16 11:34:40 +02004204 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004205
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004206 /* Initialize rps frequencies */
4207 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004208
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004209 /* disable the counters and set deterministic thresholds */
4210 I915_WRITE(GEN6_RC_CONTROL, 0);
4211
4212 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4213 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4214 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4215 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4216 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4217
Chris Wilsonb4519512012-05-11 14:29:30 +01004218 for_each_ring(ring, dev_priv, i)
4219 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004220
4221 I915_WRITE(GEN6_RC_SLEEP, 0);
4222 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004223 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004224 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4225 else
4226 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004227 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004228 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4229
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004230 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004231 rc6_mode = intel_enable_rc6(dev_priv->dev);
4232 if (rc6_mode & INTEL_RC6_ENABLE)
4233 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4234
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004235 /* We don't use those on Haswell */
4236 if (!IS_HASWELL(dev)) {
4237 if (rc6_mode & INTEL_RC6p_ENABLE)
4238 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004239
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004240 if (rc6_mode & INTEL_RC6pp_ENABLE)
4241 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4242 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004243
Ben Widawskydc39fff2013-10-18 12:32:07 -07004244 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004245
4246 I915_WRITE(GEN6_RC_CONTROL,
4247 rc6_mask |
4248 GEN6_RC_CTL_EI_MODE(1) |
4249 GEN6_RC_CTL_HW_ENABLE);
4250
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004251 /* Power down if completely idle for over 50ms */
4252 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004253 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004254
Ben Widawsky42c05262012-09-26 10:34:00 -07004255 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004256 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004257 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004258
4259 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4260 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4261 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004262 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004263 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004264 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004265 }
4266
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004267 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004268 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004269
Ben Widawsky31643d52012-09-26 10:34:01 -07004270 rc6vids = 0;
4271 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4272 if (IS_GEN6(dev) && ret) {
4273 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4274 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4275 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4276 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4277 rc6vids &= 0xffff00;
4278 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4279 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4280 if (ret)
4281 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4282 }
4283
Mika Kuoppala59bad942015-01-16 11:34:40 +02004284 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004285}
4286
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004287static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004288{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004289 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004290 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004291 unsigned int gpu_freq;
4292 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004293 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004294 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004295
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004296 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004297
Ben Widawskyeda79642013-10-07 17:15:48 -03004298 policy = cpufreq_cpu_get(0);
4299 if (policy) {
4300 max_ia_freq = policy->cpuinfo.max_freq;
4301 cpufreq_cpu_put(policy);
4302 } else {
4303 /*
4304 * Default to measured freq if none found, PCU will ensure we
4305 * don't go over
4306 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004307 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004308 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004309
4310 /* Convert from kHz to MHz */
4311 max_ia_freq /= 1000;
4312
Ben Widawsky153b4b952013-10-22 22:05:09 -07004313 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004314 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4315 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004316
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004317 /*
4318 * For each potential GPU frequency, load a ring frequency we'd like
4319 * to use for memory access. We do this by specifying the IA frequency
4320 * the PCU should use as a reference to determine the ring frequency.
4321 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004322 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004323 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004324 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004325 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004326
Ben Widawsky46c764d2013-11-02 21:07:49 -07004327 if (INTEL_INFO(dev)->gen >= 8) {
4328 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4329 ring_freq = max(min_ring_freq, gpu_freq);
4330 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004331 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004332 ring_freq = max(min_ring_freq, ring_freq);
4333 /* leave ia_freq as the default, chosen by cpufreq */
4334 } else {
4335 /* On older processors, there is no separate ring
4336 * clock domain, so in order to boost the bandwidth
4337 * of the ring, we need to upclock the CPU (ia_freq).
4338 *
4339 * For GPU frequencies less than 750MHz,
4340 * just use the lowest ring freq.
4341 */
4342 if (gpu_freq < min_freq)
4343 ia_freq = 800;
4344 else
4345 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4346 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4347 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004348
Ben Widawsky42c05262012-09-26 10:34:00 -07004349 sandybridge_pcode_write(dev_priv,
4350 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004351 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4352 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4353 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004354 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004355}
4356
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004357void gen6_update_ring_freq(struct drm_device *dev)
4358{
4359 struct drm_i915_private *dev_priv = dev->dev_private;
4360
4361 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4362 return;
4363
4364 mutex_lock(&dev_priv->rps.hw_lock);
4365 __gen6_update_ring_freq(dev);
4366 mutex_unlock(&dev_priv->rps.hw_lock);
4367}
4368
Ville Syrjälä03af2042014-06-28 02:03:53 +03004369static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304370{
Deepak S095acd52015-01-17 11:05:59 +05304371 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304372 u32 val, rp0;
4373
Deepak S095acd52015-01-17 11:05:59 +05304374 if (dev->pdev->revision >= 0x20) {
4375 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304376
Deepak S095acd52015-01-17 11:05:59 +05304377 switch (INTEL_INFO(dev)->eu_total) {
4378 case 8:
4379 /* (2 * 4) config */
4380 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4381 break;
4382 case 12:
4383 /* (2 * 6) config */
4384 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4385 break;
4386 case 16:
4387 /* (2 * 8) config */
4388 default:
4389 /* Setting (2 * 8) Min RP0 for any other combination */
4390 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4391 break;
4392 }
4393 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4394 } else {
4395 /* For pre-production hardware */
4396 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4397 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4398 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4399 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304400 return rp0;
4401}
4402
4403static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4404{
4405 u32 val, rpe;
4406
4407 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4408 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4409
4410 return rpe;
4411}
4412
Deepak S7707df42014-07-12 18:46:14 +05304413static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4414{
Deepak S095acd52015-01-17 11:05:59 +05304415 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304416 u32 val, rp1;
4417
Deepak S095acd52015-01-17 11:05:59 +05304418 if (dev->pdev->revision >= 0x20) {
4419 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4420 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4421 } else {
4422 /* For pre-production hardware */
4423 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4424 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4425 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4426 }
Deepak S7707df42014-07-12 18:46:14 +05304427 return rp1;
4428}
4429
Ville Syrjälä03af2042014-06-28 02:03:53 +03004430static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304431{
Deepak S095acd52015-01-17 11:05:59 +05304432 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304433 u32 val, rpn;
4434
Deepak S095acd52015-01-17 11:05:59 +05304435 if (dev->pdev->revision >= 0x20) {
4436 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4437 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4438 FB_GFX_FREQ_FUSE_MASK);
4439 } else { /* For pre-production hardware */
4440 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4441 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4442 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4443 }
4444
Deepak S2b6b3a02014-05-27 15:59:30 +05304445 return rpn;
4446}
4447
Deepak Sf8f2b002014-07-10 13:16:21 +05304448static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4449{
4450 u32 val, rp1;
4451
4452 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4453
4454 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4455
4456 return rp1;
4457}
4458
Ville Syrjälä03af2042014-06-28 02:03:53 +03004459static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004460{
4461 u32 val, rp0;
4462
Jani Nikula64936252013-05-22 15:36:20 +03004463 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004464
4465 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4466 /* Clamp to max */
4467 rp0 = min_t(u32, rp0, 0xea);
4468
4469 return rp0;
4470}
4471
4472static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4473{
4474 u32 val, rpe;
4475
Jani Nikula64936252013-05-22 15:36:20 +03004476 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004477 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004478 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004479 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4480
4481 return rpe;
4482}
4483
Ville Syrjälä03af2042014-06-28 02:03:53 +03004484static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004485{
Jani Nikula64936252013-05-22 15:36:20 +03004486 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004487}
4488
Imre Deakae484342014-03-31 15:10:44 +03004489/* Check that the pctx buffer wasn't move under us. */
4490static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4491{
4492 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4493
4494 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4495 dev_priv->vlv_pctx->stolen->start);
4496}
4497
Deepak S38807742014-05-23 21:00:15 +05304498
4499/* Check that the pcbr address is not empty. */
4500static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4501{
4502 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4503
4504 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4505}
4506
4507static void cherryview_setup_pctx(struct drm_device *dev)
4508{
4509 struct drm_i915_private *dev_priv = dev->dev_private;
4510 unsigned long pctx_paddr, paddr;
4511 struct i915_gtt *gtt = &dev_priv->gtt;
4512 u32 pcbr;
4513 int pctx_size = 32*1024;
4514
4515 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4516
4517 pcbr = I915_READ(VLV_PCBR);
4518 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004519 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304520 paddr = (dev_priv->mm.stolen_base +
4521 (gtt->stolen_size - pctx_size));
4522
4523 pctx_paddr = (paddr & (~4095));
4524 I915_WRITE(VLV_PCBR, pctx_paddr);
4525 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004526
4527 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304528}
4529
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004530static void valleyview_setup_pctx(struct drm_device *dev)
4531{
4532 struct drm_i915_private *dev_priv = dev->dev_private;
4533 struct drm_i915_gem_object *pctx;
4534 unsigned long pctx_paddr;
4535 u32 pcbr;
4536 int pctx_size = 24*1024;
4537
Imre Deak17b0c1f2014-02-11 21:39:06 +02004538 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4539
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004540 pcbr = I915_READ(VLV_PCBR);
4541 if (pcbr) {
4542 /* BIOS set it up already, grab the pre-alloc'd space */
4543 int pcbr_offset;
4544
4545 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4546 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4547 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004548 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004549 pctx_size);
4550 goto out;
4551 }
4552
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004553 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4554
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004555 /*
4556 * From the Gunit register HAS:
4557 * The Gfx driver is expected to program this register and ensure
4558 * proper allocation within Gfx stolen memory. For example, this
4559 * register should be programmed such than the PCBR range does not
4560 * overlap with other ranges, such as the frame buffer, protected
4561 * memory, or any other relevant ranges.
4562 */
4563 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4564 if (!pctx) {
4565 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4566 return;
4567 }
4568
4569 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4570 I915_WRITE(VLV_PCBR, pctx_paddr);
4571
4572out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004573 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004574 dev_priv->vlv_pctx = pctx;
4575}
4576
Imre Deakae484342014-03-31 15:10:44 +03004577static void valleyview_cleanup_pctx(struct drm_device *dev)
4578{
4579 struct drm_i915_private *dev_priv = dev->dev_private;
4580
4581 if (WARN_ON(!dev_priv->vlv_pctx))
4582 return;
4583
4584 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4585 dev_priv->vlv_pctx = NULL;
4586}
4587
Imre Deak4e805192014-04-14 20:24:41 +03004588static void valleyview_init_gt_powersave(struct drm_device *dev)
4589{
4590 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004591 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004592
4593 valleyview_setup_pctx(dev);
4594
4595 mutex_lock(&dev_priv->rps.hw_lock);
4596
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004597 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4598 switch ((val >> 6) & 3) {
4599 case 0:
4600 case 1:
4601 dev_priv->mem_freq = 800;
4602 break;
4603 case 2:
4604 dev_priv->mem_freq = 1066;
4605 break;
4606 case 3:
4607 dev_priv->mem_freq = 1333;
4608 break;
4609 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004610 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004611
Imre Deak4e805192014-04-14 20:24:41 +03004612 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4613 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4614 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004615 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004616 dev_priv->rps.max_freq);
4617
4618 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4619 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004620 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004621 dev_priv->rps.efficient_freq);
4622
Deepak Sf8f2b002014-07-10 13:16:21 +05304623 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4624 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004625 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304626 dev_priv->rps.rp1_freq);
4627
Imre Deak4e805192014-04-14 20:24:41 +03004628 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4629 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004630 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004631 dev_priv->rps.min_freq);
4632
4633 /* Preserve min/max settings in case of re-init */
4634 if (dev_priv->rps.max_freq_softlimit == 0)
4635 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4636
4637 if (dev_priv->rps.min_freq_softlimit == 0)
4638 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4639
4640 mutex_unlock(&dev_priv->rps.hw_lock);
4641}
4642
Deepak S38807742014-05-23 21:00:15 +05304643static void cherryview_init_gt_powersave(struct drm_device *dev)
4644{
Deepak S2b6b3a02014-05-27 15:59:30 +05304645 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004646 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304647
Deepak S38807742014-05-23 21:00:15 +05304648 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304649
4650 mutex_lock(&dev_priv->rps.hw_lock);
4651
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004652 mutex_lock(&dev_priv->dpio_lock);
4653 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4654 mutex_unlock(&dev_priv->dpio_lock);
4655
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004656 switch ((val >> 2) & 0x7) {
4657 case 0:
4658 case 1:
4659 dev_priv->rps.cz_freq = 200;
4660 dev_priv->mem_freq = 1600;
4661 break;
4662 case 2:
4663 dev_priv->rps.cz_freq = 267;
4664 dev_priv->mem_freq = 1600;
4665 break;
4666 case 3:
4667 dev_priv->rps.cz_freq = 333;
4668 dev_priv->mem_freq = 2000;
4669 break;
4670 case 4:
4671 dev_priv->rps.cz_freq = 320;
4672 dev_priv->mem_freq = 1600;
4673 break;
4674 case 5:
4675 dev_priv->rps.cz_freq = 400;
4676 dev_priv->mem_freq = 1600;
4677 break;
4678 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004679 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004680
Deepak S2b6b3a02014-05-27 15:59:30 +05304681 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4682 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4683 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004684 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304685 dev_priv->rps.max_freq);
4686
4687 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4688 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004689 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304690 dev_priv->rps.efficient_freq);
4691
Deepak S7707df42014-07-12 18:46:14 +05304692 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4693 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004694 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304695 dev_priv->rps.rp1_freq);
4696
Deepak S2b6b3a02014-05-27 15:59:30 +05304697 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4698 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004699 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304700 dev_priv->rps.min_freq);
4701
Ville Syrjälä1c147622014-08-18 14:42:43 +03004702 WARN_ONCE((dev_priv->rps.max_freq |
4703 dev_priv->rps.efficient_freq |
4704 dev_priv->rps.rp1_freq |
4705 dev_priv->rps.min_freq) & 1,
4706 "Odd GPU freq values\n");
4707
Deepak S2b6b3a02014-05-27 15:59:30 +05304708 /* Preserve min/max settings in case of re-init */
4709 if (dev_priv->rps.max_freq_softlimit == 0)
4710 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4711
4712 if (dev_priv->rps.min_freq_softlimit == 0)
4713 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4714
4715 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304716}
4717
Imre Deak4e805192014-04-14 20:24:41 +03004718static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4719{
4720 valleyview_cleanup_pctx(dev);
4721}
4722
Deepak S38807742014-05-23 21:00:15 +05304723static void cherryview_enable_rps(struct drm_device *dev)
4724{
4725 struct drm_i915_private *dev_priv = dev->dev_private;
4726 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304727 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304728 int i;
4729
4730 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4731
4732 gtfifodbg = I915_READ(GTFIFODBG);
4733 if (gtfifodbg) {
4734 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4735 gtfifodbg);
4736 I915_WRITE(GTFIFODBG, gtfifodbg);
4737 }
4738
4739 cherryview_check_pctx(dev_priv);
4740
4741 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4742 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004743 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304744
Ville Syrjälä160614a2015-01-19 13:50:47 +02004745 /* Disable RC states. */
4746 I915_WRITE(GEN6_RC_CONTROL, 0);
4747
Deepak S38807742014-05-23 21:00:15 +05304748 /* 2a: Program RC6 thresholds.*/
4749 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4750 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4751 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4752
4753 for_each_ring(ring, dev_priv, i)
4754 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4755 I915_WRITE(GEN6_RC_SLEEP, 0);
4756
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004757 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4758 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304759
4760 /* allows RC6 residency counter to work */
4761 I915_WRITE(VLV_COUNTER_CONTROL,
4762 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4763 VLV_MEDIA_RC6_COUNT_EN |
4764 VLV_RENDER_RC6_COUNT_EN));
4765
4766 /* For now we assume BIOS is allocating and populating the PCBR */
4767 pcbr = I915_READ(VLV_PCBR);
4768
Deepak S38807742014-05-23 21:00:15 +05304769 /* 3: Enable RC6 */
4770 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4771 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004772 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304773
4774 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4775
Deepak S2b6b3a02014-05-27 15:59:30 +05304776 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004777 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304778 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4779 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4780 I915_WRITE(GEN6_RP_UP_EI, 66000);
4781 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4782
4783 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4784
4785 /* 5: Enable RPS */
4786 I915_WRITE(GEN6_RP_CONTROL,
4787 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004788 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304789 GEN6_RP_ENABLE |
4790 GEN6_RP_UP_BUSY_AVG |
4791 GEN6_RP_DOWN_IDLE_AVG);
4792
4793 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4794
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004795 /* RPS code assumes GPLL is used */
4796 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4797
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004798 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304799 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4800
4801 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4802 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004803 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304804 dev_priv->rps.cur_freq);
4805
4806 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004807 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304808 dev_priv->rps.efficient_freq);
4809
4810 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4811
Mika Kuoppala59bad942015-01-16 11:34:40 +02004812 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304813}
4814
Jesse Barnes0a073b82013-04-17 15:54:58 -07004815static void valleyview_enable_rps(struct drm_device *dev)
4816{
4817 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004818 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004819 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004820 int i;
4821
4822 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4823
Imre Deakae484342014-03-31 15:10:44 +03004824 valleyview_check_pctx(dev_priv);
4825
Jesse Barnes0a073b82013-04-17 15:54:58 -07004826 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004827 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4828 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004829 I915_WRITE(GTFIFODBG, gtfifodbg);
4830 }
4831
Deepak Sc8d9a592013-11-23 14:55:42 +05304832 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004833 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004834
Ville Syrjälä160614a2015-01-19 13:50:47 +02004835 /* Disable RC states. */
4836 I915_WRITE(GEN6_RC_CONTROL, 0);
4837
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004838 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004839 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4840 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4841 I915_WRITE(GEN6_RP_UP_EI, 66000);
4842 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4843
4844 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4845
4846 I915_WRITE(GEN6_RP_CONTROL,
4847 GEN6_RP_MEDIA_TURBO |
4848 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4849 GEN6_RP_MEDIA_IS_GFX |
4850 GEN6_RP_ENABLE |
4851 GEN6_RP_UP_BUSY_AVG |
4852 GEN6_RP_DOWN_IDLE_CONT);
4853
4854 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4855 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4856 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4857
4858 for_each_ring(ring, dev_priv, i)
4859 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4860
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08004861 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004862
4863 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004864 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004865 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4866 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004867 VLV_MEDIA_RC6_COUNT_EN |
4868 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004869
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004870 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004871 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004872
4873 intel_print_rc6_info(dev, rc6_mode);
4874
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004875 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004876
Jani Nikula64936252013-05-22 15:36:20 +03004877 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004878
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004879 /* RPS code assumes GPLL is used */
4880 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4881
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004882 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07004883 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4884
Ben Widawskyb39fb292014-03-19 18:31:11 -07004885 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004886 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004887 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004888 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004889
Ville Syrjälä73008b92013-06-25 19:21:01 +03004890 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004891 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004892 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004893
Ben Widawskyb39fb292014-03-19 18:31:11 -07004894 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004895
Mika Kuoppala59bad942015-01-16 11:34:40 +02004896 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004897}
4898
Daniel Vetter930ebb42012-06-29 23:32:16 +02004899void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004900{
4901 struct drm_i915_private *dev_priv = dev->dev_private;
4902
Daniel Vetter3e373942012-11-02 19:55:04 +01004903 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004904 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004905 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4906 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004907 }
4908
Daniel Vetter3e373942012-11-02 19:55:04 +01004909 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004910 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004911 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4912 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004913 }
4914}
4915
Daniel Vetter930ebb42012-06-29 23:32:16 +02004916static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004917{
4918 struct drm_i915_private *dev_priv = dev->dev_private;
4919
4920 if (I915_READ(PWRCTXA)) {
4921 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4922 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4923 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4924 50);
4925
4926 I915_WRITE(PWRCTXA, 0);
4927 POSTING_READ(PWRCTXA);
4928
4929 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4930 POSTING_READ(RSTDBYCTL);
4931 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004932}
4933
4934static int ironlake_setup_rc6(struct drm_device *dev)
4935{
4936 struct drm_i915_private *dev_priv = dev->dev_private;
4937
Daniel Vetter3e373942012-11-02 19:55:04 +01004938 if (dev_priv->ips.renderctx == NULL)
4939 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4940 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004941 return -ENOMEM;
4942
Daniel Vetter3e373942012-11-02 19:55:04 +01004943 if (dev_priv->ips.pwrctx == NULL)
4944 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4945 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004946 ironlake_teardown_rc6(dev);
4947 return -ENOMEM;
4948 }
4949
4950 return 0;
4951}
4952
Daniel Vetter930ebb42012-06-29 23:32:16 +02004953static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004954{
4955 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004956 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004957 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004958 int ret;
4959
4960 /* rc6 disabled by default due to repeated reports of hanging during
4961 * boot and resume.
4962 */
4963 if (!intel_enable_rc6(dev))
4964 return;
4965
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004966 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4967
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004968 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004969 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004970 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004971
Chris Wilson3e960502012-11-27 16:22:54 +00004972 was_interruptible = dev_priv->mm.interruptible;
4973 dev_priv->mm.interruptible = false;
4974
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004975 /*
4976 * GPU can automatically power down the render unit if given a page
4977 * to save state.
4978 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004979 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004980 if (ret) {
4981 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004982 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004983 return;
4984 }
4985
Daniel Vetter6d90c952012-04-26 23:28:05 +02004986 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4987 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004988 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004989 MI_MM_SPACE_GTT |
4990 MI_SAVE_EXT_STATE_EN |
4991 MI_RESTORE_EXT_STATE_EN |
4992 MI_RESTORE_INHIBIT);
4993 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4994 intel_ring_emit(ring, MI_NOOP);
4995 intel_ring_emit(ring, MI_FLUSH);
4996 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004997
4998 /*
4999 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
5000 * does an implicit flush, combined with MI_FLUSH above, it should be
5001 * safe to assume that renderctx is valid
5002 */
Chris Wilson3e960502012-11-27 16:22:54 +00005003 ret = intel_ring_idle(ring);
5004 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005005 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02005006 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005007 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005008 return;
5009 }
5010
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005011 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005012 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005013
Imre Deak91ca6892014-04-14 20:24:25 +03005014 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005015}
5016
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005017static unsigned long intel_pxfreq(u32 vidfreq)
5018{
5019 unsigned long freq;
5020 int div = (vidfreq & 0x3f0000) >> 16;
5021 int post = (vidfreq & 0x3000) >> 12;
5022 int pre = (vidfreq & 0x7);
5023
5024 if (!pre)
5025 return 0;
5026
5027 freq = ((div * 133333) / ((1<<post) * pre));
5028
5029 return freq;
5030}
5031
Daniel Vettereb48eb02012-04-26 23:28:12 +02005032static const struct cparams {
5033 u16 i;
5034 u16 t;
5035 u16 m;
5036 u16 c;
5037} cparams[] = {
5038 { 1, 1333, 301, 28664 },
5039 { 1, 1066, 294, 24460 },
5040 { 1, 800, 294, 25192 },
5041 { 0, 1333, 276, 27605 },
5042 { 0, 1066, 276, 27605 },
5043 { 0, 800, 231, 23784 },
5044};
5045
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005046static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005047{
5048 u64 total_count, diff, ret;
5049 u32 count1, count2, count3, m = 0, c = 0;
5050 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5051 int i;
5052
Daniel Vetter02d71952012-08-09 16:44:54 +02005053 assert_spin_locked(&mchdev_lock);
5054
Daniel Vetter20e4d402012-08-08 23:35:39 +02005055 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005056
5057 /* Prevent division-by-zero if we are asking too fast.
5058 * Also, we don't get interesting results if we are polling
5059 * faster than once in 10ms, so just return the saved value
5060 * in such cases.
5061 */
5062 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005063 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005064
5065 count1 = I915_READ(DMIEC);
5066 count2 = I915_READ(DDREC);
5067 count3 = I915_READ(CSIEC);
5068
5069 total_count = count1 + count2 + count3;
5070
5071 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005072 if (total_count < dev_priv->ips.last_count1) {
5073 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005074 diff += total_count;
5075 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005076 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005077 }
5078
5079 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005080 if (cparams[i].i == dev_priv->ips.c_m &&
5081 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005082 m = cparams[i].m;
5083 c = cparams[i].c;
5084 break;
5085 }
5086 }
5087
5088 diff = div_u64(diff, diff1);
5089 ret = ((m * diff) + c);
5090 ret = div_u64(ret, 10);
5091
Daniel Vetter20e4d402012-08-08 23:35:39 +02005092 dev_priv->ips.last_count1 = total_count;
5093 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005094
Daniel Vetter20e4d402012-08-08 23:35:39 +02005095 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005096
5097 return ret;
5098}
5099
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005100unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5101{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005102 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005103 unsigned long val;
5104
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005105 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005106 return 0;
5107
5108 spin_lock_irq(&mchdev_lock);
5109
5110 val = __i915_chipset_val(dev_priv);
5111
5112 spin_unlock_irq(&mchdev_lock);
5113
5114 return val;
5115}
5116
Daniel Vettereb48eb02012-04-26 23:28:12 +02005117unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5118{
5119 unsigned long m, x, b;
5120 u32 tsfs;
5121
5122 tsfs = I915_READ(TSFS);
5123
5124 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5125 x = I915_READ8(TR1);
5126
5127 b = tsfs & TSFS_INTR_MASK;
5128
5129 return ((m * x) / 127) - b;
5130}
5131
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005132static int _pxvid_to_vd(u8 pxvid)
5133{
5134 if (pxvid == 0)
5135 return 0;
5136
5137 if (pxvid >= 8 && pxvid < 31)
5138 pxvid = 31;
5139
5140 return (pxvid + 2) * 125;
5141}
5142
5143static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005144{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005145 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005146 const int vd = _pxvid_to_vd(pxvid);
5147 const int vm = vd - 1125;
5148
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005149 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005150 return vm > 0 ? vm : 0;
5151
5152 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005153}
5154
Daniel Vetter02d71952012-08-09 16:44:54 +02005155static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005156{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005157 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005158 u32 count;
5159
Daniel Vetter02d71952012-08-09 16:44:54 +02005160 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005161
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005162 now = ktime_get_raw_ns();
5163 diffms = now - dev_priv->ips.last_time2;
5164 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005165
5166 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005167 if (!diffms)
5168 return;
5169
5170 count = I915_READ(GFXEC);
5171
Daniel Vetter20e4d402012-08-08 23:35:39 +02005172 if (count < dev_priv->ips.last_count2) {
5173 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005174 diff += count;
5175 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005176 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005177 }
5178
Daniel Vetter20e4d402012-08-08 23:35:39 +02005179 dev_priv->ips.last_count2 = count;
5180 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005181
5182 /* More magic constants... */
5183 diff = diff * 1181;
5184 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005185 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005186}
5187
Daniel Vetter02d71952012-08-09 16:44:54 +02005188void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5189{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005190 struct drm_device *dev = dev_priv->dev;
5191
5192 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005193 return;
5194
Daniel Vetter92703882012-08-09 16:46:01 +02005195 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005196
5197 __i915_update_gfx_val(dev_priv);
5198
Daniel Vetter92703882012-08-09 16:46:01 +02005199 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005200}
5201
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005202static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005203{
5204 unsigned long t, corr, state1, corr2, state2;
5205 u32 pxvid, ext_v;
5206
Daniel Vetter02d71952012-08-09 16:44:54 +02005207 assert_spin_locked(&mchdev_lock);
5208
Ben Widawskyb39fb292014-03-19 18:31:11 -07005209 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005210 pxvid = (pxvid >> 24) & 0x7f;
5211 ext_v = pvid_to_extvid(dev_priv, pxvid);
5212
5213 state1 = ext_v;
5214
5215 t = i915_mch_val(dev_priv);
5216
5217 /* Revel in the empirically derived constants */
5218
5219 /* Correction factor in 1/100000 units */
5220 if (t > 80)
5221 corr = ((t * 2349) + 135940);
5222 else if (t >= 50)
5223 corr = ((t * 964) + 29317);
5224 else /* < 50 */
5225 corr = ((t * 301) + 1004);
5226
5227 corr = corr * ((150142 * state1) / 10000 - 78642);
5228 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005229 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005230
5231 state2 = (corr2 * state1) / 10000;
5232 state2 /= 100; /* convert to mW */
5233
Daniel Vetter02d71952012-08-09 16:44:54 +02005234 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005235
Daniel Vetter20e4d402012-08-08 23:35:39 +02005236 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005237}
5238
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005239unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5240{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005241 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005242 unsigned long val;
5243
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005244 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005245 return 0;
5246
5247 spin_lock_irq(&mchdev_lock);
5248
5249 val = __i915_gfx_val(dev_priv);
5250
5251 spin_unlock_irq(&mchdev_lock);
5252
5253 return val;
5254}
5255
Daniel Vettereb48eb02012-04-26 23:28:12 +02005256/**
5257 * i915_read_mch_val - return value for IPS use
5258 *
5259 * Calculate and return a value for the IPS driver to use when deciding whether
5260 * we have thermal and power headroom to increase CPU or GPU power budget.
5261 */
5262unsigned long i915_read_mch_val(void)
5263{
5264 struct drm_i915_private *dev_priv;
5265 unsigned long chipset_val, graphics_val, ret = 0;
5266
Daniel Vetter92703882012-08-09 16:46:01 +02005267 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005268 if (!i915_mch_dev)
5269 goto out_unlock;
5270 dev_priv = i915_mch_dev;
5271
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005272 chipset_val = __i915_chipset_val(dev_priv);
5273 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005274
5275 ret = chipset_val + graphics_val;
5276
5277out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005278 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005279
5280 return ret;
5281}
5282EXPORT_SYMBOL_GPL(i915_read_mch_val);
5283
5284/**
5285 * i915_gpu_raise - raise GPU frequency limit
5286 *
5287 * Raise the limit; IPS indicates we have thermal headroom.
5288 */
5289bool i915_gpu_raise(void)
5290{
5291 struct drm_i915_private *dev_priv;
5292 bool ret = true;
5293
Daniel Vetter92703882012-08-09 16:46:01 +02005294 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005295 if (!i915_mch_dev) {
5296 ret = false;
5297 goto out_unlock;
5298 }
5299 dev_priv = i915_mch_dev;
5300
Daniel Vetter20e4d402012-08-08 23:35:39 +02005301 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5302 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005303
5304out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005305 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005306
5307 return ret;
5308}
5309EXPORT_SYMBOL_GPL(i915_gpu_raise);
5310
5311/**
5312 * i915_gpu_lower - lower GPU frequency limit
5313 *
5314 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5315 * frequency maximum.
5316 */
5317bool i915_gpu_lower(void)
5318{
5319 struct drm_i915_private *dev_priv;
5320 bool ret = true;
5321
Daniel Vetter92703882012-08-09 16:46:01 +02005322 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005323 if (!i915_mch_dev) {
5324 ret = false;
5325 goto out_unlock;
5326 }
5327 dev_priv = i915_mch_dev;
5328
Daniel Vetter20e4d402012-08-08 23:35:39 +02005329 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5330 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005331
5332out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005333 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005334
5335 return ret;
5336}
5337EXPORT_SYMBOL_GPL(i915_gpu_lower);
5338
5339/**
5340 * i915_gpu_busy - indicate GPU business to IPS
5341 *
5342 * Tell the IPS driver whether or not the GPU is busy.
5343 */
5344bool i915_gpu_busy(void)
5345{
5346 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005347 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005348 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005349 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005350
Daniel Vetter92703882012-08-09 16:46:01 +02005351 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005352 if (!i915_mch_dev)
5353 goto out_unlock;
5354 dev_priv = i915_mch_dev;
5355
Chris Wilsonf047e392012-07-21 12:31:41 +01005356 for_each_ring(ring, dev_priv, i)
5357 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005358
5359out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005360 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005361
5362 return ret;
5363}
5364EXPORT_SYMBOL_GPL(i915_gpu_busy);
5365
5366/**
5367 * i915_gpu_turbo_disable - disable graphics turbo
5368 *
5369 * Disable graphics turbo by resetting the max frequency and setting the
5370 * current frequency to the default.
5371 */
5372bool i915_gpu_turbo_disable(void)
5373{
5374 struct drm_i915_private *dev_priv;
5375 bool ret = true;
5376
Daniel Vetter92703882012-08-09 16:46:01 +02005377 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005378 if (!i915_mch_dev) {
5379 ret = false;
5380 goto out_unlock;
5381 }
5382 dev_priv = i915_mch_dev;
5383
Daniel Vetter20e4d402012-08-08 23:35:39 +02005384 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005385
Daniel Vetter20e4d402012-08-08 23:35:39 +02005386 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005387 ret = false;
5388
5389out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005390 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005391
5392 return ret;
5393}
5394EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5395
5396/**
5397 * Tells the intel_ips driver that the i915 driver is now loaded, if
5398 * IPS got loaded first.
5399 *
5400 * This awkward dance is so that neither module has to depend on the
5401 * other in order for IPS to do the appropriate communication of
5402 * GPU turbo limits to i915.
5403 */
5404static void
5405ips_ping_for_i915_load(void)
5406{
5407 void (*link)(void);
5408
5409 link = symbol_get(ips_link_to_i915_driver);
5410 if (link) {
5411 link();
5412 symbol_put(ips_link_to_i915_driver);
5413 }
5414}
5415
5416void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5417{
Daniel Vetter02d71952012-08-09 16:44:54 +02005418 /* We only register the i915 ips part with intel-ips once everything is
5419 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005420 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005421 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005422 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005423
5424 ips_ping_for_i915_load();
5425}
5426
5427void intel_gpu_ips_teardown(void)
5428{
Daniel Vetter92703882012-08-09 16:46:01 +02005429 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005430 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005431 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005432}
Deepak S76c3552f2014-01-30 23:08:16 +05305433
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005434static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005435{
5436 struct drm_i915_private *dev_priv = dev->dev_private;
5437 u32 lcfuse;
5438 u8 pxw[16];
5439 int i;
5440
5441 /* Disable to program */
5442 I915_WRITE(ECR, 0);
5443 POSTING_READ(ECR);
5444
5445 /* Program energy weights for various events */
5446 I915_WRITE(SDEW, 0x15040d00);
5447 I915_WRITE(CSIEW0, 0x007f0000);
5448 I915_WRITE(CSIEW1, 0x1e220004);
5449 I915_WRITE(CSIEW2, 0x04000004);
5450
5451 for (i = 0; i < 5; i++)
5452 I915_WRITE(PEW + (i * 4), 0);
5453 for (i = 0; i < 3; i++)
5454 I915_WRITE(DEW + (i * 4), 0);
5455
5456 /* Program P-state weights to account for frequency power adjustment */
5457 for (i = 0; i < 16; i++) {
5458 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5459 unsigned long freq = intel_pxfreq(pxvidfreq);
5460 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5461 PXVFREQ_PX_SHIFT;
5462 unsigned long val;
5463
5464 val = vid * vid;
5465 val *= (freq / 1000);
5466 val *= 255;
5467 val /= (127*127*900);
5468 if (val > 0xff)
5469 DRM_ERROR("bad pxval: %ld\n", val);
5470 pxw[i] = val;
5471 }
5472 /* Render standby states get 0 weight */
5473 pxw[14] = 0;
5474 pxw[15] = 0;
5475
5476 for (i = 0; i < 4; i++) {
5477 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5478 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5479 I915_WRITE(PXW + (i * 4), val);
5480 }
5481
5482 /* Adjust magic regs to magic values (more experimental results) */
5483 I915_WRITE(OGW0, 0);
5484 I915_WRITE(OGW1, 0);
5485 I915_WRITE(EG0, 0x00007f00);
5486 I915_WRITE(EG1, 0x0000000e);
5487 I915_WRITE(EG2, 0x000e0000);
5488 I915_WRITE(EG3, 0x68000300);
5489 I915_WRITE(EG4, 0x42000000);
5490 I915_WRITE(EG5, 0x00140031);
5491 I915_WRITE(EG6, 0);
5492 I915_WRITE(EG7, 0);
5493
5494 for (i = 0; i < 8; i++)
5495 I915_WRITE(PXWL + (i * 4), 0);
5496
5497 /* Enable PMON + select events */
5498 I915_WRITE(ECR, 0x80000019);
5499
5500 lcfuse = I915_READ(LCFUSE02);
5501
Daniel Vetter20e4d402012-08-08 23:35:39 +02005502 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005503}
5504
Imre Deakae484342014-03-31 15:10:44 +03005505void intel_init_gt_powersave(struct drm_device *dev)
5506{
Imre Deake6069ca2014-04-18 16:01:02 +03005507 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5508
Deepak S38807742014-05-23 21:00:15 +05305509 if (IS_CHERRYVIEW(dev))
5510 cherryview_init_gt_powersave(dev);
5511 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005512 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005513}
5514
5515void intel_cleanup_gt_powersave(struct drm_device *dev)
5516{
Deepak S38807742014-05-23 21:00:15 +05305517 if (IS_CHERRYVIEW(dev))
5518 return;
5519 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005520 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005521}
5522
Imre Deakdbea3ce2014-12-15 18:59:28 +02005523static void gen6_suspend_rps(struct drm_device *dev)
5524{
5525 struct drm_i915_private *dev_priv = dev->dev_private;
5526
5527 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5528
5529 /*
5530 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5531 * is added for it.
5532 */
5533 if (INTEL_INFO(dev)->gen < 9)
5534 gen6_disable_rps_interrupts(dev);
5535}
5536
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005537/**
5538 * intel_suspend_gt_powersave - suspend PM work and helper threads
5539 * @dev: drm device
5540 *
5541 * We don't want to disable RC6 or other features here, we just want
5542 * to make sure any work we've queued has finished and won't bother
5543 * us while we're suspended.
5544 */
5545void intel_suspend_gt_powersave(struct drm_device *dev)
5546{
5547 struct drm_i915_private *dev_priv = dev->dev_private;
5548
Imre Deakd4d70aa2014-11-19 15:30:04 +02005549 if (INTEL_INFO(dev)->gen < 6)
5550 return;
5551
Imre Deakdbea3ce2014-12-15 18:59:28 +02005552 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305553
5554 /* Force GPU to min freq during suspend */
5555 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005556}
5557
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005558void intel_disable_gt_powersave(struct drm_device *dev)
5559{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005560 struct drm_i915_private *dev_priv = dev->dev_private;
5561
Daniel Vetter930ebb42012-06-29 23:32:16 +02005562 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005563 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005564 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305565 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005566 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005567
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005568 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005569 if (INTEL_INFO(dev)->gen >= 9)
5570 gen9_disable_rps(dev);
5571 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305572 cherryview_disable_rps(dev);
5573 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005574 valleyview_disable_rps(dev);
5575 else
5576 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005577
Chris Wilsonc0951f02013-10-10 21:58:50 +01005578 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005579 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005580 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005581}
5582
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005583static void intel_gen6_powersave_work(struct work_struct *work)
5584{
5585 struct drm_i915_private *dev_priv =
5586 container_of(work, struct drm_i915_private,
5587 rps.delayed_resume_work.work);
5588 struct drm_device *dev = dev_priv->dev;
5589
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005590 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005591
Imre Deak3cc134e2014-11-19 15:30:03 +02005592 /*
5593 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5594 * added for it.
5595 */
5596 if (INTEL_INFO(dev)->gen < 9)
5597 gen6_reset_rps_interrupts(dev);
5598
Deepak S38807742014-05-23 21:00:15 +05305599 if (IS_CHERRYVIEW(dev)) {
5600 cherryview_enable_rps(dev);
5601 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005602 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005603 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005604 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005605 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005606 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005607 } else if (IS_BROADWELL(dev)) {
5608 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005609 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005610 } else {
5611 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005612 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005613 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005614 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005615
5616 if (INTEL_INFO(dev)->gen < 9)
5617 gen6_enable_rps_interrupts(dev);
5618
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005619 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005620
5621 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005622}
5623
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005624void intel_enable_gt_powersave(struct drm_device *dev)
5625{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005626 struct drm_i915_private *dev_priv = dev->dev_private;
5627
Yu Zhangf61018b2015-02-10 19:05:52 +08005628 /* Powersaving is controlled by the host when inside a VM */
5629 if (intel_vgpu_active(dev))
5630 return;
5631
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005632 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005633 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005634 ironlake_enable_drps(dev);
5635 ironlake_enable_rc6(dev);
5636 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005637 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305638 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005639 /*
5640 * PCU communication is slow and this doesn't need to be
5641 * done at any specific time, so do this out of our fast path
5642 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005643 *
5644 * We depend on the HW RC6 power context save/restore
5645 * mechanism when entering D3 through runtime PM suspend. So
5646 * disable RPM until RPS/RC6 is properly setup. We can only
5647 * get here via the driver load/system resume/runtime resume
5648 * paths, so the _noresume version is enough (and in case of
5649 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005650 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005651 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5652 round_jiffies_up_relative(HZ)))
5653 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005654 }
5655}
5656
Imre Deakc6df39b2014-04-14 20:24:29 +03005657void intel_reset_gt_powersave(struct drm_device *dev)
5658{
5659 struct drm_i915_private *dev_priv = dev->dev_private;
5660
Imre Deakdbea3ce2014-12-15 18:59:28 +02005661 if (INTEL_INFO(dev)->gen < 6)
5662 return;
5663
5664 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005665 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005666}
5667
Daniel Vetter3107bd42012-10-31 22:52:31 +01005668static void ibx_init_clock_gating(struct drm_device *dev)
5669{
5670 struct drm_i915_private *dev_priv = dev->dev_private;
5671
5672 /*
5673 * On Ibex Peak and Cougar Point, we need to disable clock
5674 * gating for the panel power sequencer or it will fail to
5675 * start up when no ports are active.
5676 */
5677 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5678}
5679
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005680static void g4x_disable_trickle_feed(struct drm_device *dev)
5681{
5682 struct drm_i915_private *dev_priv = dev->dev_private;
5683 int pipe;
5684
Damien Lespiau055e3932014-08-18 13:49:10 +01005685 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005686 I915_WRITE(DSPCNTR(pipe),
5687 I915_READ(DSPCNTR(pipe)) |
5688 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005689 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005690 }
5691}
5692
Ville Syrjälä017636c2013-12-05 15:51:37 +02005693static void ilk_init_lp_watermarks(struct drm_device *dev)
5694{
5695 struct drm_i915_private *dev_priv = dev->dev_private;
5696
5697 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5698 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5699 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5700
5701 /*
5702 * Don't touch WM1S_LP_EN here.
5703 * Doing so could cause underruns.
5704 */
5705}
5706
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005707static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005708{
5709 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005710 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005711
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005712 /*
5713 * Required for FBC
5714 * WaFbcDisableDpfcClockGating:ilk
5715 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005716 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5717 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5718 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005719
5720 I915_WRITE(PCH_3DCGDIS0,
5721 MARIUNIT_CLOCK_GATE_DISABLE |
5722 SVSMUNIT_CLOCK_GATE_DISABLE);
5723 I915_WRITE(PCH_3DCGDIS1,
5724 VFMUNIT_CLOCK_GATE_DISABLE);
5725
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005726 /*
5727 * According to the spec the following bits should be set in
5728 * order to enable memory self-refresh
5729 * The bit 22/21 of 0x42004
5730 * The bit 5 of 0x42020
5731 * The bit 15 of 0x45000
5732 */
5733 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5734 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5735 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005736 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005737 I915_WRITE(DISP_ARB_CTL,
5738 (I915_READ(DISP_ARB_CTL) |
5739 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005740
5741 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005742
5743 /*
5744 * Based on the document from hardware guys the following bits
5745 * should be set unconditionally in order to enable FBC.
5746 * The bit 22 of 0x42000
5747 * The bit 22 of 0x42004
5748 * The bit 7,8,9 of 0x42020.
5749 */
5750 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005751 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005752 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5753 I915_READ(ILK_DISPLAY_CHICKEN1) |
5754 ILK_FBCQ_DIS);
5755 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5756 I915_READ(ILK_DISPLAY_CHICKEN2) |
5757 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005758 }
5759
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005760 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5761
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005762 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5763 I915_READ(ILK_DISPLAY_CHICKEN2) |
5764 ILK_ELPIN_409_SELECT);
5765 I915_WRITE(_3D_CHICKEN2,
5766 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5767 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005768
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005769 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005770 I915_WRITE(CACHE_MODE_0,
5771 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005772
Akash Goel4e046322014-04-04 17:14:38 +05305773 /* WaDisable_RenderCache_OperationalFlush:ilk */
5774 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5775
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005776 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005777
Daniel Vetter3107bd42012-10-31 22:52:31 +01005778 ibx_init_clock_gating(dev);
5779}
5780
5781static void cpt_init_clock_gating(struct drm_device *dev)
5782{
5783 struct drm_i915_private *dev_priv = dev->dev_private;
5784 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005785 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005786
5787 /*
5788 * On Ibex Peak and Cougar Point, we need to disable clock
5789 * gating for the panel power sequencer or it will fail to
5790 * start up when no ports are active.
5791 */
Jesse Barnescd664072013-10-02 10:34:19 -07005792 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5793 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5794 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005795 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5796 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005797 /* The below fixes the weird display corruption, a few pixels shifted
5798 * downward, on (only) LVDS of some HP laptops with IVY.
5799 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005800 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005801 val = I915_READ(TRANS_CHICKEN2(pipe));
5802 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5803 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005804 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005805 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005806 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5807 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5808 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005809 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5810 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005811 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005812 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005813 I915_WRITE(TRANS_CHICKEN1(pipe),
5814 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5815 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005816}
5817
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005818static void gen6_check_mch_setup(struct drm_device *dev)
5819{
5820 struct drm_i915_private *dev_priv = dev->dev_private;
5821 uint32_t tmp;
5822
5823 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005824 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5825 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5826 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005827}
5828
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005829static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005830{
5831 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005832 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005833
Damien Lespiau231e54f2012-10-19 17:55:41 +01005834 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005835
5836 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5837 I915_READ(ILK_DISPLAY_CHICKEN2) |
5838 ILK_ELPIN_409_SELECT);
5839
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005840 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005841 I915_WRITE(_3D_CHICKEN,
5842 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5843
Akash Goel4e046322014-04-04 17:14:38 +05305844 /* WaDisable_RenderCache_OperationalFlush:snb */
5845 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5846
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005847 /*
5848 * BSpec recoomends 8x4 when MSAA is used,
5849 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005850 *
5851 * Note that PS/WM thread counts depend on the WIZ hashing
5852 * disable bit, which we don't touch here, but it's good
5853 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005854 */
5855 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005856 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005857
Ville Syrjälä017636c2013-12-05 15:51:37 +02005858 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005859
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005860 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005861 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005862
5863 I915_WRITE(GEN6_UCGCTL1,
5864 I915_READ(GEN6_UCGCTL1) |
5865 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5866 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5867
5868 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5869 * gating disable must be set. Failure to set it results in
5870 * flickering pixels due to Z write ordering failures after
5871 * some amount of runtime in the Mesa "fire" demo, and Unigine
5872 * Sanctuary and Tropics, and apparently anything else with
5873 * alpha test or pixel discard.
5874 *
5875 * According to the spec, bit 11 (RCCUNIT) must also be set,
5876 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005877 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005878 * WaDisableRCCUnitClockGating:snb
5879 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005880 */
5881 I915_WRITE(GEN6_UCGCTL2,
5882 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5883 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5884
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005885 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005886 I915_WRITE(_3D_CHICKEN3,
5887 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005888
5889 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005890 * Bspec says:
5891 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5892 * 3DSTATE_SF number of SF output attributes is more than 16."
5893 */
5894 I915_WRITE(_3D_CHICKEN3,
5895 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5896
5897 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005898 * According to the spec the following bits should be
5899 * set in order to enable memory self-refresh and fbc:
5900 * The bit21 and bit22 of 0x42000
5901 * The bit21 and bit22 of 0x42004
5902 * The bit5 and bit7 of 0x42020
5903 * The bit14 of 0x70180
5904 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005905 *
5906 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005907 */
5908 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5909 I915_READ(ILK_DISPLAY_CHICKEN1) |
5910 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5911 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5912 I915_READ(ILK_DISPLAY_CHICKEN2) |
5913 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005914 I915_WRITE(ILK_DSPCLK_GATE_D,
5915 I915_READ(ILK_DSPCLK_GATE_D) |
5916 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5917 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005918
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005919 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005920
Daniel Vetter3107bd42012-10-31 22:52:31 +01005921 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005922
5923 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005924}
5925
5926static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5927{
5928 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5929
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005930 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005931 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005932 *
5933 * This actually overrides the dispatch
5934 * mode for all thread types.
5935 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005936 reg &= ~GEN7_FF_SCHED_MASK;
5937 reg |= GEN7_FF_TS_SCHED_HW;
5938 reg |= GEN7_FF_VS_SCHED_HW;
5939 reg |= GEN7_FF_DS_SCHED_HW;
5940
5941 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5942}
5943
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005944static void lpt_init_clock_gating(struct drm_device *dev)
5945{
5946 struct drm_i915_private *dev_priv = dev->dev_private;
5947
5948 /*
5949 * TODO: this bit should only be enabled when really needed, then
5950 * disabled when not needed anymore in order to save power.
5951 */
5952 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5953 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5954 I915_READ(SOUTH_DSPCLK_GATE_D) |
5955 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005956
5957 /* WADPOClockGatingDisable:hsw */
5958 I915_WRITE(_TRANSA_CHICKEN1,
5959 I915_READ(_TRANSA_CHICKEN1) |
5960 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005961}
5962
Imre Deak7d708ee2013-04-17 14:04:50 +03005963static void lpt_suspend_hw(struct drm_device *dev)
5964{
5965 struct drm_i915_private *dev_priv = dev->dev_private;
5966
5967 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5968 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5969
5970 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5971 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5972 }
5973}
5974
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005975static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005976{
5977 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005978 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005979
5980 I915_WRITE(WM3_LP_ILK, 0);
5981 I915_WRITE(WM2_LP_ILK, 0);
5982 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005983
Ben Widawskyab57fff2013-12-12 15:28:04 -08005984 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005985 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005986
Ben Widawskyab57fff2013-12-12 15:28:04 -08005987 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005988 I915_WRITE(CHICKEN_PAR1_1,
5989 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5990
Ben Widawskyab57fff2013-12-12 15:28:04 -08005991 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005992 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005993 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005994 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005995 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005996 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005997
Ben Widawskyab57fff2013-12-12 15:28:04 -08005998 /* WaVSRefCountFullforceMissDisable:bdw */
5999 /* WaDSRefCountFullforceMissDisable:bdw */
6000 I915_WRITE(GEN7_FF_THREAD_MODE,
6001 I915_READ(GEN7_FF_THREAD_MODE) &
6002 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006003
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006004 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6005 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006006
6007 /* WaDisableSDEUnitClockGating:bdw */
6008 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6009 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006010
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006011 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006012}
6013
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006014static void haswell_init_clock_gating(struct drm_device *dev)
6015{
6016 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006017
Ville Syrjälä017636c2013-12-05 15:51:37 +02006018 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006019
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006020 /* L3 caching of data atomics doesn't work -- disable it. */
6021 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6022 I915_WRITE(HSW_ROW_CHICKEN3,
6023 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6024
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006025 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006026 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6027 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6028 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6029
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006030 /* WaVSRefCountFullforceMissDisable:hsw */
6031 I915_WRITE(GEN7_FF_THREAD_MODE,
6032 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006033
Akash Goel4e046322014-04-04 17:14:38 +05306034 /* WaDisable_RenderCache_OperationalFlush:hsw */
6035 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6036
Chia-I Wufe27c602014-01-28 13:29:33 +08006037 /* enable HiZ Raw Stall Optimization */
6038 I915_WRITE(CACHE_MODE_0_GEN7,
6039 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6040
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006041 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006042 I915_WRITE(CACHE_MODE_1,
6043 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006044
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006045 /*
6046 * BSpec recommends 8x4 when MSAA is used,
6047 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006048 *
6049 * Note that PS/WM thread counts depend on the WIZ hashing
6050 * disable bit, which we don't touch here, but it's good
6051 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006052 */
6053 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006054 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006055
Kenneth Graunke94411592014-12-31 16:23:00 -08006056 /* WaSampleCChickenBitEnable:hsw */
6057 I915_WRITE(HALF_SLICE_CHICKEN3,
6058 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6059
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006060 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006061 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6062
Paulo Zanoni90a88642013-05-03 17:23:45 -03006063 /* WaRsPkgCStateDisplayPMReq:hsw */
6064 I915_WRITE(CHICKEN_PAR1_1,
6065 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006066
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006067 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006068}
6069
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006070static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006071{
6072 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006073 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006074
Ville Syrjälä017636c2013-12-05 15:51:37 +02006075 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006076
Damien Lespiau231e54f2012-10-19 17:55:41 +01006077 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006078
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006079 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006080 I915_WRITE(_3D_CHICKEN3,
6081 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6082
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006083 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006084 I915_WRITE(IVB_CHICKEN3,
6085 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6086 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6087
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006088 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006089 if (IS_IVB_GT1(dev))
6090 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6091 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006092
Akash Goel4e046322014-04-04 17:14:38 +05306093 /* WaDisable_RenderCache_OperationalFlush:ivb */
6094 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6095
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006096 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006097 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6098 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6099
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006100 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006101 I915_WRITE(GEN7_L3CNTLREG1,
6102 GEN7_WA_FOR_GEN7_L3_CONTROL);
6103 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006104 GEN7_WA_L3_CHICKEN_MODE);
6105 if (IS_IVB_GT1(dev))
6106 I915_WRITE(GEN7_ROW_CHICKEN2,
6107 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006108 else {
6109 /* must write both registers */
6110 I915_WRITE(GEN7_ROW_CHICKEN2,
6111 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006112 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6113 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006114 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006115
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006116 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006117 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6118 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6119
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006120 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006121 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006122 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006123 */
6124 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006125 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006126
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006127 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006128 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6129 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6130 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6131
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006132 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006133
6134 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006135
Chris Wilson22721342014-03-04 09:41:43 +00006136 if (0) { /* causes HiZ corruption on ivb:gt1 */
6137 /* enable HiZ Raw Stall Optimization */
6138 I915_WRITE(CACHE_MODE_0_GEN7,
6139 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6140 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006141
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006142 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006143 I915_WRITE(CACHE_MODE_1,
6144 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006145
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006146 /*
6147 * BSpec recommends 8x4 when MSAA is used,
6148 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006149 *
6150 * Note that PS/WM thread counts depend on the WIZ hashing
6151 * disable bit, which we don't touch here, but it's good
6152 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006153 */
6154 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006155 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006156
Ben Widawsky20848222012-05-04 18:58:59 -07006157 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6158 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6159 snpcr |= GEN6_MBC_SNPCR_MED;
6160 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006161
Ben Widawskyab5c6082013-04-05 13:12:41 -07006162 if (!HAS_PCH_NOP(dev))
6163 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006164
6165 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006166}
6167
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006168static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006169{
6170 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006171
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03006172 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006173
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006174 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006175 I915_WRITE(_3D_CHICKEN3,
6176 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6177
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006178 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006179 I915_WRITE(IVB_CHICKEN3,
6180 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6181 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6182
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006183 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006184 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006185 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006186 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6187 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006188
Akash Goel4e046322014-04-04 17:14:38 +05306189 /* WaDisable_RenderCache_OperationalFlush:vlv */
6190 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6191
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006192 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006193 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6194 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6195
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006196 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006197 I915_WRITE(GEN7_ROW_CHICKEN2,
6198 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6199
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006200 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006201 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6202 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6203 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6204
Ville Syrjälä46680e02014-01-22 21:33:01 +02006205 gen7_setup_fixed_func_scheduler(dev_priv);
6206
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006207 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006208 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006209 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006210 */
6211 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006212 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006213
Akash Goelc98f5062014-03-24 23:00:07 +05306214 /* WaDisableL3Bank2xClockGate:vlv
6215 * Disabling L3 clock gating- MMIO 940c[25] = 1
6216 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6217 I915_WRITE(GEN7_UCGCTL4,
6218 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006219
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006220 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006221
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006222 /*
6223 * BSpec says this must be set, even though
6224 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6225 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006226 I915_WRITE(CACHE_MODE_1,
6227 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006228
6229 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006230 * BSpec recommends 8x4 when MSAA is used,
6231 * however in practice 16x4 seems fastest.
6232 *
6233 * Note that PS/WM thread counts depend on the WIZ hashing
6234 * disable bit, which we don't touch here, but it's good
6235 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6236 */
6237 I915_WRITE(GEN7_GT_MODE,
6238 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6239
6240 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006241 * WaIncreaseL3CreditsForVLVB0:vlv
6242 * This is the hardware default actually.
6243 */
6244 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6245
6246 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006247 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006248 * Disable clock gating on th GCFG unit to prevent a delay
6249 * in the reporting of vblank events.
6250 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006251 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006252}
6253
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006254static void cherryview_init_clock_gating(struct drm_device *dev)
6255{
6256 struct drm_i915_private *dev_priv = dev->dev_private;
6257
6258 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6259
6260 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006261
Ville Syrjälä232ce332014-04-09 13:28:35 +03006262 /* WaVSRefCountFullforceMissDisable:chv */
6263 /* WaDSRefCountFullforceMissDisable:chv */
6264 I915_WRITE(GEN7_FF_THREAD_MODE,
6265 I915_READ(GEN7_FF_THREAD_MODE) &
6266 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006267
6268 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6269 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6270 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006271
6272 /* WaDisableCSUnitClockGating:chv */
6273 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6274 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006275
6276 /* WaDisableSDEUnitClockGating:chv */
6277 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6278 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006279}
6280
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006281static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006282{
6283 struct drm_i915_private *dev_priv = dev->dev_private;
6284 uint32_t dspclk_gate;
6285
6286 I915_WRITE(RENCLK_GATE_D1, 0);
6287 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6288 GS_UNIT_CLOCK_GATE_DISABLE |
6289 CL_UNIT_CLOCK_GATE_DISABLE);
6290 I915_WRITE(RAMCLK_GATE_D, 0);
6291 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6292 OVRUNIT_CLOCK_GATE_DISABLE |
6293 OVCUNIT_CLOCK_GATE_DISABLE;
6294 if (IS_GM45(dev))
6295 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6296 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006297
6298 /* WaDisableRenderCachePipelinedFlush */
6299 I915_WRITE(CACHE_MODE_0,
6300 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006301
Akash Goel4e046322014-04-04 17:14:38 +05306302 /* WaDisable_RenderCache_OperationalFlush:g4x */
6303 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6304
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006305 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006306}
6307
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006308static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006309{
6310 struct drm_i915_private *dev_priv = dev->dev_private;
6311
6312 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6313 I915_WRITE(RENCLK_GATE_D2, 0);
6314 I915_WRITE(DSPCLK_GATE_D, 0);
6315 I915_WRITE(RAMCLK_GATE_D, 0);
6316 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006317 I915_WRITE(MI_ARB_STATE,
6318 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306319
6320 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6321 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006322}
6323
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006324static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006325{
6326 struct drm_i915_private *dev_priv = dev->dev_private;
6327
6328 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6329 I965_RCC_CLOCK_GATE_DISABLE |
6330 I965_RCPB_CLOCK_GATE_DISABLE |
6331 I965_ISC_CLOCK_GATE_DISABLE |
6332 I965_FBC_CLOCK_GATE_DISABLE);
6333 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006334 I915_WRITE(MI_ARB_STATE,
6335 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306336
6337 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6338 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006339}
6340
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006341static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006342{
6343 struct drm_i915_private *dev_priv = dev->dev_private;
6344 u32 dstate = I915_READ(D_STATE);
6345
6346 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6347 DSTATE_DOT_CLOCK_GATING;
6348 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006349
6350 if (IS_PINEVIEW(dev))
6351 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006352
6353 /* IIR "flip pending" means done if this bit is set */
6354 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006355
6356 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006357 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006358
6359 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6360 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006361
6362 I915_WRITE(MI_ARB_STATE,
6363 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006364}
6365
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006366static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006367{
6368 struct drm_i915_private *dev_priv = dev->dev_private;
6369
6370 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006371
6372 /* interrupts should cause a wake up from C3 */
6373 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6374 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006375
6376 I915_WRITE(MEM_MODE,
6377 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006378}
6379
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006380static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006381{
6382 struct drm_i915_private *dev_priv = dev->dev_private;
6383
6384 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006385
6386 I915_WRITE(MEM_MODE,
6387 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6388 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006389}
6390
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006391void intel_init_clock_gating(struct drm_device *dev)
6392{
6393 struct drm_i915_private *dev_priv = dev->dev_private;
6394
Damien Lespiauc57e3552015-02-09 19:33:05 +00006395 if (dev_priv->display.init_clock_gating)
6396 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006397}
6398
Imre Deak7d708ee2013-04-17 14:04:50 +03006399void intel_suspend_hw(struct drm_device *dev)
6400{
6401 if (HAS_PCH_LPT(dev))
6402 lpt_suspend_hw(dev);
6403}
6404
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006405/* Set up chip specific power management-related functions */
6406void intel_init_pm(struct drm_device *dev)
6407{
6408 struct drm_i915_private *dev_priv = dev->dev_private;
6409
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006410 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006411
Daniel Vetterc921aba2012-04-26 23:28:17 +02006412 /* For cxsr */
6413 if (IS_PINEVIEW(dev))
6414 i915_pineview_get_mem_freq(dev);
6415 else if (IS_GEN5(dev))
6416 i915_ironlake_get_mem_freq(dev);
6417
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006418 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006419 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006420 skl_setup_wm_latency(dev);
6421
Damien Lespiauc83155a2014-03-28 00:18:35 +05306422 dev_priv->display.init_clock_gating = gen9_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006423 dev_priv->display.update_wm = skl_update_wm;
6424 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306425 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006426 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006427
Ville Syrjäläbd602542014-01-07 16:14:10 +02006428 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6429 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6430 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6431 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6432 dev_priv->display.update_wm = ilk_update_wm;
6433 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6434 } else {
6435 DRM_DEBUG_KMS("Failed to read display plane latency. "
6436 "Disable CxSR\n");
6437 }
6438
6439 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006440 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006441 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006442 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006443 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006444 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006445 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006446 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd602542014-01-07 16:14:10 +02006447 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006448 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006449 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03006450 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306451 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006452 dev_priv->display.init_clock_gating =
6453 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006454 } else if (IS_VALLEYVIEW(dev)) {
6455 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306456 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006457 dev_priv->display.init_clock_gating =
6458 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006459 } else if (IS_PINEVIEW(dev)) {
6460 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6461 dev_priv->is_ddr3,
6462 dev_priv->fsb_freq,
6463 dev_priv->mem_freq)) {
6464 DRM_INFO("failed to find known CxSR latency "
6465 "(found ddr%s fsb freq %d, mem freq %d), "
6466 "disabling CxSR\n",
6467 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6468 dev_priv->fsb_freq, dev_priv->mem_freq);
6469 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006470 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006471 dev_priv->display.update_wm = NULL;
6472 } else
6473 dev_priv->display.update_wm = pineview_update_wm;
6474 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6475 } else if (IS_G4X(dev)) {
6476 dev_priv->display.update_wm = g4x_update_wm;
6477 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6478 } else if (IS_GEN4(dev)) {
6479 dev_priv->display.update_wm = i965_update_wm;
6480 if (IS_CRESTLINE(dev))
6481 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6482 else if (IS_BROADWATER(dev))
6483 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6484 } else if (IS_GEN3(dev)) {
6485 dev_priv->display.update_wm = i9xx_update_wm;
6486 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6487 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006488 } else if (IS_GEN2(dev)) {
6489 if (INTEL_INFO(dev)->num_pipes == 1) {
6490 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006491 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006492 } else {
6493 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006494 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006495 }
6496
6497 if (IS_I85X(dev) || IS_I865G(dev))
6498 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6499 else
6500 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6501 } else {
6502 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006503 }
6504}
6505
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006506int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006507{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006508 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006509
6510 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6511 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6512 return -EAGAIN;
6513 }
6514
6515 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006516 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006517 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6518
6519 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6520 500)) {
6521 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6522 return -ETIMEDOUT;
6523 }
6524
6525 *val = I915_READ(GEN6_PCODE_DATA);
6526 I915_WRITE(GEN6_PCODE_DATA, 0);
6527
6528 return 0;
6529}
6530
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006531int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006532{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006533 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006534
6535 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6536 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6537 return -EAGAIN;
6538 }
6539
6540 I915_WRITE(GEN6_PCODE_DATA, val);
6541 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6542
6543 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6544 500)) {
6545 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6546 return -ETIMEDOUT;
6547 }
6548
6549 I915_WRITE(GEN6_PCODE_DATA, 0);
6550
6551 return 0;
6552}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006553
Ville Syrjälädd06f882014-11-10 22:55:12 +02006554static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006555{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006556 switch (czclk_freq) {
6557 case 200:
6558 return 10;
6559 case 267:
6560 return 12;
6561 case 320:
6562 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006563 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006564 case 400:
6565 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006566 default:
6567 return -1;
6568 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006569}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006570
Ville Syrjälädd06f882014-11-10 22:55:12 +02006571static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6572{
6573 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6574
6575 div = vlv_gpu_freq_div(czclk_freq);
6576 if (div < 0)
6577 return div;
6578
6579 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006580}
6581
Fengguang Wub55dd642014-07-12 11:21:39 +02006582static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006583{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006584 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006585
Ville Syrjälädd06f882014-11-10 22:55:12 +02006586 mul = vlv_gpu_freq_div(czclk_freq);
6587 if (mul < 0)
6588 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006589
Ville Syrjälädd06f882014-11-10 22:55:12 +02006590 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006591}
6592
Fengguang Wub55dd642014-07-12 11:21:39 +02006593static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306594{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006595 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306596
Ville Syrjälädd06f882014-11-10 22:55:12 +02006597 div = vlv_gpu_freq_div(czclk_freq) / 2;
6598 if (div < 0)
6599 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306600
Ville Syrjälädd06f882014-11-10 22:55:12 +02006601 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306602}
6603
Fengguang Wub55dd642014-07-12 11:21:39 +02006604static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306605{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006606 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306607
Ville Syrjälädd06f882014-11-10 22:55:12 +02006608 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6609 if (mul < 0)
6610 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306611
Ville Syrjälä1c147622014-08-18 14:42:43 +03006612 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006613 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306614}
6615
Ville Syrjälä616bc822015-01-23 21:04:25 +02006616int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6617{
6618 if (IS_CHERRYVIEW(dev_priv->dev))
6619 return chv_gpu_freq(dev_priv, val);
6620 else if (IS_VALLEYVIEW(dev_priv->dev))
6621 return byt_gpu_freq(dev_priv, val);
6622 else
6623 return val * GT_FREQUENCY_MULTIPLIER;
6624}
6625
Ville Syrjälä616bc822015-01-23 21:04:25 +02006626int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6627{
Deepak S22b1b2f2014-07-12 14:54:33 +05306628 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006629 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306630 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006631 return byt_freq_opcode(dev_priv, val);
6632 else
6633 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306634}
6635
Daniel Vetterf742a552013-12-06 10:17:53 +01006636void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006637{
6638 struct drm_i915_private *dev_priv = dev->dev_private;
6639
Daniel Vetterf742a552013-12-06 10:17:53 +01006640 mutex_init(&dev_priv->rps.hw_lock);
6641
Chris Wilson907b28c2013-07-19 20:36:52 +01006642 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6643 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006644
Paulo Zanoni33688d92014-03-07 20:08:19 -03006645 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006646}