blob: 874ec9f2023c97aec888b709b7a275440349d1ac [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 Lespiau91e41d12014-03-26 17:42:50 +000067
68 /* Wa4x4STCOptimizationDisable:skl */
69 I915_WRITE(CACHE_MODE_1,
70 _MASKED_BIT_ENABLE(GEN8_4x4_STC_OPTIMIZATION_DISABLE));
Damien Lespiauda2078c2013-02-13 15:27:27 +000071}
72
Daniel Vetterc921aba2012-04-26 23:28:17 +020073static void i915_pineview_get_mem_freq(struct drm_device *dev)
74{
Jani Nikula50227e12014-03-31 14:27:21 +030075 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +020076 u32 tmp;
77
78 tmp = I915_READ(CLKCFG);
79
80 switch (tmp & CLKCFG_FSB_MASK) {
81 case CLKCFG_FSB_533:
82 dev_priv->fsb_freq = 533; /* 133*4 */
83 break;
84 case CLKCFG_FSB_800:
85 dev_priv->fsb_freq = 800; /* 200*4 */
86 break;
87 case CLKCFG_FSB_667:
88 dev_priv->fsb_freq = 667; /* 167*4 */
89 break;
90 case CLKCFG_FSB_400:
91 dev_priv->fsb_freq = 400; /* 100*4 */
92 break;
93 }
94
95 switch (tmp & CLKCFG_MEM_MASK) {
96 case CLKCFG_MEM_533:
97 dev_priv->mem_freq = 533;
98 break;
99 case CLKCFG_MEM_667:
100 dev_priv->mem_freq = 667;
101 break;
102 case CLKCFG_MEM_800:
103 dev_priv->mem_freq = 800;
104 break;
105 }
106
107 /* detect pineview DDR3 setting */
108 tmp = I915_READ(CSHRDDR3CTL);
109 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
110}
111
112static void i915_ironlake_get_mem_freq(struct drm_device *dev)
113{
Jani Nikula50227e12014-03-31 14:27:21 +0300114 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200115 u16 ddrpll, csipll;
116
117 ddrpll = I915_READ16(DDRMPLL1);
118 csipll = I915_READ16(CSIPLL0);
119
120 switch (ddrpll & 0xff) {
121 case 0xc:
122 dev_priv->mem_freq = 800;
123 break;
124 case 0x10:
125 dev_priv->mem_freq = 1066;
126 break;
127 case 0x14:
128 dev_priv->mem_freq = 1333;
129 break;
130 case 0x18:
131 dev_priv->mem_freq = 1600;
132 break;
133 default:
134 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
135 ddrpll & 0xff);
136 dev_priv->mem_freq = 0;
137 break;
138 }
139
Daniel Vetter20e4d402012-08-08 23:35:39 +0200140 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200141
142 switch (csipll & 0x3ff) {
143 case 0x00c:
144 dev_priv->fsb_freq = 3200;
145 break;
146 case 0x00e:
147 dev_priv->fsb_freq = 3733;
148 break;
149 case 0x010:
150 dev_priv->fsb_freq = 4266;
151 break;
152 case 0x012:
153 dev_priv->fsb_freq = 4800;
154 break;
155 case 0x014:
156 dev_priv->fsb_freq = 5333;
157 break;
158 case 0x016:
159 dev_priv->fsb_freq = 5866;
160 break;
161 case 0x018:
162 dev_priv->fsb_freq = 6400;
163 break;
164 default:
165 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
166 csipll & 0x3ff);
167 dev_priv->fsb_freq = 0;
168 break;
169 }
170
171 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200172 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200173 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200174 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200175 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200176 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200177 }
178}
179
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300180static const struct cxsr_latency cxsr_latency_table[] = {
181 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
182 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
183 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
184 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
185 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
186
187 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
188 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
189 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
190 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
191 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
192
193 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
194 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
195 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
196 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
197 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
198
199 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
200 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
201 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
202 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
203 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
204
205 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
206 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
207 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
208 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
209 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
210
211 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
212 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
213 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
214 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
215 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
216};
217
Daniel Vetter63c62272012-04-21 23:17:55 +0200218static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300219 int is_ddr3,
220 int fsb,
221 int mem)
222{
223 const struct cxsr_latency *latency;
224 int i;
225
226 if (fsb == 0 || mem == 0)
227 return NULL;
228
229 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
230 latency = &cxsr_latency_table[i];
231 if (is_desktop == latency->is_desktop &&
232 is_ddr3 == latency->is_ddr3 &&
233 fsb == latency->fsb_freq && mem == latency->mem_freq)
234 return latency;
235 }
236
237 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
238
239 return NULL;
240}
241
Imre Deak5209b1f2014-07-01 12:36:17 +0300242void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300243{
Imre Deak5209b1f2014-07-01 12:36:17 +0300244 struct drm_device *dev = dev_priv->dev;
245 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300246
Imre Deak5209b1f2014-07-01 12:36:17 +0300247 if (IS_VALLEYVIEW(dev)) {
248 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
249 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
250 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
251 } else if (IS_PINEVIEW(dev)) {
252 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
253 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
254 I915_WRITE(DSPFW3, val);
255 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
256 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
257 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
258 I915_WRITE(FW_BLC_SELF, val);
259 } else if (IS_I915GM(dev)) {
260 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
261 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
262 I915_WRITE(INSTPM, val);
263 } else {
264 return;
265 }
266
267 DRM_DEBUG_KMS("memory self-refresh is %s\n",
268 enable ? "enabled" : "disabled");
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300269}
270
271/*
272 * Latency for FIFO fetches is dependent on several factors:
273 * - memory configuration (speed, channels)
274 * - chipset
275 * - current MCH state
276 * It can be fairly high in some situations, so here we assume a fairly
277 * pessimal value. It's a tradeoff between extra memory fetches (if we
278 * set this value too high, the FIFO will fetch frequently to stay full)
279 * and power consumption (set it too low to save power and we might see
280 * FIFO underruns and display "flicker").
281 *
282 * A value of 5us seems to be a good balance; safe for very low end
283 * platforms but not overly aggressive on lower latency configs.
284 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100285static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300286
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300287static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300288{
289 struct drm_i915_private *dev_priv = dev->dev_private;
290 uint32_t dsparb = I915_READ(DSPARB);
291 int size;
292
293 size = dsparb & 0x7f;
294 if (plane)
295 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
296
297 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
298 plane ? "B" : "A", size);
299
300 return size;
301}
302
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200303static int i830_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300304{
305 struct drm_i915_private *dev_priv = dev->dev_private;
306 uint32_t dsparb = I915_READ(DSPARB);
307 int size;
308
309 size = dsparb & 0x1ff;
310 if (plane)
311 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
312 size >>= 1; /* Convert to cachelines */
313
314 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
315 plane ? "B" : "A", size);
316
317 return size;
318}
319
Eugeni Dodonov1fa61102012-04-18 15:29:26 -0300320static int i845_get_fifo_size(struct drm_device *dev, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300321{
322 struct drm_i915_private *dev_priv = dev->dev_private;
323 uint32_t dsparb = I915_READ(DSPARB);
324 int size;
325
326 size = dsparb & 0x7f;
327 size >>= 2; /* Convert to cachelines */
328
329 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
330 plane ? "B" : "A",
331 size);
332
333 return size;
334}
335
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300336/* Pineview has different values for various configs */
337static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300338 .fifo_size = PINEVIEW_DISPLAY_FIFO,
339 .max_wm = PINEVIEW_MAX_WM,
340 .default_wm = PINEVIEW_DFT_WM,
341 .guard_size = PINEVIEW_GUARD_WM,
342 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300343};
344static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300345 .fifo_size = PINEVIEW_DISPLAY_FIFO,
346 .max_wm = PINEVIEW_MAX_WM,
347 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
348 .guard_size = PINEVIEW_GUARD_WM,
349 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300350};
351static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300352 .fifo_size = PINEVIEW_CURSOR_FIFO,
353 .max_wm = PINEVIEW_CURSOR_MAX_WM,
354 .default_wm = PINEVIEW_CURSOR_DFT_WM,
355 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
356 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300357};
358static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300359 .fifo_size = PINEVIEW_CURSOR_FIFO,
360 .max_wm = PINEVIEW_CURSOR_MAX_WM,
361 .default_wm = PINEVIEW_CURSOR_DFT_WM,
362 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
363 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300364};
365static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300366 .fifo_size = G4X_FIFO_SIZE,
367 .max_wm = G4X_MAX_WM,
368 .default_wm = G4X_MAX_WM,
369 .guard_size = 2,
370 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300371};
372static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300373 .fifo_size = I965_CURSOR_FIFO,
374 .max_wm = I965_CURSOR_MAX_WM,
375 .default_wm = I965_CURSOR_DFT_WM,
376 .guard_size = 2,
377 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300378};
379static const struct intel_watermark_params valleyview_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300380 .fifo_size = VALLEYVIEW_FIFO_SIZE,
381 .max_wm = VALLEYVIEW_MAX_WM,
382 .default_wm = VALLEYVIEW_MAX_WM,
383 .guard_size = 2,
384 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300385};
386static const struct intel_watermark_params valleyview_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300387 .fifo_size = I965_CURSOR_FIFO,
388 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
389 .default_wm = I965_CURSOR_DFT_WM,
390 .guard_size = 2,
391 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300392};
393static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300394 .fifo_size = I965_CURSOR_FIFO,
395 .max_wm = I965_CURSOR_MAX_WM,
396 .default_wm = I965_CURSOR_DFT_WM,
397 .guard_size = 2,
398 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300399};
400static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300401 .fifo_size = I945_FIFO_SIZE,
402 .max_wm = I915_MAX_WM,
403 .default_wm = 1,
404 .guard_size = 2,
405 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300406};
407static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300408 .fifo_size = I915_FIFO_SIZE,
409 .max_wm = I915_MAX_WM,
410 .default_wm = 1,
411 .guard_size = 2,
412 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300413};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300414static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300415 .fifo_size = I855GM_FIFO_SIZE,
416 .max_wm = I915_MAX_WM,
417 .default_wm = 1,
418 .guard_size = 2,
419 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300420};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300421static const struct intel_watermark_params i830_bc_wm_info = {
422 .fifo_size = I855GM_FIFO_SIZE,
423 .max_wm = I915_MAX_WM/2,
424 .default_wm = 1,
425 .guard_size = 2,
426 .cacheline_size = I830_FIFO_LINE_SIZE,
427};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200428static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300429 .fifo_size = I830_FIFO_SIZE,
430 .max_wm = I915_MAX_WM,
431 .default_wm = 1,
432 .guard_size = 2,
433 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300434};
435
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300436/**
437 * intel_calculate_wm - calculate watermark level
438 * @clock_in_khz: pixel clock
439 * @wm: chip FIFO params
440 * @pixel_size: display pixel size
441 * @latency_ns: memory latency for the platform
442 *
443 * Calculate the watermark level (the level at which the display plane will
444 * start fetching from memory again). Each chip has a different display
445 * FIFO size and allocation, so the caller needs to figure that out and pass
446 * in the correct intel_watermark_params structure.
447 *
448 * As the pixel clock runs, the FIFO will be drained at a rate that depends
449 * on the pixel size. When it reaches the watermark level, it'll start
450 * fetching FIFO line sized based chunks from memory until the FIFO fills
451 * past the watermark point. If the FIFO drains completely, a FIFO underrun
452 * will occur, and a display engine hang could result.
453 */
454static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
455 const struct intel_watermark_params *wm,
456 int fifo_size,
457 int pixel_size,
458 unsigned long latency_ns)
459{
460 long entries_required, wm_size;
461
462 /*
463 * Note: we need to make sure we don't overflow for various clock &
464 * latency values.
465 * clocks go from a few thousand to several hundred thousand.
466 * latency is usually a few thousand
467 */
468 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
469 1000;
470 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
471
472 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
473
474 wm_size = fifo_size - (entries_required + wm->guard_size);
475
476 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
477
478 /* Don't promote wm_size to unsigned... */
479 if (wm_size > (long)wm->max_wm)
480 wm_size = wm->max_wm;
481 if (wm_size <= 0)
482 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300483
484 /*
485 * Bspec seems to indicate that the value shouldn't be lower than
486 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
487 * Lets go for 8 which is the burst size since certain platforms
488 * already use a hardcoded 8 (which is what the spec says should be
489 * done).
490 */
491 if (wm_size <= 8)
492 wm_size = 8;
493
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300494 return wm_size;
495}
496
497static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
498{
499 struct drm_crtc *crtc, *enabled = NULL;
500
Damien Lespiau70e1e0e2014-05-13 23:32:24 +0100501 for_each_crtc(dev, crtc) {
Chris Wilson3490ea52013-01-07 10:11:40 +0000502 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300503 if (enabled)
504 return NULL;
505 enabled = crtc;
506 }
507 }
508
509 return enabled;
510}
511
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300512static void pineview_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300513{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300514 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300515 struct drm_i915_private *dev_priv = dev->dev_private;
516 struct drm_crtc *crtc;
517 const struct cxsr_latency *latency;
518 u32 reg;
519 unsigned long wm;
520
521 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
522 dev_priv->fsb_freq, dev_priv->mem_freq);
523 if (!latency) {
524 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300525 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526 return;
527 }
528
529 crtc = single_enabled_crtc(dev);
530 if (crtc) {
Damien Lespiau241bfc32013-09-25 16:45:37 +0100531 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -0700532 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100533 int clock;
534
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200535 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100536 clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300537
538 /* Display SR */
539 wm = intel_calculate_wm(clock, &pineview_display_wm,
540 pineview_display_wm.fifo_size,
541 pixel_size, latency->display_sr);
542 reg = I915_READ(DSPFW1);
543 reg &= ~DSPFW_SR_MASK;
544 reg |= wm << DSPFW_SR_SHIFT;
545 I915_WRITE(DSPFW1, reg);
546 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
547
548 /* cursor SR */
549 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
550 pineview_display_wm.fifo_size,
551 pixel_size, latency->cursor_sr);
552 reg = I915_READ(DSPFW3);
553 reg &= ~DSPFW_CURSOR_SR_MASK;
554 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
555 I915_WRITE(DSPFW3, reg);
556
557 /* Display HPLL off SR */
558 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
559 pineview_display_hplloff_wm.fifo_size,
560 pixel_size, latency->display_hpll_disable);
561 reg = I915_READ(DSPFW3);
562 reg &= ~DSPFW_HPLL_SR_MASK;
563 reg |= wm & DSPFW_HPLL_SR_MASK;
564 I915_WRITE(DSPFW3, reg);
565
566 /* cursor HPLL off SR */
567 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
568 pineview_display_hplloff_wm.fifo_size,
569 pixel_size, latency->cursor_hpll_disable);
570 reg = I915_READ(DSPFW3);
571 reg &= ~DSPFW_HPLL_CURSOR_MASK;
572 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
573 I915_WRITE(DSPFW3, reg);
574 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
575
Imre Deak5209b1f2014-07-01 12:36:17 +0300576 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300577 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300578 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300579 }
580}
581
582static bool g4x_compute_wm0(struct drm_device *dev,
583 int plane,
584 const struct intel_watermark_params *display,
585 int display_latency_ns,
586 const struct intel_watermark_params *cursor,
587 int cursor_latency_ns,
588 int *plane_wm,
589 int *cursor_wm)
590{
591 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300592 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300593 int htotal, hdisplay, clock, pixel_size;
594 int line_time_us, line_count;
595 int entries, tlb_miss;
596
597 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson3490ea52013-01-07 10:11:40 +0000598 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300599 *cursor_wm = cursor->guard_size;
600 *plane_wm = display->guard_size;
601 return false;
602 }
603
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200604 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100605 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800606 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200607 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -0700608 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300609
610 /* Use the small buffer method to calculate plane watermark */
611 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
612 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
613 if (tlb_miss > 0)
614 entries += tlb_miss;
615 entries = DIV_ROUND_UP(entries, display->cacheline_size);
616 *plane_wm = entries + display->guard_size;
617 if (*plane_wm > (int)display->max_wm)
618 *plane_wm = display->max_wm;
619
620 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200621 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300622 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Chris Wilson7bb836d2014-03-26 12:38:14 +0000623 entries = line_count * to_intel_crtc(crtc)->cursor_width * pixel_size;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300624 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
625 if (tlb_miss > 0)
626 entries += tlb_miss;
627 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
628 *cursor_wm = entries + cursor->guard_size;
629 if (*cursor_wm > (int)cursor->max_wm)
630 *cursor_wm = (int)cursor->max_wm;
631
632 return true;
633}
634
635/*
636 * Check the wm result.
637 *
638 * If any calculated watermark values is larger than the maximum value that
639 * can be programmed into the associated watermark register, that watermark
640 * must be disabled.
641 */
642static bool g4x_check_srwm(struct drm_device *dev,
643 int display_wm, int cursor_wm,
644 const struct intel_watermark_params *display,
645 const struct intel_watermark_params *cursor)
646{
647 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
648 display_wm, cursor_wm);
649
650 if (display_wm > display->max_wm) {
651 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
652 display_wm, display->max_wm);
653 return false;
654 }
655
656 if (cursor_wm > cursor->max_wm) {
657 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
658 cursor_wm, cursor->max_wm);
659 return false;
660 }
661
662 if (!(display_wm || cursor_wm)) {
663 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
664 return false;
665 }
666
667 return true;
668}
669
670static bool g4x_compute_srwm(struct drm_device *dev,
671 int plane,
672 int latency_ns,
673 const struct intel_watermark_params *display,
674 const struct intel_watermark_params *cursor,
675 int *display_wm, int *cursor_wm)
676{
677 struct drm_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300678 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300679 int hdisplay, htotal, pixel_size, clock;
680 unsigned long line_time_us;
681 int line_count, line_size;
682 int small, large;
683 int entries;
684
685 if (!latency_ns) {
686 *display_wm = *cursor_wm = 0;
687 return false;
688 }
689
690 crtc = intel_get_crtc_for_plane(dev, plane);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200691 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100692 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800693 htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200694 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -0700695 pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300696
Ville Syrjälä922044c2014-02-14 14:18:57 +0200697 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300698 line_count = (latency_ns / line_time_us + 1000) / 1000;
699 line_size = hdisplay * pixel_size;
700
701 /* Use the minimum of the small and large buffer method for primary */
702 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
703 large = line_count * line_size;
704
705 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
706 *display_wm = entries + display->guard_size;
707
708 /* calculate the self-refresh watermark for display cursor */
Chris Wilson7bb836d2014-03-26 12:38:14 +0000709 entries = line_count * pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300710 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
711 *cursor_wm = entries + cursor->guard_size;
712
713 return g4x_check_srwm(dev,
714 *display_wm, *cursor_wm,
715 display, cursor);
716}
717
Gajanan Bhat0948c262014-08-07 01:58:24 +0530718static bool vlv_compute_drain_latency(struct drm_crtc *crtc,
719 int pixel_size,
720 int *prec_mult,
721 int *drain_latency)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700723 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300724 int entries;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200725 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300726
Gajanan Bhat0948c262014-08-07 01:58:24 +0530727 if (WARN(clock == 0, "Pixel clock is zero!\n"))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300728 return false;
729
Gajanan Bhat0948c262014-08-07 01:58:24 +0530730 if (WARN(pixel_size == 0, "Pixel size is zero!\n"))
731 return false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530733 entries = DIV_ROUND_UP(clock, 1000) * pixel_size;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700734 if (IS_CHERRYVIEW(dev))
735 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_32 :
736 DRAIN_LATENCY_PRECISION_16;
737 else
738 *prec_mult = (entries > 128) ? DRAIN_LATENCY_PRECISION_64 :
739 DRAIN_LATENCY_PRECISION_32;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530740 *drain_latency = (64 * (*prec_mult) * 4) / entries;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300741
Gajanan Bhata398e9c2014-08-05 23:15:54 +0530742 if (*drain_latency > DRAIN_LATENCY_MASK)
743 *drain_latency = DRAIN_LATENCY_MASK;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300744
745 return true;
746}
747
748/*
749 * Update drain latency registers of memory arbiter
750 *
751 * Valleyview SoC has a new memory arbiter and needs drain latency registers
752 * to be programmed. Each plane has a drain latency multiplier and a drain
753 * latency value.
754 */
755
Gajanan Bhat41aad812014-07-16 18:24:03 +0530756static void vlv_update_drain_latency(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757{
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700758 struct drm_device *dev = crtc->dev;
759 struct drm_i915_private *dev_priv = dev->dev_private;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
761 int pixel_size;
762 int drain_latency;
763 enum pipe pipe = intel_crtc->pipe;
764 int plane_prec, prec_mult, plane_dl;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700765 const int high_precision = IS_CHERRYVIEW(dev) ?
766 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300767
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700768 plane_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_PLANE_PRECISION_HIGH |
769 DRAIN_LATENCY_MASK | DDL_CURSOR_PRECISION_HIGH |
Gajanan Bhat0948c262014-08-07 01:58:24 +0530770 (DRAIN_LATENCY_MASK << DDL_CURSOR_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300771
Gajanan Bhat0948c262014-08-07 01:58:24 +0530772 if (!intel_crtc_active(crtc)) {
773 I915_WRITE(VLV_DDL(pipe), plane_dl);
774 return;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300775 }
776
Gajanan Bhat0948c262014-08-07 01:58:24 +0530777 /* Primary plane Drain Latency */
778 pixel_size = crtc->primary->fb->bits_per_pixel / 8; /* BPP */
779 if (vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700780 plane_prec = (prec_mult == high_precision) ?
781 DDL_PLANE_PRECISION_HIGH :
782 DDL_PLANE_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530783 plane_dl |= plane_prec | drain_latency;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300784 }
Gajanan Bhat0948c262014-08-07 01:58:24 +0530785
786 /* Cursor Drain Latency
787 * BPP is always 4 for cursor
788 */
789 pixel_size = 4;
790
791 /* Program cursor DL only if it is enabled */
792 if (intel_crtc->cursor_base &&
793 vlv_compute_drain_latency(crtc, pixel_size, &prec_mult, &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700794 plane_prec = (prec_mult == high_precision) ?
795 DDL_CURSOR_PRECISION_HIGH :
796 DDL_CURSOR_PRECISION_LOW;
Gajanan Bhat0948c262014-08-07 01:58:24 +0530797 plane_dl |= plane_prec | (drain_latency << DDL_CURSOR_SHIFT);
798 }
799
800 I915_WRITE(VLV_DDL(pipe), plane_dl);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300801}
802
803#define single_plane_enabled(mask) is_power_of_2(mask)
804
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300805static void valleyview_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300806{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300807 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300808 static const int sr_latency_ns = 12000;
809 struct drm_i915_private *dev_priv = dev->dev_private;
810 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
811 int plane_sr, cursor_sr;
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000812 int ignore_plane_sr, ignore_cursor_sr;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300813 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300814 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300815
Gajanan Bhat41aad812014-07-16 18:24:03 +0530816 vlv_update_drain_latency(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300817
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200818 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100819 &valleyview_wm_info, pessimal_latency_ns,
820 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300821 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200822 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300823
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200824 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100825 &valleyview_wm_info, pessimal_latency_ns,
826 &valleyview_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300827 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200828 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300829
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300830 if (single_plane_enabled(enabled) &&
831 g4x_compute_srwm(dev, ffs(enabled) - 1,
832 sr_latency_ns,
833 &valleyview_wm_info,
834 &valleyview_cursor_wm_info,
Chris Wilsonaf6c4572012-12-11 12:01:43 +0000835 &plane_sr, &ignore_cursor_sr) &&
836 g4x_compute_srwm(dev, ffs(enabled) - 1,
837 2*sr_latency_ns,
838 &valleyview_wm_info,
839 &valleyview_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +0000840 &ignore_plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +0300841 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +0000842 } else {
Imre Deak98584252014-06-13 14:54:20 +0300843 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300844 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +0000845 plane_sr = cursor_sr = 0;
846 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300847
Ville Syrjäläa5043452014-06-28 02:04:18 +0300848 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
849 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300850 planea_wm, cursora_wm,
851 planeb_wm, cursorb_wm,
852 plane_sr, cursor_sr);
853
854 I915_WRITE(DSPFW1,
855 (plane_sr << DSPFW_SR_SHIFT) |
856 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
857 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +0300858 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300859 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +0000860 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300861 (cursora_wm << DSPFW_CURSORA_SHIFT));
862 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +0000863 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
864 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +0300865
866 if (cxsr_enabled)
867 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868}
869
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300870static void cherryview_update_wm(struct drm_crtc *crtc)
871{
872 struct drm_device *dev = crtc->dev;
873 static const int sr_latency_ns = 12000;
874 struct drm_i915_private *dev_priv = dev->dev_private;
875 int planea_wm, planeb_wm, planec_wm;
876 int cursora_wm, cursorb_wm, cursorc_wm;
877 int plane_sr, cursor_sr;
878 int ignore_plane_sr, ignore_cursor_sr;
879 unsigned int enabled = 0;
880 bool cxsr_enabled;
881
882 vlv_update_drain_latency(crtc);
883
884 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100885 &valleyview_wm_info, pessimal_latency_ns,
886 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300887 &planea_wm, &cursora_wm))
888 enabled |= 1 << PIPE_A;
889
890 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100891 &valleyview_wm_info, pessimal_latency_ns,
892 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300893 &planeb_wm, &cursorb_wm))
894 enabled |= 1 << PIPE_B;
895
896 if (g4x_compute_wm0(dev, PIPE_C,
Chris Wilson5aef6002014-09-03 11:56:07 +0100897 &valleyview_wm_info, pessimal_latency_ns,
898 &valleyview_cursor_wm_info, pessimal_latency_ns,
Ville Syrjälä3c2777f2014-06-26 17:03:06 +0300899 &planec_wm, &cursorc_wm))
900 enabled |= 1 << PIPE_C;
901
902 if (single_plane_enabled(enabled) &&
903 g4x_compute_srwm(dev, ffs(enabled) - 1,
904 sr_latency_ns,
905 &valleyview_wm_info,
906 &valleyview_cursor_wm_info,
907 &plane_sr, &ignore_cursor_sr) &&
908 g4x_compute_srwm(dev, ffs(enabled) - 1,
909 2*sr_latency_ns,
910 &valleyview_wm_info,
911 &valleyview_cursor_wm_info,
912 &ignore_plane_sr, &cursor_sr)) {
913 cxsr_enabled = true;
914 } else {
915 cxsr_enabled = false;
916 intel_set_memory_cxsr(dev_priv, false);
917 plane_sr = cursor_sr = 0;
918 }
919
920 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
921 "B: plane=%d, cursor=%d, C: plane=%d, cursor=%d, "
922 "SR: plane=%d, cursor=%d\n",
923 planea_wm, cursora_wm,
924 planeb_wm, cursorb_wm,
925 planec_wm, cursorc_wm,
926 plane_sr, cursor_sr);
927
928 I915_WRITE(DSPFW1,
929 (plane_sr << DSPFW_SR_SHIFT) |
930 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
931 (planeb_wm << DSPFW_PLANEB_SHIFT) |
932 (planea_wm << DSPFW_PLANEA_SHIFT));
933 I915_WRITE(DSPFW2,
934 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
935 (cursora_wm << DSPFW_CURSORA_SHIFT));
936 I915_WRITE(DSPFW3,
937 (I915_READ(DSPFW3) & ~DSPFW_CURSOR_SR_MASK) |
938 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
939 I915_WRITE(DSPFW9_CHV,
940 (I915_READ(DSPFW9_CHV) & ~(DSPFW_PLANEC_MASK |
941 DSPFW_CURSORC_MASK)) |
942 (planec_wm << DSPFW_PLANEC_SHIFT) |
943 (cursorc_wm << DSPFW_CURSORC_SHIFT));
944
945 if (cxsr_enabled)
946 intel_set_memory_cxsr(dev_priv, true);
947}
948
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530949static void valleyview_update_sprite_wm(struct drm_plane *plane,
950 struct drm_crtc *crtc,
951 uint32_t sprite_width,
952 uint32_t sprite_height,
953 int pixel_size,
954 bool enabled, bool scaled)
955{
956 struct drm_device *dev = crtc->dev;
957 struct drm_i915_private *dev_priv = dev->dev_private;
958 int pipe = to_intel_plane(plane)->pipe;
959 int sprite = to_intel_plane(plane)->plane;
960 int drain_latency;
961 int plane_prec;
962 int sprite_dl;
963 int prec_mult;
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700964 const int high_precision = IS_CHERRYVIEW(dev) ?
965 DRAIN_LATENCY_PRECISION_32 : DRAIN_LATENCY_PRECISION_64;
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530966
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700967 sprite_dl = I915_READ(VLV_DDL(pipe)) & ~(DDL_SPRITE_PRECISION_HIGH(sprite) |
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530968 (DRAIN_LATENCY_MASK << DDL_SPRITE_SHIFT(sprite)));
969
970 if (enabled && vlv_compute_drain_latency(crtc, pixel_size, &prec_mult,
971 &drain_latency)) {
Rodrigo Vivi5e56ba42014-10-17 08:05:08 -0700972 plane_prec = (prec_mult == high_precision) ?
973 DDL_SPRITE_PRECISION_HIGH(sprite) :
974 DDL_SPRITE_PRECISION_LOW(sprite);
Gajanan Bhat01e184c2014-08-07 17:03:30 +0530975 sprite_dl |= plane_prec |
976 (drain_latency << DDL_SPRITE_SHIFT(sprite));
977 }
978
979 I915_WRITE(VLV_DDL(pipe), sprite_dl);
980}
981
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300982static void g4x_update_wm(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300983{
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300984 struct drm_device *dev = crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300985 static const int sr_latency_ns = 12000;
986 struct drm_i915_private *dev_priv = dev->dev_private;
987 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
988 int plane_sr, cursor_sr;
989 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +0300990 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300991
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200992 if (g4x_compute_wm0(dev, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +0100993 &g4x_wm_info, pessimal_latency_ns,
994 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300995 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200996 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300997
Ville Syrjälä51cea1f2013-03-21 13:10:44 +0200998 if (g4x_compute_wm0(dev, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +0100999 &g4x_wm_info, pessimal_latency_ns,
1000 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001001 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001002 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001003
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001004 if (single_plane_enabled(enabled) &&
1005 g4x_compute_srwm(dev, ffs(enabled) - 1,
1006 sr_latency_ns,
1007 &g4x_wm_info,
1008 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001009 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001010 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001011 } else {
Imre Deak98584252014-06-13 14:54:20 +03001012 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001013 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001014 plane_sr = cursor_sr = 0;
1015 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001016
Ville Syrjäläa5043452014-06-28 02:04:18 +03001017 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1018 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001019 planea_wm, cursora_wm,
1020 planeb_wm, cursorb_wm,
1021 plane_sr, cursor_sr);
1022
1023 I915_WRITE(DSPFW1,
1024 (plane_sr << DSPFW_SR_SHIFT) |
1025 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
1026 (planeb_wm << DSPFW_PLANEB_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001027 (planea_wm << DSPFW_PLANEA_SHIFT));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001028 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001029 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001030 (cursora_wm << DSPFW_CURSORA_SHIFT));
1031 /* HPLL off in SR has some issues on G4x... disable it */
1032 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001033 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001034 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001035
1036 if (cxsr_enabled)
1037 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001038}
1039
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001040static void i965_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001041{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001042 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001043 struct drm_i915_private *dev_priv = dev->dev_private;
1044 struct drm_crtc *crtc;
1045 int srwm = 1;
1046 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001047 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001048
1049 /* Calc sr entries for one plane configs */
1050 crtc = single_enabled_crtc(dev);
1051 if (crtc) {
1052 /* self-refresh has much higher latency */
1053 static const int sr_latency_ns = 12000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001054 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001055 &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001056 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001057 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001058 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001059 int pixel_size = crtc->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001060 unsigned long line_time_us;
1061 int entries;
1062
Ville Syrjälä922044c2014-02-14 14:18:57 +02001063 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001064
1065 /* Use ns/us then divide to preserve precision */
1066 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1067 pixel_size * hdisplay;
1068 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1069 srwm = I965_FIFO_SIZE - entries;
1070 if (srwm < 0)
1071 srwm = 1;
1072 srwm &= 0x1ff;
1073 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1074 entries, srwm);
1075
1076 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson7bb836d2014-03-26 12:38:14 +00001077 pixel_size * to_intel_crtc(crtc)->cursor_width;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001078 entries = DIV_ROUND_UP(entries,
1079 i965_cursor_wm_info.cacheline_size);
1080 cursor_sr = i965_cursor_wm_info.fifo_size -
1081 (entries + i965_cursor_wm_info.guard_size);
1082
1083 if (cursor_sr > i965_cursor_wm_info.max_wm)
1084 cursor_sr = i965_cursor_wm_info.max_wm;
1085
1086 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1087 "cursor %d\n", srwm, cursor_sr);
1088
Imre Deak98584252014-06-13 14:54:20 +03001089 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001090 } else {
Imre Deak98584252014-06-13 14:54:20 +03001091 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001092 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001093 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001094 }
1095
1096 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1097 srwm);
1098
1099 /* 965 has limitations... */
1100 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
Ville Syrjälä0a560672014-06-11 16:51:18 +03001101 (8 << DSPFW_CURSORB_SHIFT) |
1102 (8 << DSPFW_PLANEB_SHIFT) |
1103 (8 << DSPFW_PLANEA_SHIFT));
1104 I915_WRITE(DSPFW2, (8 << DSPFW_CURSORA_SHIFT) |
1105 (8 << DSPFW_PLANEC_SHIFT_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001106 /* update cursor SR watermark */
1107 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Imre Deak98584252014-06-13 14:54:20 +03001108
1109 if (cxsr_enabled)
1110 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001111}
1112
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001113static void i9xx_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001114{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001115 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001116 struct drm_i915_private *dev_priv = dev->dev_private;
1117 const struct intel_watermark_params *wm_info;
1118 uint32_t fwater_lo;
1119 uint32_t fwater_hi;
1120 int cwm, srwm = 1;
1121 int fifo_size;
1122 int planea_wm, planeb_wm;
1123 struct drm_crtc *crtc, *enabled = NULL;
1124
1125 if (IS_I945GM(dev))
1126 wm_info = &i945_wm_info;
1127 else if (!IS_GEN2(dev))
1128 wm_info = &i915_wm_info;
1129 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001130 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001131
1132 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1133 crtc = intel_get_crtc_for_plane(dev, 0);
Chris Wilson3490ea52013-01-07 10:11:40 +00001134 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001135 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001136 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001137 if (IS_GEN2(dev))
1138 cpp = 4;
1139
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001140 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001141 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001142 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001143 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001144 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001145 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001146 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001147 if (planea_wm > (long)wm_info->max_wm)
1148 planea_wm = wm_info->max_wm;
1149 }
1150
1151 if (IS_GEN2(dev))
1152 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001153
1154 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1155 crtc = intel_get_crtc_for_plane(dev, 1);
Chris Wilson3490ea52013-01-07 10:11:40 +00001156 if (intel_crtc_active(crtc)) {
Damien Lespiau241bfc32013-09-25 16:45:37 +01001157 const struct drm_display_mode *adjusted_mode;
Matt Roperf4510a22014-04-01 15:22:40 -07001158 int cpp = crtc->primary->fb->bits_per_pixel / 8;
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001159 if (IS_GEN2(dev))
1160 cpp = 4;
1161
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001162 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001163 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001164 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001165 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001166 if (enabled == NULL)
1167 enabled = crtc;
1168 else
1169 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001170 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001171 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001172 if (planeb_wm > (long)wm_info->max_wm)
1173 planeb_wm = wm_info->max_wm;
1174 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001175
1176 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1177
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001178 if (IS_I915GM(dev) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001179 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001180
Matt Roper2ff8fde2014-07-08 07:50:07 -07001181 obj = intel_fb_obj(enabled->primary->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001182
1183 /* self-refresh seems busted with untiled */
Matt Roper2ff8fde2014-07-08 07:50:07 -07001184 if (obj->tiling_mode == I915_TILING_NONE)
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001185 enabled = NULL;
1186 }
1187
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001188 /*
1189 * Overlay gets an aggressive default since video jitter is bad.
1190 */
1191 cwm = 2;
1192
1193 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001194 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001195
1196 /* Calc sr entries for one plane configs */
1197 if (HAS_FW_BLC(dev) && enabled) {
1198 /* self-refresh has much higher latency */
1199 static const int sr_latency_ns = 6000;
Ville Syrjälä4fe85902013-09-04 18:25:22 +03001200 const struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001201 &to_intel_crtc(enabled)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001202 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001203 int htotal = adjusted_mode->crtc_htotal;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001204 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
Matt Roperf4510a22014-04-01 15:22:40 -07001205 int pixel_size = enabled->primary->fb->bits_per_pixel / 8;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001206 unsigned long line_time_us;
1207 int entries;
1208
Ville Syrjälä922044c2014-02-14 14:18:57 +02001209 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001210
1211 /* Use ns/us then divide to preserve precision */
1212 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1213 pixel_size * hdisplay;
1214 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1215 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1216 srwm = wm_info->fifo_size - entries;
1217 if (srwm < 0)
1218 srwm = 1;
1219
1220 if (IS_I945G(dev) || IS_I945GM(dev))
1221 I915_WRITE(FW_BLC_SELF,
1222 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1223 else if (IS_I915GM(dev))
1224 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1225 }
1226
1227 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1228 planea_wm, planeb_wm, cwm, srwm);
1229
1230 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1231 fwater_hi = (cwm & 0x1f);
1232
1233 /* Set request length to 8 cachelines per fetch */
1234 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1235 fwater_hi = fwater_hi | (1 << 8);
1236
1237 I915_WRITE(FW_BLC, fwater_lo);
1238 I915_WRITE(FW_BLC2, fwater_hi);
1239
Imre Deak5209b1f2014-07-01 12:36:17 +03001240 if (enabled)
1241 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001242}
1243
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001244static void i845_update_wm(struct drm_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001245{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03001246 struct drm_device *dev = unused_crtc->dev;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001247 struct drm_i915_private *dev_priv = dev->dev_private;
1248 struct drm_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001249 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001250 uint32_t fwater_lo;
1251 int planea_wm;
1252
1253 crtc = single_enabled_crtc(dev);
1254 if (crtc == NULL)
1255 return;
1256
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001257 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001258 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001259 &i845_wm_info,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001260 dev_priv->display.get_fifo_size(dev, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001261 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001262 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1263 fwater_lo |= (3<<8) | planea_wm;
1264
1265 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1266
1267 I915_WRITE(FW_BLC, fwater_lo);
1268}
1269
Ville Syrjälä36587292013-07-05 11:57:16 +03001270static uint32_t ilk_pipe_pixel_rate(struct drm_device *dev,
1271 struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001272{
1273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01001274 uint32_t pixel_rate;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001275
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001276 pixel_rate = intel_crtc->config->base.adjusted_mode.crtc_clock;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001277
1278 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1279 * adjust the pixel_rate here. */
1280
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001281 if (intel_crtc->config->pch_pfit.enabled) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001282 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001283 uint32_t pfit_size = intel_crtc->config->pch_pfit.size;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001284
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001285 pipe_w = intel_crtc->config->pipe_src_w;
1286 pipe_h = intel_crtc->config->pipe_src_h;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001287 pfit_w = (pfit_size >> 16) & 0xFFFF;
1288 pfit_h = pfit_size & 0xFFFF;
1289 if (pipe_w < pfit_w)
1290 pipe_w = pfit_w;
1291 if (pipe_h < pfit_h)
1292 pipe_h = pfit_h;
1293
1294 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1295 pfit_w * pfit_h);
1296 }
1297
1298 return pixel_rate;
1299}
1300
Ville Syrjälä37126462013-08-01 16:18:55 +03001301/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001302static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001303 uint32_t latency)
1304{
1305 uint64_t ret;
1306
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001307 if (WARN(latency == 0, "Latency value missing\n"))
1308 return UINT_MAX;
1309
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001310 ret = (uint64_t) pixel_rate * bytes_per_pixel * latency;
1311 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1312
1313 return ret;
1314}
1315
Ville Syrjälä37126462013-08-01 16:18:55 +03001316/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001317static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001318 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
1319 uint32_t latency)
1320{
1321 uint32_t ret;
1322
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001323 if (WARN(latency == 0, "Latency value missing\n"))
1324 return UINT_MAX;
1325
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001326 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1327 ret = (ret + 1) * horiz_pixels * bytes_per_pixel;
1328 ret = DIV_ROUND_UP(ret, 64) + 2;
1329 return ret;
1330}
1331
Ville Syrjälä23297042013-07-05 11:57:17 +03001332static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001333 uint8_t bytes_per_pixel)
1334{
1335 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * bytes_per_pixel) + 2;
1336}
1337
Pradeep Bhat2ac96d22014-11-04 17:06:40 +00001338struct skl_pipe_wm_parameters {
1339 bool active;
1340 uint32_t pipe_htotal;
1341 uint32_t pixel_rate; /* in KHz */
1342 struct intel_plane_wm_parameters plane[I915_MAX_PLANES];
1343 struct intel_plane_wm_parameters cursor;
1344};
1345
Imre Deak820c1982013-12-17 14:46:36 +02001346struct ilk_pipe_wm_parameters {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001347 bool active;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001348 uint32_t pipe_htotal;
1349 uint32_t pixel_rate;
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001350 struct intel_plane_wm_parameters pri;
1351 struct intel_plane_wm_parameters spr;
1352 struct intel_plane_wm_parameters cur;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001353};
1354
Imre Deak820c1982013-12-17 14:46:36 +02001355struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001356 uint16_t pri;
1357 uint16_t spr;
1358 uint16_t cur;
1359 uint16_t fbc;
1360};
1361
Ville Syrjälä240264f2013-08-07 13:29:12 +03001362/* used in computing the new watermarks state */
1363struct intel_wm_config {
1364 unsigned int num_pipes_active;
1365 bool sprites_enabled;
1366 bool sprites_scaled;
Ville Syrjälä240264f2013-08-07 13:29:12 +03001367};
1368
Ville Syrjälä37126462013-08-01 16:18:55 +03001369/*
1370 * For both WM_PIPE and WM_LP.
1371 * mem_value must be in 0.1us units.
1372 */
Imre Deak820c1982013-12-17 14:46:36 +02001373static uint32_t ilk_compute_pri_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001374 uint32_t mem_value,
1375 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001376{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001377 uint32_t method1, method2;
1378
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001379 if (!params->active || !params->pri.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001380 return 0;
1381
Ville Syrjälä23297042013-07-05 11:57:17 +03001382 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001383 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001384 mem_value);
1385
1386 if (!is_lp)
1387 return method1;
1388
Ville Syrjälä23297042013-07-05 11:57:17 +03001389 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001390 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001391 params->pri.horiz_pixels,
1392 params->pri.bytes_per_pixel,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001393 mem_value);
1394
1395 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001396}
1397
Ville Syrjälä37126462013-08-01 16:18:55 +03001398/*
1399 * For both WM_PIPE and WM_LP.
1400 * mem_value must be in 0.1us units.
1401 */
Imre Deak820c1982013-12-17 14:46:36 +02001402static uint32_t ilk_compute_spr_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001403 uint32_t mem_value)
1404{
1405 uint32_t method1, method2;
1406
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001407 if (!params->active || !params->spr.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001408 return 0;
1409
Ville Syrjälä23297042013-07-05 11:57:17 +03001410 method1 = ilk_wm_method1(params->pixel_rate,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001411 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001412 mem_value);
Ville Syrjälä23297042013-07-05 11:57:17 +03001413 method2 = ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001414 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001415 params->spr.horiz_pixels,
1416 params->spr.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001417 mem_value);
1418 return min(method1, method2);
1419}
1420
Ville Syrjälä37126462013-08-01 16:18:55 +03001421/*
1422 * For both WM_PIPE and WM_LP.
1423 * mem_value must be in 0.1us units.
1424 */
Imre Deak820c1982013-12-17 14:46:36 +02001425static uint32_t ilk_compute_cur_wm(const struct ilk_pipe_wm_parameters *params,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001426 uint32_t mem_value)
1427{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001428 if (!params->active || !params->cur.enabled)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001429 return 0;
1430
Ville Syrjälä23297042013-07-05 11:57:17 +03001431 return ilk_wm_method2(params->pixel_rate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001432 params->pipe_htotal,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001433 params->cur.horiz_pixels,
1434 params->cur.bytes_per_pixel,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001435 mem_value);
1436}
1437
Paulo Zanonicca32e92013-05-31 11:45:06 -03001438/* Only for WM_LP. */
Imre Deak820c1982013-12-17 14:46:36 +02001439static uint32_t ilk_compute_fbc_wm(const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03001440 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001441{
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001442 if (!params->active || !params->pri.enabled)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001443 return 0;
1444
Ville Syrjälä23297042013-07-05 11:57:17 +03001445 return ilk_wm_fbc(pri_val,
Ville Syrjäläc35426d2013-08-07 13:29:50 +03001446 params->pri.horiz_pixels,
1447 params->pri.bytes_per_pixel);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001448}
1449
Ville Syrjälä158ae642013-08-07 13:28:19 +03001450static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1451{
Ville Syrjälä416f4722013-11-02 21:07:46 -07001452 if (INTEL_INFO(dev)->gen >= 8)
1453 return 3072;
1454 else if (INTEL_INFO(dev)->gen >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001455 return 768;
1456 else
1457 return 512;
1458}
1459
Ville Syrjälä4e975082014-03-07 18:32:11 +02001460static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1461 int level, bool is_sprite)
1462{
1463 if (INTEL_INFO(dev)->gen >= 8)
1464 /* BDW primary/sprite plane watermarks */
1465 return level == 0 ? 255 : 2047;
1466 else if (INTEL_INFO(dev)->gen >= 7)
1467 /* IVB/HSW primary/sprite plane watermarks */
1468 return level == 0 ? 127 : 1023;
1469 else if (!is_sprite)
1470 /* ILK/SNB primary plane watermarks */
1471 return level == 0 ? 127 : 511;
1472 else
1473 /* ILK/SNB sprite plane watermarks */
1474 return level == 0 ? 63 : 255;
1475}
1476
1477static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1478 int level)
1479{
1480 if (INTEL_INFO(dev)->gen >= 7)
1481 return level == 0 ? 63 : 255;
1482 else
1483 return level == 0 ? 31 : 63;
1484}
1485
1486static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1487{
1488 if (INTEL_INFO(dev)->gen >= 8)
1489 return 31;
1490 else
1491 return 15;
1492}
1493
Ville Syrjälä158ae642013-08-07 13:28:19 +03001494/* Calculate the maximum primary/sprite plane watermark */
1495static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1496 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001497 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03001498 enum intel_ddb_partitioning ddb_partitioning,
1499 bool is_sprite)
1500{
1501 unsigned int fifo_size = ilk_display_fifo_size(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001502
1503 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001504 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001505 return 0;
1506
1507 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001508 if (level == 0 || config->num_pipes_active > 1) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001509 fifo_size /= INTEL_INFO(dev)->num_pipes;
1510
1511 /*
1512 * For some reason the non self refresh
1513 * FIFO size is only half of the self
1514 * refresh FIFO size on ILK/SNB.
1515 */
1516 if (INTEL_INFO(dev)->gen <= 6)
1517 fifo_size /= 2;
1518 }
1519
Ville Syrjälä240264f2013-08-07 13:29:12 +03001520 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03001521 /* level 0 is always calculated with 1:1 split */
1522 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1523 if (is_sprite)
1524 fifo_size *= 5;
1525 fifo_size /= 6;
1526 } else {
1527 fifo_size /= 2;
1528 }
1529 }
1530
1531 /* clamp to max that the registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001532 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03001533}
1534
1535/* Calculate the maximum cursor plane watermark */
1536static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03001537 int level,
1538 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001539{
1540 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03001541 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001542 return 64;
1543
1544 /* otherwise just report max that registers can hold */
Ville Syrjälä4e975082014-03-07 18:32:11 +02001545 return ilk_cursor_wm_reg_max(dev, level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001546}
1547
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001548static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03001549 int level,
1550 const struct intel_wm_config *config,
1551 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02001552 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03001553{
Ville Syrjälä240264f2013-08-07 13:29:12 +03001554 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1555 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1556 max->cur = ilk_cursor_wm_max(dev, level, config);
Ville Syrjälä4e975082014-03-07 18:32:11 +02001557 max->fbc = ilk_fbc_wm_reg_max(dev);
Ville Syrjälä158ae642013-08-07 13:28:19 +03001558}
1559
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001560static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1561 int level,
1562 struct ilk_wm_maximums *max)
1563{
1564 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1565 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1566 max->cur = ilk_cursor_wm_reg_max(dev, level);
1567 max->fbc = ilk_fbc_wm_reg_max(dev);
1568}
1569
Ville Syrjäläd9395652013-10-09 19:18:10 +03001570static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02001571 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03001572 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001573{
1574 bool ret;
1575
1576 /* already determined to be invalid? */
1577 if (!result->enable)
1578 return false;
1579
1580 result->enable = result->pri_val <= max->pri &&
1581 result->spr_val <= max->spr &&
1582 result->cur_val <= max->cur;
1583
1584 ret = result->enable;
1585
1586 /*
1587 * HACK until we can pre-compute everything,
1588 * and thus fail gracefully if LP0 watermarks
1589 * are exceeded...
1590 */
1591 if (level == 0 && !result->enable) {
1592 if (result->pri_val > max->pri)
1593 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1594 level, result->pri_val, max->pri);
1595 if (result->spr_val > max->spr)
1596 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1597 level, result->spr_val, max->spr);
1598 if (result->cur_val > max->cur)
1599 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1600 level, result->cur_val, max->cur);
1601
1602 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1603 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1604 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1605 result->enable = true;
1606 }
1607
Ville Syrjäläa9786a12013-08-07 13:24:47 +03001608 return ret;
1609}
1610
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001611static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001612 int level,
Imre Deak820c1982013-12-17 14:46:36 +02001613 const struct ilk_pipe_wm_parameters *p,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03001614 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03001615{
1616 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1617 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1618 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1619
1620 /* WM1+ latency values stored in 0.5us units */
1621 if (level > 0) {
1622 pri_latency *= 5;
1623 spr_latency *= 5;
1624 cur_latency *= 5;
1625 }
1626
1627 result->pri_val = ilk_compute_pri_wm(p, pri_latency, level);
1628 result->spr_val = ilk_compute_spr_wm(p, spr_latency);
1629 result->cur_val = ilk_compute_cur_wm(p, cur_latency);
1630 result->fbc_val = ilk_compute_fbc_wm(p, result->pri_val);
1631 result->enable = true;
1632}
1633
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001634static uint32_t
1635hsw_compute_linetime_wm(struct drm_device *dev, struct drm_crtc *crtc)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001636{
1637 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001639 struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001640 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001641
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001642 if (!intel_crtc_active(crtc))
1643 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03001644
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001645 /* The WM are computed with base on how long it takes to fill a single
1646 * row at the given clock rate, multiplied by 8.
1647 * */
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001648 linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
1649 mode->crtc_clock);
1650 ips_linetime = DIV_ROUND_CLOSEST(mode->crtc_htotal * 1000 * 8,
Paulo Zanoni85a02de2013-05-03 17:23:43 -03001651 intel_ddi_get_cdclk_freq(dev_priv));
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001652
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001653 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
1654 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03001655}
1656
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001657static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001658{
1659 struct drm_i915_private *dev_priv = dev->dev_private;
1660
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001661 if (IS_GEN9(dev)) {
1662 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00001663 int ret, i;
Vandana Kannan367294b2014-11-04 17:06:46 +00001664 int level, max_level = ilk_wm_max_level(dev);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001665
1666 /* read the first set of memory latencies[0:3] */
1667 val = 0; /* data0 to be programmed to 0 for first set */
1668 mutex_lock(&dev_priv->rps.hw_lock);
1669 ret = sandybridge_pcode_read(dev_priv,
1670 GEN9_PCODE_READ_MEM_LATENCY,
1671 &val);
1672 mutex_unlock(&dev_priv->rps.hw_lock);
1673
1674 if (ret) {
1675 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1676 return;
1677 }
1678
1679 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1680 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1681 GEN9_MEM_LATENCY_LEVEL_MASK;
1682 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1683 GEN9_MEM_LATENCY_LEVEL_MASK;
1684 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1685 GEN9_MEM_LATENCY_LEVEL_MASK;
1686
1687 /* read the second set of memory latencies[4:7] */
1688 val = 1; /* data0 to be programmed to 1 for second set */
1689 mutex_lock(&dev_priv->rps.hw_lock);
1690 ret = sandybridge_pcode_read(dev_priv,
1691 GEN9_PCODE_READ_MEM_LATENCY,
1692 &val);
1693 mutex_unlock(&dev_priv->rps.hw_lock);
1694 if (ret) {
1695 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
1696 return;
1697 }
1698
1699 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
1700 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
1701 GEN9_MEM_LATENCY_LEVEL_MASK;
1702 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
1703 GEN9_MEM_LATENCY_LEVEL_MASK;
1704 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
1705 GEN9_MEM_LATENCY_LEVEL_MASK;
1706
Vandana Kannan367294b2014-11-04 17:06:46 +00001707 /*
1708 * punit doesn't take into account the read latency so we need
1709 * to add 2us to the various latency levels we retrieve from
1710 * the punit.
1711 * - W0 is a bit special in that it's the only level that
1712 * can't be disabled if we want to have display working, so
1713 * we always add 2us there.
1714 * - For levels >=1, punit returns 0us latency when they are
1715 * disabled, so we respect that and don't add 2us then
Vandana Kannan4f947382014-11-04 17:06:47 +00001716 *
1717 * Additionally, if a level n (n > 1) has a 0us latency, all
1718 * levels m (m >= n) need to be disabled. We make sure to
1719 * sanitize the values out of the punit to satisfy this
1720 * requirement.
Vandana Kannan367294b2014-11-04 17:06:46 +00001721 */
1722 wm[0] += 2;
1723 for (level = 1; level <= max_level; level++)
1724 if (wm[level] != 0)
1725 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00001726 else {
1727 for (i = level + 1; i <= max_level; i++)
1728 wm[i] = 0;
Vandana Kannan367294b2014-11-04 17:06:46 +00001729
Vandana Kannan4f947382014-11-04 17:06:47 +00001730 break;
1731 }
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001732 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001733 uint64_t sskpd = I915_READ64(MCH_SSKPD);
1734
1735 wm[0] = (sskpd >> 56) & 0xFF;
1736 if (wm[0] == 0)
1737 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03001738 wm[1] = (sskpd >> 4) & 0xFF;
1739 wm[2] = (sskpd >> 12) & 0xFF;
1740 wm[3] = (sskpd >> 20) & 0x1FF;
1741 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03001742 } else if (INTEL_INFO(dev)->gen >= 6) {
1743 uint32_t sskpd = I915_READ(MCH_SSKPD);
1744
1745 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
1746 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
1747 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
1748 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03001749 } else if (INTEL_INFO(dev)->gen >= 5) {
1750 uint32_t mltr = I915_READ(MLTR_ILK);
1751
1752 /* ILK primary LP0 latency is 700 ns */
1753 wm[0] = 7;
1754 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
1755 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03001756 }
1757}
1758
Ville Syrjälä53615a52013-08-01 16:18:50 +03001759static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
1760{
1761 /* ILK sprite LP0 latency is 1300 ns */
1762 if (INTEL_INFO(dev)->gen == 5)
1763 wm[0] = 13;
1764}
1765
1766static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
1767{
1768 /* ILK cursor LP0 latency is 1300 ns */
1769 if (INTEL_INFO(dev)->gen == 5)
1770 wm[0] = 13;
1771
1772 /* WaDoubleCursorLP3Latency:ivb */
1773 if (IS_IVYBRIDGE(dev))
1774 wm[3] *= 2;
1775}
1776
Damien Lespiau546c81f2014-05-13 15:30:26 +01001777int ilk_wm_max_level(const struct drm_device *dev)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001778{
1779 /* how many WM levels are we expecting */
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001780 if (IS_GEN9(dev))
1781 return 7;
1782 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001783 return 4;
1784 else if (INTEL_INFO(dev)->gen >= 6)
1785 return 3;
1786 else
1787 return 2;
1788}
Daniel Vetter7526ed72014-09-29 15:07:19 +02001789
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001790static void intel_print_wm_latency(struct drm_device *dev,
1791 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001792 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001793{
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03001794 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001795
1796 for (level = 0; level <= max_level; level++) {
1797 unsigned int latency = wm[level];
1798
1799 if (latency == 0) {
1800 DRM_ERROR("%s WM%d latency not provided\n",
1801 name, level);
1802 continue;
1803 }
1804
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001805 /*
1806 * - latencies are in us on gen9.
1807 * - before then, WM1+ latency values are in 0.5us units
1808 */
1809 if (IS_GEN9(dev))
1810 latency *= 10;
1811 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001812 latency *= 5;
1813
1814 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
1815 name, level, wm[level],
1816 latency / 10, latency % 10);
1817 }
1818}
1819
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001820static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
1821 uint16_t wm[5], uint16_t min)
1822{
1823 int level, max_level = ilk_wm_max_level(dev_priv->dev);
1824
1825 if (wm[0] >= min)
1826 return false;
1827
1828 wm[0] = max(wm[0], min);
1829 for (level = 1; level <= max_level; level++)
1830 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
1831
1832 return true;
1833}
1834
1835static void snb_wm_latency_quirk(struct drm_device *dev)
1836{
1837 struct drm_i915_private *dev_priv = dev->dev_private;
1838 bool changed;
1839
1840 /*
1841 * The BIOS provided WM memory latency values are often
1842 * inadequate for high resolution displays. Adjust them.
1843 */
1844 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
1845 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
1846 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
1847
1848 if (!changed)
1849 return;
1850
1851 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
1852 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1853 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1854 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
1855}
1856
Damien Lespiaufa50ad62014-03-17 18:01:16 +00001857static void ilk_setup_wm_latency(struct drm_device *dev)
Ville Syrjälä53615a52013-08-01 16:18:50 +03001858{
1859 struct drm_i915_private *dev_priv = dev->dev_private;
1860
1861 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
1862
1863 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
1864 sizeof(dev_priv->wm.pri_latency));
1865 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
1866 sizeof(dev_priv->wm.pri_latency));
1867
1868 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
1869 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03001870
1871 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
1872 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
1873 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03001874
1875 if (IS_GEN6(dev))
1876 snb_wm_latency_quirk(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03001877}
1878
Pradeep Bhat2af30a52014-11-04 17:06:38 +00001879static void skl_setup_wm_latency(struct drm_device *dev)
1880{
1881 struct drm_i915_private *dev_priv = dev->dev_private;
1882
1883 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
1884 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
1885}
1886
Imre Deak820c1982013-12-17 14:46:36 +02001887static void ilk_compute_wm_parameters(struct drm_crtc *crtc,
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001888 struct ilk_pipe_wm_parameters *p)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001889{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001890 struct drm_device *dev = crtc->dev;
1891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1892 enum pipe pipe = intel_crtc->pipe;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001893 struct drm_plane *plane;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001894
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001895 if (!intel_crtc_active(crtc))
1896 return;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001897
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001898 p->active = true;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001899 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001900 p->pixel_rate = ilk_pipe_pixel_rate(dev, crtc);
1901 p->pri.bytes_per_pixel = crtc->primary->fb->bits_per_pixel / 8;
1902 p->cur.bytes_per_pixel = 4;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001903 p->pri.horiz_pixels = intel_crtc->config->pipe_src_w;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001904 p->cur.horiz_pixels = intel_crtc->cursor_width;
1905 /* TODO: for now, assume primary and cursor planes are always enabled. */
1906 p->pri.enabled = true;
1907 p->cur.enabled = true;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001908
Matt Roperaf2b6532014-04-01 15:22:32 -07001909 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001910 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001911
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001912 if (intel_plane->pipe == pipe) {
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03001913 p->spr = intel_plane->wm;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001914 break;
1915 }
1916 }
1917}
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001918
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001919static void ilk_compute_wm_config(struct drm_device *dev,
1920 struct intel_wm_config *config)
1921{
1922 struct intel_crtc *intel_crtc;
1923
1924 /* Compute the currently _active_ config */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01001925 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001926 const struct intel_pipe_wm *wm = &intel_crtc->wm.active;
1927
1928 if (!wm->pipe_enabled)
1929 continue;
1930
1931 config->sprites_enabled |= wm->sprites_enabled;
1932 config->sprites_scaled |= wm->sprites_scaled;
1933 config->num_pipes_active++;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001934 }
1935}
1936
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001937/* Compute new watermarks for the pipe */
1938static bool intel_compute_pipe_wm(struct drm_crtc *crtc,
Imre Deak820c1982013-12-17 14:46:36 +02001939 const struct ilk_pipe_wm_parameters *params,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001940 struct intel_pipe_wm *pipe_wm)
1941{
1942 struct drm_device *dev = crtc->dev;
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00001943 const struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001944 int level, max_level = ilk_wm_max_level(dev);
1945 /* LP0 watermark maximums depend on this pipe alone */
1946 struct intel_wm_config config = {
1947 .num_pipes_active = 1,
1948 .sprites_enabled = params->spr.enabled,
1949 .sprites_scaled = params->spr.scaled,
1950 };
Imre Deak820c1982013-12-17 14:46:36 +02001951 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001952
Ville Syrjälä2a44b762014-03-07 18:32:09 +02001953 pipe_wm->pipe_enabled = params->active;
1954 pipe_wm->sprites_enabled = params->spr.enabled;
1955 pipe_wm->sprites_scaled = params->spr.scaled;
1956
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02001957 /* ILK/SNB: LP2+ watermarks only w/o sprites */
1958 if (INTEL_INFO(dev)->gen <= 6 && params->spr.enabled)
1959 max_level = 1;
1960
1961 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
1962 if (params->spr.scaled)
1963 max_level = 0;
1964
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001965 ilk_compute_wm_level(dev_priv, 0, params, &pipe_wm->wm[0]);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001966
Ville Syrjäläa42a5712014-01-07 16:14:08 +02001967 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02001968 pipe_wm->linetime = hsw_compute_linetime_wm(dev, crtc);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001969
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001970 /* LP0 watermarks always use 1/2 DDB partitioning */
1971 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
1972
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001973 /* At least LP0 must be valid */
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03001974 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
1975 return false;
1976
1977 ilk_compute_wm_reg_maximums(dev, 1, &max);
1978
1979 for (level = 1; level <= max_level; level++) {
1980 struct intel_wm_level wm = {};
1981
1982 ilk_compute_wm_level(dev_priv, level, params, &wm);
1983
1984 /*
1985 * Disable any watermark level that exceeds the
1986 * register maximums since such watermarks are
1987 * always invalid.
1988 */
1989 if (!ilk_validate_wm_level(level, &max, &wm))
1990 break;
1991
1992 pipe_wm->wm[level] = wm;
1993 }
1994
1995 return true;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03001996}
1997
1998/*
1999 * Merge the watermarks from all active pipes for a specific level.
2000 */
2001static void ilk_merge_wm_level(struct drm_device *dev,
2002 int level,
2003 struct intel_wm_level *ret_wm)
2004{
2005 const struct intel_crtc *intel_crtc;
2006
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002007 ret_wm->enable = true;
2008
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002009 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002010 const struct intel_pipe_wm *active = &intel_crtc->wm.active;
2011 const struct intel_wm_level *wm = &active->wm[level];
2012
2013 if (!active->pipe_enabled)
2014 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002015
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002016 /*
2017 * The watermark values may have been used in the past,
2018 * so we must maintain them in the registers for some
2019 * time even if the level is now disabled.
2020 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002021 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002022 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002023
2024 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2025 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2026 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2027 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2028 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002029}
2030
2031/*
2032 * Merge all low power watermarks for all active pipes.
2033 */
2034static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002035 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002036 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002037 struct intel_pipe_wm *merged)
2038{
2039 int level, max_level = ilk_wm_max_level(dev);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002040 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002041
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002042 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2043 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2044 config->num_pipes_active > 1)
2045 return;
2046
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002047 /* ILK: FBC WM must be disabled always */
2048 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002049
2050 /* merge each WM1+ level */
2051 for (level = 1; level <= max_level; level++) {
2052 struct intel_wm_level *wm = &merged->wm[level];
2053
2054 ilk_merge_wm_level(dev, level, wm);
2055
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002056 if (level > last_enabled_level)
2057 wm->enable = false;
2058 else if (!ilk_validate_wm_level(level, max, wm))
2059 /* make sure all following levels get disabled */
2060 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002061
2062 /*
2063 * The spec says it is preferred to disable
2064 * FBC WMs instead of disabling a WM level.
2065 */
2066 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002067 if (wm->enable)
2068 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002069 wm->fbc_val = 0;
2070 }
2071 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002072
2073 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2074 /*
2075 * FIXME this is racy. FBC might get enabled later.
2076 * What we should check here is whether FBC can be
2077 * enabled sometime later.
2078 */
2079 if (IS_GEN5(dev) && !merged->fbc_wm_enabled && intel_fbc_enabled(dev)) {
2080 for (level = 2; level <= max_level; level++) {
2081 struct intel_wm_level *wm = &merged->wm[level];
2082
2083 wm->enable = false;
2084 }
2085 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002086}
2087
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002088static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2089{
2090 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2091 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2092}
2093
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002094/* The value we need to program into the WM_LPx latency field */
2095static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2096{
2097 struct drm_i915_private *dev_priv = dev->dev_private;
2098
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002099 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002100 return 2 * level;
2101 else
2102 return dev_priv->wm.pri_latency[level];
2103}
2104
Imre Deak820c1982013-12-17 14:46:36 +02002105static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002106 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002107 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002108 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002109{
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002110 struct intel_crtc *intel_crtc;
2111 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002112
Ville Syrjälä0362c782013-10-09 19:17:57 +03002113 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002114 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002115
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002116 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002117 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002118 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002119
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002120 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002121
Ville Syrjälä0362c782013-10-09 19:17:57 +03002122 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002123
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002124 /*
2125 * Maintain the watermark values even if the level is
2126 * disabled. Doing otherwise could cause underruns.
2127 */
2128 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002129 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002130 (r->pri_val << WM1_LP_SR_SHIFT) |
2131 r->cur_val;
2132
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002133 if (r->enable)
2134 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2135
Ville Syrjälä416f4722013-11-02 21:07:46 -07002136 if (INTEL_INFO(dev)->gen >= 8)
2137 results->wm_lp[wm_lp - 1] |=
2138 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2139 else
2140 results->wm_lp[wm_lp - 1] |=
2141 r->fbc_val << WM1_LP_FBC_SHIFT;
2142
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002143 /*
2144 * Always set WM1S_LP_EN when spr_val != 0, even if the
2145 * level is disabled. Doing otherwise could cause underruns.
2146 */
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002147 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2148 WARN_ON(wm_lp != 1);
2149 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2150 } else
2151 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002152 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002153
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002154 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002155 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002156 enum pipe pipe = intel_crtc->pipe;
2157 const struct intel_wm_level *r =
2158 &intel_crtc->wm.active.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002159
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002160 if (WARN_ON(!r->enable))
2161 continue;
2162
2163 results->wm_linetime[pipe] = intel_crtc->wm.active.linetime;
2164
2165 results->wm_pipe[pipe] =
2166 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2167 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2168 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002169 }
2170}
2171
Paulo Zanoni861f3382013-05-31 10:19:21 -03002172/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2173 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002174static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002175 struct intel_pipe_wm *r1,
2176 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002177{
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002178 int level, max_level = ilk_wm_max_level(dev);
2179 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002180
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002181 for (level = 1; level <= max_level; level++) {
2182 if (r1->wm[level].enable)
2183 level1 = level;
2184 if (r2->wm[level].enable)
2185 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002186 }
2187
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002188 if (level1 == level2) {
2189 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002190 return r2;
2191 else
2192 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002193 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002194 return r1;
2195 } else {
2196 return r2;
2197 }
2198}
2199
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002200/* dirty bits used to track which watermarks need changes */
2201#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2202#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2203#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2204#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2205#define WM_DIRTY_FBC (1 << 24)
2206#define WM_DIRTY_DDB (1 << 25)
2207
Damien Lespiau055e3932014-08-18 13:49:10 +01002208static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002209 const struct ilk_wm_values *old,
2210 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002211{
2212 unsigned int dirty = 0;
2213 enum pipe pipe;
2214 int wm_lp;
2215
Damien Lespiau055e3932014-08-18 13:49:10 +01002216 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002217 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2218 dirty |= WM_DIRTY_LINETIME(pipe);
2219 /* Must disable LP1+ watermarks too */
2220 dirty |= WM_DIRTY_LP_ALL;
2221 }
2222
2223 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2224 dirty |= WM_DIRTY_PIPE(pipe);
2225 /* Must disable LP1+ watermarks too */
2226 dirty |= WM_DIRTY_LP_ALL;
2227 }
2228 }
2229
2230 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2231 dirty |= WM_DIRTY_FBC;
2232 /* Must disable LP1+ watermarks too */
2233 dirty |= WM_DIRTY_LP_ALL;
2234 }
2235
2236 if (old->partitioning != new->partitioning) {
2237 dirty |= WM_DIRTY_DDB;
2238 /* Must disable LP1+ watermarks too */
2239 dirty |= WM_DIRTY_LP_ALL;
2240 }
2241
2242 /* LP1+ watermarks already deemed dirty, no need to continue */
2243 if (dirty & WM_DIRTY_LP_ALL)
2244 return dirty;
2245
2246 /* Find the lowest numbered LP1+ watermark in need of an update... */
2247 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2248 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2249 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2250 break;
2251 }
2252
2253 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2254 for (; wm_lp <= 3; wm_lp++)
2255 dirty |= WM_DIRTY_LP(wm_lp);
2256
2257 return dirty;
2258}
2259
Ville Syrjälä8553c182013-12-05 15:51:39 +02002260static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2261 unsigned int dirty)
2262{
Imre Deak820c1982013-12-17 14:46:36 +02002263 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002264 bool changed = false;
2265
2266 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2267 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2268 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2269 changed = true;
2270 }
2271 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2272 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2273 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2274 changed = true;
2275 }
2276 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2277 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2278 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2279 changed = true;
2280 }
2281
2282 /*
2283 * Don't touch WM1S_LP_EN here.
2284 * Doing so could cause underruns.
2285 */
2286
2287 return changed;
2288}
2289
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002290/*
2291 * The spec says we shouldn't write when we don't need, because every write
2292 * causes WMs to be re-evaluated, expending some power.
2293 */
Imre Deak820c1982013-12-17 14:46:36 +02002294static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2295 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002296{
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002297 struct drm_device *dev = dev_priv->dev;
Imre Deak820c1982013-12-17 14:46:36 +02002298 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002299 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002300 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002301
Damien Lespiau055e3932014-08-18 13:49:10 +01002302 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002303 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002304 return;
2305
Ville Syrjälä8553c182013-12-05 15:51:39 +02002306 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002307
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002308 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002309 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002310 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002311 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002312 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002313 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2314
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002315 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002316 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002317 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002318 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002319 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002320 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2321
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002322 if (dirty & WM_DIRTY_DDB) {
Ville Syrjäläa42a5712014-01-07 16:14:08 +02002323 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002324 val = I915_READ(WM_MISC);
2325 if (results->partitioning == INTEL_DDB_PART_1_2)
2326 val &= ~WM_MISC_DATA_PARTITION_5_6;
2327 else
2328 val |= WM_MISC_DATA_PARTITION_5_6;
2329 I915_WRITE(WM_MISC, val);
2330 } else {
2331 val = I915_READ(DISP_ARB_CTL2);
2332 if (results->partitioning == INTEL_DDB_PART_1_2)
2333 val &= ~DISP_DATA_PARTITION_5_6;
2334 else
2335 val |= DISP_DATA_PARTITION_5_6;
2336 I915_WRITE(DISP_ARB_CTL2, val);
2337 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002338 }
2339
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002340 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03002341 val = I915_READ(DISP_ARB_CTL);
2342 if (results->enable_fbc_wm)
2343 val &= ~DISP_FBC_WM_DIS;
2344 else
2345 val |= DISP_FBC_WM_DIS;
2346 I915_WRITE(DISP_ARB_CTL, val);
2347 }
2348
Imre Deak954911e2013-12-17 14:46:34 +02002349 if (dirty & WM_DIRTY_LP(1) &&
2350 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2351 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2352
2353 if (INTEL_INFO(dev)->gen >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002354 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2355 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2356 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2357 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2358 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002359
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002360 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002361 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002362 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002363 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02002364 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002365 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03002366
2367 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002368}
2369
Ville Syrjälä8553c182013-12-05 15:51:39 +02002370static bool ilk_disable_lp_wm(struct drm_device *dev)
2371{
2372 struct drm_i915_private *dev_priv = dev->dev_private;
2373
2374 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2375}
2376
Damien Lespiaub9cec072014-11-04 17:06:43 +00002377/*
2378 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2379 * different active planes.
2380 */
2381
2382#define SKL_DDB_SIZE 896 /* in blocks */
2383
2384static void
2385skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2386 struct drm_crtc *for_crtc,
2387 const struct intel_wm_config *config,
2388 const struct skl_pipe_wm_parameters *params,
2389 struct skl_ddb_entry *alloc /* out */)
2390{
2391 struct drm_crtc *crtc;
2392 unsigned int pipe_size, ddb_size;
2393 int nth_active_pipe;
2394
2395 if (!params->active) {
2396 alloc->start = 0;
2397 alloc->end = 0;
2398 return;
2399 }
2400
2401 ddb_size = SKL_DDB_SIZE;
2402
2403 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2404
2405 nth_active_pipe = 0;
2406 for_each_crtc(dev, crtc) {
2407 if (!intel_crtc_active(crtc))
2408 continue;
2409
2410 if (crtc == for_crtc)
2411 break;
2412
2413 nth_active_pipe++;
2414 }
2415
2416 pipe_size = ddb_size / config->num_pipes_active;
2417 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
Damien Lespiau16160e32014-11-04 17:06:53 +00002418 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002419}
2420
2421static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2422{
2423 if (config->num_pipes_active == 1)
2424 return 32;
2425
2426 return 8;
2427}
2428
Damien Lespiaua269c582014-11-04 17:06:49 +00002429static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2430{
2431 entry->start = reg & 0x3ff;
2432 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00002433 if (entry->end)
2434 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00002435}
2436
Damien Lespiau08db6652014-11-04 17:06:52 +00002437void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2438 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00002439{
2440 struct drm_device *dev = dev_priv->dev;
2441 enum pipe pipe;
2442 int plane;
2443 u32 val;
2444
2445 for_each_pipe(dev_priv, pipe) {
2446 for_each_plane(pipe, plane) {
2447 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2448 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2449 val);
2450 }
2451
2452 val = I915_READ(CUR_BUF_CFG(pipe));
2453 skl_ddb_entry_init_from_hw(&ddb->cursor[pipe], val);
2454 }
2455}
2456
Damien Lespiaub9cec072014-11-04 17:06:43 +00002457static unsigned int
2458skl_plane_relative_data_rate(const struct intel_plane_wm_parameters *p)
2459{
2460 return p->horiz_pixels * p->vert_pixels * p->bytes_per_pixel;
2461}
2462
2463/*
2464 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2465 * a 8192x4096@32bpp framebuffer:
2466 * 3 * 4096 * 8192 * 4 < 2^32
2467 */
2468static unsigned int
2469skl_get_total_relative_data_rate(struct intel_crtc *intel_crtc,
2470 const struct skl_pipe_wm_parameters *params)
2471{
2472 unsigned int total_data_rate = 0;
2473 int plane;
2474
2475 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2476 const struct intel_plane_wm_parameters *p;
2477
2478 p = &params->plane[plane];
2479 if (!p->enabled)
2480 continue;
2481
2482 total_data_rate += skl_plane_relative_data_rate(p);
2483 }
2484
2485 return total_data_rate;
2486}
2487
2488static void
2489skl_allocate_pipe_ddb(struct drm_crtc *crtc,
2490 const struct intel_wm_config *config,
2491 const struct skl_pipe_wm_parameters *params,
2492 struct skl_ddb_allocation *ddb /* out */)
2493{
2494 struct drm_device *dev = crtc->dev;
2495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2496 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002497 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
Damien Lespiaub9cec072014-11-04 17:06:43 +00002498 uint16_t alloc_size, start, cursor_blocks;
2499 unsigned int total_data_rate;
2500 int plane;
2501
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002502 skl_ddb_get_pipe_allocation_limits(dev, crtc, config, params, alloc);
2503 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00002504 if (alloc_size == 0) {
2505 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2506 memset(&ddb->cursor[pipe], 0, sizeof(ddb->cursor[pipe]));
2507 return;
2508 }
2509
2510 cursor_blocks = skl_cursor_allocation(config);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002511 ddb->cursor[pipe].start = alloc->end - cursor_blocks;
2512 ddb->cursor[pipe].end = alloc->end;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002513
2514 alloc_size -= cursor_blocks;
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002515 alloc->end -= cursor_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002516
2517 /*
2518 * Each active plane get a portion of the remaining space, in
2519 * proportion to the amount of data they need to fetch from memory.
2520 *
2521 * FIXME: we may not allocate every single block here.
2522 */
2523 total_data_rate = skl_get_total_relative_data_rate(intel_crtc, params);
2524
Damien Lespiau34bb56a2014-11-04 17:07:01 +00002525 start = alloc->start;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002526 for (plane = 0; plane < intel_num_planes(intel_crtc); plane++) {
2527 const struct intel_plane_wm_parameters *p;
2528 unsigned int data_rate;
2529 uint16_t plane_blocks;
2530
2531 p = &params->plane[plane];
2532 if (!p->enabled)
2533 continue;
2534
2535 data_rate = skl_plane_relative_data_rate(p);
2536
2537 /*
2538 * promote the expression to 64 bits to avoid overflowing, the
2539 * result is < available as data_rate / total_data_rate < 1
2540 */
2541 plane_blocks = div_u64((uint64_t)alloc_size * data_rate,
2542 total_data_rate);
2543
2544 ddb->plane[pipe][plane].start = start;
Damien Lespiau16160e32014-11-04 17:06:53 +00002545 ddb->plane[pipe][plane].end = start + plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00002546
2547 start += plane_blocks;
2548 }
2549
2550}
2551
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002552static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002553{
2554 /* TODO: Take into account the scalers once we support them */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002555 return config->base.adjusted_mode.crtc_clock;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002556}
2557
2558/*
2559 * The max latency should be 257 (max the punit can code is 255 and we add 2us
2560 * for the read latency) and bytes_per_pixel should always be <= 8, so that
2561 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
2562 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
2563*/
2564static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t bytes_per_pixel,
2565 uint32_t latency)
2566{
2567 uint32_t wm_intermediate_val, ret;
2568
2569 if (latency == 0)
2570 return UINT_MAX;
2571
2572 wm_intermediate_val = latency * pixel_rate * bytes_per_pixel;
2573 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
2574
2575 return ret;
2576}
2577
2578static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
2579 uint32_t horiz_pixels, uint8_t bytes_per_pixel,
2580 uint32_t latency)
2581{
2582 uint32_t ret, plane_bytes_per_line, wm_intermediate_val;
2583
2584 if (latency == 0)
2585 return UINT_MAX;
2586
2587 plane_bytes_per_line = horiz_pixels * bytes_per_pixel;
2588 wm_intermediate_val = latency * pixel_rate;
2589 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
2590 plane_bytes_per_line;
2591
2592 return ret;
2593}
2594
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002595static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
2596 const struct intel_crtc *intel_crtc)
2597{
2598 struct drm_device *dev = intel_crtc->base.dev;
2599 struct drm_i915_private *dev_priv = dev->dev_private;
2600 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2601 enum pipe pipe = intel_crtc->pipe;
2602
2603 if (memcmp(new_ddb->plane[pipe], cur_ddb->plane[pipe],
2604 sizeof(new_ddb->plane[pipe])))
2605 return true;
2606
2607 if (memcmp(&new_ddb->cursor[pipe], &cur_ddb->cursor[pipe],
2608 sizeof(new_ddb->cursor[pipe])))
2609 return true;
2610
2611 return false;
2612}
2613
2614static void skl_compute_wm_global_parameters(struct drm_device *dev,
2615 struct intel_wm_config *config)
2616{
2617 struct drm_crtc *crtc;
2618 struct drm_plane *plane;
2619
2620 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2621 config->num_pipes_active += intel_crtc_active(crtc);
2622
2623 /* FIXME: I don't think we need those two global parameters on SKL */
2624 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2625 struct intel_plane *intel_plane = to_intel_plane(plane);
2626
2627 config->sprites_enabled |= intel_plane->wm.enabled;
2628 config->sprites_scaled |= intel_plane->wm.scaled;
2629 }
2630}
2631
2632static void skl_compute_wm_pipe_parameters(struct drm_crtc *crtc,
2633 struct skl_pipe_wm_parameters *p)
2634{
2635 struct drm_device *dev = crtc->dev;
2636 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2637 enum pipe pipe = intel_crtc->pipe;
2638 struct drm_plane *plane;
2639 int i = 1; /* Index for sprite planes start */
2640
2641 p->active = intel_crtc_active(crtc);
2642 if (p->active) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002643 p->pipe_htotal = intel_crtc->config->base.adjusted_mode.crtc_htotal;
2644 p->pixel_rate = skl_pipe_pixel_rate(intel_crtc->config);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002645
2646 /*
2647 * For now, assume primary and cursor planes are always enabled.
2648 */
2649 p->plane[0].enabled = true;
2650 p->plane[0].bytes_per_pixel =
2651 crtc->primary->fb->bits_per_pixel / 8;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002652 p->plane[0].horiz_pixels = intel_crtc->config->pipe_src_w;
2653 p->plane[0].vert_pixels = intel_crtc->config->pipe_src_h;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002654
2655 p->cursor.enabled = true;
2656 p->cursor.bytes_per_pixel = 4;
2657 p->cursor.horiz_pixels = intel_crtc->cursor_width ?
2658 intel_crtc->cursor_width : 64;
2659 }
2660
2661 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
2662 struct intel_plane *intel_plane = to_intel_plane(plane);
2663
Sonika Jindala712f8e2014-12-09 10:59:15 +05302664 if (intel_plane->pipe == pipe &&
2665 plane->type == DRM_PLANE_TYPE_OVERLAY)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002666 p->plane[i++] = intel_plane->wm;
2667 }
2668}
2669
2670static bool skl_compute_plane_wm(struct skl_pipe_wm_parameters *p,
Damien Lespiauafb024a2014-11-04 17:06:59 +00002671 struct intel_plane_wm_parameters *p_params,
2672 uint16_t ddb_allocation,
2673 uint32_t mem_value,
2674 uint16_t *out_blocks, /* out */
2675 uint8_t *out_lines /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002676{
Damien Lespiaue6d66172014-11-04 17:06:55 +00002677 uint32_t method1, method2, plane_bytes_per_line, res_blocks, res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002678 uint32_t result_bytes;
2679
Vandana Kannan4f947382014-11-04 17:06:47 +00002680 if (mem_value == 0 || !p->active || !p_params->enabled)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002681 return false;
2682
2683 method1 = skl_wm_method1(p->pixel_rate,
2684 p_params->bytes_per_pixel,
2685 mem_value);
2686 method2 = skl_wm_method2(p->pixel_rate,
2687 p->pipe_htotal,
2688 p_params->horiz_pixels,
2689 p_params->bytes_per_pixel,
2690 mem_value);
2691
2692 plane_bytes_per_line = p_params->horiz_pixels *
2693 p_params->bytes_per_pixel;
2694
2695 /* For now xtile and linear */
Damien Lespiau21fca252014-11-04 17:06:54 +00002696 if (((ddb_allocation * 512) / plane_bytes_per_line) >= 1)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002697 result_bytes = min(method1, method2);
2698 else
2699 result_bytes = method1;
2700
Damien Lespiaue6d66172014-11-04 17:06:55 +00002701 res_blocks = DIV_ROUND_UP(result_bytes, 512) + 1;
2702 res_lines = DIV_ROUND_UP(result_bytes, plane_bytes_per_line);
2703
2704 if (res_blocks > ddb_allocation || res_lines > 31)
2705 return false;
2706
2707 *out_blocks = res_blocks;
2708 *out_lines = res_lines;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002709
2710 return true;
2711}
2712
2713static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
2714 struct skl_ddb_allocation *ddb,
2715 struct skl_pipe_wm_parameters *p,
2716 enum pipe pipe,
2717 int level,
2718 int num_planes,
2719 struct skl_wm_level *result)
2720{
2721 uint16_t latency = dev_priv->wm.skl_latency[level];
2722 uint16_t ddb_blocks;
2723 int i;
2724
2725 for (i = 0; i < num_planes; i++) {
2726 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
2727
2728 result->plane_en[i] = skl_compute_plane_wm(p, &p->plane[i],
2729 ddb_blocks,
2730 latency,
2731 &result->plane_res_b[i],
2732 &result->plane_res_l[i]);
2733 }
2734
2735 ddb_blocks = skl_ddb_entry_size(&ddb->cursor[pipe]);
2736 result->cursor_en = skl_compute_plane_wm(p, &p->cursor, ddb_blocks,
2737 latency, &result->cursor_res_b,
2738 &result->cursor_res_l);
2739}
2740
Damien Lespiau407b50f2014-11-04 17:06:57 +00002741static uint32_t
2742skl_compute_linetime_wm(struct drm_crtc *crtc, struct skl_pipe_wm_parameters *p)
2743{
2744 if (!intel_crtc_active(crtc))
2745 return 0;
2746
2747 return DIV_ROUND_UP(8 * p->pipe_htotal * 1000, p->pixel_rate);
2748
2749}
2750
2751static void skl_compute_transition_wm(struct drm_crtc *crtc,
2752 struct skl_pipe_wm_parameters *params,
Damien Lespiau9414f562014-11-04 17:06:58 +00002753 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00002754{
Damien Lespiau9414f562014-11-04 17:06:58 +00002755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2756 int i;
2757
Damien Lespiau407b50f2014-11-04 17:06:57 +00002758 if (!params->active)
2759 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00002760
2761 /* Until we know more, just disable transition WMs */
2762 for (i = 0; i < intel_num_planes(intel_crtc); i++)
2763 trans_wm->plane_en[i] = false;
2764 trans_wm->cursor_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00002765}
2766
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002767static void skl_compute_pipe_wm(struct drm_crtc *crtc,
2768 struct skl_ddb_allocation *ddb,
2769 struct skl_pipe_wm_parameters *params,
2770 struct skl_pipe_wm *pipe_wm)
2771{
2772 struct drm_device *dev = crtc->dev;
2773 const struct drm_i915_private *dev_priv = dev->dev_private;
2774 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2775 int level, max_level = ilk_wm_max_level(dev);
2776
2777 for (level = 0; level <= max_level; level++) {
2778 skl_compute_wm_level(dev_priv, ddb, params, intel_crtc->pipe,
2779 level, intel_num_planes(intel_crtc),
2780 &pipe_wm->wm[level]);
2781 }
2782 pipe_wm->linetime = skl_compute_linetime_wm(crtc, params);
2783
Damien Lespiau9414f562014-11-04 17:06:58 +00002784 skl_compute_transition_wm(crtc, params, &pipe_wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002785}
2786
2787static void skl_compute_wm_results(struct drm_device *dev,
2788 struct skl_pipe_wm_parameters *p,
2789 struct skl_pipe_wm *p_wm,
2790 struct skl_wm_values *r,
2791 struct intel_crtc *intel_crtc)
2792{
2793 int level, max_level = ilk_wm_max_level(dev);
2794 enum pipe pipe = intel_crtc->pipe;
Damien Lespiau9414f562014-11-04 17:06:58 +00002795 uint32_t temp;
2796 int i;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002797
2798 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002799 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2800 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002801
2802 temp |= p_wm->wm[level].plane_res_l[i] <<
2803 PLANE_WM_LINES_SHIFT;
2804 temp |= p_wm->wm[level].plane_res_b[i];
2805 if (p_wm->wm[level].plane_en[i])
2806 temp |= PLANE_WM_EN;
2807
2808 r->plane[pipe][i][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002809 }
2810
2811 temp = 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002812
2813 temp |= p_wm->wm[level].cursor_res_l << PLANE_WM_LINES_SHIFT;
2814 temp |= p_wm->wm[level].cursor_res_b;
2815
2816 if (p_wm->wm[level].cursor_en)
2817 temp |= PLANE_WM_EN;
2818
2819 r->cursor[pipe][level] = temp;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002820
2821 }
2822
Damien Lespiau9414f562014-11-04 17:06:58 +00002823 /* transition WMs */
2824 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
2825 temp = 0;
2826 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
2827 temp |= p_wm->trans_wm.plane_res_b[i];
2828 if (p_wm->trans_wm.plane_en[i])
2829 temp |= PLANE_WM_EN;
2830
2831 r->plane_trans[pipe][i] = temp;
2832 }
2833
2834 temp = 0;
2835 temp |= p_wm->trans_wm.cursor_res_l << PLANE_WM_LINES_SHIFT;
2836 temp |= p_wm->trans_wm.cursor_res_b;
2837 if (p_wm->trans_wm.cursor_en)
2838 temp |= PLANE_WM_EN;
2839
2840 r->cursor_trans[pipe] = temp;
2841
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002842 r->wm_linetime[pipe] = p_wm->linetime;
2843}
2844
Damien Lespiau16160e32014-11-04 17:06:53 +00002845static void skl_ddb_entry_write(struct drm_i915_private *dev_priv, uint32_t reg,
2846 const struct skl_ddb_entry *entry)
2847{
2848 if (entry->end)
2849 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
2850 else
2851 I915_WRITE(reg, 0);
2852}
2853
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002854static void skl_write_wm_values(struct drm_i915_private *dev_priv,
2855 const struct skl_wm_values *new)
2856{
2857 struct drm_device *dev = dev_priv->dev;
2858 struct intel_crtc *crtc;
2859
2860 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
2861 int i, level, max_level = ilk_wm_max_level(dev);
2862 enum pipe pipe = crtc->pipe;
2863
Damien Lespiau5d374d92014-11-04 17:07:00 +00002864 if (!new->dirty[pipe])
2865 continue;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002866
Damien Lespiau5d374d92014-11-04 17:07:00 +00002867 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
2868
2869 for (level = 0; level <= max_level; level++) {
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002870 for (i = 0; i < intel_num_planes(crtc); i++)
Damien Lespiau5d374d92014-11-04 17:07:00 +00002871 I915_WRITE(PLANE_WM(pipe, i, level),
2872 new->plane[pipe][i][level]);
2873 I915_WRITE(CUR_WM(pipe, level),
2874 new->cursor[pipe][level]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002875 }
Damien Lespiau5d374d92014-11-04 17:07:00 +00002876 for (i = 0; i < intel_num_planes(crtc); i++)
2877 I915_WRITE(PLANE_WM_TRANS(pipe, i),
2878 new->plane_trans[pipe][i]);
2879 I915_WRITE(CUR_WM_TRANS(pipe), new->cursor_trans[pipe]);
2880
2881 for (i = 0; i < intel_num_planes(crtc); i++)
2882 skl_ddb_entry_write(dev_priv,
2883 PLANE_BUF_CFG(pipe, i),
2884 &new->ddb.plane[pipe][i]);
2885
2886 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
2887 &new->ddb.cursor[pipe]);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002888 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00002889}
2890
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002891/*
2892 * When setting up a new DDB allocation arrangement, we need to correctly
2893 * sequence the times at which the new allocations for the pipes are taken into
2894 * account or we'll have pipes fetching from space previously allocated to
2895 * another pipe.
2896 *
2897 * Roughly the sequence looks like:
2898 * 1. re-allocate the pipe(s) with the allocation being reduced and not
2899 * overlapping with a previous light-up pipe (another way to put it is:
2900 * pipes with their new allocation strickly included into their old ones).
2901 * 2. re-allocate the other pipes that get their allocation reduced
2902 * 3. allocate the pipes having their allocation increased
2903 *
2904 * Steps 1. and 2. are here to take care of the following case:
2905 * - Initially DDB looks like this:
2906 * | B | C |
2907 * - enable pipe A.
2908 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
2909 * allocation
2910 * | A | B | C |
2911 *
2912 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
2913 */
2914
Damien Lespiaud21b7952014-11-04 17:07:03 +00002915static void
2916skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002917{
2918 struct drm_device *dev = dev_priv->dev;
2919 int plane;
2920
Damien Lespiaud21b7952014-11-04 17:07:03 +00002921 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
2922
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002923 for_each_plane(pipe, plane) {
2924 I915_WRITE(PLANE_SURF(pipe, plane),
2925 I915_READ(PLANE_SURF(pipe, plane)));
2926 }
2927 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
2928}
2929
2930static bool
2931skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
2932 const struct skl_ddb_allocation *new,
2933 enum pipe pipe)
2934{
2935 uint16_t old_size, new_size;
2936
2937 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
2938 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
2939
2940 return old_size != new_size &&
2941 new->pipe[pipe].start >= old->pipe[pipe].start &&
2942 new->pipe[pipe].end <= old->pipe[pipe].end;
2943}
2944
2945static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
2946 struct skl_wm_values *new_values)
2947{
2948 struct drm_device *dev = dev_priv->dev;
2949 struct skl_ddb_allocation *cur_ddb, *new_ddb;
2950 bool reallocated[I915_MAX_PIPES] = {false, false, false};
2951 struct intel_crtc *crtc;
2952 enum pipe pipe;
2953
2954 new_ddb = &new_values->ddb;
2955 cur_ddb = &dev_priv->wm.skl_hw.ddb;
2956
2957 /*
2958 * First pass: flush the pipes with the new allocation contained into
2959 * the old space.
2960 *
2961 * We'll wait for the vblank on those pipes to ensure we can safely
2962 * re-allocate the freed space without this pipe fetching from it.
2963 */
2964 for_each_intel_crtc(dev, crtc) {
2965 if (!crtc->active)
2966 continue;
2967
2968 pipe = crtc->pipe;
2969
2970 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
2971 continue;
2972
Damien Lespiaud21b7952014-11-04 17:07:03 +00002973 skl_wm_flush_pipe(dev_priv, pipe, 1);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002974 intel_wait_for_vblank(dev, pipe);
2975
2976 reallocated[pipe] = true;
2977 }
2978
2979
2980 /*
2981 * Second pass: flush the pipes that are having their allocation
2982 * reduced, but overlapping with a previous allocation.
2983 *
2984 * Here as well we need to wait for the vblank to make sure the freed
2985 * space is not used anymore.
2986 */
2987 for_each_intel_crtc(dev, crtc) {
2988 if (!crtc->active)
2989 continue;
2990
2991 pipe = crtc->pipe;
2992
2993 if (reallocated[pipe])
2994 continue;
2995
2996 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
2997 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
Damien Lespiaud21b7952014-11-04 17:07:03 +00002998 skl_wm_flush_pipe(dev_priv, pipe, 2);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00002999 intel_wait_for_vblank(dev, pipe);
Sonika Jindald9d8e6b2014-12-11 17:58:15 +05303000 reallocated[pipe] = true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003001 }
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003002 }
3003
3004 /*
3005 * Third pass: flush the pipes that got more space allocated.
3006 *
3007 * We don't need to actively wait for the update here, next vblank
3008 * will just get more DDB space with the correct WM values.
3009 */
3010 for_each_intel_crtc(dev, crtc) {
3011 if (!crtc->active)
3012 continue;
3013
3014 pipe = crtc->pipe;
3015
3016 /*
3017 * At this point, only the pipes more space than before are
3018 * left to re-allocate.
3019 */
3020 if (reallocated[pipe])
3021 continue;
3022
Damien Lespiaud21b7952014-11-04 17:07:03 +00003023 skl_wm_flush_pipe(dev_priv, pipe, 3);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003024 }
3025}
3026
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003027static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3028 struct skl_pipe_wm_parameters *params,
3029 struct intel_wm_config *config,
3030 struct skl_ddb_allocation *ddb, /* out */
3031 struct skl_pipe_wm *pipe_wm /* out */)
3032{
3033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3034
3035 skl_compute_wm_pipe_parameters(crtc, params);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003036 skl_allocate_pipe_ddb(crtc, config, params, ddb);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003037 skl_compute_pipe_wm(crtc, ddb, params, pipe_wm);
3038
3039 if (!memcmp(&intel_crtc->wm.skl_active, pipe_wm, sizeof(*pipe_wm)))
3040 return false;
3041
3042 intel_crtc->wm.skl_active = *pipe_wm;
3043 return true;
3044}
3045
3046static void skl_update_other_pipe_wm(struct drm_device *dev,
3047 struct drm_crtc *crtc,
3048 struct intel_wm_config *config,
3049 struct skl_wm_values *r)
3050{
3051 struct intel_crtc *intel_crtc;
3052 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3053
3054 /*
3055 * If the WM update hasn't changed the allocation for this_crtc (the
3056 * crtc we are currently computing the new WM values for), other
3057 * enabled crtcs will keep the same allocation and we don't need to
3058 * recompute anything for them.
3059 */
3060 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3061 return;
3062
3063 /*
3064 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3065 * other active pipes need new DDB allocation and WM values.
3066 */
3067 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
3068 base.head) {
3069 struct skl_pipe_wm_parameters params = {};
3070 struct skl_pipe_wm pipe_wm = {};
3071 bool wm_changed;
3072
3073 if (this_crtc->pipe == intel_crtc->pipe)
3074 continue;
3075
3076 if (!intel_crtc->active)
3077 continue;
3078
3079 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3080 &params, config,
3081 &r->ddb, &pipe_wm);
3082
3083 /*
3084 * If we end up re-computing the other pipe WM values, it's
3085 * because it was really needed, so we expect the WM values to
3086 * be different.
3087 */
3088 WARN_ON(!wm_changed);
3089
3090 skl_compute_wm_results(dev, &params, &pipe_wm, r, intel_crtc);
3091 r->dirty[intel_crtc->pipe] = true;
3092 }
3093}
3094
3095static void skl_update_wm(struct drm_crtc *crtc)
3096{
3097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3098 struct drm_device *dev = crtc->dev;
3099 struct drm_i915_private *dev_priv = dev->dev_private;
3100 struct skl_pipe_wm_parameters params = {};
3101 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3102 struct skl_pipe_wm pipe_wm = {};
3103 struct intel_wm_config config = {};
3104
3105 memset(results, 0, sizeof(*results));
3106
3107 skl_compute_wm_global_parameters(dev, &config);
3108
3109 if (!skl_update_pipe_wm(crtc, &params, &config,
3110 &results->ddb, &pipe_wm))
3111 return;
3112
3113 skl_compute_wm_results(dev, &params, &pipe_wm, results, intel_crtc);
3114 results->dirty[intel_crtc->pipe] = true;
3115
3116 skl_update_other_pipe_wm(dev, crtc, &config, results);
3117 skl_write_wm_values(dev_priv, results);
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00003118 skl_flush_wm_values(dev_priv, results);
Damien Lespiau53b0deb2014-11-04 17:06:48 +00003119
3120 /* store the new configuration */
3121 dev_priv->wm.skl_hw = *results;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003122}
3123
3124static void
3125skl_update_sprite_wm(struct drm_plane *plane, struct drm_crtc *crtc,
3126 uint32_t sprite_width, uint32_t sprite_height,
3127 int pixel_size, bool enabled, bool scaled)
3128{
3129 struct intel_plane *intel_plane = to_intel_plane(plane);
3130
3131 intel_plane->wm.enabled = enabled;
3132 intel_plane->wm.scaled = scaled;
3133 intel_plane->wm.horiz_pixels = sprite_width;
3134 intel_plane->wm.vert_pixels = sprite_height;
3135 intel_plane->wm.bytes_per_pixel = pixel_size;
3136
3137 skl_update_wm(crtc);
3138}
3139
Imre Deak820c1982013-12-17 14:46:36 +02003140static void ilk_update_wm(struct drm_crtc *crtc)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003141{
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003143 struct drm_device *dev = crtc->dev;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003144 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003145 struct ilk_wm_maximums max;
3146 struct ilk_pipe_wm_parameters params = {};
3147 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003148 enum intel_ddb_partitioning partitioning;
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003149 struct intel_pipe_wm pipe_wm = {};
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003150 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003151 struct intel_wm_config config = {};
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003152
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003153 ilk_compute_wm_parameters(crtc, &params);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003154
Ville Syrjälä7c4a3952013-10-09 19:17:56 +03003155 intel_compute_pipe_wm(crtc, &params, &pipe_wm);
3156
3157 if (!memcmp(&intel_crtc->wm.active, &pipe_wm, sizeof(pipe_wm)))
3158 return;
3159
3160 intel_crtc->wm.active = pipe_wm;
3161
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003162 ilk_compute_wm_config(dev, &config);
3163
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003164 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003165 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03003166
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003167 /* 5/6 split only in single pipe config on IVB+ */
Ville Syrjäläec98c8d2013-10-11 15:26:26 +03003168 if (INTEL_INFO(dev)->gen >= 7 &&
3169 config.num_pipes_active == 1 && config.sprites_enabled) {
Ville Syrjälä34982fe2013-10-09 19:18:09 +03003170 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02003171 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03003172
Imre Deak820c1982013-12-17 14:46:36 +02003173 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03003174 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003175 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003176 }
3177
Ville Syrjälä198a1e92013-10-09 19:17:58 +03003178 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03003179 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03003180
Imre Deak820c1982013-12-17 14:46:36 +02003181 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003182
Imre Deak820c1982013-12-17 14:46:36 +02003183 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003184}
3185
Damien Lespiaued57cb82014-07-15 09:21:24 +02003186static void
3187ilk_update_sprite_wm(struct drm_plane *plane,
3188 struct drm_crtc *crtc,
3189 uint32_t sprite_width, uint32_t sprite_height,
3190 int pixel_size, bool enabled, bool scaled)
Paulo Zanoni526682e2013-05-24 11:59:18 -03003191{
Ville Syrjälä8553c182013-12-05 15:51:39 +02003192 struct drm_device *dev = plane->dev;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003193 struct intel_plane *intel_plane = to_intel_plane(plane);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003194
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003195 intel_plane->wm.enabled = enabled;
3196 intel_plane->wm.scaled = scaled;
3197 intel_plane->wm.horiz_pixels = sprite_width;
Damien Lespiaued57cb82014-07-15 09:21:24 +02003198 intel_plane->wm.vert_pixels = sprite_width;
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003199 intel_plane->wm.bytes_per_pixel = pixel_size;
Paulo Zanoni526682e2013-05-24 11:59:18 -03003200
Ville Syrjälä8553c182013-12-05 15:51:39 +02003201 /*
3202 * IVB workaround: must disable low power watermarks for at least
3203 * one frame before enabling scaling. LP watermarks can be re-enabled
3204 * when scaling is disabled.
3205 *
3206 * WaCxSRDisabledForSpriteScaling:ivb
3207 */
3208 if (IS_IVYBRIDGE(dev) && scaled && ilk_disable_lp_wm(dev))
3209 intel_wait_for_vblank(dev, intel_plane->pipe);
3210
Imre Deak820c1982013-12-17 14:46:36 +02003211 ilk_update_wm(crtc);
Paulo Zanoni526682e2013-05-24 11:59:18 -03003212}
3213
Pradeep Bhat30789992014-11-04 17:06:45 +00003214static void skl_pipe_wm_active_state(uint32_t val,
3215 struct skl_pipe_wm *active,
3216 bool is_transwm,
3217 bool is_cursor,
3218 int i,
3219 int level)
3220{
3221 bool is_enabled = (val & PLANE_WM_EN) != 0;
3222
3223 if (!is_transwm) {
3224 if (!is_cursor) {
3225 active->wm[level].plane_en[i] = is_enabled;
3226 active->wm[level].plane_res_b[i] =
3227 val & PLANE_WM_BLOCKS_MASK;
3228 active->wm[level].plane_res_l[i] =
3229 (val >> PLANE_WM_LINES_SHIFT) &
3230 PLANE_WM_LINES_MASK;
3231 } else {
3232 active->wm[level].cursor_en = is_enabled;
3233 active->wm[level].cursor_res_b =
3234 val & PLANE_WM_BLOCKS_MASK;
3235 active->wm[level].cursor_res_l =
3236 (val >> PLANE_WM_LINES_SHIFT) &
3237 PLANE_WM_LINES_MASK;
3238 }
3239 } else {
3240 if (!is_cursor) {
3241 active->trans_wm.plane_en[i] = is_enabled;
3242 active->trans_wm.plane_res_b[i] =
3243 val & PLANE_WM_BLOCKS_MASK;
3244 active->trans_wm.plane_res_l[i] =
3245 (val >> PLANE_WM_LINES_SHIFT) &
3246 PLANE_WM_LINES_MASK;
3247 } else {
3248 active->trans_wm.cursor_en = is_enabled;
3249 active->trans_wm.cursor_res_b =
3250 val & PLANE_WM_BLOCKS_MASK;
3251 active->trans_wm.cursor_res_l =
3252 (val >> PLANE_WM_LINES_SHIFT) &
3253 PLANE_WM_LINES_MASK;
3254 }
3255 }
3256}
3257
3258static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3259{
3260 struct drm_device *dev = crtc->dev;
3261 struct drm_i915_private *dev_priv = dev->dev_private;
3262 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3264 struct skl_pipe_wm *active = &intel_crtc->wm.skl_active;
3265 enum pipe pipe = intel_crtc->pipe;
3266 int level, i, max_level;
3267 uint32_t temp;
3268
3269 max_level = ilk_wm_max_level(dev);
3270
3271 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3272
3273 for (level = 0; level <= max_level; level++) {
3274 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3275 hw->plane[pipe][i][level] =
3276 I915_READ(PLANE_WM(pipe, i, level));
3277 hw->cursor[pipe][level] = I915_READ(CUR_WM(pipe, level));
3278 }
3279
3280 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3281 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3282 hw->cursor_trans[pipe] = I915_READ(CUR_WM_TRANS(pipe));
3283
3284 if (!intel_crtc_active(crtc))
3285 return;
3286
3287 hw->dirty[pipe] = true;
3288
3289 active->linetime = hw->wm_linetime[pipe];
3290
3291 for (level = 0; level <= max_level; level++) {
3292 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3293 temp = hw->plane[pipe][i][level];
3294 skl_pipe_wm_active_state(temp, active, false,
3295 false, i, level);
3296 }
3297 temp = hw->cursor[pipe][level];
3298 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3299 }
3300
3301 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3302 temp = hw->plane_trans[pipe][i];
3303 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3304 }
3305
3306 temp = hw->cursor_trans[pipe];
3307 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3308}
3309
3310void skl_wm_get_hw_state(struct drm_device *dev)
3311{
Damien Lespiaua269c582014-11-04 17:06:49 +00003312 struct drm_i915_private *dev_priv = dev->dev_private;
3313 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00003314 struct drm_crtc *crtc;
3315
Damien Lespiaua269c582014-11-04 17:06:49 +00003316 skl_ddb_get_hw_state(dev_priv, ddb);
Pradeep Bhat30789992014-11-04 17:06:45 +00003317 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3318 skl_pipe_wm_get_hw_state(crtc);
3319}
3320
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003321static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3322{
3323 struct drm_device *dev = crtc->dev;
3324 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003325 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3327 struct intel_pipe_wm *active = &intel_crtc->wm.active;
3328 enum pipe pipe = intel_crtc->pipe;
3329 static const unsigned int wm0_pipe_reg[] = {
3330 [PIPE_A] = WM0_PIPEA_ILK,
3331 [PIPE_B] = WM0_PIPEB_ILK,
3332 [PIPE_C] = WM0_PIPEC_IVB,
3333 };
3334
3335 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003336 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02003337 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003338
Ville Syrjälä2a44b762014-03-07 18:32:09 +02003339 active->pipe_enabled = intel_crtc_active(crtc);
3340
3341 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003342 u32 tmp = hw->wm_pipe[pipe];
3343
3344 /*
3345 * For active pipes LP0 watermark is marked as
3346 * enabled, and LP1+ watermaks as disabled since
3347 * we can't really reverse compute them in case
3348 * multiple pipes are active.
3349 */
3350 active->wm[0].enable = true;
3351 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3352 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3353 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3354 active->linetime = hw->wm_linetime[pipe];
3355 } else {
3356 int level, max_level = ilk_wm_max_level(dev);
3357
3358 /*
3359 * For inactive pipes, all watermark levels
3360 * should be marked as enabled but zeroed,
3361 * which is what we'd compute them to.
3362 */
3363 for (level = 0; level <= max_level; level++)
3364 active->wm[level].enable = true;
3365 }
3366}
3367
3368void ilk_wm_get_hw_state(struct drm_device *dev)
3369{
3370 struct drm_i915_private *dev_priv = dev->dev_private;
Imre Deak820c1982013-12-17 14:46:36 +02003371 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003372 struct drm_crtc *crtc;
3373
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003374 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003375 ilk_pipe_wm_get_hw_state(crtc);
3376
3377 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
3378 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
3379 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
3380
3381 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Ville Syrjäläcfa76982014-03-07 18:32:08 +02003382 if (INTEL_INFO(dev)->gen >= 7) {
3383 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
3384 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
3385 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003386
Ville Syrjäläa42a5712014-01-07 16:14:08 +02003387 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003388 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
3389 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
3390 else if (IS_IVYBRIDGE(dev))
3391 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
3392 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03003393
3394 hw->enable_fbc_wm =
3395 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
3396}
3397
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003398/**
3399 * intel_update_watermarks - update FIFO watermark values based on current modes
3400 *
3401 * Calculate watermark values for the various WM regs based on current mode
3402 * and plane configuration.
3403 *
3404 * There are several cases to deal with here:
3405 * - normal (i.e. non-self-refresh)
3406 * - self-refresh (SR) mode
3407 * - lines are large relative to FIFO size (buffer can hold up to 2)
3408 * - lines are small relative to FIFO size (buffer can hold more than 2
3409 * lines), so need to account for TLB latency
3410 *
3411 * The normal calculation is:
3412 * watermark = dotclock * bytes per pixel * latency
3413 * where latency is platform & configuration dependent (we assume pessimal
3414 * values here).
3415 *
3416 * The SR calculation is:
3417 * watermark = (trunc(latency/line time)+1) * surface width *
3418 * bytes per pixel
3419 * where
3420 * line time = htotal / dotclock
3421 * surface width = hdisplay for normal plane and 64 for cursor
3422 * and latency is assumed to be high, as above.
3423 *
3424 * The final value programmed to the register should always be rounded up,
3425 * and include an extra 2 entries to account for clock crossings.
3426 *
3427 * We don't use the sprite, so we can ignore that. And on Crestline we have
3428 * to set the non-SR watermarks to 8.
3429 */
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003430void intel_update_watermarks(struct drm_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003431{
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003432 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003433
3434 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03003435 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003436}
3437
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003438void intel_update_sprite_watermarks(struct drm_plane *plane,
3439 struct drm_crtc *crtc,
Damien Lespiaued57cb82014-07-15 09:21:24 +02003440 uint32_t sprite_width,
3441 uint32_t sprite_height,
3442 int pixel_size,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003443 bool enabled, bool scaled)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003444{
Ville Syrjäläadf3d352013-08-06 22:24:11 +03003445 struct drm_i915_private *dev_priv = plane->dev->dev_private;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003446
3447 if (dev_priv->display.update_sprite_wm)
Damien Lespiaued57cb82014-07-15 09:21:24 +02003448 dev_priv->display.update_sprite_wm(plane, crtc,
3449 sprite_width, sprite_height,
Ville Syrjälä39db4a42013-08-06 22:24:00 +03003450 pixel_size, enabled, scaled);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03003451}
3452
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003453static struct drm_i915_gem_object *
3454intel_alloc_context_page(struct drm_device *dev)
3455{
3456 struct drm_i915_gem_object *ctx;
3457 int ret;
3458
3459 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
3460
3461 ctx = i915_gem_alloc_object(dev, 4096);
3462 if (!ctx) {
3463 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
3464 return NULL;
3465 }
3466
Daniel Vetterc69766f2014-02-14 14:01:17 +01003467 ret = i915_gem_obj_ggtt_pin(ctx, 4096, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003468 if (ret) {
3469 DRM_ERROR("failed to pin power context: %d\n", ret);
3470 goto err_unref;
3471 }
3472
3473 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
3474 if (ret) {
3475 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
3476 goto err_unpin;
3477 }
3478
3479 return ctx;
3480
3481err_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08003482 i915_gem_object_ggtt_unpin(ctx);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003483err_unref:
3484 drm_gem_object_unreference(&ctx->base);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003485 return NULL;
3486}
3487
Daniel Vetter92703882012-08-09 16:46:01 +02003488/**
3489 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02003490 */
3491DEFINE_SPINLOCK(mchdev_lock);
3492
3493/* Global for IPS driver to get at the current i915 device. Protected by
3494 * mchdev_lock. */
3495static struct drm_i915_private *i915_mch_dev;
3496
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003497bool ironlake_set_drps(struct drm_device *dev, u8 val)
3498{
3499 struct drm_i915_private *dev_priv = dev->dev_private;
3500 u16 rgvswctl;
3501
Daniel Vetter92703882012-08-09 16:46:01 +02003502 assert_spin_locked(&mchdev_lock);
3503
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003504 rgvswctl = I915_READ16(MEMSWCTL);
3505 if (rgvswctl & MEMCTL_CMD_STS) {
3506 DRM_DEBUG("gpu busy, RCS change rejected\n");
3507 return false; /* still busy with another command */
3508 }
3509
3510 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
3511 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
3512 I915_WRITE16(MEMSWCTL, rgvswctl);
3513 POSTING_READ16(MEMSWCTL);
3514
3515 rgvswctl |= MEMCTL_CMD_STS;
3516 I915_WRITE16(MEMSWCTL, rgvswctl);
3517
3518 return true;
3519}
3520
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003521static void ironlake_enable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003522{
3523 struct drm_i915_private *dev_priv = dev->dev_private;
3524 u32 rgvmodectl = I915_READ(MEMMODECTL);
3525 u8 fmax, fmin, fstart, vstart;
3526
Daniel Vetter92703882012-08-09 16:46:01 +02003527 spin_lock_irq(&mchdev_lock);
3528
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003529 /* Enable temp reporting */
3530 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
3531 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
3532
3533 /* 100ms RC evaluation intervals */
3534 I915_WRITE(RCUPEI, 100000);
3535 I915_WRITE(RCDNEI, 100000);
3536
3537 /* Set max/min thresholds to 90ms and 80ms respectively */
3538 I915_WRITE(RCBMAXAVG, 90000);
3539 I915_WRITE(RCBMINAVG, 80000);
3540
3541 I915_WRITE(MEMIHYST, 1);
3542
3543 /* Set up min, max, and cur for interrupt handling */
3544 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
3545 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
3546 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
3547 MEMMODE_FSTART_SHIFT;
3548
3549 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
3550 PXVFREQ_PX_SHIFT;
3551
Daniel Vetter20e4d402012-08-08 23:35:39 +02003552 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
3553 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003554
Daniel Vetter20e4d402012-08-08 23:35:39 +02003555 dev_priv->ips.max_delay = fstart;
3556 dev_priv->ips.min_delay = fmin;
3557 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003558
3559 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
3560 fmax, fmin, fstart);
3561
3562 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
3563
3564 /*
3565 * Interrupts will be enabled in ironlake_irq_postinstall
3566 */
3567
3568 I915_WRITE(VIDSTART, vstart);
3569 POSTING_READ(VIDSTART);
3570
3571 rgvmodectl |= MEMMODE_SWMODE_EN;
3572 I915_WRITE(MEMMODECTL, rgvmodectl);
3573
Daniel Vetter92703882012-08-09 16:46:01 +02003574 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003575 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetter92703882012-08-09 16:46:01 +02003576 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003577
3578 ironlake_set_drps(dev, fstart);
3579
Daniel Vetter20e4d402012-08-08 23:35:39 +02003580 dev_priv->ips.last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003581 I915_READ(0x112e0);
Daniel Vetter20e4d402012-08-08 23:35:39 +02003582 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
3583 dev_priv->ips.last_count2 = I915_READ(0x112f4);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00003584 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02003585
3586 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003587}
3588
Daniel Vetter8090c6b2012-06-24 16:42:32 +02003589static void ironlake_disable_drps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003590{
3591 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter92703882012-08-09 16:46:01 +02003592 u16 rgvswctl;
3593
3594 spin_lock_irq(&mchdev_lock);
3595
3596 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003597
3598 /* Ack interrupts, disable EFC interrupt */
3599 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
3600 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
3601 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
3602 I915_WRITE(DEIIR, DE_PCU_EVENT);
3603 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
3604
3605 /* Go back to the starting frequency */
Daniel Vetter20e4d402012-08-08 23:35:39 +02003606 ironlake_set_drps(dev, dev_priv->ips.fstart);
Daniel Vetter92703882012-08-09 16:46:01 +02003607 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003608 rgvswctl |= MEMCTL_CMD_STS;
3609 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetter92703882012-08-09 16:46:01 +02003610 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003611
Daniel Vetter92703882012-08-09 16:46:01 +02003612 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003613}
3614
Daniel Vetteracbe9472012-07-26 11:50:05 +02003615/* There's a funny hw issue where the hw returns all 0 when reading from
3616 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
3617 * ourselves, instead of doing a rmw cycle (which might result in us clearing
3618 * all limits and the gpu stuck at whatever frequency it is at atm).
3619 */
Chris Wilson6917c7b2013-11-06 13:56:26 -02003620static u32 gen6_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003621{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003622 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003623
Daniel Vetter20b46e52012-07-26 11:16:14 +02003624 /* Only set the down limit when we've reached the lowest level to avoid
3625 * getting more interrupts, otherwise leave this clear. This prevents a
3626 * race in the hw when coming out of rc6: There's a tiny window where
3627 * the hw runs at the minimal clock before selecting the desired
3628 * frequency, if the down threshold expires in that window we will not
3629 * receive a down interrupt. */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003630 limits = dev_priv->rps.max_freq_softlimit << 24;
3631 if (val <= dev_priv->rps.min_freq_softlimit)
3632 limits |= dev_priv->rps.min_freq_softlimit << 16;
Daniel Vetter20b46e52012-07-26 11:16:14 +02003633
3634 return limits;
3635}
3636
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003637static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
3638{
3639 int new_power;
3640
3641 new_power = dev_priv->rps.power;
3642 switch (dev_priv->rps.power) {
3643 case LOW_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003644 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003645 new_power = BETWEEN;
3646 break;
3647
3648 case BETWEEN:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003649 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003650 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003651 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003652 new_power = HIGH_POWER;
3653 break;
3654
3655 case HIGH_POWER:
Ben Widawskyb39fb292014-03-19 18:31:11 -07003656 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 +01003657 new_power = BETWEEN;
3658 break;
3659 }
3660 /* Max/min bins are special */
Ben Widawskyb39fb292014-03-19 18:31:11 -07003661 if (val == dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003662 new_power = LOW_POWER;
Ben Widawskyb39fb292014-03-19 18:31:11 -07003663 if (val == dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01003664 new_power = HIGH_POWER;
3665 if (new_power == dev_priv->rps.power)
3666 return;
3667
3668 /* Note the units here are not exactly 1us, but 1280ns. */
3669 switch (new_power) {
3670 case LOW_POWER:
3671 /* Upclock if more than 95% busy over 16ms */
3672 I915_WRITE(GEN6_RP_UP_EI, 12500);
3673 I915_WRITE(GEN6_RP_UP_THRESHOLD, 11800);
3674
3675 /* Downclock if less than 85% busy over 32ms */
3676 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3677 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 21250);
3678
3679 I915_WRITE(GEN6_RP_CONTROL,
3680 GEN6_RP_MEDIA_TURBO |
3681 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3682 GEN6_RP_MEDIA_IS_GFX |
3683 GEN6_RP_ENABLE |
3684 GEN6_RP_UP_BUSY_AVG |
3685 GEN6_RP_DOWN_IDLE_AVG);
3686 break;
3687
3688 case BETWEEN:
3689 /* Upclock if more than 90% busy over 13ms */
3690 I915_WRITE(GEN6_RP_UP_EI, 10250);
3691 I915_WRITE(GEN6_RP_UP_THRESHOLD, 9225);
3692
3693 /* Downclock if less than 75% busy over 32ms */
3694 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3695 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 18750);
3696
3697 I915_WRITE(GEN6_RP_CONTROL,
3698 GEN6_RP_MEDIA_TURBO |
3699 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3700 GEN6_RP_MEDIA_IS_GFX |
3701 GEN6_RP_ENABLE |
3702 GEN6_RP_UP_BUSY_AVG |
3703 GEN6_RP_DOWN_IDLE_AVG);
3704 break;
3705
3706 case HIGH_POWER:
3707 /* Upclock if more than 85% busy over 10ms */
3708 I915_WRITE(GEN6_RP_UP_EI, 8000);
3709 I915_WRITE(GEN6_RP_UP_THRESHOLD, 6800);
3710
3711 /* Downclock if less than 60% busy over 32ms */
3712 I915_WRITE(GEN6_RP_DOWN_EI, 25000);
3713 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 15000);
3714
3715 I915_WRITE(GEN6_RP_CONTROL,
3716 GEN6_RP_MEDIA_TURBO |
3717 GEN6_RP_MEDIA_HW_NORMAL_MODE |
3718 GEN6_RP_MEDIA_IS_GFX |
3719 GEN6_RP_ENABLE |
3720 GEN6_RP_UP_BUSY_AVG |
3721 GEN6_RP_DOWN_IDLE_AVG);
3722 break;
3723 }
3724
3725 dev_priv->rps.power = new_power;
3726 dev_priv->rps.last_adj = 0;
3727}
3728
Chris Wilson2876ce72014-03-28 08:03:34 +00003729static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
3730{
3731 u32 mask = 0;
3732
3733 if (val > dev_priv->rps.min_freq_softlimit)
3734 mask |= GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
3735 if (val < dev_priv->rps.max_freq_softlimit)
3736 mask |= GEN6_PM_RP_UP_THRESHOLD;
3737
Chris Wilson7b3c29f2014-07-10 20:31:19 +01003738 mask |= dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED);
3739 mask &= dev_priv->pm_rps_events;
3740
Imre Deak59d02a12014-12-19 19:33:26 +02003741 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00003742}
3743
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003744/* gen6_set_rps is called to update the frequency request, but should also be
3745 * called when the range (min_delay and max_delay) is modified so that we can
3746 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003747static void gen6_set_rps(struct drm_device *dev, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02003748{
3749 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003750
Jesse Barnes4fc688c2012-11-02 11:14:01 -07003751 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawskyb39fb292014-03-19 18:31:11 -07003752 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3753 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
Daniel Vetter004777c2012-08-09 15:07:01 +02003754
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003755 /* min/max delay may still have been modified so be sure to
3756 * write the limits value.
3757 */
3758 if (val != dev_priv->rps.cur_freq) {
3759 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003760
Ben Widawsky50e6a2a2014-03-31 17:16:43 -07003761 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003762 I915_WRITE(GEN6_RPNSWREQ,
3763 HSW_FREQUENCY(val));
3764 else
3765 I915_WRITE(GEN6_RPNSWREQ,
3766 GEN6_FREQUENCY(val) |
3767 GEN6_OFFSET(0) |
3768 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06003769 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003770
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003771 /* Make sure we continue to get interrupts
3772 * until we hit the minimum or maximum frequencies.
3773 */
Chris Wilsoneb64cad2014-03-27 08:24:20 +00003774 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, gen6_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00003775 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01003776
Ben Widawskyd5570a72012-09-07 19:43:41 -07003777 POSTING_READ(GEN6_RPNSWREQ);
3778
Ben Widawskyb39fb292014-03-19 18:31:11 -07003779 dev_priv->rps.cur_freq = val;
Daniel Vetterbe2cde92012-08-30 13:26:48 +02003780 trace_intel_gpu_freq_change(val * 50);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003781}
3782
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003783static void valleyview_set_rps(struct drm_device *dev, u8 val)
3784{
3785 struct drm_i915_private *dev_priv = dev->dev_private;
3786
3787 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
3788 WARN_ON(val > dev_priv->rps.max_freq_softlimit);
3789 WARN_ON(val < dev_priv->rps.min_freq_softlimit);
3790
3791 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
3792 "Odd GPU freq value\n"))
3793 val &= ~1;
3794
3795 if (val != dev_priv->rps.cur_freq)
3796 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
3797
3798 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
3799
3800 dev_priv->rps.cur_freq = val;
3801 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
3802}
3803
Deepak S76c3552f2014-01-30 23:08:16 +05303804/* vlv_set_rps_idle: Set the frequency to Rpn if Gfx clocks are down
3805 *
3806 * * If Gfx is Idle, then
3807 * 1. Mask Turbo interrupts
3808 * 2. Bring up Gfx clock
3809 * 3. Change the freq to Rpn and wait till P-Unit updates freq
3810 * 4. Clear the Force GFX CLK ON bit so that Gfx can down
3811 * 5. Unmask Turbo interrupts
3812*/
3813static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
3814{
Deepak S5549d252014-06-28 11:26:11 +05303815 struct drm_device *dev = dev_priv->dev;
3816
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003817 /* CHV and latest VLV don't need to force the gfx clock */
3818 if (IS_CHERRYVIEW(dev) || dev->pdev->revision >= 0xd) {
Deepak S5549d252014-06-28 11:26:11 +05303819 valleyview_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
3820 return;
3821 }
3822
Deepak S76c3552f2014-01-30 23:08:16 +05303823 /*
3824 * When we are idle. Drop to min voltage state.
3825 */
3826
Ben Widawskyb39fb292014-03-19 18:31:11 -07003827 if (dev_priv->rps.cur_freq <= dev_priv->rps.min_freq_softlimit)
Deepak S76c3552f2014-01-30 23:08:16 +05303828 return;
3829
3830 /* Mask turbo interrupt so that they will not come in between */
Imre Deakf24eeb12014-12-19 19:33:27 +02003831 I915_WRITE(GEN6_PMINTRMSK,
3832 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Deepak S76c3552f2014-01-30 23:08:16 +05303833
Imre Deak650ad972014-04-18 16:35:02 +03003834 vlv_force_gfx_clock(dev_priv, true);
Deepak S76c3552f2014-01-30 23:08:16 +05303835
Ben Widawskyb39fb292014-03-19 18:31:11 -07003836 dev_priv->rps.cur_freq = dev_priv->rps.min_freq_softlimit;
Deepak S76c3552f2014-01-30 23:08:16 +05303837
3838 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ,
Ben Widawskyb39fb292014-03-19 18:31:11 -07003839 dev_priv->rps.min_freq_softlimit);
Deepak S76c3552f2014-01-30 23:08:16 +05303840
3841 if (wait_for(((vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS))
Imre Deak2837ac42014-11-19 16:25:38 +02003842 & GENFREQSTATUS) == 0, 100))
Deepak S76c3552f2014-01-30 23:08:16 +05303843 DRM_ERROR("timed out waiting for Punit\n");
3844
Imre Deak650ad972014-04-18 16:35:02 +03003845 vlv_force_gfx_clock(dev_priv, false);
Deepak S76c3552f2014-01-30 23:08:16 +05303846
Chris Wilson2876ce72014-03-28 08:03:34 +00003847 I915_WRITE(GEN6_PMINTRMSK,
3848 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Deepak S76c3552f2014-01-30 23:08:16 +05303849}
3850
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003851void gen6_rps_idle(struct drm_i915_private *dev_priv)
3852{
Damien Lespiau691bb712013-12-12 14:36:36 +00003853 struct drm_device *dev = dev_priv->dev;
3854
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003855 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003856 if (dev_priv->rps.enabled) {
Ville Syrjälä21a11ff2015-01-27 16:36:15 +02003857 if (IS_VALLEYVIEW(dev))
Deepak S76c3552f2014-01-30 23:08:16 +05303858 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02003859 else
Ben Widawskyb39fb292014-03-19 18:31:11 -07003860 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003861 dev_priv->rps.last_adj = 0;
3862 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003863 mutex_unlock(&dev_priv->rps.hw_lock);
3864}
3865
3866void gen6_rps_boost(struct drm_i915_private *dev_priv)
3867{
3868 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003869 if (dev_priv->rps.enabled) {
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003870 intel_set_rps(dev_priv->dev, dev_priv->rps.max_freq_softlimit);
Chris Wilsonc0951f02013-10-10 21:58:50 +01003871 dev_priv->rps.last_adj = 0;
3872 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01003873 mutex_unlock(&dev_priv->rps.hw_lock);
3874}
3875
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003876void intel_set_rps(struct drm_device *dev, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07003877{
Ville Syrjäläffe02b42015-02-02 19:09:50 +02003878 if (IS_VALLEYVIEW(dev))
3879 valleyview_set_rps(dev, val);
3880 else
3881 gen6_set_rps(dev, val);
Jesse Barnes0a073b82013-04-17 15:54:58 -07003882}
3883
Zhe Wang20e49362014-11-04 17:07:05 +00003884static void gen9_disable_rps(struct drm_device *dev)
3885{
3886 struct drm_i915_private *dev_priv = dev->dev_private;
3887
3888 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00003889 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00003890}
3891
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003892static void gen6_disable_rps(struct drm_device *dev)
3893{
3894 struct drm_i915_private *dev_priv = dev->dev_private;
3895
3896 I915_WRITE(GEN6_RC_CONTROL, 0);
3897 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02003898}
3899
Deepak S38807742014-05-23 21:00:15 +05303900static void cherryview_disable_rps(struct drm_device *dev)
3901{
3902 struct drm_i915_private *dev_priv = dev->dev_private;
3903
3904 I915_WRITE(GEN6_RC_CONTROL, 0);
3905}
3906
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003907static void valleyview_disable_rps(struct drm_device *dev)
3908{
3909 struct drm_i915_private *dev_priv = dev->dev_private;
3910
Deepak S98a2e5f2014-08-18 10:35:27 -07003911 /* we're doing forcewake before Disabling RC6,
3912 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02003913 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07003914
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003915 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003916
Mika Kuoppala59bad942015-01-16 11:34:40 +02003917 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07003918}
3919
Ben Widawskydc39fff2013-10-18 12:32:07 -07003920static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
3921{
Imre Deak91ca6892014-04-14 20:24:25 +03003922 if (IS_VALLEYVIEW(dev)) {
3923 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
3924 mode = GEN6_RC_CTL_RC6_ENABLE;
3925 else
3926 mode = 0;
3927 }
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003928 if (HAS_RC6p(dev))
3929 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
3930 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off",
3931 (mode & GEN6_RC_CTL_RC6p_ENABLE) ? "on" : "off",
3932 (mode & GEN6_RC_CTL_RC6pp_ENABLE) ? "on" : "off");
3933
3934 else
3935 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
3936 (mode & GEN6_RC_CTL_RC6_ENABLE) ? "on" : "off");
Ben Widawskydc39fff2013-10-18 12:32:07 -07003937}
3938
Imre Deake6069ca2014-04-18 16:01:02 +03003939static int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003940{
Damien Lespiaueb4926e2013-06-07 17:41:14 +01003941 /* No RC6 before Ironlake */
3942 if (INTEL_INFO(dev)->gen < 5)
3943 return 0;
3944
Imre Deake6069ca2014-04-18 16:01:02 +03003945 /* RC6 is only on Ironlake mobile not on desktop */
3946 if (INTEL_INFO(dev)->gen == 5 && !IS_IRONLAKE_M(dev))
3947 return 0;
3948
Daniel Vetter456470e2012-08-08 23:35:40 +02003949 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03003950 if (enable_rc6 >= 0) {
3951 int mask;
3952
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07003953 if (HAS_RC6p(dev))
Imre Deake6069ca2014-04-18 16:01:02 +03003954 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
3955 INTEL_RC6pp_ENABLE;
3956 else
3957 mask = INTEL_RC6_ENABLE;
3958
3959 if ((enable_rc6 & mask) != enable_rc6)
Daniel Vetter8dfd1f02014-08-04 11:15:56 +02003960 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
3961 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03003962
3963 return enable_rc6 & mask;
3964 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003965
Chris Wilson6567d742012-11-10 10:00:06 +00003966 /* Disable RC6 on Ironlake */
3967 if (INTEL_INFO(dev)->gen == 5)
3968 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003969
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003970 if (IS_IVYBRIDGE(dev))
Ben Widawskycca84a12014-01-28 20:25:38 -08003971 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08003972
3973 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03003974}
3975
Imre Deake6069ca2014-04-18 16:01:02 +03003976int intel_enable_rc6(const struct drm_device *dev)
3977{
3978 return i915.enable_rc6;
3979}
3980
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003981static void gen6_init_rps_frequencies(struct drm_device *dev)
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003982{
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003983 struct drm_i915_private *dev_priv = dev->dev_private;
3984 uint32_t rp_state_cap;
3985 u32 ddcc_status = 0;
3986 int ret;
3987
3988 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003989 /* All of these values are in units of 50MHz */
3990 dev_priv->rps.cur_freq = 0;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003991 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003992 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003993 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003994 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07003995 /* hw_max = RP0 until we check for overclocking */
3996 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
3997
Tom O'Rourke93ee2922014-11-19 14:21:52 -08003998 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
3999 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
4000 ret = sandybridge_pcode_read(dev_priv,
4001 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4002 &ddcc_status);
4003 if (0 == ret)
4004 dev_priv->rps.efficient_freq =
4005 (ddcc_status >> 8) & 0xff;
4006 }
4007
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004008 /* Preserve min/max settings in case of re-init */
4009 if (dev_priv->rps.max_freq_softlimit == 0)
4010 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4011
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004012 if (dev_priv->rps.min_freq_softlimit == 0) {
4013 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4014 dev_priv->rps.min_freq_softlimit =
Tom O'Rourkef4ab4082014-11-19 14:21:53 -08004015 /* max(RPe, 450 MHz) */
4016 max(dev_priv->rps.efficient_freq, (u8) 9);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004017 else
4018 dev_priv->rps.min_freq_softlimit =
4019 dev_priv->rps.min_freq;
4020 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07004021}
4022
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004023/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Zhe Wang20e49362014-11-04 17:07:05 +00004024static void gen9_enable_rps(struct drm_device *dev)
4025{
4026 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004027
4028 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4029
Damien Lespiauba1c5542015-01-16 18:07:26 +00004030 gen6_init_rps_frequencies(dev);
4031
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00004032 I915_WRITE(GEN6_RPNSWREQ, 0xc800000);
4033 I915_WRITE(GEN6_RC_VIDEO_FREQ, 0xc800000);
4034
4035 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 0xf4240);
4036 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, 0x12060000);
4037 I915_WRITE(GEN6_RP_UP_THRESHOLD, 0xe808);
4038 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 0x3bd08);
4039 I915_WRITE(GEN6_RP_UP_EI, 0x101d0);
4040 I915_WRITE(GEN6_RP_DOWN_EI, 0x55730);
4041 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4042 I915_WRITE(GEN6_PMINTRMSK, 0x6);
4043 I915_WRITE(GEN6_RP_CONTROL, GEN6_RP_MEDIA_TURBO |
4044 GEN6_RP_MEDIA_HW_MODE | GEN6_RP_MEDIA_IS_GFX |
4045 GEN6_RP_ENABLE | GEN6_RP_UP_BUSY_AVG |
4046 GEN6_RP_DOWN_IDLE_AVG);
4047
4048 gen6_enable_rps_interrupts(dev);
4049
4050 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4051}
4052
4053static void gen9_enable_rc6(struct drm_device *dev)
4054{
4055 struct drm_i915_private *dev_priv = dev->dev_private;
Zhe Wang20e49362014-11-04 17:07:05 +00004056 struct intel_engine_cs *ring;
4057 uint32_t rc6_mask = 0;
4058 int unused;
4059
4060 /* 1a: Software RC state - RC0 */
4061 I915_WRITE(GEN6_RC_STATE, 0);
4062
4063 /* 1b: Get forcewake during program sequence. Although the driver
4064 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004065 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004066
4067 /* 2a: Disable RC states. */
4068 I915_WRITE(GEN6_RC_CONTROL, 0);
4069
4070 /* 2b: Program RC6 thresholds.*/
4071 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4072 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4073 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4074 for_each_ring(ring, dev_priv, unused)
4075 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4076 I915_WRITE(GEN6_RC_SLEEP, 0);
4077 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4078
Zhe Wang38c23522015-01-20 12:23:04 +00004079 /* 2c: Program Coarse Power Gating Policies. */
4080 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4081 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4082
Zhe Wang20e49362014-11-04 17:07:05 +00004083 /* 3a: Enable RC6 */
4084 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4085 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4086 DRM_INFO("RC6 %s\n", (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4087 "on" : "off");
4088 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4089 GEN6_RC_CTL_EI_MODE(1) |
4090 rc6_mask);
4091
Zhe Wang38c23522015-01-20 12:23:04 +00004092 /* 3b: Enable Coarse Power Gating only when RC6 is enabled */
4093 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ? 3 : 0);
4094
Mika Kuoppala59bad942015-01-16 11:34:40 +02004095 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00004096
4097}
4098
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004099static void gen8_enable_rps(struct drm_device *dev)
4100{
4101 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004102 struct intel_engine_cs *ring;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004103 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004104 int unused;
4105
4106 /* 1a: Software RC state - RC0 */
4107 I915_WRITE(GEN6_RC_STATE, 0);
4108
4109 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4110 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004111 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004112
4113 /* 2a: Disable RC states. */
4114 I915_WRITE(GEN6_RC_CONTROL, 0);
4115
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004116 /* Initialize rps frequencies */
4117 gen6_init_rps_frequencies(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004118
4119 /* 2b: Program RC6 thresholds.*/
4120 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4121 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4122 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4123 for_each_ring(ring, dev_priv, unused)
4124 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4125 I915_WRITE(GEN6_RC_SLEEP, 0);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004126 if (IS_BROADWELL(dev))
4127 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4128 else
4129 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004130
4131 /* 3: Enable RC6 */
4132 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4133 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Ben Widawskyabbf9d22014-01-28 20:25:41 -08004134 intel_print_rc6_info(dev, rc6_mask);
Tom O'Rourke0d68b252014-04-09 11:44:06 -07004135 if (IS_BROADWELL(dev))
4136 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4137 GEN7_RC_CTL_TO_MODE |
4138 rc6_mask);
4139 else
4140 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4141 GEN6_RC_CTL_EI_MODE(1) |
4142 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004143
4144 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07004145 I915_WRITE(GEN6_RPNSWREQ,
4146 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4147 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4148 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02004149 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4150 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004151
Daniel Vetter7526ed72014-09-29 15:07:19 +02004152 /* Docs recommend 900MHz, and 300 MHz respectively */
4153 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4154 dev_priv->rps.max_freq_softlimit << 24 |
4155 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004156
Daniel Vetter7526ed72014-09-29 15:07:19 +02004157 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4158 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4159 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4160 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004161
Daniel Vetter7526ed72014-09-29 15:07:19 +02004162 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004163
4164 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02004165 I915_WRITE(GEN6_RP_CONTROL,
4166 GEN6_RP_MEDIA_TURBO |
4167 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4168 GEN6_RP_MEDIA_IS_GFX |
4169 GEN6_RP_ENABLE |
4170 GEN6_RP_UP_BUSY_AVG |
4171 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004172
Daniel Vetter7526ed72014-09-29 15:07:19 +02004173 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004174
Tom O'Rourkec7f31532014-11-19 14:21:54 -08004175 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4176 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Daniel Vetter7526ed72014-09-29 15:07:19 +02004177
Mika Kuoppala59bad942015-01-16 11:34:40 +02004178 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07004179}
4180
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004181static void gen6_enable_rps(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004182{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004183 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004184 struct intel_engine_cs *ring;
Ben Widawskyd060c162014-03-19 18:31:08 -07004185 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004186 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004187 int rc6_mode;
Ben Widawsky42c05262012-09-26 10:34:00 -07004188 int i, ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004189
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004190 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004191
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004192 /* Here begins a magic sequence of register writes to enable
4193 * auto-downclocking.
4194 *
4195 * Perhaps there might be some value in exposing these to
4196 * userspace...
4197 */
4198 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004199
4200 /* Clear the DBG now so we don't confuse earlier errors */
4201 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4202 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4203 I915_WRITE(GTFIFODBG, gtfifodbg);
4204 }
4205
Mika Kuoppala59bad942015-01-16 11:34:40 +02004206 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004207
Tom O'Rourke93ee2922014-11-19 14:21:52 -08004208 /* Initialize rps frequencies */
4209 gen6_init_rps_frequencies(dev);
Jeff McGeedd0a1aa2014-02-04 11:32:31 -06004210
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004211 /* disable the counters and set deterministic thresholds */
4212 I915_WRITE(GEN6_RC_CONTROL, 0);
4213
4214 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4215 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4216 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4217 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4218 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4219
Chris Wilsonb4519512012-05-11 14:29:30 +01004220 for_each_ring(ring, dev_priv, i)
4221 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004222
4223 I915_WRITE(GEN6_RC_SLEEP, 0);
4224 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Daniel Vetter29c78f62013-11-16 16:04:26 +01004225 if (IS_IVYBRIDGE(dev))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07004226 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4227 else
4228 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08004229 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004230 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4231
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004232 /* Check if we are enabling RC6 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004233 rc6_mode = intel_enable_rc6(dev_priv->dev);
4234 if (rc6_mode & INTEL_RC6_ENABLE)
4235 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4236
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004237 /* We don't use those on Haswell */
4238 if (!IS_HASWELL(dev)) {
4239 if (rc6_mode & INTEL_RC6p_ENABLE)
4240 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004241
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03004242 if (rc6_mode & INTEL_RC6pp_ENABLE)
4243 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4244 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004245
Ben Widawskydc39fff2013-10-18 12:32:07 -07004246 intel_print_rc6_info(dev, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004247
4248 I915_WRITE(GEN6_RC_CONTROL,
4249 rc6_mask |
4250 GEN6_RC_CTL_EI_MODE(1) |
4251 GEN6_RC_CTL_HW_ENABLE);
4252
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004253 /* Power down if completely idle for over 50ms */
4254 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004255 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004256
Ben Widawsky42c05262012-09-26 10:34:00 -07004257 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
Ben Widawskyd060c162014-03-19 18:31:08 -07004258 if (ret)
Ben Widawsky42c05262012-09-26 10:34:00 -07004259 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
Ben Widawskyd060c162014-03-19 18:31:08 -07004260
4261 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
4262 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
4263 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
Ben Widawskyb39fb292014-03-19 18:31:11 -07004264 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
Ben Widawskyd060c162014-03-19 18:31:08 -07004265 (pcu_mbox & 0xff) * 50);
Ben Widawskyb39fb292014-03-19 18:31:11 -07004266 dev_priv->rps.max_freq = pcu_mbox & 0xff;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004267 }
4268
Chris Wilsondd75fdc2013-09-25 17:34:57 +01004269 dev_priv->rps.power = HIGH_POWER; /* force a reset */
Ben Widawskyb39fb292014-03-19 18:31:11 -07004270 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004271
Ben Widawsky31643d52012-09-26 10:34:01 -07004272 rc6vids = 0;
4273 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
4274 if (IS_GEN6(dev) && ret) {
4275 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
4276 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
4277 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
4278 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
4279 rc6vids &= 0xffff00;
4280 rc6vids |= GEN6_ENCODE_RC6_VID(450);
4281 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
4282 if (ret)
4283 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
4284 }
4285
Mika Kuoppala59bad942015-01-16 11:34:40 +02004286 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004287}
4288
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004289static void __gen6_update_ring_freq(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004290{
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004291 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004292 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004293 unsigned int gpu_freq;
4294 unsigned int max_ia_freq, min_ring_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004295 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03004296 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004297
Jesse Barnes4fc688c2012-11-02 11:14:01 -07004298 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004299
Ben Widawskyeda79642013-10-07 17:15:48 -03004300 policy = cpufreq_cpu_get(0);
4301 if (policy) {
4302 max_ia_freq = policy->cpuinfo.max_freq;
4303 cpufreq_cpu_put(policy);
4304 } else {
4305 /*
4306 * Default to measured freq if none found, PCU will ensure we
4307 * don't go over
4308 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004309 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03004310 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004311
4312 /* Convert from kHz to MHz */
4313 max_ia_freq /= 1000;
4314
Ben Widawsky153b4b952013-10-22 22:05:09 -07004315 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07004316 /* convert DDR frequency from units of 266.6MHz to bandwidth */
4317 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004318
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004319 /*
4320 * For each potential GPU frequency, load a ring frequency we'd like
4321 * to use for memory access. We do this by specifying the IA frequency
4322 * the PCU should use as a reference to determine the ring frequency.
4323 */
Tom O'Rourke6985b352014-11-19 14:21:55 -08004324 for (gpu_freq = dev_priv->rps.max_freq; gpu_freq >= dev_priv->rps.min_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004325 gpu_freq--) {
Tom O'Rourke6985b352014-11-19 14:21:55 -08004326 int diff = dev_priv->rps.max_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01004327 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004328
Ben Widawsky46c764d2013-11-02 21:07:49 -07004329 if (INTEL_INFO(dev)->gen >= 8) {
4330 /* max(2 * GT, DDR). NB: GT is 50MHz units */
4331 ring_freq = max(min_ring_freq, gpu_freq);
4332 } else if (IS_HASWELL(dev)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07004333 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01004334 ring_freq = max(min_ring_freq, ring_freq);
4335 /* leave ia_freq as the default, chosen by cpufreq */
4336 } else {
4337 /* On older processors, there is no separate ring
4338 * clock domain, so in order to boost the bandwidth
4339 * of the ring, we need to upclock the CPU (ia_freq).
4340 *
4341 * For GPU frequencies less than 750MHz,
4342 * just use the lowest ring freq.
4343 */
4344 if (gpu_freq < min_freq)
4345 ia_freq = 800;
4346 else
4347 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
4348 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
4349 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004350
Ben Widawsky42c05262012-09-26 10:34:00 -07004351 sandybridge_pcode_write(dev_priv,
4352 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01004353 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
4354 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
4355 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004356 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004357}
4358
Imre Deakc2bc2fc2014-04-18 16:16:23 +03004359void gen6_update_ring_freq(struct drm_device *dev)
4360{
4361 struct drm_i915_private *dev_priv = dev->dev_private;
4362
4363 if (INTEL_INFO(dev)->gen < 6 || IS_VALLEYVIEW(dev))
4364 return;
4365
4366 mutex_lock(&dev_priv->rps.hw_lock);
4367 __gen6_update_ring_freq(dev);
4368 mutex_unlock(&dev_priv->rps.hw_lock);
4369}
4370
Ville Syrjälä03af2042014-06-28 02:03:53 +03004371static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304372{
Deepak S095acd52015-01-17 11:05:59 +05304373 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304374 u32 val, rp0;
4375
Deepak S095acd52015-01-17 11:05:59 +05304376 if (dev->pdev->revision >= 0x20) {
4377 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05304378
Deepak S095acd52015-01-17 11:05:59 +05304379 switch (INTEL_INFO(dev)->eu_total) {
4380 case 8:
4381 /* (2 * 4) config */
4382 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
4383 break;
4384 case 12:
4385 /* (2 * 6) config */
4386 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
4387 break;
4388 case 16:
4389 /* (2 * 8) config */
4390 default:
4391 /* Setting (2 * 8) Min RP0 for any other combination */
4392 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
4393 break;
4394 }
4395 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
4396 } else {
4397 /* For pre-production hardware */
4398 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4399 rp0 = (val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4400 PUNIT_GPU_STATUS_MAX_FREQ_MASK;
4401 }
Deepak S2b6b3a02014-05-27 15:59:30 +05304402 return rp0;
4403}
4404
4405static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4406{
4407 u32 val, rpe;
4408
4409 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
4410 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
4411
4412 return rpe;
4413}
4414
Deepak S7707df42014-07-12 18:46:14 +05304415static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
4416{
Deepak S095acd52015-01-17 11:05:59 +05304417 struct drm_device *dev = dev_priv->dev;
Deepak S7707df42014-07-12 18:46:14 +05304418 u32 val, rp1;
4419
Deepak S095acd52015-01-17 11:05:59 +05304420 if (dev->pdev->revision >= 0x20) {
4421 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
4422 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
4423 } else {
4424 /* For pre-production hardware */
4425 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4426 rp1 = ((val >> PUNIT_GPU_STATUS_MAX_FREQ_SHIFT) &
4427 PUNIT_GPU_STATUS_MAX_FREQ_MASK);
4428 }
Deepak S7707df42014-07-12 18:46:14 +05304429 return rp1;
4430}
4431
Ville Syrjälä03af2042014-06-28 02:03:53 +03004432static int cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05304433{
Deepak S095acd52015-01-17 11:05:59 +05304434 struct drm_device *dev = dev_priv->dev;
Deepak S2b6b3a02014-05-27 15:59:30 +05304435 u32 val, rpn;
4436
Deepak S095acd52015-01-17 11:05:59 +05304437 if (dev->pdev->revision >= 0x20) {
4438 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
4439 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
4440 FB_GFX_FREQ_FUSE_MASK);
4441 } else { /* For pre-production hardware */
4442 val = vlv_punit_read(dev_priv, PUNIT_GPU_STATUS_REG);
4443 rpn = ((val >> PUNIT_GPU_STATIS_GFX_MIN_FREQ_SHIFT) &
4444 PUNIT_GPU_STATUS_GFX_MIN_FREQ_MASK);
4445 }
4446
Deepak S2b6b3a02014-05-27 15:59:30 +05304447 return rpn;
4448}
4449
Deepak Sf8f2b002014-07-10 13:16:21 +05304450static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
4451{
4452 u32 val, rp1;
4453
4454 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
4455
4456 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
4457
4458 return rp1;
4459}
4460
Ville Syrjälä03af2042014-06-28 02:03:53 +03004461static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004462{
4463 u32 val, rp0;
4464
Jani Nikula64936252013-05-22 15:36:20 +03004465 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004466
4467 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
4468 /* Clamp to max */
4469 rp0 = min_t(u32, rp0, 0xea);
4470
4471 return rp0;
4472}
4473
4474static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
4475{
4476 u32 val, rpe;
4477
Jani Nikula64936252013-05-22 15:36:20 +03004478 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004479 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03004480 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004481 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
4482
4483 return rpe;
4484}
4485
Ville Syrjälä03af2042014-06-28 02:03:53 +03004486static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07004487{
Jani Nikula64936252013-05-22 15:36:20 +03004488 return vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004489}
4490
Imre Deakae484342014-03-31 15:10:44 +03004491/* Check that the pctx buffer wasn't move under us. */
4492static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
4493{
4494 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4495
4496 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
4497 dev_priv->vlv_pctx->stolen->start);
4498}
4499
Deepak S38807742014-05-23 21:00:15 +05304500
4501/* Check that the pcbr address is not empty. */
4502static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
4503{
4504 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
4505
4506 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
4507}
4508
4509static void cherryview_setup_pctx(struct drm_device *dev)
4510{
4511 struct drm_i915_private *dev_priv = dev->dev_private;
4512 unsigned long pctx_paddr, paddr;
4513 struct i915_gtt *gtt = &dev_priv->gtt;
4514 u32 pcbr;
4515 int pctx_size = 32*1024;
4516
4517 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4518
4519 pcbr = I915_READ(VLV_PCBR);
4520 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004521 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05304522 paddr = (dev_priv->mm.stolen_base +
4523 (gtt->stolen_size - pctx_size));
4524
4525 pctx_paddr = (paddr & (~4095));
4526 I915_WRITE(VLV_PCBR, pctx_paddr);
4527 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004528
4529 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05304530}
4531
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004532static void valleyview_setup_pctx(struct drm_device *dev)
4533{
4534 struct drm_i915_private *dev_priv = dev->dev_private;
4535 struct drm_i915_gem_object *pctx;
4536 unsigned long pctx_paddr;
4537 u32 pcbr;
4538 int pctx_size = 24*1024;
4539
Imre Deak17b0c1f2014-02-11 21:39:06 +02004540 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4541
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004542 pcbr = I915_READ(VLV_PCBR);
4543 if (pcbr) {
4544 /* BIOS set it up already, grab the pre-alloc'd space */
4545 int pcbr_offset;
4546
4547 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
4548 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
4549 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02004550 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004551 pctx_size);
4552 goto out;
4553 }
4554
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004555 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
4556
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004557 /*
4558 * From the Gunit register HAS:
4559 * The Gfx driver is expected to program this register and ensure
4560 * proper allocation within Gfx stolen memory. For example, this
4561 * register should be programmed such than the PCBR range does not
4562 * overlap with other ranges, such as the frame buffer, protected
4563 * memory, or any other relevant ranges.
4564 */
4565 pctx = i915_gem_object_create_stolen(dev, pctx_size);
4566 if (!pctx) {
4567 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
4568 return;
4569 }
4570
4571 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
4572 I915_WRITE(VLV_PCBR, pctx_paddr);
4573
4574out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02004575 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07004576 dev_priv->vlv_pctx = pctx;
4577}
4578
Imre Deakae484342014-03-31 15:10:44 +03004579static void valleyview_cleanup_pctx(struct drm_device *dev)
4580{
4581 struct drm_i915_private *dev_priv = dev->dev_private;
4582
4583 if (WARN_ON(!dev_priv->vlv_pctx))
4584 return;
4585
4586 drm_gem_object_unreference(&dev_priv->vlv_pctx->base);
4587 dev_priv->vlv_pctx = NULL;
4588}
4589
Imre Deak4e805192014-04-14 20:24:41 +03004590static void valleyview_init_gt_powersave(struct drm_device *dev)
4591{
4592 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004593 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03004594
4595 valleyview_setup_pctx(dev);
4596
4597 mutex_lock(&dev_priv->rps.hw_lock);
4598
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004599 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4600 switch ((val >> 6) & 3) {
4601 case 0:
4602 case 1:
4603 dev_priv->mem_freq = 800;
4604 break;
4605 case 2:
4606 dev_priv->mem_freq = 1066;
4607 break;
4608 case 3:
4609 dev_priv->mem_freq = 1333;
4610 break;
4611 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004612 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004613
Imre Deak4e805192014-04-14 20:24:41 +03004614 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
4615 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4616 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004617 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004618 dev_priv->rps.max_freq);
4619
4620 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
4621 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004622 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004623 dev_priv->rps.efficient_freq);
4624
Deepak Sf8f2b002014-07-10 13:16:21 +05304625 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
4626 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004627 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05304628 dev_priv->rps.rp1_freq);
4629
Imre Deak4e805192014-04-14 20:24:41 +03004630 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
4631 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004632 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03004633 dev_priv->rps.min_freq);
4634
4635 /* Preserve min/max settings in case of re-init */
4636 if (dev_priv->rps.max_freq_softlimit == 0)
4637 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4638
4639 if (dev_priv->rps.min_freq_softlimit == 0)
4640 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4641
4642 mutex_unlock(&dev_priv->rps.hw_lock);
4643}
4644
Deepak S38807742014-05-23 21:00:15 +05304645static void cherryview_init_gt_powersave(struct drm_device *dev)
4646{
Deepak S2b6b3a02014-05-27 15:59:30 +05304647 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004648 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05304649
Deepak S38807742014-05-23 21:00:15 +05304650 cherryview_setup_pctx(dev);
Deepak S2b6b3a02014-05-27 15:59:30 +05304651
4652 mutex_lock(&dev_priv->rps.hw_lock);
4653
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02004654 mutex_lock(&dev_priv->dpio_lock);
4655 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
4656 mutex_unlock(&dev_priv->dpio_lock);
4657
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004658 switch ((val >> 2) & 0x7) {
4659 case 0:
4660 case 1:
4661 dev_priv->rps.cz_freq = 200;
4662 dev_priv->mem_freq = 1600;
4663 break;
4664 case 2:
4665 dev_priv->rps.cz_freq = 267;
4666 dev_priv->mem_freq = 1600;
4667 break;
4668 case 3:
4669 dev_priv->rps.cz_freq = 333;
4670 dev_priv->mem_freq = 2000;
4671 break;
4672 case 4:
4673 dev_priv->rps.cz_freq = 320;
4674 dev_priv->mem_freq = 1600;
4675 break;
4676 case 5:
4677 dev_priv->rps.cz_freq = 400;
4678 dev_priv->mem_freq = 1600;
4679 break;
4680 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02004681 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03004682
Deepak S2b6b3a02014-05-27 15:59:30 +05304683 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
4684 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
4685 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004686 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304687 dev_priv->rps.max_freq);
4688
4689 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
4690 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004691 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304692 dev_priv->rps.efficient_freq);
4693
Deepak S7707df42014-07-12 18:46:14 +05304694 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
4695 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004696 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05304697 dev_priv->rps.rp1_freq);
4698
Deepak S2b6b3a02014-05-27 15:59:30 +05304699 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
4700 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004701 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304702 dev_priv->rps.min_freq);
4703
Ville Syrjälä1c147622014-08-18 14:42:43 +03004704 WARN_ONCE((dev_priv->rps.max_freq |
4705 dev_priv->rps.efficient_freq |
4706 dev_priv->rps.rp1_freq |
4707 dev_priv->rps.min_freq) & 1,
4708 "Odd GPU freq values\n");
4709
Deepak S2b6b3a02014-05-27 15:59:30 +05304710 /* Preserve min/max settings in case of re-init */
4711 if (dev_priv->rps.max_freq_softlimit == 0)
4712 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4713
4714 if (dev_priv->rps.min_freq_softlimit == 0)
4715 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
4716
4717 mutex_unlock(&dev_priv->rps.hw_lock);
Deepak S38807742014-05-23 21:00:15 +05304718}
4719
Imre Deak4e805192014-04-14 20:24:41 +03004720static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
4721{
4722 valleyview_cleanup_pctx(dev);
4723}
4724
Deepak S38807742014-05-23 21:00:15 +05304725static void cherryview_enable_rps(struct drm_device *dev)
4726{
4727 struct drm_i915_private *dev_priv = dev->dev_private;
4728 struct intel_engine_cs *ring;
Deepak S2b6b3a02014-05-27 15:59:30 +05304729 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05304730 int i;
4731
4732 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4733
4734 gtfifodbg = I915_READ(GTFIFODBG);
4735 if (gtfifodbg) {
4736 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4737 gtfifodbg);
4738 I915_WRITE(GTFIFODBG, gtfifodbg);
4739 }
4740
4741 cherryview_check_pctx(dev_priv);
4742
4743 /* 1a & 1b: Get forcewake during program sequence. Although the driver
4744 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02004745 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304746
Ville Syrjälä160614a2015-01-19 13:50:47 +02004747 /* Disable RC states. */
4748 I915_WRITE(GEN6_RC_CONTROL, 0);
4749
Deepak S38807742014-05-23 21:00:15 +05304750 /* 2a: Program RC6 thresholds.*/
4751 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4752 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4753 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4754
4755 for_each_ring(ring, dev_priv, i)
4756 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4757 I915_WRITE(GEN6_RC_SLEEP, 0);
4758
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004759 /* TO threshold set to 1750 us ( 0x557 * 1.28 us) */
4760 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Deepak S38807742014-05-23 21:00:15 +05304761
4762 /* allows RC6 residency counter to work */
4763 I915_WRITE(VLV_COUNTER_CONTROL,
4764 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
4765 VLV_MEDIA_RC6_COUNT_EN |
4766 VLV_RENDER_RC6_COUNT_EN));
4767
4768 /* For now we assume BIOS is allocating and populating the PCBR */
4769 pcbr = I915_READ(VLV_PCBR);
4770
Deepak S38807742014-05-23 21:00:15 +05304771 /* 3: Enable RC6 */
4772 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
4773 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02004774 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05304775
4776 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
4777
Deepak S2b6b3a02014-05-27 15:59:30 +05304778 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02004779 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05304780 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4781 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4782 I915_WRITE(GEN6_RP_UP_EI, 66000);
4783 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4784
4785 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4786
4787 /* 5: Enable RPS */
4788 I915_WRITE(GEN6_RP_CONTROL,
4789 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02004790 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05304791 GEN6_RP_ENABLE |
4792 GEN6_RP_UP_BUSY_AVG |
4793 GEN6_RP_DOWN_IDLE_AVG);
4794
4795 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
4796
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004797 /* RPS code assumes GPLL is used */
4798 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4799
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004800 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Deepak S2b6b3a02014-05-27 15:59:30 +05304801 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4802
4803 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
4804 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004805 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304806 dev_priv->rps.cur_freq);
4807
4808 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004809 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05304810 dev_priv->rps.efficient_freq);
4811
4812 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
4813
Mika Kuoppala59bad942015-01-16 11:34:40 +02004814 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05304815}
4816
Jesse Barnes0a073b82013-04-17 15:54:58 -07004817static void valleyview_enable_rps(struct drm_device *dev)
4818{
4819 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004820 struct intel_engine_cs *ring;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07004821 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07004822 int i;
4823
4824 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4825
Imre Deakae484342014-03-31 15:10:44 +03004826 valleyview_check_pctx(dev_priv);
4827
Jesse Barnes0a073b82013-04-17 15:54:58 -07004828 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07004829 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
4830 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004831 I915_WRITE(GTFIFODBG, gtfifodbg);
4832 }
4833
Deepak Sc8d9a592013-11-23 14:55:42 +05304834 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02004835 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004836
Ville Syrjälä160614a2015-01-19 13:50:47 +02004837 /* Disable RC states. */
4838 I915_WRITE(GEN6_RC_CONTROL, 0);
4839
Ville Syrjäläcad725f2015-01-19 13:50:48 +02004840 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004841 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
4842 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
4843 I915_WRITE(GEN6_RP_UP_EI, 66000);
4844 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
4845
4846 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4847
4848 I915_WRITE(GEN6_RP_CONTROL,
4849 GEN6_RP_MEDIA_TURBO |
4850 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4851 GEN6_RP_MEDIA_IS_GFX |
4852 GEN6_RP_ENABLE |
4853 GEN6_RP_UP_BUSY_AVG |
4854 GEN6_RP_DOWN_IDLE_CONT);
4855
4856 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
4857 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4858 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4859
4860 for_each_ring(ring, dev_priv, i)
4861 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4862
Jesse Barnes2f0aa302013-11-15 09:32:11 -08004863 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004864
4865 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07004866 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04004867 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
4868 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07004869 VLV_MEDIA_RC6_COUNT_EN |
4870 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04004871
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004872 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08004873 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07004874
4875 intel_print_rc6_info(dev, rc6_mode);
4876
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07004877 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004878
Jani Nikula64936252013-05-22 15:36:20 +03004879 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004880
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02004881 /* RPS code assumes GPLL is used */
4882 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
4883
Ville Syrjäläc8e96272014-11-07 21:33:44 +02004884 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", val & GPLLENABLE ? "yes" : "no");
Jesse Barnes0a073b82013-04-17 15:54:58 -07004885 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
4886
Ben Widawskyb39fb292014-03-19 18:31:11 -07004887 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
Ville Syrjälä73008b92013-06-25 19:21:01 +03004888 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004889 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004890 dev_priv->rps.cur_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004891
Ville Syrjälä73008b92013-06-25 19:21:01 +03004892 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02004893 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Ben Widawskyb39fb292014-03-19 18:31:11 -07004894 dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004895
Ben Widawskyb39fb292014-03-19 18:31:11 -07004896 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004897
Mika Kuoppala59bad942015-01-16 11:34:40 +02004898 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07004899}
4900
Daniel Vetter930ebb42012-06-29 23:32:16 +02004901void ironlake_teardown_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004902{
4903 struct drm_i915_private *dev_priv = dev->dev_private;
4904
Daniel Vetter3e373942012-11-02 19:55:04 +01004905 if (dev_priv->ips.renderctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004906 i915_gem_object_ggtt_unpin(dev_priv->ips.renderctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004907 drm_gem_object_unreference(&dev_priv->ips.renderctx->base);
4908 dev_priv->ips.renderctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004909 }
4910
Daniel Vetter3e373942012-11-02 19:55:04 +01004911 if (dev_priv->ips.pwrctx) {
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08004912 i915_gem_object_ggtt_unpin(dev_priv->ips.pwrctx);
Daniel Vetter3e373942012-11-02 19:55:04 +01004913 drm_gem_object_unreference(&dev_priv->ips.pwrctx->base);
4914 dev_priv->ips.pwrctx = NULL;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004915 }
4916}
4917
Daniel Vetter930ebb42012-06-29 23:32:16 +02004918static void ironlake_disable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004919{
4920 struct drm_i915_private *dev_priv = dev->dev_private;
4921
4922 if (I915_READ(PWRCTXA)) {
4923 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
4924 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
4925 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
4926 50);
4927
4928 I915_WRITE(PWRCTXA, 0);
4929 POSTING_READ(PWRCTXA);
4930
4931 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
4932 POSTING_READ(RSTDBYCTL);
4933 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004934}
4935
4936static int ironlake_setup_rc6(struct drm_device *dev)
4937{
4938 struct drm_i915_private *dev_priv = dev->dev_private;
4939
Daniel Vetter3e373942012-11-02 19:55:04 +01004940 if (dev_priv->ips.renderctx == NULL)
4941 dev_priv->ips.renderctx = intel_alloc_context_page(dev);
4942 if (!dev_priv->ips.renderctx)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004943 return -ENOMEM;
4944
Daniel Vetter3e373942012-11-02 19:55:04 +01004945 if (dev_priv->ips.pwrctx == NULL)
4946 dev_priv->ips.pwrctx = intel_alloc_context_page(dev);
4947 if (!dev_priv->ips.pwrctx) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004948 ironlake_teardown_rc6(dev);
4949 return -ENOMEM;
4950 }
4951
4952 return 0;
4953}
4954
Daniel Vetter930ebb42012-06-29 23:32:16 +02004955static void ironlake_enable_rc6(struct drm_device *dev)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004956{
4957 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01004958 struct intel_engine_cs *ring = &dev_priv->ring[RCS];
Chris Wilson3e960502012-11-27 16:22:54 +00004959 bool was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004960 int ret;
4961
4962 /* rc6 disabled by default due to repeated reports of hanging during
4963 * boot and resume.
4964 */
4965 if (!intel_enable_rc6(dev))
4966 return;
4967
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004968 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
4969
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004970 ret = ironlake_setup_rc6(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02004971 if (ret)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004972 return;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004973
Chris Wilson3e960502012-11-27 16:22:54 +00004974 was_interruptible = dev_priv->mm.interruptible;
4975 dev_priv->mm.interruptible = false;
4976
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004977 /*
4978 * GPU can automatically power down the render unit if given a page
4979 * to save state.
4980 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02004981 ret = intel_ring_begin(ring, 6);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004982 if (ret) {
4983 ironlake_teardown_rc6(dev);
Chris Wilson3e960502012-11-27 16:22:54 +00004984 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004985 return;
4986 }
4987
Daniel Vetter6d90c952012-04-26 23:28:05 +02004988 intel_ring_emit(ring, MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
4989 intel_ring_emit(ring, MI_SET_CONTEXT);
Ben Widawskyf343c5f2013-07-05 14:41:04 -07004990 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(dev_priv->ips.renderctx) |
Daniel Vetter6d90c952012-04-26 23:28:05 +02004991 MI_MM_SPACE_GTT |
4992 MI_SAVE_EXT_STATE_EN |
4993 MI_RESTORE_EXT_STATE_EN |
4994 MI_RESTORE_INHIBIT);
4995 intel_ring_emit(ring, MI_SUSPEND_FLUSH);
4996 intel_ring_emit(ring, MI_NOOP);
4997 intel_ring_emit(ring, MI_FLUSH);
4998 intel_ring_advance(ring);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004999
5000 /*
5001 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
5002 * does an implicit flush, combined with MI_FLUSH above, it should be
5003 * safe to assume that renderctx is valid
5004 */
Chris Wilson3e960502012-11-27 16:22:54 +00005005 ret = intel_ring_idle(ring);
5006 dev_priv->mm.interruptible = was_interruptible;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005007 if (ret) {
Jani Nikuladef27a52013-03-12 10:49:19 +02005008 DRM_ERROR("failed to enable ironlake power savings\n");
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005009 ironlake_teardown_rc6(dev);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005010 return;
5011 }
5012
Ben Widawskyf343c5f2013-07-05 14:41:04 -07005013 I915_WRITE(PWRCTXA, i915_gem_obj_ggtt_offset(dev_priv->ips.pwrctx) | PWRCTX_EN);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005014 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawskydc39fff2013-10-18 12:32:07 -07005015
Imre Deak91ca6892014-04-14 20:24:25 +03005016 intel_print_rc6_info(dev, GEN6_RC_CTL_RC6_ENABLE);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005017}
5018
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005019static unsigned long intel_pxfreq(u32 vidfreq)
5020{
5021 unsigned long freq;
5022 int div = (vidfreq & 0x3f0000) >> 16;
5023 int post = (vidfreq & 0x3000) >> 12;
5024 int pre = (vidfreq & 0x7);
5025
5026 if (!pre)
5027 return 0;
5028
5029 freq = ((div * 133333) / ((1<<post) * pre));
5030
5031 return freq;
5032}
5033
Daniel Vettereb48eb02012-04-26 23:28:12 +02005034static const struct cparams {
5035 u16 i;
5036 u16 t;
5037 u16 m;
5038 u16 c;
5039} cparams[] = {
5040 { 1, 1333, 301, 28664 },
5041 { 1, 1066, 294, 24460 },
5042 { 1, 800, 294, 25192 },
5043 { 0, 1333, 276, 27605 },
5044 { 0, 1066, 276, 27605 },
5045 { 0, 800, 231, 23784 },
5046};
5047
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005048static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005049{
5050 u64 total_count, diff, ret;
5051 u32 count1, count2, count3, m = 0, c = 0;
5052 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5053 int i;
5054
Daniel Vetter02d71952012-08-09 16:44:54 +02005055 assert_spin_locked(&mchdev_lock);
5056
Daniel Vetter20e4d402012-08-08 23:35:39 +02005057 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005058
5059 /* Prevent division-by-zero if we are asking too fast.
5060 * Also, we don't get interesting results if we are polling
5061 * faster than once in 10ms, so just return the saved value
5062 * in such cases.
5063 */
5064 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02005065 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005066
5067 count1 = I915_READ(DMIEC);
5068 count2 = I915_READ(DDREC);
5069 count3 = I915_READ(CSIEC);
5070
5071 total_count = count1 + count2 + count3;
5072
5073 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02005074 if (total_count < dev_priv->ips.last_count1) {
5075 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005076 diff += total_count;
5077 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005078 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005079 }
5080
5081 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005082 if (cparams[i].i == dev_priv->ips.c_m &&
5083 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02005084 m = cparams[i].m;
5085 c = cparams[i].c;
5086 break;
5087 }
5088 }
5089
5090 diff = div_u64(diff, diff1);
5091 ret = ((m * diff) + c);
5092 ret = div_u64(ret, 10);
5093
Daniel Vetter20e4d402012-08-08 23:35:39 +02005094 dev_priv->ips.last_count1 = total_count;
5095 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005096
Daniel Vetter20e4d402012-08-08 23:35:39 +02005097 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005098
5099 return ret;
5100}
5101
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005102unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5103{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005104 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005105 unsigned long val;
5106
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005107 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005108 return 0;
5109
5110 spin_lock_irq(&mchdev_lock);
5111
5112 val = __i915_chipset_val(dev_priv);
5113
5114 spin_unlock_irq(&mchdev_lock);
5115
5116 return val;
5117}
5118
Daniel Vettereb48eb02012-04-26 23:28:12 +02005119unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5120{
5121 unsigned long m, x, b;
5122 u32 tsfs;
5123
5124 tsfs = I915_READ(TSFS);
5125
5126 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5127 x = I915_READ8(TR1);
5128
5129 b = tsfs & TSFS_INTR_MASK;
5130
5131 return ((m * x) / 127) - b;
5132}
5133
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005134static int _pxvid_to_vd(u8 pxvid)
5135{
5136 if (pxvid == 0)
5137 return 0;
5138
5139 if (pxvid >= 8 && pxvid < 31)
5140 pxvid = 31;
5141
5142 return (pxvid + 2) * 125;
5143}
5144
5145static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005146{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005147 struct drm_device *dev = dev_priv->dev;
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005148 const int vd = _pxvid_to_vd(pxvid);
5149 const int vm = vd - 1125;
5150
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005151 if (INTEL_INFO(dev)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02005152 return vm > 0 ? vm : 0;
5153
5154 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005155}
5156
Daniel Vetter02d71952012-08-09 16:44:54 +02005157static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005158{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005159 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005160 u32 count;
5161
Daniel Vetter02d71952012-08-09 16:44:54 +02005162 assert_spin_locked(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005163
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005164 now = ktime_get_raw_ns();
5165 diffms = now - dev_priv->ips.last_time2;
5166 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005167
5168 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02005169 if (!diffms)
5170 return;
5171
5172 count = I915_READ(GFXEC);
5173
Daniel Vetter20e4d402012-08-08 23:35:39 +02005174 if (count < dev_priv->ips.last_count2) {
5175 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005176 diff += count;
5177 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02005178 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005179 }
5180
Daniel Vetter20e4d402012-08-08 23:35:39 +02005181 dev_priv->ips.last_count2 = count;
5182 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005183
5184 /* More magic constants... */
5185 diff = diff * 1181;
5186 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005187 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005188}
5189
Daniel Vetter02d71952012-08-09 16:44:54 +02005190void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5191{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005192 struct drm_device *dev = dev_priv->dev;
5193
5194 if (INTEL_INFO(dev)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02005195 return;
5196
Daniel Vetter92703882012-08-09 16:46:01 +02005197 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005198
5199 __i915_update_gfx_val(dev_priv);
5200
Daniel Vetter92703882012-08-09 16:46:01 +02005201 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02005202}
5203
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005204static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02005205{
5206 unsigned long t, corr, state1, corr2, state2;
5207 u32 pxvid, ext_v;
5208
Daniel Vetter02d71952012-08-09 16:44:54 +02005209 assert_spin_locked(&mchdev_lock);
5210
Ben Widawskyb39fb292014-03-19 18:31:11 -07005211 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->rps.cur_freq * 4));
Daniel Vettereb48eb02012-04-26 23:28:12 +02005212 pxvid = (pxvid >> 24) & 0x7f;
5213 ext_v = pvid_to_extvid(dev_priv, pxvid);
5214
5215 state1 = ext_v;
5216
5217 t = i915_mch_val(dev_priv);
5218
5219 /* Revel in the empirically derived constants */
5220
5221 /* Correction factor in 1/100000 units */
5222 if (t > 80)
5223 corr = ((t * 2349) + 135940);
5224 else if (t >= 50)
5225 corr = ((t * 964) + 29317);
5226 else /* < 50 */
5227 corr = ((t * 301) + 1004);
5228
5229 corr = corr * ((150142 * state1) / 10000 - 78642);
5230 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02005231 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005232
5233 state2 = (corr2 * state1) / 10000;
5234 state2 /= 100; /* convert to mW */
5235
Daniel Vetter02d71952012-08-09 16:44:54 +02005236 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005237
Daniel Vetter20e4d402012-08-08 23:35:39 +02005238 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005239}
5240
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005241unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5242{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005243 struct drm_device *dev = dev_priv->dev;
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005244 unsigned long val;
5245
Damien Lespiau3d13ef22014-02-07 19:12:47 +00005246 if (INTEL_INFO(dev)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005247 return 0;
5248
5249 spin_lock_irq(&mchdev_lock);
5250
5251 val = __i915_gfx_val(dev_priv);
5252
5253 spin_unlock_irq(&mchdev_lock);
5254
5255 return val;
5256}
5257
Daniel Vettereb48eb02012-04-26 23:28:12 +02005258/**
5259 * i915_read_mch_val - return value for IPS use
5260 *
5261 * Calculate and return a value for the IPS driver to use when deciding whether
5262 * we have thermal and power headroom to increase CPU or GPU power budget.
5263 */
5264unsigned long i915_read_mch_val(void)
5265{
5266 struct drm_i915_private *dev_priv;
5267 unsigned long chipset_val, graphics_val, ret = 0;
5268
Daniel Vetter92703882012-08-09 16:46:01 +02005269 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005270 if (!i915_mch_dev)
5271 goto out_unlock;
5272 dev_priv = i915_mch_dev;
5273
Chris Wilsonf531dcb2012-09-25 10:16:12 +01005274 chipset_val = __i915_chipset_val(dev_priv);
5275 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005276
5277 ret = chipset_val + graphics_val;
5278
5279out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005280 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005281
5282 return ret;
5283}
5284EXPORT_SYMBOL_GPL(i915_read_mch_val);
5285
5286/**
5287 * i915_gpu_raise - raise GPU frequency limit
5288 *
5289 * Raise the limit; IPS indicates we have thermal headroom.
5290 */
5291bool i915_gpu_raise(void)
5292{
5293 struct drm_i915_private *dev_priv;
5294 bool ret = true;
5295
Daniel Vetter92703882012-08-09 16:46:01 +02005296 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005297 if (!i915_mch_dev) {
5298 ret = false;
5299 goto out_unlock;
5300 }
5301 dev_priv = i915_mch_dev;
5302
Daniel Vetter20e4d402012-08-08 23:35:39 +02005303 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5304 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005305
5306out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005307 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005308
5309 return ret;
5310}
5311EXPORT_SYMBOL_GPL(i915_gpu_raise);
5312
5313/**
5314 * i915_gpu_lower - lower GPU frequency limit
5315 *
5316 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5317 * frequency maximum.
5318 */
5319bool i915_gpu_lower(void)
5320{
5321 struct drm_i915_private *dev_priv;
5322 bool ret = true;
5323
Daniel Vetter92703882012-08-09 16:46:01 +02005324 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005325 if (!i915_mch_dev) {
5326 ret = false;
5327 goto out_unlock;
5328 }
5329 dev_priv = i915_mch_dev;
5330
Daniel Vetter20e4d402012-08-08 23:35:39 +02005331 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5332 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005333
5334out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005335 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005336
5337 return ret;
5338}
5339EXPORT_SYMBOL_GPL(i915_gpu_lower);
5340
5341/**
5342 * i915_gpu_busy - indicate GPU business to IPS
5343 *
5344 * Tell the IPS driver whether or not the GPU is busy.
5345 */
5346bool i915_gpu_busy(void)
5347{
5348 struct drm_i915_private *dev_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01005349 struct intel_engine_cs *ring;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005350 bool ret = false;
Chris Wilsonf047e392012-07-21 12:31:41 +01005351 int i;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005352
Daniel Vetter92703882012-08-09 16:46:01 +02005353 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005354 if (!i915_mch_dev)
5355 goto out_unlock;
5356 dev_priv = i915_mch_dev;
5357
Chris Wilsonf047e392012-07-21 12:31:41 +01005358 for_each_ring(ring, dev_priv, i)
5359 ret |= !list_empty(&ring->request_list);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005360
5361out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005362 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005363
5364 return ret;
5365}
5366EXPORT_SYMBOL_GPL(i915_gpu_busy);
5367
5368/**
5369 * i915_gpu_turbo_disable - disable graphics turbo
5370 *
5371 * Disable graphics turbo by resetting the max frequency and setting the
5372 * current frequency to the default.
5373 */
5374bool i915_gpu_turbo_disable(void)
5375{
5376 struct drm_i915_private *dev_priv;
5377 bool ret = true;
5378
Daniel Vetter92703882012-08-09 16:46:01 +02005379 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005380 if (!i915_mch_dev) {
5381 ret = false;
5382 goto out_unlock;
5383 }
5384 dev_priv = i915_mch_dev;
5385
Daniel Vetter20e4d402012-08-08 23:35:39 +02005386 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02005387
Daniel Vetter20e4d402012-08-08 23:35:39 +02005388 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02005389 ret = false;
5390
5391out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02005392 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005393
5394 return ret;
5395}
5396EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
5397
5398/**
5399 * Tells the intel_ips driver that the i915 driver is now loaded, if
5400 * IPS got loaded first.
5401 *
5402 * This awkward dance is so that neither module has to depend on the
5403 * other in order for IPS to do the appropriate communication of
5404 * GPU turbo limits to i915.
5405 */
5406static void
5407ips_ping_for_i915_load(void)
5408{
5409 void (*link)(void);
5410
5411 link = symbol_get(ips_link_to_i915_driver);
5412 if (link) {
5413 link();
5414 symbol_put(ips_link_to_i915_driver);
5415 }
5416}
5417
5418void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
5419{
Daniel Vetter02d71952012-08-09 16:44:54 +02005420 /* We only register the i915 ips part with intel-ips once everything is
5421 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02005422 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005423 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02005424 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005425
5426 ips_ping_for_i915_load();
5427}
5428
5429void intel_gpu_ips_teardown(void)
5430{
Daniel Vetter92703882012-08-09 16:46:01 +02005431 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005432 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02005433 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02005434}
Deepak S76c3552f2014-01-30 23:08:16 +05305435
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005436static void intel_init_emon(struct drm_device *dev)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005437{
5438 struct drm_i915_private *dev_priv = dev->dev_private;
5439 u32 lcfuse;
5440 u8 pxw[16];
5441 int i;
5442
5443 /* Disable to program */
5444 I915_WRITE(ECR, 0);
5445 POSTING_READ(ECR);
5446
5447 /* Program energy weights for various events */
5448 I915_WRITE(SDEW, 0x15040d00);
5449 I915_WRITE(CSIEW0, 0x007f0000);
5450 I915_WRITE(CSIEW1, 0x1e220004);
5451 I915_WRITE(CSIEW2, 0x04000004);
5452
5453 for (i = 0; i < 5; i++)
5454 I915_WRITE(PEW + (i * 4), 0);
5455 for (i = 0; i < 3; i++)
5456 I915_WRITE(DEW + (i * 4), 0);
5457
5458 /* Program P-state weights to account for frequency power adjustment */
5459 for (i = 0; i < 16; i++) {
5460 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
5461 unsigned long freq = intel_pxfreq(pxvidfreq);
5462 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
5463 PXVFREQ_PX_SHIFT;
5464 unsigned long val;
5465
5466 val = vid * vid;
5467 val *= (freq / 1000);
5468 val *= 255;
5469 val /= (127*127*900);
5470 if (val > 0xff)
5471 DRM_ERROR("bad pxval: %ld\n", val);
5472 pxw[i] = val;
5473 }
5474 /* Render standby states get 0 weight */
5475 pxw[14] = 0;
5476 pxw[15] = 0;
5477
5478 for (i = 0; i < 4; i++) {
5479 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
5480 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
5481 I915_WRITE(PXW + (i * 4), val);
5482 }
5483
5484 /* Adjust magic regs to magic values (more experimental results) */
5485 I915_WRITE(OGW0, 0);
5486 I915_WRITE(OGW1, 0);
5487 I915_WRITE(EG0, 0x00007f00);
5488 I915_WRITE(EG1, 0x0000000e);
5489 I915_WRITE(EG2, 0x000e0000);
5490 I915_WRITE(EG3, 0x68000300);
5491 I915_WRITE(EG4, 0x42000000);
5492 I915_WRITE(EG5, 0x00140031);
5493 I915_WRITE(EG6, 0);
5494 I915_WRITE(EG7, 0);
5495
5496 for (i = 0; i < 8; i++)
5497 I915_WRITE(PXWL + (i * 4), 0);
5498
5499 /* Enable PMON + select events */
5500 I915_WRITE(ECR, 0x80000019);
5501
5502 lcfuse = I915_READ(LCFUSE02);
5503
Daniel Vetter20e4d402012-08-08 23:35:39 +02005504 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03005505}
5506
Imre Deakae484342014-03-31 15:10:44 +03005507void intel_init_gt_powersave(struct drm_device *dev)
5508{
Imre Deake6069ca2014-04-18 16:01:02 +03005509 i915.enable_rc6 = sanitize_rc6_option(dev, i915.enable_rc6);
5510
Deepak S38807742014-05-23 21:00:15 +05305511 if (IS_CHERRYVIEW(dev))
5512 cherryview_init_gt_powersave(dev);
5513 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005514 valleyview_init_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005515}
5516
5517void intel_cleanup_gt_powersave(struct drm_device *dev)
5518{
Deepak S38807742014-05-23 21:00:15 +05305519 if (IS_CHERRYVIEW(dev))
5520 return;
5521 else if (IS_VALLEYVIEW(dev))
Imre Deak4e805192014-04-14 20:24:41 +03005522 valleyview_cleanup_gt_powersave(dev);
Imre Deakae484342014-03-31 15:10:44 +03005523}
5524
Imre Deakdbea3ce2014-12-15 18:59:28 +02005525static void gen6_suspend_rps(struct drm_device *dev)
5526{
5527 struct drm_i915_private *dev_priv = dev->dev_private;
5528
5529 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
5530
5531 /*
5532 * TODO: disable RPS interrupts on GEN9+ too once RPS support
5533 * is added for it.
5534 */
5535 if (INTEL_INFO(dev)->gen < 9)
5536 gen6_disable_rps_interrupts(dev);
5537}
5538
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005539/**
5540 * intel_suspend_gt_powersave - suspend PM work and helper threads
5541 * @dev: drm device
5542 *
5543 * We don't want to disable RC6 or other features here, we just want
5544 * to make sure any work we've queued has finished and won't bother
5545 * us while we're suspended.
5546 */
5547void intel_suspend_gt_powersave(struct drm_device *dev)
5548{
5549 struct drm_i915_private *dev_priv = dev->dev_private;
5550
Imre Deakd4d70aa2014-11-19 15:30:04 +02005551 if (INTEL_INFO(dev)->gen < 6)
5552 return;
5553
Imre Deakdbea3ce2014-12-15 18:59:28 +02005554 gen6_suspend_rps(dev);
Deepak Sb47adc12014-06-20 20:03:02 +05305555
5556 /* Force GPU to min freq during suspend */
5557 gen6_rps_idle(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07005558}
5559
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005560void intel_disable_gt_powersave(struct drm_device *dev)
5561{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005562 struct drm_i915_private *dev_priv = dev->dev_private;
5563
Daniel Vetter930ebb42012-06-29 23:32:16 +02005564 if (IS_IRONLAKE_M(dev)) {
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005565 ironlake_disable_drps(dev);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005566 ironlake_disable_rc6(dev);
Deepak S38807742014-05-23 21:00:15 +05305567 } else if (INTEL_INFO(dev)->gen >= 6) {
Daniel Vetter10d8d362014-06-12 17:48:52 +02005568 intel_suspend_gt_powersave(dev);
Imre Deake4948372014-05-12 18:35:04 +03005569
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005570 mutex_lock(&dev_priv->rps.hw_lock);
Zhe Wang20e49362014-11-04 17:07:05 +00005571 if (INTEL_INFO(dev)->gen >= 9)
5572 gen9_disable_rps(dev);
5573 else if (IS_CHERRYVIEW(dev))
Deepak S38807742014-05-23 21:00:15 +05305574 cherryview_disable_rps(dev);
5575 else if (IS_VALLEYVIEW(dev))
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005576 valleyview_disable_rps(dev);
5577 else
5578 gen6_disable_rps(dev);
Imre Deake5347702014-11-19 15:30:02 +02005579
Chris Wilsonc0951f02013-10-10 21:58:50 +01005580 dev_priv->rps.enabled = false;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005581 mutex_unlock(&dev_priv->rps.hw_lock);
Daniel Vetter930ebb42012-06-29 23:32:16 +02005582 }
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005583}
5584
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005585static void intel_gen6_powersave_work(struct work_struct *work)
5586{
5587 struct drm_i915_private *dev_priv =
5588 container_of(work, struct drm_i915_private,
5589 rps.delayed_resume_work.work);
5590 struct drm_device *dev = dev_priv->dev;
5591
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005592 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005593
Imre Deak3cc134e2014-11-19 15:30:03 +02005594 /*
5595 * TODO: reset/enable RPS interrupts on GEN9+ too, once RPS support is
5596 * added for it.
5597 */
5598 if (INTEL_INFO(dev)->gen < 9)
5599 gen6_reset_rps_interrupts(dev);
5600
Deepak S38807742014-05-23 21:00:15 +05305601 if (IS_CHERRYVIEW(dev)) {
5602 cherryview_enable_rps(dev);
5603 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes0a073b82013-04-17 15:54:58 -07005604 valleyview_enable_rps(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005605 } else if (INTEL_INFO(dev)->gen >= 9) {
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005606 gen9_enable_rc6(dev);
Zhe Wang20e49362014-11-04 17:07:05 +00005607 gen9_enable_rps(dev);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005608 __gen6_update_ring_freq(dev);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005609 } else if (IS_BROADWELL(dev)) {
5610 gen8_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005611 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005612 } else {
5613 gen6_enable_rps(dev);
Imre Deakc2bc2fc2014-04-18 16:16:23 +03005614 __gen6_update_ring_freq(dev);
Jesse Barnes0a073b82013-04-17 15:54:58 -07005615 }
Chris Wilsonc0951f02013-10-10 21:58:50 +01005616 dev_priv->rps.enabled = true;
Imre Deak3cc134e2014-11-19 15:30:03 +02005617
5618 if (INTEL_INFO(dev)->gen < 9)
5619 gen6_enable_rps_interrupts(dev);
5620
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005621 mutex_unlock(&dev_priv->rps.hw_lock);
Imre Deakc6df39b2014-04-14 20:24:29 +03005622
5623 intel_runtime_pm_put(dev_priv);
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005624}
5625
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005626void intel_enable_gt_powersave(struct drm_device *dev)
5627{
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005628 struct drm_i915_private *dev_priv = dev->dev_private;
5629
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005630 if (IS_IRONLAKE_M(dev)) {
Imre Deakdc1d0132014-04-14 20:24:28 +03005631 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005632 ironlake_enable_drps(dev);
5633 ironlake_enable_rc6(dev);
5634 intel_init_emon(dev);
Imre Deakdc1d0132014-04-14 20:24:28 +03005635 mutex_unlock(&dev->struct_mutex);
Deepak S38807742014-05-23 21:00:15 +05305636 } else if (INTEL_INFO(dev)->gen >= 6) {
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005637 /*
5638 * PCU communication is slow and this doesn't need to be
5639 * done at any specific time, so do this out of our fast path
5640 * to make resume and init faster.
Imre Deakc6df39b2014-04-14 20:24:29 +03005641 *
5642 * We depend on the HW RC6 power context save/restore
5643 * mechanism when entering D3 through runtime PM suspend. So
5644 * disable RPM until RPS/RC6 is properly setup. We can only
5645 * get here via the driver load/system resume/runtime resume
5646 * paths, so the _noresume version is enough (and in case of
5647 * runtime resume it's necessary).
Jesse Barnes1a01ab32012-11-02 11:14:00 -07005648 */
Imre Deakc6df39b2014-04-14 20:24:29 +03005649 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
5650 round_jiffies_up_relative(HZ)))
5651 intel_runtime_pm_get_noresume(dev_priv);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02005652 }
5653}
5654
Imre Deakc6df39b2014-04-14 20:24:29 +03005655void intel_reset_gt_powersave(struct drm_device *dev)
5656{
5657 struct drm_i915_private *dev_priv = dev->dev_private;
5658
Imre Deakdbea3ce2014-12-15 18:59:28 +02005659 if (INTEL_INFO(dev)->gen < 6)
5660 return;
5661
5662 gen6_suspend_rps(dev);
Imre Deakc6df39b2014-04-14 20:24:29 +03005663 dev_priv->rps.enabled = false;
Imre Deakc6df39b2014-04-14 20:24:29 +03005664}
5665
Daniel Vetter3107bd42012-10-31 22:52:31 +01005666static void ibx_init_clock_gating(struct drm_device *dev)
5667{
5668 struct drm_i915_private *dev_priv = dev->dev_private;
5669
5670 /*
5671 * On Ibex Peak and Cougar Point, we need to disable clock
5672 * gating for the panel power sequencer or it will fail to
5673 * start up when no ports are active.
5674 */
5675 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
5676}
5677
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005678static void g4x_disable_trickle_feed(struct drm_device *dev)
5679{
5680 struct drm_i915_private *dev_priv = dev->dev_private;
5681 int pipe;
5682
Damien Lespiau055e3932014-08-18 13:49:10 +01005683 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005684 I915_WRITE(DSPCNTR(pipe),
5685 I915_READ(DSPCNTR(pipe)) |
5686 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03005687 intel_flush_primary_plane(dev_priv, pipe);
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005688 }
5689}
5690
Ville Syrjälä017636c2013-12-05 15:51:37 +02005691static void ilk_init_lp_watermarks(struct drm_device *dev)
5692{
5693 struct drm_i915_private *dev_priv = dev->dev_private;
5694
5695 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
5696 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
5697 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
5698
5699 /*
5700 * Don't touch WM1S_LP_EN here.
5701 * Doing so could cause underruns.
5702 */
5703}
5704
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005705static void ironlake_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005706{
5707 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005708 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005709
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01005710 /*
5711 * Required for FBC
5712 * WaFbcDisableDpfcClockGating:ilk
5713 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005714 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
5715 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
5716 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005717
5718 I915_WRITE(PCH_3DCGDIS0,
5719 MARIUNIT_CLOCK_GATE_DISABLE |
5720 SVSMUNIT_CLOCK_GATE_DISABLE);
5721 I915_WRITE(PCH_3DCGDIS1,
5722 VFMUNIT_CLOCK_GATE_DISABLE);
5723
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005724 /*
5725 * According to the spec the following bits should be set in
5726 * order to enable memory self-refresh
5727 * The bit 22/21 of 0x42004
5728 * The bit 5 of 0x42020
5729 * The bit 15 of 0x45000
5730 */
5731 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5732 (I915_READ(ILK_DISPLAY_CHICKEN2) |
5733 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005734 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005735 I915_WRITE(DISP_ARB_CTL,
5736 (I915_READ(DISP_ARB_CTL) |
5737 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02005738
5739 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005740
5741 /*
5742 * Based on the document from hardware guys the following bits
5743 * should be set unconditionally in order to enable FBC.
5744 * The bit 22 of 0x42000
5745 * The bit 22 of 0x42004
5746 * The bit 7,8,9 of 0x42020.
5747 */
5748 if (IS_IRONLAKE_M(dev)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01005749 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005750 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5751 I915_READ(ILK_DISPLAY_CHICKEN1) |
5752 ILK_FBCQ_DIS);
5753 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5754 I915_READ(ILK_DISPLAY_CHICKEN2) |
5755 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005756 }
5757
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01005758 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
5759
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005760 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5761 I915_READ(ILK_DISPLAY_CHICKEN2) |
5762 ILK_ELPIN_409_SELECT);
5763 I915_WRITE(_3D_CHICKEN2,
5764 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
5765 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02005766
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005767 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02005768 I915_WRITE(CACHE_MODE_0,
5769 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01005770
Akash Goel4e046322014-04-04 17:14:38 +05305771 /* WaDisable_RenderCache_OperationalFlush:ilk */
5772 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5773
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005774 g4x_disable_trickle_feed(dev);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03005775
Daniel Vetter3107bd42012-10-31 22:52:31 +01005776 ibx_init_clock_gating(dev);
5777}
5778
5779static void cpt_init_clock_gating(struct drm_device *dev)
5780{
5781 struct drm_i915_private *dev_priv = dev->dev_private;
5782 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005783 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01005784
5785 /*
5786 * On Ibex Peak and Cougar Point, we need to disable clock
5787 * gating for the panel power sequencer or it will fail to
5788 * start up when no ports are active.
5789 */
Jesse Barnescd664072013-10-02 10:34:19 -07005790 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
5791 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
5792 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01005793 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
5794 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01005795 /* The below fixes the weird display corruption, a few pixels shifted
5796 * downward, on (only) LVDS of some HP laptops with IVY.
5797 */
Damien Lespiau055e3932014-08-18 13:49:10 +01005798 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005799 val = I915_READ(TRANS_CHICKEN2(pipe));
5800 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
5801 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005802 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005803 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03005804 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
5805 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
5806 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03005807 I915_WRITE(TRANS_CHICKEN2(pipe), val);
5808 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01005809 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01005810 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01005811 I915_WRITE(TRANS_CHICKEN1(pipe),
5812 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
5813 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005814}
5815
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005816static void gen6_check_mch_setup(struct drm_device *dev)
5817{
5818 struct drm_i915_private *dev_priv = dev->dev_private;
5819 uint32_t tmp;
5820
5821 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02005822 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
5823 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
5824 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005825}
5826
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03005827static void gen6_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005828{
5829 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau231e54f2012-10-19 17:55:41 +01005830 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005831
Damien Lespiau231e54f2012-10-19 17:55:41 +01005832 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005833
5834 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5835 I915_READ(ILK_DISPLAY_CHICKEN2) |
5836 ILK_ELPIN_409_SELECT);
5837
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01005838 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01005839 I915_WRITE(_3D_CHICKEN,
5840 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
5841
Akash Goel4e046322014-04-04 17:14:38 +05305842 /* WaDisable_RenderCache_OperationalFlush:snb */
5843 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
5844
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005845 /*
5846 * BSpec recoomends 8x4 when MSAA is used,
5847 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02005848 *
5849 * Note that PS/WM thread counts depend on the WIZ hashing
5850 * disable bit, which we don't touch here, but it's good
5851 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005852 */
5853 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00005854 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02005855
Ville Syrjälä017636c2013-12-05 15:51:37 +02005856 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005857
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005858 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02005859 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005860
5861 I915_WRITE(GEN6_UCGCTL1,
5862 I915_READ(GEN6_UCGCTL1) |
5863 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
5864 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
5865
5866 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
5867 * gating disable must be set. Failure to set it results in
5868 * flickering pixels due to Z write ordering failures after
5869 * some amount of runtime in the Mesa "fire" demo, and Unigine
5870 * Sanctuary and Tropics, and apparently anything else with
5871 * alpha test or pixel discard.
5872 *
5873 * According to the spec, bit 11 (RCCUNIT) must also be set,
5874 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07005875 *
Ville Syrjäläef593182014-01-22 21:32:47 +02005876 * WaDisableRCCUnitClockGating:snb
5877 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005878 */
5879 I915_WRITE(GEN6_UCGCTL2,
5880 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
5881 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
5882
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02005883 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02005884 I915_WRITE(_3D_CHICKEN3,
5885 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005886
5887 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02005888 * Bspec says:
5889 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
5890 * 3DSTATE_SF number of SF output attributes is more than 16."
5891 */
5892 I915_WRITE(_3D_CHICKEN3,
5893 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
5894
5895 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005896 * According to the spec the following bits should be
5897 * set in order to enable memory self-refresh and fbc:
5898 * The bit21 and bit22 of 0x42000
5899 * The bit21 and bit22 of 0x42004
5900 * The bit5 and bit7 of 0x42020
5901 * The bit14 of 0x70180
5902 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01005903 *
5904 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005905 */
5906 I915_WRITE(ILK_DISPLAY_CHICKEN1,
5907 I915_READ(ILK_DISPLAY_CHICKEN1) |
5908 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
5909 I915_WRITE(ILK_DISPLAY_CHICKEN2,
5910 I915_READ(ILK_DISPLAY_CHICKEN2) |
5911 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01005912 I915_WRITE(ILK_DSPCLK_GATE_D,
5913 I915_READ(ILK_DSPCLK_GATE_D) |
5914 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
5915 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005916
Ville Syrjälä0e088b82013-06-07 10:47:04 +03005917 g4x_disable_trickle_feed(dev);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07005918
Daniel Vetter3107bd42012-10-31 22:52:31 +01005919 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01005920
5921 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005922}
5923
5924static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
5925{
5926 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
5927
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005928 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02005929 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02005930 *
5931 * This actually overrides the dispatch
5932 * mode for all thread types.
5933 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03005934 reg &= ~GEN7_FF_SCHED_MASK;
5935 reg |= GEN7_FF_TS_SCHED_HW;
5936 reg |= GEN7_FF_VS_SCHED_HW;
5937 reg |= GEN7_FF_DS_SCHED_HW;
5938
5939 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
5940}
5941
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005942static void lpt_init_clock_gating(struct drm_device *dev)
5943{
5944 struct drm_i915_private *dev_priv = dev->dev_private;
5945
5946 /*
5947 * TODO: this bit should only be enabled when really needed, then
5948 * disabled when not needed anymore in order to save power.
5949 */
5950 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE)
5951 I915_WRITE(SOUTH_DSPCLK_GATE_D,
5952 I915_READ(SOUTH_DSPCLK_GATE_D) |
5953 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03005954
5955 /* WADPOClockGatingDisable:hsw */
5956 I915_WRITE(_TRANSA_CHICKEN1,
5957 I915_READ(_TRANSA_CHICKEN1) |
5958 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02005959}
5960
Imre Deak7d708ee2013-04-17 14:04:50 +03005961static void lpt_suspend_hw(struct drm_device *dev)
5962{
5963 struct drm_i915_private *dev_priv = dev->dev_private;
5964
5965 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
5966 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
5967
5968 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
5969 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
5970 }
5971}
5972
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03005973static void broadwell_init_clock_gating(struct drm_device *dev)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005974{
5975 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau07d27e22014-03-03 17:31:46 +00005976 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07005977
5978 I915_WRITE(WM3_LP_ILK, 0);
5979 I915_WRITE(WM2_LP_ILK, 0);
5980 I915_WRITE(WM1_LP_ILK, 0);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005981
Ben Widawskyab57fff2013-12-12 15:28:04 -08005982 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07005983 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005984
Ben Widawskyab57fff2013-12-12 15:28:04 -08005985 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005986 I915_WRITE(CHICKEN_PAR1_1,
5987 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
5988
Ben Widawskyab57fff2013-12-12 15:28:04 -08005989 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01005990 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00005991 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02005992 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02005993 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07005994 }
Ben Widawsky63801f22013-12-12 17:26:03 -08005995
Ben Widawskyab57fff2013-12-12 15:28:04 -08005996 /* WaVSRefCountFullforceMissDisable:bdw */
5997 /* WaDSRefCountFullforceMissDisable:bdw */
5998 I915_WRITE(GEN7_FF_THREAD_MODE,
5999 I915_READ(GEN7_FF_THREAD_MODE) &
6000 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02006001
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02006002 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6003 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02006004
6005 /* WaDisableSDEUnitClockGating:bdw */
6006 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6007 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00006008
Paulo Zanoni89d6b2b2014-08-21 17:09:36 -03006009 lpt_init_clock_gating(dev);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07006010}
6011
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006012static void haswell_init_clock_gating(struct drm_device *dev)
6013{
6014 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006015
Ville Syrjälä017636c2013-12-05 15:51:37 +02006016 ilk_init_lp_watermarks(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006017
Francisco Jerezf3fc4882013-10-02 15:53:16 -07006018 /* L3 caching of data atomics doesn't work -- disable it. */
6019 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6020 I915_WRITE(HSW_ROW_CHICKEN3,
6021 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6022
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006023 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006024 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6025 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6026 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6027
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02006028 /* WaVSRefCountFullforceMissDisable:hsw */
6029 I915_WRITE(GEN7_FF_THREAD_MODE,
6030 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006031
Akash Goel4e046322014-04-04 17:14:38 +05306032 /* WaDisable_RenderCache_OperationalFlush:hsw */
6033 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6034
Chia-I Wufe27c602014-01-28 13:29:33 +08006035 /* enable HiZ Raw Stall Optimization */
6036 I915_WRITE(CACHE_MODE_0_GEN7,
6037 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6038
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006039 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006040 I915_WRITE(CACHE_MODE_1,
6041 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006042
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006043 /*
6044 * BSpec recommends 8x4 when MSAA is used,
6045 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006046 *
6047 * Note that PS/WM thread counts depend on the WIZ hashing
6048 * disable bit, which we don't touch here, but it's good
6049 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006050 */
6051 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006052 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02006053
Kenneth Graunke94411592014-12-31 16:23:00 -08006054 /* WaSampleCChickenBitEnable:hsw */
6055 I915_WRITE(HALF_SLICE_CHICKEN3,
6056 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6057
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006058 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07006059 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6060
Paulo Zanoni90a88642013-05-03 17:23:45 -03006061 /* WaRsPkgCStateDisplayPMReq:hsw */
6062 I915_WRITE(CHICKEN_PAR1_1,
6063 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03006064
Paulo Zanoni17a303e2012-11-20 15:12:07 -02006065 lpt_init_clock_gating(dev);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006066}
6067
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006068static void ivybridge_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006069{
6070 struct drm_i915_private *dev_priv = dev->dev_private;
Ben Widawsky20848222012-05-04 18:58:59 -07006071 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006072
Ville Syrjälä017636c2013-12-05 15:51:37 +02006073 ilk_init_lp_watermarks(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006074
Damien Lespiau231e54f2012-10-19 17:55:41 +01006075 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006076
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006077 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05006078 I915_WRITE(_3D_CHICKEN3,
6079 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6080
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006081 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006082 I915_WRITE(IVB_CHICKEN3,
6083 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6084 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6085
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006086 /* WaDisablePSDDualDispatchEnable:ivb */
Jesse Barnes12f33822012-10-25 12:15:45 -07006087 if (IS_IVB_GT1(dev))
6088 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6089 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006090
Akash Goel4e046322014-04-04 17:14:38 +05306091 /* WaDisable_RenderCache_OperationalFlush:ivb */
6092 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6093
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006094 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006095 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6096 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6097
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006098 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006099 I915_WRITE(GEN7_L3CNTLREG1,
6100 GEN7_WA_FOR_GEN7_L3_CONTROL);
6101 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07006102 GEN7_WA_L3_CHICKEN_MODE);
6103 if (IS_IVB_GT1(dev))
6104 I915_WRITE(GEN7_ROW_CHICKEN2,
6105 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006106 else {
6107 /* must write both registers */
6108 I915_WRITE(GEN7_ROW_CHICKEN2,
6109 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07006110 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6111 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02006112 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006113
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006114 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05006115 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6116 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6117
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02006118 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006119 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006120 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006121 */
6122 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02006123 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006124
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006125 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006126 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6127 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6128 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6129
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006130 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006131
6132 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02006133
Chris Wilson22721342014-03-04 09:41:43 +00006134 if (0) { /* causes HiZ corruption on ivb:gt1 */
6135 /* enable HiZ Raw Stall Optimization */
6136 I915_WRITE(CACHE_MODE_0_GEN7,
6137 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6138 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08006139
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006140 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02006141 I915_WRITE(CACHE_MODE_1,
6142 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07006143
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006144 /*
6145 * BSpec recommends 8x4 when MSAA is used,
6146 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02006147 *
6148 * Note that PS/WM thread counts depend on the WIZ hashing
6149 * disable bit, which we don't touch here, but it's good
6150 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006151 */
6152 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00006153 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02006154
Ben Widawsky20848222012-05-04 18:58:59 -07006155 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6156 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6157 snpcr |= GEN6_MBC_SNPCR_MED;
6158 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01006159
Ben Widawskyab5c6082013-04-05 13:12:41 -07006160 if (!HAS_PCH_NOP(dev))
6161 cpt_init_clock_gating(dev);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01006162
6163 gen6_check_mch_setup(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006164}
6165
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006166static void valleyview_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006167{
6168 struct drm_i915_private *dev_priv = dev->dev_private;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006169
Ville Syrjäläd7fe0cc2013-05-21 18:01:50 +03006170 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006171
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006172 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05006173 I915_WRITE(_3D_CHICKEN3,
6174 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6175
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006176 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006177 I915_WRITE(IVB_CHICKEN3,
6178 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6179 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6180
Ville Syrjäläfad7d362014-01-22 21:32:39 +02006181 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006182 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07006183 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08006184 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6185 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07006186
Akash Goel4e046322014-04-04 17:14:38 +05306187 /* WaDisable_RenderCache_OperationalFlush:vlv */
6188 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6189
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006190 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05006191 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6192 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6193
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006194 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07006195 I915_WRITE(GEN7_ROW_CHICKEN2,
6196 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6197
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006198 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006199 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6200 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6201 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6202
Ville Syrjälä46680e02014-01-22 21:33:01 +02006203 gen7_setup_fixed_func_scheduler(dev_priv);
6204
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006205 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07006206 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006207 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07006208 */
6209 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02006210 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07006211
Akash Goelc98f5062014-03-24 23:00:07 +05306212 /* WaDisableL3Bank2xClockGate:vlv
6213 * Disabling L3 clock gating- MMIO 940c[25] = 1
6214 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6215 I915_WRITE(GEN7_UCGCTL4,
6216 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07006217
Ville Syrjäläe0d8d592013-06-12 22:11:18 +03006218 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006219
Ville Syrjäläafd58e72014-01-22 21:33:03 +02006220 /*
6221 * BSpec says this must be set, even though
6222 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6223 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02006224 I915_WRITE(CACHE_MODE_1,
6225 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07006226
6227 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02006228 * BSpec recommends 8x4 when MSAA is used,
6229 * however in practice 16x4 seems fastest.
6230 *
6231 * Note that PS/WM thread counts depend on the WIZ hashing
6232 * disable bit, which we don't touch here, but it's good
6233 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6234 */
6235 I915_WRITE(GEN7_GT_MODE,
6236 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6237
6238 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02006239 * WaIncreaseL3CreditsForVLVB0:vlv
6240 * This is the hardware default actually.
6241 */
6242 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6243
6244 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01006245 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07006246 * Disable clock gating on th GCFG unit to prevent a delay
6247 * in the reporting of vblank events.
6248 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02006249 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006250}
6251
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006252static void cherryview_init_clock_gating(struct drm_device *dev)
6253{
6254 struct drm_i915_private *dev_priv = dev->dev_private;
6255
6256 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6257
6258 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
Ville Syrjälädd811e72014-04-09 13:28:33 +03006259
Ville Syrjälä232ce332014-04-09 13:28:35 +03006260 /* WaVSRefCountFullforceMissDisable:chv */
6261 /* WaDSRefCountFullforceMissDisable:chv */
6262 I915_WRITE(GEN7_FF_THREAD_MODE,
6263 I915_READ(GEN7_FF_THREAD_MODE) &
6264 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03006265
6266 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6267 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6268 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03006269
6270 /* WaDisableCSUnitClockGating:chv */
6271 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6272 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03006273
6274 /* WaDisableSDEUnitClockGating:chv */
6275 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6276 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006277}
6278
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006279static void g4x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006280{
6281 struct drm_i915_private *dev_priv = dev->dev_private;
6282 uint32_t dspclk_gate;
6283
6284 I915_WRITE(RENCLK_GATE_D1, 0);
6285 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6286 GS_UNIT_CLOCK_GATE_DISABLE |
6287 CL_UNIT_CLOCK_GATE_DISABLE);
6288 I915_WRITE(RAMCLK_GATE_D, 0);
6289 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6290 OVRUNIT_CLOCK_GATE_DISABLE |
6291 OVCUNIT_CLOCK_GATE_DISABLE;
6292 if (IS_GM45(dev))
6293 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6294 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02006295
6296 /* WaDisableRenderCachePipelinedFlush */
6297 I915_WRITE(CACHE_MODE_0,
6298 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03006299
Akash Goel4e046322014-04-04 17:14:38 +05306300 /* WaDisable_RenderCache_OperationalFlush:g4x */
6301 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6302
Ville Syrjälä0e088b82013-06-07 10:47:04 +03006303 g4x_disable_trickle_feed(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006304}
6305
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006306static void crestline_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006307{
6308 struct drm_i915_private *dev_priv = dev->dev_private;
6309
6310 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6311 I915_WRITE(RENCLK_GATE_D2, 0);
6312 I915_WRITE(DSPCLK_GATE_D, 0);
6313 I915_WRITE(RAMCLK_GATE_D, 0);
6314 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006315 I915_WRITE(MI_ARB_STATE,
6316 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306317
6318 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6319 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006320}
6321
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006322static void broadwater_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006323{
6324 struct drm_i915_private *dev_priv = dev->dev_private;
6325
6326 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6327 I965_RCC_CLOCK_GATE_DISABLE |
6328 I965_RCPB_CLOCK_GATE_DISABLE |
6329 I965_ISC_CLOCK_GATE_DISABLE |
6330 I965_FBC_CLOCK_GATE_DISABLE);
6331 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03006332 I915_WRITE(MI_ARB_STATE,
6333 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05306334
6335 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6336 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006337}
6338
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006339static void gen3_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006340{
6341 struct drm_i915_private *dev_priv = dev->dev_private;
6342 u32 dstate = I915_READ(D_STATE);
6343
6344 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
6345 DSTATE_DOT_CLOCK_GATING;
6346 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01006347
6348 if (IS_PINEVIEW(dev))
6349 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02006350
6351 /* IIR "flip pending" means done if this bit is set */
6352 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02006353
6354 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02006355 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02006356
6357 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
6358 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006359
6360 I915_WRITE(MI_ARB_STATE,
6361 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006362}
6363
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006364static void i85x_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006365{
6366 struct drm_i915_private *dev_priv = dev->dev_private;
6367
6368 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02006369
6370 /* interrupts should cause a wake up from C3 */
6371 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
6372 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03006373
6374 I915_WRITE(MEM_MODE,
6375 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006376}
6377
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006378static void i830_init_clock_gating(struct drm_device *dev)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006379{
6380 struct drm_i915_private *dev_priv = dev->dev_private;
6381
6382 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä10383922014-08-15 01:21:54 +03006383
6384 I915_WRITE(MEM_MODE,
6385 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
6386 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006387}
6388
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006389void intel_init_clock_gating(struct drm_device *dev)
6390{
6391 struct drm_i915_private *dev_priv = dev->dev_private;
6392
6393 dev_priv->display.init_clock_gating(dev);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03006394}
6395
Imre Deak7d708ee2013-04-17 14:04:50 +03006396void intel_suspend_hw(struct drm_device *dev)
6397{
6398 if (HAS_PCH_LPT(dev))
6399 lpt_suspend_hw(dev);
6400}
6401
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006402/* Set up chip specific power management-related functions */
6403void intel_init_pm(struct drm_device *dev)
6404{
6405 struct drm_i915_private *dev_priv = dev->dev_private;
6406
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006407 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006408
Daniel Vetterc921aba2012-04-26 23:28:17 +02006409 /* For cxsr */
6410 if (IS_PINEVIEW(dev))
6411 i915_pineview_get_mem_freq(dev);
6412 else if (IS_GEN5(dev))
6413 i915_ironlake_get_mem_freq(dev);
6414
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006415 /* For FIFO watermark updates */
Damien Lespiauf5ed50c2014-11-13 17:51:52 +00006416 if (INTEL_INFO(dev)->gen >= 9) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00006417 skl_setup_wm_latency(dev);
6418
Damien Lespiauc83155a2014-03-28 00:18:35 +05306419 dev_priv->display.init_clock_gating = gen9_init_clock_gating;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00006420 dev_priv->display.update_wm = skl_update_wm;
6421 dev_priv->display.update_sprite_wm = skl_update_sprite_wm;
Damien Lespiauc83155a2014-03-28 00:18:35 +05306422 } else if (HAS_PCH_SPLIT(dev)) {
Damien Lespiaufa50ad62014-03-17 18:01:16 +00006423 ilk_setup_wm_latency(dev);
Ville Syrjälä53615a52013-08-01 16:18:50 +03006424
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006425 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
6426 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
6427 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
6428 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
6429 dev_priv->display.update_wm = ilk_update_wm;
6430 dev_priv->display.update_sprite_wm = ilk_update_sprite_wm;
6431 } else {
6432 DRM_DEBUG_KMS("Failed to read display plane latency. "
6433 "Disable CxSR\n");
6434 }
6435
6436 if (IS_GEN5(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006437 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006438 else if (IS_GEN6(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006439 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006440 else if (IS_IVYBRIDGE(dev))
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006441 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006442 else if (IS_HASWELL(dev))
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03006443 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02006444 else if (INTEL_INFO(dev)->gen == 8)
Paulo Zanoni47c2bd92014-08-21 17:09:37 -03006445 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006446 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03006447 dev_priv->display.update_wm = cherryview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306448 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Ville Syrjäläa4565da2014-04-09 13:28:10 +03006449 dev_priv->display.init_clock_gating =
6450 cherryview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006451 } else if (IS_VALLEYVIEW(dev)) {
6452 dev_priv->display.update_wm = valleyview_update_wm;
Gajanan Bhat01e184c2014-08-07 17:03:30 +05306453 dev_priv->display.update_sprite_wm = valleyview_update_sprite_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006454 dev_priv->display.init_clock_gating =
6455 valleyview_init_clock_gating;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006456 } else if (IS_PINEVIEW(dev)) {
6457 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6458 dev_priv->is_ddr3,
6459 dev_priv->fsb_freq,
6460 dev_priv->mem_freq)) {
6461 DRM_INFO("failed to find known CxSR latency "
6462 "(found ddr%s fsb freq %d, mem freq %d), "
6463 "disabling CxSR\n",
6464 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6465 dev_priv->fsb_freq, dev_priv->mem_freq);
6466 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03006467 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006468 dev_priv->display.update_wm = NULL;
6469 } else
6470 dev_priv->display.update_wm = pineview_update_wm;
6471 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6472 } else if (IS_G4X(dev)) {
6473 dev_priv->display.update_wm = g4x_update_wm;
6474 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6475 } else if (IS_GEN4(dev)) {
6476 dev_priv->display.update_wm = i965_update_wm;
6477 if (IS_CRESTLINE(dev))
6478 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6479 else if (IS_BROADWATER(dev))
6480 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6481 } else if (IS_GEN3(dev)) {
6482 dev_priv->display.update_wm = i9xx_update_wm;
6483 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6484 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006485 } else if (IS_GEN2(dev)) {
6486 if (INTEL_INFO(dev)->num_pipes == 1) {
6487 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006488 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006489 } else {
6490 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006491 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02006492 }
6493
6494 if (IS_I85X(dev) || IS_I865G(dev))
6495 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6496 else
6497 dev_priv->display.init_clock_gating = i830_init_clock_gating;
6498 } else {
6499 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006500 }
6501}
6502
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006503int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006504{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006505 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006506
6507 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6508 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
6509 return -EAGAIN;
6510 }
6511
6512 I915_WRITE(GEN6_PCODE_DATA, *val);
Damien Lespiaudddab342014-11-13 17:51:50 +00006513 I915_WRITE(GEN6_PCODE_DATA1, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07006514 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6515
6516 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6517 500)) {
6518 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
6519 return -ETIMEDOUT;
6520 }
6521
6522 *val = I915_READ(GEN6_PCODE_DATA);
6523 I915_WRITE(GEN6_PCODE_DATA, 0);
6524
6525 return 0;
6526}
6527
Tom O'Rourke151a49d2014-11-13 18:50:10 -08006528int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07006529{
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006530 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07006531
6532 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
6533 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
6534 return -EAGAIN;
6535 }
6536
6537 I915_WRITE(GEN6_PCODE_DATA, val);
6538 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
6539
6540 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
6541 500)) {
6542 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
6543 return -ETIMEDOUT;
6544 }
6545
6546 I915_WRITE(GEN6_PCODE_DATA, 0);
6547
6548 return 0;
6549}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07006550
Ville Syrjälädd06f882014-11-10 22:55:12 +02006551static int vlv_gpu_freq_div(unsigned int czclk_freq)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006552{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006553 switch (czclk_freq) {
6554 case 200:
6555 return 10;
6556 case 267:
6557 return 12;
6558 case 320:
6559 case 333:
Ville Syrjälädd06f882014-11-10 22:55:12 +02006560 return 16;
Ville Syrjäläab3fb152014-11-10 22:55:15 +02006561 case 400:
6562 return 20;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006563 default:
6564 return -1;
6565 }
Ville Syrjälädd06f882014-11-10 22:55:12 +02006566}
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006567
Ville Syrjälädd06f882014-11-10 22:55:12 +02006568static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
6569{
6570 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
6571
6572 div = vlv_gpu_freq_div(czclk_freq);
6573 if (div < 0)
6574 return div;
6575
6576 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006577}
6578
Fengguang Wub55dd642014-07-12 11:21:39 +02006579static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006580{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006581 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->mem_freq, 4);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006582
Ville Syrjälädd06f882014-11-10 22:55:12 +02006583 mul = vlv_gpu_freq_div(czclk_freq);
6584 if (mul < 0)
6585 return mul;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006586
Ville Syrjälädd06f882014-11-10 22:55:12 +02006587 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07006588}
6589
Fengguang Wub55dd642014-07-12 11:21:39 +02006590static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306591{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006592 int div, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306593
Ville Syrjälädd06f882014-11-10 22:55:12 +02006594 div = vlv_gpu_freq_div(czclk_freq) / 2;
6595 if (div < 0)
6596 return div;
Deepak S22b1b2f2014-07-12 14:54:33 +05306597
Ville Syrjälädd06f882014-11-10 22:55:12 +02006598 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306599}
6600
Fengguang Wub55dd642014-07-12 11:21:39 +02006601static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05306602{
Ville Syrjälädd06f882014-11-10 22:55:12 +02006603 int mul, czclk_freq = dev_priv->rps.cz_freq;
Deepak S22b1b2f2014-07-12 14:54:33 +05306604
Ville Syrjälädd06f882014-11-10 22:55:12 +02006605 mul = vlv_gpu_freq_div(czclk_freq) / 2;
6606 if (mul < 0)
6607 return mul;
Deepak S22b1b2f2014-07-12 14:54:33 +05306608
Ville Syrjälä1c147622014-08-18 14:42:43 +03006609 /* CHV needs even values */
Ville Syrjälädd06f882014-11-10 22:55:12 +02006610 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05306611}
6612
Ville Syrjälä616bc822015-01-23 21:04:25 +02006613int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
6614{
6615 if (IS_CHERRYVIEW(dev_priv->dev))
6616 return chv_gpu_freq(dev_priv, val);
6617 else if (IS_VALLEYVIEW(dev_priv->dev))
6618 return byt_gpu_freq(dev_priv, val);
6619 else
6620 return val * GT_FREQUENCY_MULTIPLIER;
6621}
6622
Ville Syrjälä616bc822015-01-23 21:04:25 +02006623int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
6624{
Deepak S22b1b2f2014-07-12 14:54:33 +05306625 if (IS_CHERRYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006626 return chv_freq_opcode(dev_priv, val);
Deepak S22b1b2f2014-07-12 14:54:33 +05306627 else if (IS_VALLEYVIEW(dev_priv->dev))
Ville Syrjälä616bc822015-01-23 21:04:25 +02006628 return byt_freq_opcode(dev_priv, val);
6629 else
6630 return val / GT_FREQUENCY_MULTIPLIER;
Deepak S22b1b2f2014-07-12 14:54:33 +05306631}
6632
Daniel Vetterf742a552013-12-06 10:17:53 +01006633void intel_pm_setup(struct drm_device *dev)
Chris Wilson907b28c2013-07-19 20:36:52 +01006634{
6635 struct drm_i915_private *dev_priv = dev->dev_private;
6636
Daniel Vetterf742a552013-12-06 10:17:53 +01006637 mutex_init(&dev_priv->rps.hw_lock);
6638
Chris Wilson907b28c2013-07-19 20:36:52 +01006639 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
6640 intel_gen6_powersave_work);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03006641
Paulo Zanoni33688d92014-03-07 20:08:19 -03006642 dev_priv->pm.suspended = false;
Chris Wilson907b28c2013-07-19 20:36:52 +01006643}