blob: b7ae42a67494f5bc6abf1144cdb5a4a74f239fbd [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>
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -070029#include <drm/drm_plane_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030030#include "i915_drv.h"
31#include "intel_drv.h"
Daniel Vettereb48eb02012-04-26 23:28:12 +020032#include "../../../platform/x86/intel_ips.h"
33#include <linux/module.h>
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +020034#include <drm/drm_atomic_helper.h>
Eugeni Dodonov85208be2012-04-16 22:20:34 -030035
Ben Widawskydc39fff2013-10-18 12:32:07 -070036/**
Jani Nikula18afd442016-01-18 09:19:48 +020037 * DOC: RC6
38 *
Ben Widawskydc39fff2013-10-18 12:32:07 -070039 * RC6 is a special power stage which allows the GPU to enter an very
40 * low-voltage mode when idle, using down to 0V while at this stage. This
41 * stage is entered automatically when the GPU is idle when RC6 support is
42 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
43 *
44 * There are different RC6 modes available in Intel GPU, which differentiate
45 * among each other with the latency required to enter and leave RC6 and
46 * voltage consumed by the GPU in different states.
47 *
48 * The combination of the following flags define which states GPU is allowed
49 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
50 * RC6pp is deepest RC6. Their support by hardware varies according to the
51 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
52 * which brings the most power savings; deeper states save more power, but
53 * require higher latency to switch to and wake up.
54 */
55#define INTEL_RC6_ENABLE (1<<0)
56#define INTEL_RC6p_ENABLE (1<<1)
57#define INTEL_RC6pp_ENABLE (1<<2)
58
Ville Syrjälä46f16e62016-10-31 22:37:22 +020059static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppalab033bb62016-06-07 17:19:04 +030060{
Mika Kuoppalab033bb62016-06-07 17:19:04 +030061 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl */
62 I915_WRITE(CHICKEN_PAR1_1,
63 I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
64
65 I915_WRITE(GEN8_CONFIG0,
66 I915_READ(GEN8_CONFIG0) | GEN9_DEFAULT_FIXES);
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030067
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020068 /* WaEnableChickenDCPR:skl,bxt,kbl,glk */
Mika Kuoppala590e8ff2016-06-07 17:19:13 +030069 I915_WRITE(GEN8_CHICKEN_DCPR_1,
70 I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
Mika Kuoppala0f78dee2016-06-07 17:19:16 +030071
72 /* WaFbcTurnOffFbcWatermark:skl,bxt,kbl */
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +020073 /* WaFbcWakeMemOn:skl,bxt,kbl,glk */
Mika Kuoppala303d4ea2016-06-07 17:19:17 +030074 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
75 DISP_FBC_WM_DIS |
76 DISP_FBC_MEMORY_WAKE);
Mika Kuoppalad1b4eef2016-06-07 17:19:19 +030077
78 /* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl */
79 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
80 ILK_DPFC_DISABLE_DUMMY0);
Mika Kuoppalab033bb62016-06-07 17:19:04 +030081}
82
Ville Syrjälä46f16e62016-10-31 22:37:22 +020083static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deaka82abe42015-03-27 14:00:04 +020084{
Ville Syrjälä46f16e62016-10-31 22:37:22 +020085 gen9_init_clock_gating(dev_priv);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +020086
Nick Hoatha7546152015-06-29 14:07:32 +010087 /* WaDisableSDEUnitClockGating:bxt */
88 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
89 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
90
Imre Deak32608ca2015-03-11 11:10:27 +020091 /*
92 * FIXME:
Ben Widawsky868434c2015-03-11 10:49:32 +020093 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
Imre Deak32608ca2015-03-11 11:10:27 +020094 */
Imre Deak32608ca2015-03-11 11:10:27 +020095 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
Ben Widawsky868434c2015-03-11 10:49:32 +020096 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
Imre Deakd965e7a2015-12-01 10:23:52 +020097
98 /*
99 * Wa: Backlight PWM may stop in the asserted state, causing backlight
100 * to stay fully on.
101 */
Jani Nikula8aeaf642017-02-15 17:21:37 +0200102 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
103 PWM1_GATING_DIS | PWM2_GATING_DIS);
Imre Deaka82abe42015-03-27 14:00:04 +0200104}
105
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200106static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
107{
108 gen9_init_clock_gating(dev_priv);
109
110 /*
111 * WaDisablePWMClockGating:glk
112 * Backlight PWM may stop in the asserted state, causing backlight
113 * to stay fully on.
114 */
115 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
116 PWM1_GATING_DIS | PWM2_GATING_DIS);
Ander Conselvan de Oliveiraf4f4b592017-02-22 08:34:29 +0200117
118 /* WaDDIIOTimeout:glk */
119 if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
120 u32 val = I915_READ(CHICKEN_MISC_2);
121 val &= ~(GLK_CL0_PWR_DOWN |
122 GLK_CL1_PWR_DOWN |
123 GLK_CL2_PWR_DOWN);
124 I915_WRITE(CHICKEN_MISC_2, val);
125 }
126
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +0200127}
128
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200129static void i915_pineview_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200130{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200131 u32 tmp;
132
133 tmp = I915_READ(CLKCFG);
134
135 switch (tmp & CLKCFG_FSB_MASK) {
136 case CLKCFG_FSB_533:
137 dev_priv->fsb_freq = 533; /* 133*4 */
138 break;
139 case CLKCFG_FSB_800:
140 dev_priv->fsb_freq = 800; /* 200*4 */
141 break;
142 case CLKCFG_FSB_667:
143 dev_priv->fsb_freq = 667; /* 167*4 */
144 break;
145 case CLKCFG_FSB_400:
146 dev_priv->fsb_freq = 400; /* 100*4 */
147 break;
148 }
149
150 switch (tmp & CLKCFG_MEM_MASK) {
151 case CLKCFG_MEM_533:
152 dev_priv->mem_freq = 533;
153 break;
154 case CLKCFG_MEM_667:
155 dev_priv->mem_freq = 667;
156 break;
157 case CLKCFG_MEM_800:
158 dev_priv->mem_freq = 800;
159 break;
160 }
161
162 /* detect pineview DDR3 setting */
163 tmp = I915_READ(CSHRDDR3CTL);
164 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
165}
166
Ville Syrjälä148ac1f2016-10-31 22:37:16 +0200167static void i915_ironlake_get_mem_freq(struct drm_i915_private *dev_priv)
Daniel Vetterc921aba2012-04-26 23:28:17 +0200168{
Daniel Vetterc921aba2012-04-26 23:28:17 +0200169 u16 ddrpll, csipll;
170
171 ddrpll = I915_READ16(DDRMPLL1);
172 csipll = I915_READ16(CSIPLL0);
173
174 switch (ddrpll & 0xff) {
175 case 0xc:
176 dev_priv->mem_freq = 800;
177 break;
178 case 0x10:
179 dev_priv->mem_freq = 1066;
180 break;
181 case 0x14:
182 dev_priv->mem_freq = 1333;
183 break;
184 case 0x18:
185 dev_priv->mem_freq = 1600;
186 break;
187 default:
188 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
189 ddrpll & 0xff);
190 dev_priv->mem_freq = 0;
191 break;
192 }
193
Daniel Vetter20e4d402012-08-08 23:35:39 +0200194 dev_priv->ips.r_t = dev_priv->mem_freq;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200195
196 switch (csipll & 0x3ff) {
197 case 0x00c:
198 dev_priv->fsb_freq = 3200;
199 break;
200 case 0x00e:
201 dev_priv->fsb_freq = 3733;
202 break;
203 case 0x010:
204 dev_priv->fsb_freq = 4266;
205 break;
206 case 0x012:
207 dev_priv->fsb_freq = 4800;
208 break;
209 case 0x014:
210 dev_priv->fsb_freq = 5333;
211 break;
212 case 0x016:
213 dev_priv->fsb_freq = 5866;
214 break;
215 case 0x018:
216 dev_priv->fsb_freq = 6400;
217 break;
218 default:
219 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
220 csipll & 0x3ff);
221 dev_priv->fsb_freq = 0;
222 break;
223 }
224
225 if (dev_priv->fsb_freq == 3200) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200226 dev_priv->ips.c_m = 0;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200227 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200228 dev_priv->ips.c_m = 1;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200229 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +0200230 dev_priv->ips.c_m = 2;
Daniel Vetterc921aba2012-04-26 23:28:17 +0200231 }
232}
233
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300234static const struct cxsr_latency cxsr_latency_table[] = {
235 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
236 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
237 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
238 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
239 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
240
241 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
242 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
243 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
244 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
245 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
246
247 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
248 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
249 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
250 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
251 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
252
253 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
254 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
255 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
256 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
257 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
258
259 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
260 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
261 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
262 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
263 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
264
265 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
266 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
267 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
268 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
269 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
270};
271
Tvrtko Ursulin44a655c2016-10-13 11:09:23 +0100272static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
273 bool is_ddr3,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300274 int fsb,
275 int mem)
276{
277 const struct cxsr_latency *latency;
278 int i;
279
280 if (fsb == 0 || mem == 0)
281 return NULL;
282
283 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
284 latency = &cxsr_latency_table[i];
285 if (is_desktop == latency->is_desktop &&
286 is_ddr3 == latency->is_ddr3 &&
287 fsb == latency->fsb_freq && mem == latency->mem_freq)
288 return latency;
289 }
290
291 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
292
293 return NULL;
294}
295
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200296static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
297{
298 u32 val;
299
300 mutex_lock(&dev_priv->rps.hw_lock);
301
302 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
303 if (enable)
304 val &= ~FORCE_DDR_HIGH_FREQ;
305 else
306 val |= FORCE_DDR_HIGH_FREQ;
307 val &= ~FORCE_DDR_LOW_FREQ;
308 val |= FORCE_DDR_FREQ_REQ_ACK;
309 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
310
311 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
312 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
313 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
314
315 mutex_unlock(&dev_priv->rps.hw_lock);
316}
317
Ville Syrjäläcfb41412015-03-05 21:19:51 +0200318static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
319{
320 u32 val;
321
322 mutex_lock(&dev_priv->rps.hw_lock);
323
324 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
325 if (enable)
326 val |= DSP_MAXFIFO_PM5_ENABLE;
327 else
328 val &= ~DSP_MAXFIFO_PM5_ENABLE;
329 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
330
331 mutex_unlock(&dev_priv->rps.hw_lock);
332}
333
Ville Syrjäläf4998962015-03-10 17:02:21 +0200334#define FW_WM(value, plane) \
335 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
336
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200337static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300338{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200339 bool was_enabled;
Imre Deak5209b1f2014-07-01 12:36:17 +0300340 u32 val;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300341
Tvrtko Ursulin920a14b2016-10-14 10:13:44 +0100342 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200343 was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300344 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300345 POSTING_READ(FW_BLC_SELF_VLV);
Jani Nikulac0f86832016-12-07 12:13:04 +0200346 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200347 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300348 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300349 POSTING_READ(FW_BLC_SELF);
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +0200350 } else if (IS_PINEVIEW(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200351 val = I915_READ(DSPFW3);
352 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
353 if (enable)
354 val |= PINEVIEW_SELF_REFRESH_EN;
355 else
356 val &= ~PINEVIEW_SELF_REFRESH_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300357 I915_WRITE(DSPFW3, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300358 POSTING_READ(DSPFW3);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100359 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200360 was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300361 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
362 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
363 I915_WRITE(FW_BLC_SELF, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300364 POSTING_READ(FW_BLC_SELF);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100365 } else if (IS_I915GM(dev_priv)) {
Ville Syrjäläacb91352016-07-29 17:57:02 +0300366 /*
367 * FIXME can't find a bit like this for 915G, and
368 * and yet it does have the related watermark in
369 * FW_BLC_SELF. What's going on?
370 */
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200371 was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
Imre Deak5209b1f2014-07-01 12:36:17 +0300372 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
373 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
374 I915_WRITE(INSTPM, val);
Ville Syrjäläa7a6c492015-06-24 22:00:01 +0300375 POSTING_READ(INSTPM);
Imre Deak5209b1f2014-07-01 12:36:17 +0300376 } else {
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200377 return false;
Imre Deak5209b1f2014-07-01 12:36:17 +0300378 }
379
Ville Syrjälä1489bba2017-03-02 19:15:07 +0200380 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
381
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200382 DRM_DEBUG_KMS("memory self-refresh is %s (was %s)\n",
383 enableddisabled(enable),
384 enableddisabled(was_enabled));
385
386 return was_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300387}
388
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200389bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200390{
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200391 bool ret;
392
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200393 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200394 ret = _intel_set_memory_cxsr(dev_priv, enable);
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200395 dev_priv->wm.vlv.cxsr = enable;
396 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjälä11a85d62016-11-28 19:37:12 +0200397
398 return ret;
Ville Syrjälä3d90e642016-11-28 19:37:11 +0200399}
Ville Syrjäläfc1ac8d2015-03-05 21:19:52 +0200400
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300401/*
402 * Latency for FIFO fetches is dependent on several factors:
403 * - memory configuration (speed, channels)
404 * - chipset
405 * - current MCH state
406 * It can be fairly high in some situations, so here we assume a fairly
407 * pessimal value. It's a tradeoff between extra memory fetches (if we
408 * set this value too high, the FIFO will fetch frequently to stay full)
409 * and power consumption (set it too low to save power and we might see
410 * FIFO underruns and display "flicker").
411 *
412 * A value of 5us seems to be a good balance; safe for very low end
413 * platforms but not overly aggressive on lower latency configs.
414 */
Chris Wilson5aef6002014-09-03 11:56:07 +0100415static const int pessimal_latency_ns = 5000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300416
Ville Syrjäläb5004722015-03-05 21:19:47 +0200417#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
418 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
419
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200420static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
Ville Syrjäläb5004722015-03-05 21:19:47 +0200421{
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200422 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200423 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +0200424 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200425 enum pipe pipe = crtc->pipe;
426 int sprite0_start, sprite1_start;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200427
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200428 switch (pipe) {
Ville Syrjäläb5004722015-03-05 21:19:47 +0200429 uint32_t dsparb, dsparb2, dsparb3;
430 case PIPE_A:
431 dsparb = I915_READ(DSPARB);
432 dsparb2 = I915_READ(DSPARB2);
433 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
434 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
435 break;
436 case PIPE_B:
437 dsparb = I915_READ(DSPARB);
438 dsparb2 = I915_READ(DSPARB2);
439 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
440 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
441 break;
442 case PIPE_C:
443 dsparb2 = I915_READ(DSPARB2);
444 dsparb3 = I915_READ(DSPARB3);
445 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
446 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
447 break;
448 default:
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200449 MISSING_CASE(pipe);
450 return;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200451 }
452
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200453 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
454 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
455 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
456 fifo_state->plane[PLANE_CURSOR] = 63;
Ville Syrjäläb5004722015-03-05 21:19:47 +0200457
Ville Syrjäläf07d43d2017-03-02 19:14:52 +0200458 DRM_DEBUG_KMS("Pipe %c FIFO size: %d/%d/%d/%d\n",
459 pipe_name(pipe),
460 fifo_state->plane[PLANE_PRIMARY],
461 fifo_state->plane[PLANE_SPRITE0],
462 fifo_state->plane[PLANE_SPRITE1],
463 fifo_state->plane[PLANE_CURSOR]);
Ville Syrjäläb5004722015-03-05 21:19:47 +0200464}
465
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200466static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300467{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300468 uint32_t dsparb = I915_READ(DSPARB);
469 int size;
470
471 size = dsparb & 0x7f;
472 if (plane)
473 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
474
475 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
476 plane ? "B" : "A", size);
477
478 return size;
479}
480
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200481static int i830_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300482{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300483 uint32_t dsparb = I915_READ(DSPARB);
484 int size;
485
486 size = dsparb & 0x1ff;
487 if (plane)
488 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
489 size >>= 1; /* Convert to cachelines */
490
491 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
492 plane ? "B" : "A", size);
493
494 return size;
495}
496
Ville Syrjäläef0f5e92016-10-31 22:37:17 +0200497static int i845_get_fifo_size(struct drm_i915_private *dev_priv, int plane)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300498{
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300499 uint32_t dsparb = I915_READ(DSPARB);
500 int size;
501
502 size = dsparb & 0x7f;
503 size >>= 2; /* Convert to cachelines */
504
505 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
506 plane ? "B" : "A",
507 size);
508
509 return size;
510}
511
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300512/* Pineview has different values for various configs */
513static const struct intel_watermark_params pineview_display_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300514 .fifo_size = PINEVIEW_DISPLAY_FIFO,
515 .max_wm = PINEVIEW_MAX_WM,
516 .default_wm = PINEVIEW_DFT_WM,
517 .guard_size = PINEVIEW_GUARD_WM,
518 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300519};
520static const struct intel_watermark_params pineview_display_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300521 .fifo_size = PINEVIEW_DISPLAY_FIFO,
522 .max_wm = PINEVIEW_MAX_WM,
523 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
524 .guard_size = PINEVIEW_GUARD_WM,
525 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300526};
527static const struct intel_watermark_params pineview_cursor_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300528 .fifo_size = PINEVIEW_CURSOR_FIFO,
529 .max_wm = PINEVIEW_CURSOR_MAX_WM,
530 .default_wm = PINEVIEW_CURSOR_DFT_WM,
531 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
532 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300533};
534static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300535 .fifo_size = PINEVIEW_CURSOR_FIFO,
536 .max_wm = PINEVIEW_CURSOR_MAX_WM,
537 .default_wm = PINEVIEW_CURSOR_DFT_WM,
538 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
539 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300540};
541static const struct intel_watermark_params g4x_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300542 .fifo_size = G4X_FIFO_SIZE,
543 .max_wm = G4X_MAX_WM,
544 .default_wm = G4X_MAX_WM,
545 .guard_size = 2,
546 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300547};
548static const struct intel_watermark_params g4x_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300549 .fifo_size = I965_CURSOR_FIFO,
550 .max_wm = I965_CURSOR_MAX_WM,
551 .default_wm = I965_CURSOR_DFT_WM,
552 .guard_size = 2,
553 .cacheline_size = G4X_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300554};
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300555static const struct intel_watermark_params i965_cursor_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300556 .fifo_size = I965_CURSOR_FIFO,
557 .max_wm = I965_CURSOR_MAX_WM,
558 .default_wm = I965_CURSOR_DFT_WM,
559 .guard_size = 2,
560 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300561};
562static const struct intel_watermark_params i945_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300563 .fifo_size = I945_FIFO_SIZE,
564 .max_wm = I915_MAX_WM,
565 .default_wm = 1,
566 .guard_size = 2,
567 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300568};
569static const struct intel_watermark_params i915_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300570 .fifo_size = I915_FIFO_SIZE,
571 .max_wm = I915_MAX_WM,
572 .default_wm = 1,
573 .guard_size = 2,
574 .cacheline_size = I915_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300575};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300576static const struct intel_watermark_params i830_a_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300577 .fifo_size = I855GM_FIFO_SIZE,
578 .max_wm = I915_MAX_WM,
579 .default_wm = 1,
580 .guard_size = 2,
581 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300582};
Ville Syrjälä9d539102014-08-15 01:21:53 +0300583static const struct intel_watermark_params i830_bc_wm_info = {
584 .fifo_size = I855GM_FIFO_SIZE,
585 .max_wm = I915_MAX_WM/2,
586 .default_wm = 1,
587 .guard_size = 2,
588 .cacheline_size = I830_FIFO_LINE_SIZE,
589};
Daniel Vetterfeb56b92013-12-14 20:38:30 -0200590static const struct intel_watermark_params i845_wm_info = {
Ville Syrjäläe0f02732014-06-05 19:15:50 +0300591 .fifo_size = I830_FIFO_SIZE,
592 .max_wm = I915_MAX_WM,
593 .default_wm = 1,
594 .guard_size = 2,
595 .cacheline_size = I830_FIFO_LINE_SIZE,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300596};
597
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300598/**
599 * intel_calculate_wm - calculate watermark level
600 * @clock_in_khz: pixel clock
601 * @wm: chip FIFO params
Ville Syrjäläac484962016-01-20 21:05:26 +0200602 * @cpp: bytes per pixel
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300603 * @latency_ns: memory latency for the platform
604 *
605 * Calculate the watermark level (the level at which the display plane will
606 * start fetching from memory again). Each chip has a different display
607 * FIFO size and allocation, so the caller needs to figure that out and pass
608 * in the correct intel_watermark_params structure.
609 *
610 * As the pixel clock runs, the FIFO will be drained at a rate that depends
611 * on the pixel size. When it reaches the watermark level, it'll start
612 * fetching FIFO line sized based chunks from memory until the FIFO fills
613 * past the watermark point. If the FIFO drains completely, a FIFO underrun
614 * will occur, and a display engine hang could result.
615 */
616static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
617 const struct intel_watermark_params *wm,
Ville Syrjäläac484962016-01-20 21:05:26 +0200618 int fifo_size, int cpp,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300619 unsigned long latency_ns)
620{
621 long entries_required, wm_size;
622
623 /*
624 * Note: we need to make sure we don't overflow for various clock &
625 * latency values.
626 * clocks go from a few thousand to several hundred thousand.
627 * latency is usually a few thousand
628 */
Ville Syrjäläac484962016-01-20 21:05:26 +0200629 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300630 1000;
631 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
632
633 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
634
635 wm_size = fifo_size - (entries_required + wm->guard_size);
636
637 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
638
639 /* Don't promote wm_size to unsigned... */
640 if (wm_size > (long)wm->max_wm)
641 wm_size = wm->max_wm;
642 if (wm_size <= 0)
643 wm_size = wm->default_wm;
Ville Syrjäläd6feb192014-09-05 21:54:13 +0300644
645 /*
646 * Bspec seems to indicate that the value shouldn't be lower than
647 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
648 * Lets go for 8 which is the burst size since certain platforms
649 * already use a hardcoded 8 (which is what the spec says should be
650 * done).
651 */
652 if (wm_size <= 8)
653 wm_size = 8;
654
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300655 return wm_size;
656}
657
Ville Syrjälä24304d82017-03-14 17:10:49 +0200658static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
659 const struct intel_plane_state *plane_state)
660{
661 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
662
663 /* FIXME check the 'enable' instead */
664 if (!crtc_state->base.active)
665 return false;
666
667 /*
668 * Treat cursor with fb as always visible since cursor updates
669 * can happen faster than the vrefresh rate, and the current
670 * watermark code doesn't handle that correctly. Cursor updates
671 * which set/clear the fb or change the cursor size are going
672 * to get throttled by intel_legacy_cursor_update() to work
673 * around this problem with the watermark code.
674 */
675 if (plane->id == PLANE_CURSOR)
676 return plane_state->base.fb != NULL;
677 else
678 return plane_state->base.visible;
679}
680
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200681static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300682{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200683 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300684
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200685 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200686 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300687 if (enabled)
688 return NULL;
689 enabled = crtc;
690 }
691 }
692
693 return enabled;
694}
695
Ville Syrjälä432081b2016-10-31 22:37:03 +0200696static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300697{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200698 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200699 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300700 const struct cxsr_latency *latency;
701 u32 reg;
702 unsigned long wm;
703
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100704 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
705 dev_priv->is_ddr3,
706 dev_priv->fsb_freq,
707 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300708 if (!latency) {
709 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300710 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300711 return;
712 }
713
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200714 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300715 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200716 const struct drm_display_mode *adjusted_mode =
717 &crtc->config->base.adjusted_mode;
718 const struct drm_framebuffer *fb =
719 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200720 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300721 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300722
723 /* Display SR */
724 wm = intel_calculate_wm(clock, &pineview_display_wm,
725 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200726 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300727 reg = I915_READ(DSPFW1);
728 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200729 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300730 I915_WRITE(DSPFW1, reg);
731 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
732
733 /* cursor SR */
734 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
735 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200736 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300737 reg = I915_READ(DSPFW3);
738 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200739 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300740 I915_WRITE(DSPFW3, reg);
741
742 /* Display HPLL off SR */
743 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
744 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200745 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300746 reg = I915_READ(DSPFW3);
747 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200748 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300749 I915_WRITE(DSPFW3, reg);
750
751 /* cursor HPLL off SR */
752 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
753 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200754 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300755 reg = I915_READ(DSPFW3);
756 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200757 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300758 I915_WRITE(DSPFW3, reg);
759 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
760
Imre Deak5209b1f2014-07-01 12:36:17 +0300761 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300762 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300763 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300764 }
765}
766
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200767static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300768 int plane,
769 const struct intel_watermark_params *display,
770 int display_latency_ns,
771 const struct intel_watermark_params *cursor,
772 int cursor_latency_ns,
773 int *plane_wm,
774 int *cursor_wm)
775{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200776 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300777 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200778 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200779 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300780 int line_time_us, line_count;
781 int entries, tlb_miss;
782
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200783 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200784 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300785 *cursor_wm = cursor->guard_size;
786 *plane_wm = display->guard_size;
787 return false;
788 }
789
Ville Syrjäläefc26112016-10-31 22:37:04 +0200790 adjusted_mode = &crtc->config->base.adjusted_mode;
791 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100792 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800793 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200794 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200795 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300796
797 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200798 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300799 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
800 if (tlb_miss > 0)
801 entries += tlb_miss;
802 entries = DIV_ROUND_UP(entries, display->cacheline_size);
803 *plane_wm = entries + display->guard_size;
804 if (*plane_wm > (int)display->max_wm)
805 *plane_wm = display->max_wm;
806
807 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200808 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300809 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200810 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300811 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
812 if (tlb_miss > 0)
813 entries += tlb_miss;
814 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
815 *cursor_wm = entries + cursor->guard_size;
816 if (*cursor_wm > (int)cursor->max_wm)
817 *cursor_wm = (int)cursor->max_wm;
818
819 return true;
820}
821
822/*
823 * Check the wm result.
824 *
825 * If any calculated watermark values is larger than the maximum value that
826 * can be programmed into the associated watermark register, that watermark
827 * must be disabled.
828 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200829static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300830 int display_wm, int cursor_wm,
831 const struct intel_watermark_params *display,
832 const struct intel_watermark_params *cursor)
833{
834 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
835 display_wm, cursor_wm);
836
837 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100838 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300839 display_wm, display->max_wm);
840 return false;
841 }
842
843 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100844 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845 cursor_wm, cursor->max_wm);
846 return false;
847 }
848
849 if (!(display_wm || cursor_wm)) {
850 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
851 return false;
852 }
853
854 return true;
855}
856
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200857static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300858 int plane,
859 int latency_ns,
860 const struct intel_watermark_params *display,
861 const struct intel_watermark_params *cursor,
862 int *display_wm, int *cursor_wm)
863{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200864 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300865 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200866 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200867 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300868 unsigned long line_time_us;
869 int line_count, line_size;
870 int small, large;
871 int entries;
872
873 if (!latency_ns) {
874 *display_wm = *cursor_wm = 0;
875 return false;
876 }
877
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200878 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200879 adjusted_mode = &crtc->config->base.adjusted_mode;
880 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100881 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800882 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200883 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200884 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300885
Ville Syrjälä922044c2014-02-14 14:18:57 +0200886 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300887 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200888 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300889
890 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200891 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300892 large = line_count * line_size;
893
894 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
895 *display_wm = entries + display->guard_size;
896
897 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200898 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300899 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
900 *cursor_wm = entries + cursor->guard_size;
901
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200902 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300903 *display_wm, *cursor_wm,
904 display, cursor);
905}
906
Ville Syrjälä15665972015-03-10 16:16:28 +0200907#define FW_WM_VLV(value, plane) \
908 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
909
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200910static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200911 const struct vlv_wm_values *wm)
912{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200913 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200914
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200915 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200916 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
917
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200918 I915_WRITE(VLV_DDL(pipe),
919 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
920 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
921 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
922 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
923 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200924
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200925 /*
926 * Zero the (unused) WM1 watermarks, and also clear all the
927 * high order bits so that there are no out of bounds values
928 * present in the registers during the reprogramming.
929 */
930 I915_WRITE(DSPHOWM, 0);
931 I915_WRITE(DSPHOWM1, 0);
932 I915_WRITE(DSPFW4, 0);
933 I915_WRITE(DSPFW5, 0);
934 I915_WRITE(DSPFW6, 0);
935
Ville Syrjäläae801522015-03-05 21:19:49 +0200936 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200937 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200938 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
939 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
940 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200941 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200942 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
943 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
944 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200945 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200946 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200947
948 if (IS_CHERRYVIEW(dev_priv)) {
949 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200950 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
951 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200952 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200953 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
954 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200955 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200956 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
957 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200958 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200959 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200960 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
961 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
962 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
963 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
964 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
965 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
966 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
967 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
968 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200969 } else {
970 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200971 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
972 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200973 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200974 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200975 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
976 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
977 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
978 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
979 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
980 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200981 }
982
983 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200984}
985
Ville Syrjälä15665972015-03-10 16:16:28 +0200986#undef FW_WM_VLV
987
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300988/* latency must be in 0.1us units. */
989static unsigned int vlv_wm_method2(unsigned int pixel_rate,
990 unsigned int pipe_htotal,
991 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200992 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300993 unsigned int latency)
994{
995 unsigned int ret;
996
997 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200998 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300999 ret = DIV_ROUND_UP(ret, 64);
1000
1001 return ret;
1002}
1003
Ville Syrjäläbb726512016-10-31 22:37:24 +02001004static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001005{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001006 /* all latencies in usec */
1007 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1008
Ville Syrjälä58590c12015-09-08 21:05:12 +03001009 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1010
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001011 if (IS_CHERRYVIEW(dev_priv)) {
1012 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1013 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +03001014
1015 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001016 }
1017}
1018
Ville Syrjäläe339d672016-11-28 19:37:17 +02001019static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1020 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001021 int level)
1022{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001023 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001024 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001025 const struct drm_display_mode *adjusted_mode =
1026 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001027 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001028
1029 if (dev_priv->wm.pri_latency[level] == 0)
1030 return USHRT_MAX;
1031
Ville Syrjäläe339d672016-11-28 19:37:17 +02001032 if (!plane_state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001033 return 0;
1034
Daniel Vetteref426c12017-01-04 11:41:10 +01001035 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001036 clock = adjusted_mode->crtc_clock;
1037 htotal = adjusted_mode->crtc_htotal;
1038 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001039 if (WARN_ON(htotal == 0))
1040 htotal = 1;
1041
1042 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1043 /*
1044 * FIXME the formula gives values that are
1045 * too big for the cursor FIFO, and hence we
1046 * would never be able to use cursors. For
1047 * now just hardcode the watermark.
1048 */
1049 wm = 63;
1050 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001051 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001052 dev_priv->wm.pri_latency[level] * 10);
1053 }
1054
1055 return min_t(int, wm, USHRT_MAX);
1056}
1057
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001058static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1059{
1060 return (active_planes & (BIT(PLANE_SPRITE0) |
1061 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1062}
1063
Ville Syrjälä5012e602017-03-02 19:14:56 +02001064static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001065{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001066 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001067 const struct vlv_pipe_wm *raw =
1068 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001069 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001070 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1071 int num_active_planes = hweight32(active_planes);
1072 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001073 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001074 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001075 unsigned int total_rate;
1076 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001077
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001078 /*
1079 * When enabling sprite0 after sprite1 has already been enabled
1080 * we tend to get an underrun unless sprite0 already has some
1081 * FIFO space allcoated. Hence we always allocate at least one
1082 * cacheline for sprite0 whenever sprite1 is enabled.
1083 *
1084 * All other plane enable sequences appear immune to this problem.
1085 */
1086 if (vlv_need_sprite0_fifo_workaround(active_planes))
1087 sprite0_fifo_extra = 1;
1088
Ville Syrjälä5012e602017-03-02 19:14:56 +02001089 total_rate = raw->plane[PLANE_PRIMARY] +
1090 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001091 raw->plane[PLANE_SPRITE1] +
1092 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001093
Ville Syrjälä5012e602017-03-02 19:14:56 +02001094 if (total_rate > fifo_size)
1095 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001096
Ville Syrjälä5012e602017-03-02 19:14:56 +02001097 if (total_rate == 0)
1098 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001099
Ville Syrjälä5012e602017-03-02 19:14:56 +02001100 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001101 unsigned int rate;
1102
Ville Syrjälä5012e602017-03-02 19:14:56 +02001103 if ((active_planes & BIT(plane_id)) == 0) {
1104 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001105 continue;
1106 }
1107
Ville Syrjälä5012e602017-03-02 19:14:56 +02001108 rate = raw->plane[plane_id];
1109 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1110 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001111 }
1112
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001113 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1114 fifo_left -= sprite0_fifo_extra;
1115
Ville Syrjälä5012e602017-03-02 19:14:56 +02001116 fifo_state->plane[PLANE_CURSOR] = 63;
1117
1118 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001119
1120 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001121 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001122 int plane_extra;
1123
1124 if (fifo_left == 0)
1125 break;
1126
Ville Syrjälä5012e602017-03-02 19:14:56 +02001127 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001128 continue;
1129
1130 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001131 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001132 fifo_left -= plane_extra;
1133 }
1134
Ville Syrjälä5012e602017-03-02 19:14:56 +02001135 WARN_ON(active_planes != 0 && fifo_left != 0);
1136
1137 /* give it all to the first plane if none are active */
1138 if (active_planes == 0) {
1139 WARN_ON(fifo_left != fifo_size);
1140 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1141 }
1142
1143 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001144}
1145
Ville Syrjäläff32c542017-03-02 19:14:57 +02001146static int vlv_num_wm_levels(struct drm_i915_private *dev_priv)
1147{
1148 return dev_priv->wm.max_level + 1;
1149}
1150
1151/* mark all levels starting from 'level' as invalid */
1152static void vlv_invalidate_wms(struct intel_crtc *crtc,
1153 struct vlv_wm_state *wm_state, int level)
1154{
1155 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1156
1157 for (; level < vlv_num_wm_levels(dev_priv); level++) {
1158 enum plane_id plane_id;
1159
1160 for_each_plane_id_on_crtc(crtc, plane_id)
1161 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1162
1163 wm_state->sr[level].cursor = USHRT_MAX;
1164 wm_state->sr[level].plane = USHRT_MAX;
1165 }
1166}
1167
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001168static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1169{
1170 if (wm > fifo_size)
1171 return USHRT_MAX;
1172 else
1173 return fifo_size - wm;
1174}
1175
Ville Syrjäläff32c542017-03-02 19:14:57 +02001176/*
1177 * Starting from 'level' set all higher
1178 * levels to 'value' in the "raw" watermarks.
1179 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001180static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001181 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001182{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001183 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1184 int num_levels = vlv_num_wm_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001185 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001186
Ville Syrjäläff32c542017-03-02 19:14:57 +02001187 for (; level < num_levels; level++) {
1188 struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001189
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001190 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001191 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001192 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001193
1194 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001195}
1196
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001197static bool vlv_plane_wm_compute(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001198 const struct intel_plane_state *plane_state)
1199{
1200 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1201 enum plane_id plane_id = plane->id;
1202 int num_levels = vlv_num_wm_levels(to_i915(plane->base.dev));
1203 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001204 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001205
1206 if (!plane_state->base.visible) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001207 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1208 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001209 }
1210
1211 for (level = 0; level < num_levels; level++) {
1212 struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1213 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1214 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1215
Ville Syrjäläff32c542017-03-02 19:14:57 +02001216 if (wm > max_wm)
1217 break;
1218
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001219 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001220 raw->plane[plane_id] = wm;
1221 }
1222
1223 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001224 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001225
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001226out:
1227 if (dirty)
1228 DRM_DEBUG_KMS("%s wms: [0]=%d,[1]=%d,[2]=%d\n",
1229 plane->base.name,
1230 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1231 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1232 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1233
1234 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001235}
1236
1237static bool vlv_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1238 enum plane_id plane_id, int level)
1239{
1240 const struct vlv_pipe_wm *raw =
1241 &crtc_state->wm.vlv.raw[level];
1242 const struct vlv_fifo_state *fifo_state =
1243 &crtc_state->wm.vlv.fifo_state;
1244
1245 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1246}
1247
1248static bool vlv_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1249{
1250 return vlv_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1251 vlv_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1252 vlv_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1253 vlv_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1254}
1255
1256static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001257{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001258 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001259 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001260 struct intel_atomic_state *state =
1261 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001262 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001263 const struct vlv_fifo_state *fifo_state =
1264 &crtc_state->wm.vlv.fifo_state;
1265 int num_active_planes = hweight32(crtc_state->active_planes &
1266 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001267 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001268 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001269 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001270 enum plane_id plane_id;
1271 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001272 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001273
Ville Syrjäläff32c542017-03-02 19:14:57 +02001274 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1275 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001276 to_intel_plane_state(plane->base.state);
1277
Ville Syrjäläff32c542017-03-02 19:14:57 +02001278 if (plane_state->base.crtc != &crtc->base &&
1279 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001280 continue;
1281
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001282 if (vlv_plane_wm_compute(crtc_state, plane_state))
1283 dirty |= BIT(plane->id);
1284 }
1285
1286 /*
1287 * DSPARB registers may have been reset due to the
1288 * power well being turned off. Make sure we restore
1289 * them to a consistent state even if no primary/sprite
1290 * planes are initially active.
1291 */
1292 if (needs_modeset)
1293 crtc_state->fifo_changed = true;
1294
1295 if (!dirty)
1296 return 0;
1297
1298 /* cursor changes don't warrant a FIFO recompute */
1299 if (dirty & ~BIT(PLANE_CURSOR)) {
1300 const struct intel_crtc_state *old_crtc_state =
1301 to_intel_crtc_state(crtc->base.state);
1302 const struct vlv_fifo_state *old_fifo_state =
1303 &old_crtc_state->wm.vlv.fifo_state;
1304
1305 ret = vlv_compute_fifo(crtc_state);
1306 if (ret)
1307 return ret;
1308
1309 if (needs_modeset ||
1310 memcmp(old_fifo_state, fifo_state,
1311 sizeof(*fifo_state)) != 0)
1312 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001313 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001314
Ville Syrjäläff32c542017-03-02 19:14:57 +02001315 /* initially allow all levels */
1316 wm_state->num_levels = vlv_num_wm_levels(dev_priv);
1317 /*
1318 * Note that enabling cxsr with no primary/sprite planes
1319 * enabled can wedge the pipe. Hence we only allow cxsr
1320 * with exactly one enabled primary/sprite plane.
1321 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001322 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001323
Ville Syrjälä5012e602017-03-02 19:14:56 +02001324 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001325 const struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1326 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001327
Ville Syrjäläff32c542017-03-02 19:14:57 +02001328 if (!vlv_crtc_wm_is_valid(crtc_state, level))
1329 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001330
Ville Syrjäläff32c542017-03-02 19:14:57 +02001331 for_each_plane_id_on_crtc(crtc, plane_id) {
1332 wm_state->wm[level].plane[plane_id] =
1333 vlv_invert_wm_value(raw->plane[plane_id],
1334 fifo_state->plane[plane_id]);
1335 }
1336
1337 wm_state->sr[level].plane =
1338 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001339 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001340 raw->plane[PLANE_SPRITE1]),
1341 sr_fifo_size);
1342
1343 wm_state->sr[level].cursor =
1344 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1345 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001346 }
1347
Ville Syrjäläff32c542017-03-02 19:14:57 +02001348 if (level == 0)
1349 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001350
Ville Syrjäläff32c542017-03-02 19:14:57 +02001351 /* limit to only levels we can actually handle */
1352 wm_state->num_levels = level;
1353
1354 /* invalidate the higher levels */
1355 vlv_invalidate_wms(crtc, wm_state, level);
1356
1357 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001358}
1359
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001360#define VLV_FIFO(plane, value) \
1361 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1362
Ville Syrjäläff32c542017-03-02 19:14:57 +02001363static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1364 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001365{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001366 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001367 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001368 const struct vlv_fifo_state *fifo_state =
1369 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001370 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001371
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001372 if (!crtc_state->fifo_changed)
1373 return;
1374
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001375 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1376 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1377 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001378
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001379 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1380 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001381
Ville Syrjäläc137d662017-03-02 19:15:06 +02001382 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1383
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001384 /*
1385 * uncore.lock serves a double purpose here. It allows us to
1386 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1387 * it protects the DSPARB registers from getting clobbered by
1388 * parallel updates from multiple pipes.
1389 *
1390 * intel_pipe_update_start() has already disabled interrupts
1391 * for us, so a plain spin_lock() is sufficient here.
1392 */
1393 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001394
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001395 switch (crtc->pipe) {
1396 uint32_t dsparb, dsparb2, dsparb3;
1397 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001398 dsparb = I915_READ_FW(DSPARB);
1399 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001400
1401 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1402 VLV_FIFO(SPRITEB, 0xff));
1403 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1404 VLV_FIFO(SPRITEB, sprite1_start));
1405
1406 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1407 VLV_FIFO(SPRITEB_HI, 0x1));
1408 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1409 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1410
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001411 I915_WRITE_FW(DSPARB, dsparb);
1412 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001413 break;
1414 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001415 dsparb = I915_READ_FW(DSPARB);
1416 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001417
1418 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1419 VLV_FIFO(SPRITED, 0xff));
1420 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1421 VLV_FIFO(SPRITED, sprite1_start));
1422
1423 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1424 VLV_FIFO(SPRITED_HI, 0xff));
1425 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1426 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1427
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001428 I915_WRITE_FW(DSPARB, dsparb);
1429 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001430 break;
1431 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001432 dsparb3 = I915_READ_FW(DSPARB3);
1433 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001434
1435 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1436 VLV_FIFO(SPRITEF, 0xff));
1437 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1438 VLV_FIFO(SPRITEF, sprite1_start));
1439
1440 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1441 VLV_FIFO(SPRITEF_HI, 0xff));
1442 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1443 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1444
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001445 I915_WRITE_FW(DSPARB3, dsparb3);
1446 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001447 break;
1448 default:
1449 break;
1450 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001451
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001452 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001453
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001454 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001455}
1456
1457#undef VLV_FIFO
1458
Ville Syrjälä4841da52017-03-02 19:14:59 +02001459static int vlv_compute_intermediate_wm(struct drm_device *dev,
1460 struct intel_crtc *crtc,
1461 struct intel_crtc_state *crtc_state)
1462{
1463 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
1464 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
1465 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
1466 int level;
1467
1468 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001469 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1470 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001471
1472 for (level = 0; level < intermediate->num_levels; level++) {
1473 enum plane_id plane_id;
1474
1475 for_each_plane_id_on_crtc(crtc, plane_id) {
1476 intermediate->wm[level].plane[plane_id] =
1477 min(optimal->wm[level].plane[plane_id],
1478 active->wm[level].plane[plane_id]);
1479 }
1480
1481 intermediate->sr[level].plane = min(optimal->sr[level].plane,
1482 active->sr[level].plane);
1483 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
1484 active->sr[level].cursor);
1485 }
1486
1487 vlv_invalidate_wms(crtc, intermediate, level);
1488
1489 /*
1490 * If our intermediate WM are identical to the final WM, then we can
1491 * omit the post-vblank programming; only update if it's different.
1492 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001493 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1494 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001495
1496 return 0;
1497}
1498
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001499static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001500 struct vlv_wm_values *wm)
1501{
1502 struct intel_crtc *crtc;
1503 int num_active_crtcs = 0;
1504
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001505 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001506 wm->cxsr = true;
1507
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001508 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001509 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001510
1511 if (!crtc->active)
1512 continue;
1513
1514 if (!wm_state->cxsr)
1515 wm->cxsr = false;
1516
1517 num_active_crtcs++;
1518 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1519 }
1520
1521 if (num_active_crtcs != 1)
1522 wm->cxsr = false;
1523
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001524 if (num_active_crtcs > 1)
1525 wm->level = VLV_WM_LEVEL_PM2;
1526
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001527 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001528 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001529 enum pipe pipe = crtc->pipe;
1530
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001531 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001532 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001533 wm->sr = wm_state->sr[wm->level];
1534
Ville Syrjälä1b313892016-11-28 19:37:08 +02001535 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
1536 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
1537 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
1538 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001539 }
1540}
1541
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001542static bool is_disabling(int old, int new, int threshold)
1543{
1544 return old >= threshold && new < threshold;
1545}
1546
1547static bool is_enabling(int old, int new, int threshold)
1548{
1549 return old < threshold && new >= threshold;
1550}
1551
Ville Syrjäläff32c542017-03-02 19:14:57 +02001552static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001553{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001554 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
1555 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001556
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001557 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001558
Ville Syrjäläff32c542017-03-02 19:14:57 +02001559 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001560 return;
1561
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001562 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001563 chv_set_memory_dvfs(dev_priv, false);
1564
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001565 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001566 chv_set_memory_pm5(dev_priv, false);
1567
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001568 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001569 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001570
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001571 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001572
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001573 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001574 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001575
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001576 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001577 chv_set_memory_pm5(dev_priv, true);
1578
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001579 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001580 chv_set_memory_dvfs(dev_priv, true);
1581
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001582 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001583}
1584
Ville Syrjäläff32c542017-03-02 19:14:57 +02001585static void vlv_initial_watermarks(struct intel_atomic_state *state,
1586 struct intel_crtc_state *crtc_state)
1587{
1588 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1589 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1590
1591 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02001592 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
1593 vlv_program_watermarks(dev_priv);
1594 mutex_unlock(&dev_priv->wm.wm_mutex);
1595}
1596
1597static void vlv_optimize_watermarks(struct intel_atomic_state *state,
1598 struct intel_crtc_state *crtc_state)
1599{
1600 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1601 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1602
1603 if (!crtc_state->wm.need_postvbl_update)
1604 return;
1605
1606 mutex_lock(&dev_priv->wm.wm_mutex);
1607 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001608 vlv_program_watermarks(dev_priv);
1609 mutex_unlock(&dev_priv->wm.wm_mutex);
1610}
1611
Ville Syrjäläae801522015-03-05 21:19:49 +02001612#define single_plane_enabled(mask) is_power_of_2(mask)
1613
Ville Syrjälä432081b2016-10-31 22:37:03 +02001614static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001615{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001616 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001617 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001618 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1619 int plane_sr, cursor_sr;
1620 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001621 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001622
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001623 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001624 &g4x_wm_info, pessimal_latency_ns,
1625 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001626 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001627 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001628
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001629 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001630 &g4x_wm_info, pessimal_latency_ns,
1631 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001632 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001633 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001634
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001635 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001636 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001637 sr_latency_ns,
1638 &g4x_wm_info,
1639 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001640 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001641 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001642 } else {
Imre Deak98584252014-06-13 14:54:20 +03001643 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001644 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001645 plane_sr = cursor_sr = 0;
1646 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001647
Ville Syrjäläa5043452014-06-28 02:04:18 +03001648 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1649 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001650 planea_wm, cursora_wm,
1651 planeb_wm, cursorb_wm,
1652 plane_sr, cursor_sr);
1653
1654 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001655 FW_WM(plane_sr, SR) |
1656 FW_WM(cursorb_wm, CURSORB) |
1657 FW_WM(planeb_wm, PLANEB) |
1658 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001659 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001660 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001661 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001662 /* HPLL off in SR has some issues on G4x... disable it */
1663 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001664 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001665 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001666
1667 if (cxsr_enabled)
1668 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001669}
1670
Ville Syrjälä432081b2016-10-31 22:37:03 +02001671static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001672{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001673 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001674 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001675 int srwm = 1;
1676 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001677 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001678
1679 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001680 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001681 if (crtc) {
1682 /* self-refresh has much higher latency */
1683 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001684 const struct drm_display_mode *adjusted_mode =
1685 &crtc->config->base.adjusted_mode;
1686 const struct drm_framebuffer *fb =
1687 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001688 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001689 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001690 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02001691 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001692 unsigned long line_time_us;
1693 int entries;
1694
Ville Syrjälä922044c2014-02-14 14:18:57 +02001695 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001696
1697 /* Use ns/us then divide to preserve precision */
1698 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001699 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001700 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1701 srwm = I965_FIFO_SIZE - entries;
1702 if (srwm < 0)
1703 srwm = 1;
1704 srwm &= 0x1ff;
1705 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1706 entries, srwm);
1707
1708 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001709 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001710 entries = DIV_ROUND_UP(entries,
1711 i965_cursor_wm_info.cacheline_size);
1712 cursor_sr = i965_cursor_wm_info.fifo_size -
1713 (entries + i965_cursor_wm_info.guard_size);
1714
1715 if (cursor_sr > i965_cursor_wm_info.max_wm)
1716 cursor_sr = i965_cursor_wm_info.max_wm;
1717
1718 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1719 "cursor %d\n", srwm, cursor_sr);
1720
Imre Deak98584252014-06-13 14:54:20 +03001721 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001722 } else {
Imre Deak98584252014-06-13 14:54:20 +03001723 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001724 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001725 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001726 }
1727
1728 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1729 srwm);
1730
1731 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001732 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1733 FW_WM(8, CURSORB) |
1734 FW_WM(8, PLANEB) |
1735 FW_WM(8, PLANEA));
1736 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1737 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001738 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001739 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001740
1741 if (cxsr_enabled)
1742 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001743}
1744
Ville Syrjäläf4998962015-03-10 17:02:21 +02001745#undef FW_WM
1746
Ville Syrjälä432081b2016-10-31 22:37:03 +02001747static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001748{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001749 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001750 const struct intel_watermark_params *wm_info;
1751 uint32_t fwater_lo;
1752 uint32_t fwater_hi;
1753 int cwm, srwm = 1;
1754 int fifo_size;
1755 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001756 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001757
Ville Syrjäläa9097be2016-10-31 22:37:20 +02001758 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001759 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001760 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001761 wm_info = &i915_wm_info;
1762 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001763 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001764
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001765 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001766 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001767 if (intel_crtc_active(crtc)) {
1768 const struct drm_display_mode *adjusted_mode =
1769 &crtc->config->base.adjusted_mode;
1770 const struct drm_framebuffer *fb =
1771 crtc->base.primary->state->fb;
1772 int cpp;
1773
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001774 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001775 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001776 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001777 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001778
Damien Lespiau241bfc32013-09-25 16:45:37 +01001779 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001780 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001781 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001782 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001783 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001784 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001785 if (planea_wm > (long)wm_info->max_wm)
1786 planea_wm = wm_info->max_wm;
1787 }
1788
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001789 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001790 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001791
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001792 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001793 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001794 if (intel_crtc_active(crtc)) {
1795 const struct drm_display_mode *adjusted_mode =
1796 &crtc->config->base.adjusted_mode;
1797 const struct drm_framebuffer *fb =
1798 crtc->base.primary->state->fb;
1799 int cpp;
1800
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001801 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001802 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001803 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001804 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001805
Damien Lespiau241bfc32013-09-25 16:45:37 +01001806 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001807 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001808 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001809 if (enabled == NULL)
1810 enabled = crtc;
1811 else
1812 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001813 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001814 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001815 if (planeb_wm > (long)wm_info->max_wm)
1816 planeb_wm = wm_info->max_wm;
1817 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001818
1819 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1820
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001821 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001822 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001823
Ville Syrjäläefc26112016-10-31 22:37:04 +02001824 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001825
1826 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001827 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001828 enabled = NULL;
1829 }
1830
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001831 /*
1832 * Overlay gets an aggressive default since video jitter is bad.
1833 */
1834 cwm = 2;
1835
1836 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001837 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001838
1839 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02001840 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001841 /* self-refresh has much higher latency */
1842 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001843 const struct drm_display_mode *adjusted_mode =
1844 &enabled->config->base.adjusted_mode;
1845 const struct drm_framebuffer *fb =
1846 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001847 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001848 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001849 int hdisplay = enabled->config->pipe_src_w;
1850 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001851 unsigned long line_time_us;
1852 int entries;
1853
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001854 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001855 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001856 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001857 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001858
Ville Syrjälä922044c2014-02-14 14:18:57 +02001859 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001860
1861 /* Use ns/us then divide to preserve precision */
1862 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001863 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001864 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1865 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1866 srwm = wm_info->fifo_size - entries;
1867 if (srwm < 0)
1868 srwm = 1;
1869
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001870 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001871 I915_WRITE(FW_BLC_SELF,
1872 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001873 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001874 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1875 }
1876
1877 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1878 planea_wm, planeb_wm, cwm, srwm);
1879
1880 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1881 fwater_hi = (cwm & 0x1f);
1882
1883 /* Set request length to 8 cachelines per fetch */
1884 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1885 fwater_hi = fwater_hi | (1 << 8);
1886
1887 I915_WRITE(FW_BLC, fwater_lo);
1888 I915_WRITE(FW_BLC2, fwater_hi);
1889
Imre Deak5209b1f2014-07-01 12:36:17 +03001890 if (enabled)
1891 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001892}
1893
Ville Syrjälä432081b2016-10-31 22:37:03 +02001894static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001895{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001896 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001897 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001898 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001899 uint32_t fwater_lo;
1900 int planea_wm;
1901
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001902 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001903 if (crtc == NULL)
1904 return;
1905
Ville Syrjäläefc26112016-10-31 22:37:04 +02001906 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001907 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001908 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001909 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001910 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001911 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1912 fwater_lo |= (3<<8) | planea_wm;
1913
1914 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1915
1916 I915_WRITE(FW_BLC, fwater_lo);
1917}
1918
Ville Syrjälä37126462013-08-01 16:18:55 +03001919/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001920static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001921{
1922 uint64_t ret;
1923
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001924 if (WARN(latency == 0, "Latency value missing\n"))
1925 return UINT_MAX;
1926
Ville Syrjäläac484962016-01-20 21:05:26 +02001927 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001928 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1929
1930 return ret;
1931}
1932
Ville Syrjälä37126462013-08-01 16:18:55 +03001933/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001934static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001935 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001936 uint32_t latency)
1937{
1938 uint32_t ret;
1939
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001940 if (WARN(latency == 0, "Latency value missing\n"))
1941 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001942 if (WARN_ON(!pipe_htotal))
1943 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001944
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001945 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001946 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001947 ret = DIV_ROUND_UP(ret, 64) + 2;
1948 return ret;
1949}
1950
Ville Syrjälä23297042013-07-05 11:57:17 +03001951static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001952 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001953{
Matt Roper15126882015-12-03 11:37:40 -08001954 /*
1955 * Neither of these should be possible since this function shouldn't be
1956 * called if the CRTC is off or the plane is invisible. But let's be
1957 * extra paranoid to avoid a potential divide-by-zero if we screw up
1958 * elsewhere in the driver.
1959 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001960 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001961 return 0;
1962 if (WARN_ON(!horiz_pixels))
1963 return 0;
1964
Ville Syrjäläac484962016-01-20 21:05:26 +02001965 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001966}
1967
Imre Deak820c1982013-12-17 14:46:36 +02001968struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001969 uint16_t pri;
1970 uint16_t spr;
1971 uint16_t cur;
1972 uint16_t fbc;
1973};
1974
Ville Syrjälä37126462013-08-01 16:18:55 +03001975/*
1976 * For both WM_PIPE and WM_LP.
1977 * mem_value must be in 0.1us units.
1978 */
Matt Roper7221fc32015-09-24 15:53:08 -07001979static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001980 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001981 uint32_t mem_value,
1982 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001983{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001984 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001985 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001986
Ville Syrjälä24304d82017-03-14 17:10:49 +02001987 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001988 return 0;
1989
Ville Syrjälä353c8592016-12-14 23:30:57 +02001990 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001991
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001992 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001993
1994 if (!is_lp)
1995 return method1;
1996
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001997 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07001998 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001999 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002000 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002001
2002 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002003}
2004
Ville Syrjälä37126462013-08-01 16:18:55 +03002005/*
2006 * For both WM_PIPE and WM_LP.
2007 * mem_value must be in 0.1us units.
2008 */
Matt Roper7221fc32015-09-24 15:53:08 -07002009static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002010 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002011 uint32_t mem_value)
2012{
2013 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02002014 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002015
Ville Syrjälä24304d82017-03-14 17:10:49 +02002016 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002017 return 0;
2018
Ville Syrjälä353c8592016-12-14 23:30:57 +02002019 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002020
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002021 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
2022 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002023 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002024 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002025 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002026 return min(method1, method2);
2027}
2028
Ville Syrjälä37126462013-08-01 16:18:55 +03002029/*
2030 * For both WM_PIPE and WM_LP.
2031 * mem_value must be in 0.1us units.
2032 */
Matt Roper7221fc32015-09-24 15:53:08 -07002033static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002034 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002035 uint32_t mem_value)
2036{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002037 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002038
Ville Syrjälä24304d82017-03-14 17:10:49 +02002039 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002040 return 0;
2041
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002042 cpp = pstate->base.fb->format->cpp[0];
2043
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002044 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002045 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002046 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002047}
2048
Paulo Zanonicca32e92013-05-31 11:45:06 -03002049/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002050static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002051 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002052 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002053{
Ville Syrjälä83054942016-11-18 21:53:00 +02002054 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002055
Ville Syrjälä24304d82017-03-14 17:10:49 +02002056 if (!intel_wm_plane_visible(cstate, pstate))
Paulo Zanonicca32e92013-05-31 11:45:06 -03002057 return 0;
2058
Ville Syrjälä353c8592016-12-14 23:30:57 +02002059 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002060
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002061 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002062}
2063
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002064static unsigned int
2065ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002066{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002067 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002068 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002069 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002070 return 768;
2071 else
2072 return 512;
2073}
2074
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002075static unsigned int
2076ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2077 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002078{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002079 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002080 /* BDW primary/sprite plane watermarks */
2081 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002082 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002083 /* IVB/HSW primary/sprite plane watermarks */
2084 return level == 0 ? 127 : 1023;
2085 else if (!is_sprite)
2086 /* ILK/SNB primary plane watermarks */
2087 return level == 0 ? 127 : 511;
2088 else
2089 /* ILK/SNB sprite plane watermarks */
2090 return level == 0 ? 63 : 255;
2091}
2092
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002093static unsigned int
2094ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002095{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002096 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002097 return level == 0 ? 63 : 255;
2098 else
2099 return level == 0 ? 31 : 63;
2100}
2101
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002102static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002103{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002104 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002105 return 31;
2106 else
2107 return 15;
2108}
2109
Ville Syrjälä158ae642013-08-07 13:28:19 +03002110/* Calculate the maximum primary/sprite plane watermark */
2111static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2112 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002113 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002114 enum intel_ddb_partitioning ddb_partitioning,
2115 bool is_sprite)
2116{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002117 struct drm_i915_private *dev_priv = to_i915(dev);
2118 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002119
2120 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002121 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002122 return 0;
2123
2124 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002125 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002126 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002127
2128 /*
2129 * For some reason the non self refresh
2130 * FIFO size is only half of the self
2131 * refresh FIFO size on ILK/SNB.
2132 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002133 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002134 fifo_size /= 2;
2135 }
2136
Ville Syrjälä240264f2013-08-07 13:29:12 +03002137 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002138 /* level 0 is always calculated with 1:1 split */
2139 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2140 if (is_sprite)
2141 fifo_size *= 5;
2142 fifo_size /= 6;
2143 } else {
2144 fifo_size /= 2;
2145 }
2146 }
2147
2148 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002149 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002150}
2151
2152/* Calculate the maximum cursor plane watermark */
2153static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002154 int level,
2155 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002156{
2157 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002158 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002159 return 64;
2160
2161 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002162 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002163}
2164
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002165static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002166 int level,
2167 const struct intel_wm_config *config,
2168 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002169 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002170{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002171 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2172 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2173 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002174 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002175}
2176
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002177static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002178 int level,
2179 struct ilk_wm_maximums *max)
2180{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002181 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2182 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2183 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2184 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002185}
2186
Ville Syrjäläd9395652013-10-09 19:18:10 +03002187static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002188 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002189 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002190{
2191 bool ret;
2192
2193 /* already determined to be invalid? */
2194 if (!result->enable)
2195 return false;
2196
2197 result->enable = result->pri_val <= max->pri &&
2198 result->spr_val <= max->spr &&
2199 result->cur_val <= max->cur;
2200
2201 ret = result->enable;
2202
2203 /*
2204 * HACK until we can pre-compute everything,
2205 * and thus fail gracefully if LP0 watermarks
2206 * are exceeded...
2207 */
2208 if (level == 0 && !result->enable) {
2209 if (result->pri_val > max->pri)
2210 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2211 level, result->pri_val, max->pri);
2212 if (result->spr_val > max->spr)
2213 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2214 level, result->spr_val, max->spr);
2215 if (result->cur_val > max->cur)
2216 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2217 level, result->cur_val, max->cur);
2218
2219 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2220 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2221 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2222 result->enable = true;
2223 }
2224
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002225 return ret;
2226}
2227
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002228static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002229 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002230 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002231 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002232 struct intel_plane_state *pristate,
2233 struct intel_plane_state *sprstate,
2234 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002235 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002236{
2237 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2238 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2239 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2240
2241 /* WM1+ latency values stored in 0.5us units */
2242 if (level > 0) {
2243 pri_latency *= 5;
2244 spr_latency *= 5;
2245 cur_latency *= 5;
2246 }
2247
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002248 if (pristate) {
2249 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2250 pri_latency, level);
2251 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2252 }
2253
2254 if (sprstate)
2255 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2256
2257 if (curstate)
2258 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2259
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002260 result->enable = true;
2261}
2262
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002263static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002264hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002265{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002266 const struct intel_atomic_state *intel_state =
2267 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002268 const struct drm_display_mode *adjusted_mode =
2269 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002270 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002271
Matt Roperee91a152015-12-03 11:37:39 -08002272 if (!cstate->base.active)
2273 return 0;
2274 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2275 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002276 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002277 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002278
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002279 /* The WM are computed with base on how long it takes to fill a single
2280 * row at the given clock rate, multiplied by 8.
2281 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002282 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2283 adjusted_mode->crtc_clock);
2284 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002285 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002286
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002287 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2288 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002289}
2290
Ville Syrjäläbb726512016-10-31 22:37:24 +02002291static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2292 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002293{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002294 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002295 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002296 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002297 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002298
2299 /* read the first set of memory latencies[0:3] */
2300 val = 0; /* data0 to be programmed to 0 for first set */
2301 mutex_lock(&dev_priv->rps.hw_lock);
2302 ret = sandybridge_pcode_read(dev_priv,
2303 GEN9_PCODE_READ_MEM_LATENCY,
2304 &val);
2305 mutex_unlock(&dev_priv->rps.hw_lock);
2306
2307 if (ret) {
2308 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2309 return;
2310 }
2311
2312 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2313 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2314 GEN9_MEM_LATENCY_LEVEL_MASK;
2315 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2316 GEN9_MEM_LATENCY_LEVEL_MASK;
2317 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2318 GEN9_MEM_LATENCY_LEVEL_MASK;
2319
2320 /* read the second set of memory latencies[4:7] */
2321 val = 1; /* data0 to be programmed to 1 for second set */
2322 mutex_lock(&dev_priv->rps.hw_lock);
2323 ret = sandybridge_pcode_read(dev_priv,
2324 GEN9_PCODE_READ_MEM_LATENCY,
2325 &val);
2326 mutex_unlock(&dev_priv->rps.hw_lock);
2327 if (ret) {
2328 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2329 return;
2330 }
2331
2332 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2333 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2334 GEN9_MEM_LATENCY_LEVEL_MASK;
2335 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2336 GEN9_MEM_LATENCY_LEVEL_MASK;
2337 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2338 GEN9_MEM_LATENCY_LEVEL_MASK;
2339
Vandana Kannan367294b2014-11-04 17:06:46 +00002340 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002341 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2342 * need to be disabled. We make sure to sanitize the values out
2343 * of the punit to satisfy this requirement.
2344 */
2345 for (level = 1; level <= max_level; level++) {
2346 if (wm[level] == 0) {
2347 for (i = level + 1; i <= max_level; i++)
2348 wm[i] = 0;
2349 break;
2350 }
2351 }
2352
2353 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002354 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002355 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002356 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002357 * to add 2us to the various latency levels we retrieve from the
2358 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002359 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002360 if (wm[0] == 0) {
2361 wm[0] += 2;
2362 for (level = 1; level <= max_level; level++) {
2363 if (wm[level] == 0)
2364 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002365 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002366 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002367 }
2368
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002369 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002370 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2371
2372 wm[0] = (sskpd >> 56) & 0xFF;
2373 if (wm[0] == 0)
2374 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002375 wm[1] = (sskpd >> 4) & 0xFF;
2376 wm[2] = (sskpd >> 12) & 0xFF;
2377 wm[3] = (sskpd >> 20) & 0x1FF;
2378 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002379 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002380 uint32_t sskpd = I915_READ(MCH_SSKPD);
2381
2382 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2383 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2384 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2385 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002386 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002387 uint32_t mltr = I915_READ(MLTR_ILK);
2388
2389 /* ILK primary LP0 latency is 700 ns */
2390 wm[0] = 7;
2391 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2392 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002393 }
2394}
2395
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002396static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2397 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002398{
2399 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002400 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002401 wm[0] = 13;
2402}
2403
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002404static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2405 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002406{
2407 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002408 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002409 wm[0] = 13;
2410
2411 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002412 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002413 wm[3] *= 2;
2414}
2415
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002416int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002417{
2418 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002419 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002420 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002421 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002422 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002423 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002424 return 3;
2425 else
2426 return 2;
2427}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002428
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002429static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002430 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002431 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002432{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002433 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002434
2435 for (level = 0; level <= max_level; level++) {
2436 unsigned int latency = wm[level];
2437
2438 if (latency == 0) {
2439 DRM_ERROR("%s WM%d latency not provided\n",
2440 name, level);
2441 continue;
2442 }
2443
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002444 /*
2445 * - latencies are in us on gen9.
2446 * - before then, WM1+ latency values are in 0.5us units
2447 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002448 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002449 latency *= 10;
2450 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002451 latency *= 5;
2452
2453 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2454 name, level, wm[level],
2455 latency / 10, latency % 10);
2456 }
2457}
2458
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002459static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2460 uint16_t wm[5], uint16_t min)
2461{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002462 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002463
2464 if (wm[0] >= min)
2465 return false;
2466
2467 wm[0] = max(wm[0], min);
2468 for (level = 1; level <= max_level; level++)
2469 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2470
2471 return true;
2472}
2473
Ville Syrjäläbb726512016-10-31 22:37:24 +02002474static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002475{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002476 bool changed;
2477
2478 /*
2479 * The BIOS provided WM memory latency values are often
2480 * inadequate for high resolution displays. Adjust them.
2481 */
2482 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2483 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2484 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2485
2486 if (!changed)
2487 return;
2488
2489 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002490 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2491 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2492 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002493}
2494
Ville Syrjäläbb726512016-10-31 22:37:24 +02002495static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002496{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002497 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002498
2499 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2500 sizeof(dev_priv->wm.pri_latency));
2501 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2502 sizeof(dev_priv->wm.pri_latency));
2503
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002504 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002505 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002506
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002507 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2508 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2509 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002510
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002511 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002512 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002513}
2514
Ville Syrjäläbb726512016-10-31 22:37:24 +02002515static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002516{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002517 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002518 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002519}
2520
Matt Ropered4a6a72016-02-23 17:20:13 -08002521static bool ilk_validate_pipe_wm(struct drm_device *dev,
2522 struct intel_pipe_wm *pipe_wm)
2523{
2524 /* LP0 watermark maximums depend on this pipe alone */
2525 const struct intel_wm_config config = {
2526 .num_pipes_active = 1,
2527 .sprites_enabled = pipe_wm->sprites_enabled,
2528 .sprites_scaled = pipe_wm->sprites_scaled,
2529 };
2530 struct ilk_wm_maximums max;
2531
2532 /* LP0 watermarks always use 1/2 DDB partitioning */
2533 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2534
2535 /* At least LP0 must be valid */
2536 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2537 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2538 return false;
2539 }
2540
2541 return true;
2542}
2543
Matt Roper261a27d2015-10-08 15:28:25 -07002544/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002545static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002546{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002547 struct drm_atomic_state *state = cstate->base.state;
2548 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002549 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002550 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002551 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002552 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002553 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002554 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002555 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002556 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002557 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002558
Matt Ropere8f1f022016-05-12 07:05:55 -07002559 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002560
Matt Roper43d59ed2015-09-24 15:53:07 -07002561 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002562 struct intel_plane_state *ps;
2563
2564 ps = intel_atomic_get_existing_plane_state(state,
2565 intel_plane);
2566 if (!ps)
2567 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002568
2569 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002570 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002571 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002572 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002573 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002574 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002575 }
2576
Matt Ropered4a6a72016-02-23 17:20:13 -08002577 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002578 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002579 pipe_wm->sprites_enabled = sprstate->base.visible;
2580 pipe_wm->sprites_scaled = sprstate->base.visible &&
2581 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2582 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002583 }
2584
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002585 usable_level = max_level;
2586
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002587 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002588 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002589 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002590
2591 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002592 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002593 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002594
Matt Roper86c8bbb2015-09-24 15:53:16 -07002595 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002596 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2597
2598 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2599 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002600
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002601 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002602 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002603
Matt Ropered4a6a72016-02-23 17:20:13 -08002604 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002605 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002606
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002607 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002608
2609 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002610 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002611
Matt Roper86c8bbb2015-09-24 15:53:16 -07002612 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002613 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002614
2615 /*
2616 * Disable any watermark level that exceeds the
2617 * register maximums since such watermarks are
2618 * always invalid.
2619 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002620 if (level > usable_level)
2621 continue;
2622
2623 if (ilk_validate_wm_level(level, &max, wm))
2624 pipe_wm->wm[level] = *wm;
2625 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002626 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002627 }
2628
Matt Roper86c8bbb2015-09-24 15:53:16 -07002629 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002630}
2631
2632/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002633 * Build a set of 'intermediate' watermark values that satisfy both the old
2634 * state and the new state. These can be programmed to the hardware
2635 * immediately.
2636 */
2637static int ilk_compute_intermediate_wm(struct drm_device *dev,
2638 struct intel_crtc *intel_crtc,
2639 struct intel_crtc_state *newstate)
2640{
Matt Ropere8f1f022016-05-12 07:05:55 -07002641 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002642 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002643 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002644
2645 /*
2646 * Start with the final, target watermarks, then combine with the
2647 * currently active watermarks to get values that are safe both before
2648 * and after the vblank.
2649 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002650 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002651 a->pipe_enabled |= b->pipe_enabled;
2652 a->sprites_enabled |= b->sprites_enabled;
2653 a->sprites_scaled |= b->sprites_scaled;
2654
2655 for (level = 0; level <= max_level; level++) {
2656 struct intel_wm_level *a_wm = &a->wm[level];
2657 const struct intel_wm_level *b_wm = &b->wm[level];
2658
2659 a_wm->enable &= b_wm->enable;
2660 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2661 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2662 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2663 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2664 }
2665
2666 /*
2667 * We need to make sure that these merged watermark values are
2668 * actually a valid configuration themselves. If they're not,
2669 * there's no safe way to transition from the old state to
2670 * the new state, so we need to fail the atomic transaction.
2671 */
2672 if (!ilk_validate_pipe_wm(dev, a))
2673 return -EINVAL;
2674
2675 /*
2676 * If our intermediate WM are identical to the final WM, then we can
2677 * omit the post-vblank programming; only update if it's different.
2678 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002679 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
2680 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08002681
2682 return 0;
2683}
2684
2685/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002686 * Merge the watermarks from all active pipes for a specific level.
2687 */
2688static void ilk_merge_wm_level(struct drm_device *dev,
2689 int level,
2690 struct intel_wm_level *ret_wm)
2691{
2692 const struct intel_crtc *intel_crtc;
2693
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002694 ret_wm->enable = true;
2695
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002696 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002697 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002698 const struct intel_wm_level *wm = &active->wm[level];
2699
2700 if (!active->pipe_enabled)
2701 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002702
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002703 /*
2704 * The watermark values may have been used in the past,
2705 * so we must maintain them in the registers for some
2706 * time even if the level is now disabled.
2707 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002708 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002709 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002710
2711 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2712 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2713 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2714 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2715 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002716}
2717
2718/*
2719 * Merge all low power watermarks for all active pipes.
2720 */
2721static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002722 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002723 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002724 struct intel_pipe_wm *merged)
2725{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002726 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002727 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002728 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002729
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002730 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002731 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002732 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002733 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002734
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002735 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002736 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002737
2738 /* merge each WM1+ level */
2739 for (level = 1; level <= max_level; level++) {
2740 struct intel_wm_level *wm = &merged->wm[level];
2741
2742 ilk_merge_wm_level(dev, level, wm);
2743
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002744 if (level > last_enabled_level)
2745 wm->enable = false;
2746 else if (!ilk_validate_wm_level(level, max, wm))
2747 /* make sure all following levels get disabled */
2748 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002749
2750 /*
2751 * The spec says it is preferred to disable
2752 * FBC WMs instead of disabling a WM level.
2753 */
2754 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002755 if (wm->enable)
2756 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002757 wm->fbc_val = 0;
2758 }
2759 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002760
2761 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2762 /*
2763 * FIXME this is racy. FBC might get enabled later.
2764 * What we should check here is whether FBC can be
2765 * enabled sometime later.
2766 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002767 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002768 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002769 for (level = 2; level <= max_level; level++) {
2770 struct intel_wm_level *wm = &merged->wm[level];
2771
2772 wm->enable = false;
2773 }
2774 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002775}
2776
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002777static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2778{
2779 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2780 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2781}
2782
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002783/* The value we need to program into the WM_LPx latency field */
2784static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2785{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002786 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002787
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002788 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002789 return 2 * level;
2790 else
2791 return dev_priv->wm.pri_latency[level];
2792}
2793
Imre Deak820c1982013-12-17 14:46:36 +02002794static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002795 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002796 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002797 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002798{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002799 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002800 struct intel_crtc *intel_crtc;
2801 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002802
Ville Syrjälä0362c782013-10-09 19:17:57 +03002803 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002804 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002805
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002806 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002807 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002808 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002809
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002810 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002811
Ville Syrjälä0362c782013-10-09 19:17:57 +03002812 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002813
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002814 /*
2815 * Maintain the watermark values even if the level is
2816 * disabled. Doing otherwise could cause underruns.
2817 */
2818 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002819 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002820 (r->pri_val << WM1_LP_SR_SHIFT) |
2821 r->cur_val;
2822
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002823 if (r->enable)
2824 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2825
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002826 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002827 results->wm_lp[wm_lp - 1] |=
2828 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2829 else
2830 results->wm_lp[wm_lp - 1] |=
2831 r->fbc_val << WM1_LP_FBC_SHIFT;
2832
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002833 /*
2834 * Always set WM1S_LP_EN when spr_val != 0, even if the
2835 * level is disabled. Doing otherwise could cause underruns.
2836 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002837 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002838 WARN_ON(wm_lp != 1);
2839 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2840 } else
2841 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002842 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002843
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002844 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002845 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002846 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002847 const struct intel_wm_level *r =
2848 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002849
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002850 if (WARN_ON(!r->enable))
2851 continue;
2852
Matt Ropered4a6a72016-02-23 17:20:13 -08002853 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002854
2855 results->wm_pipe[pipe] =
2856 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2857 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2858 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002859 }
2860}
2861
Paulo Zanoni861f3382013-05-31 10:19:21 -03002862/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2863 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002864static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002865 struct intel_pipe_wm *r1,
2866 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002867{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002868 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002869 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002870
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002871 for (level = 1; level <= max_level; level++) {
2872 if (r1->wm[level].enable)
2873 level1 = level;
2874 if (r2->wm[level].enable)
2875 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002876 }
2877
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002878 if (level1 == level2) {
2879 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002880 return r2;
2881 else
2882 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002883 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002884 return r1;
2885 } else {
2886 return r2;
2887 }
2888}
2889
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002890/* dirty bits used to track which watermarks need changes */
2891#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2892#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2893#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2894#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2895#define WM_DIRTY_FBC (1 << 24)
2896#define WM_DIRTY_DDB (1 << 25)
2897
Damien Lespiau055e3932014-08-18 13:49:10 +01002898static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002899 const struct ilk_wm_values *old,
2900 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002901{
2902 unsigned int dirty = 0;
2903 enum pipe pipe;
2904 int wm_lp;
2905
Damien Lespiau055e3932014-08-18 13:49:10 +01002906 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002907 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2908 dirty |= WM_DIRTY_LINETIME(pipe);
2909 /* Must disable LP1+ watermarks too */
2910 dirty |= WM_DIRTY_LP_ALL;
2911 }
2912
2913 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2914 dirty |= WM_DIRTY_PIPE(pipe);
2915 /* Must disable LP1+ watermarks too */
2916 dirty |= WM_DIRTY_LP_ALL;
2917 }
2918 }
2919
2920 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2921 dirty |= WM_DIRTY_FBC;
2922 /* Must disable LP1+ watermarks too */
2923 dirty |= WM_DIRTY_LP_ALL;
2924 }
2925
2926 if (old->partitioning != new->partitioning) {
2927 dirty |= WM_DIRTY_DDB;
2928 /* Must disable LP1+ watermarks too */
2929 dirty |= WM_DIRTY_LP_ALL;
2930 }
2931
2932 /* LP1+ watermarks already deemed dirty, no need to continue */
2933 if (dirty & WM_DIRTY_LP_ALL)
2934 return dirty;
2935
2936 /* Find the lowest numbered LP1+ watermark in need of an update... */
2937 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2938 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2939 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2940 break;
2941 }
2942
2943 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2944 for (; wm_lp <= 3; wm_lp++)
2945 dirty |= WM_DIRTY_LP(wm_lp);
2946
2947 return dirty;
2948}
2949
Ville Syrjälä8553c182013-12-05 15:51:39 +02002950static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2951 unsigned int dirty)
2952{
Imre Deak820c1982013-12-17 14:46:36 +02002953 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002954 bool changed = false;
2955
2956 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2957 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2958 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2959 changed = true;
2960 }
2961 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2962 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2963 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2964 changed = true;
2965 }
2966 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2967 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2968 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2969 changed = true;
2970 }
2971
2972 /*
2973 * Don't touch WM1S_LP_EN here.
2974 * Doing so could cause underruns.
2975 */
2976
2977 return changed;
2978}
2979
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002980/*
2981 * The spec says we shouldn't write when we don't need, because every write
2982 * causes WMs to be re-evaluated, expending some power.
2983 */
Imre Deak820c1982013-12-17 14:46:36 +02002984static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2985 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002986{
Imre Deak820c1982013-12-17 14:46:36 +02002987 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002988 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002989 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002990
Damien Lespiau055e3932014-08-18 13:49:10 +01002991 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002992 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002993 return;
2994
Ville Syrjälä8553c182013-12-05 15:51:39 +02002995 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002996
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002997 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002998 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002999 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003000 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003001 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003002 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
3003
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003004 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003005 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003006 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003007 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003008 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003009 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
3010
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003011 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01003012 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02003013 val = I915_READ(WM_MISC);
3014 if (results->partitioning == INTEL_DDB_PART_1_2)
3015 val &= ~WM_MISC_DATA_PARTITION_5_6;
3016 else
3017 val |= WM_MISC_DATA_PARTITION_5_6;
3018 I915_WRITE(WM_MISC, val);
3019 } else {
3020 val = I915_READ(DISP_ARB_CTL2);
3021 if (results->partitioning == INTEL_DDB_PART_1_2)
3022 val &= ~DISP_DATA_PARTITION_5_6;
3023 else
3024 val |= DISP_DATA_PARTITION_5_6;
3025 I915_WRITE(DISP_ARB_CTL2, val);
3026 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003027 }
3028
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003029 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003030 val = I915_READ(DISP_ARB_CTL);
3031 if (results->enable_fbc_wm)
3032 val &= ~DISP_FBC_WM_DIS;
3033 else
3034 val |= DISP_FBC_WM_DIS;
3035 I915_WRITE(DISP_ARB_CTL, val);
3036 }
3037
Imre Deak954911e2013-12-17 14:46:34 +02003038 if (dirty & WM_DIRTY_LP(1) &&
3039 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3040 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3041
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003042 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003043 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3044 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3045 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3046 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3047 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003048
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003049 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003050 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003051 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003052 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003053 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003054 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003055
3056 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003057}
3058
Matt Ropered4a6a72016-02-23 17:20:13 -08003059bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003060{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003061 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003062
3063 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3064}
3065
Lyude656d1b82016-08-17 15:55:54 -04003066#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003067
Matt Roper024c9042015-09-24 15:53:11 -07003068/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003069 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3070 * so assume we'll always need it in order to avoid underruns.
3071 */
3072static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3073{
3074 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3075
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003076 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003077 return true;
3078
3079 return false;
3080}
3081
Paulo Zanoni56feca92016-09-22 18:00:28 -03003082static bool
3083intel_has_sagv(struct drm_i915_private *dev_priv)
3084{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003085 if (IS_KABYLAKE(dev_priv))
3086 return true;
3087
3088 if (IS_SKYLAKE(dev_priv) &&
3089 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3090 return true;
3091
3092 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003093}
3094
Lyude656d1b82016-08-17 15:55:54 -04003095/*
3096 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3097 * depending on power and performance requirements. The display engine access
3098 * to system memory is blocked during the adjustment time. Because of the
3099 * blocking time, having this enabled can cause full system hangs and/or pipe
3100 * underruns if we don't meet all of the following requirements:
3101 *
3102 * - <= 1 pipe enabled
3103 * - All planes can enable watermarks for latencies >= SAGV engine block time
3104 * - We're not using an interlaced display configuration
3105 */
3106int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003107intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003108{
3109 int ret;
3110
Paulo Zanoni56feca92016-09-22 18:00:28 -03003111 if (!intel_has_sagv(dev_priv))
3112 return 0;
3113
3114 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003115 return 0;
3116
3117 DRM_DEBUG_KMS("Enabling the SAGV\n");
3118 mutex_lock(&dev_priv->rps.hw_lock);
3119
3120 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3121 GEN9_SAGV_ENABLE);
3122
3123 /* We don't need to wait for the SAGV when enabling */
3124 mutex_unlock(&dev_priv->rps.hw_lock);
3125
3126 /*
3127 * Some skl systems, pre-release machines in particular,
3128 * don't actually have an SAGV.
3129 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003130 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003131 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003132 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003133 return 0;
3134 } else if (ret < 0) {
3135 DRM_ERROR("Failed to enable the SAGV\n");
3136 return ret;
3137 }
3138
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003139 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003140 return 0;
3141}
3142
Lyude656d1b82016-08-17 15:55:54 -04003143int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003144intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003145{
Imre Deakb3b8e992016-12-05 18:27:38 +02003146 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003147
Paulo Zanoni56feca92016-09-22 18:00:28 -03003148 if (!intel_has_sagv(dev_priv))
3149 return 0;
3150
3151 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003152 return 0;
3153
3154 DRM_DEBUG_KMS("Disabling the SAGV\n");
3155 mutex_lock(&dev_priv->rps.hw_lock);
3156
3157 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003158 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3159 GEN9_SAGV_DISABLE,
3160 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3161 1);
Lyude656d1b82016-08-17 15:55:54 -04003162 mutex_unlock(&dev_priv->rps.hw_lock);
3163
Lyude656d1b82016-08-17 15:55:54 -04003164 /*
3165 * Some skl systems, pre-release machines in particular,
3166 * don't actually have an SAGV.
3167 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003168 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003169 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003170 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003171 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003172 } else if (ret < 0) {
3173 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3174 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003175 }
3176
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003177 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003178 return 0;
3179}
3180
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003181bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003182{
3183 struct drm_device *dev = state->dev;
3184 struct drm_i915_private *dev_priv = to_i915(dev);
3185 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003186 struct intel_crtc *crtc;
3187 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003188 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003189 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003190 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003191
Paulo Zanoni56feca92016-09-22 18:00:28 -03003192 if (!intel_has_sagv(dev_priv))
3193 return false;
3194
Lyude656d1b82016-08-17 15:55:54 -04003195 /*
3196 * SKL workaround: bspec recommends we disable the SAGV when we have
3197 * more then one pipe enabled
3198 *
3199 * If there are no active CRTCs, no additional checks need be performed
3200 */
3201 if (hweight32(intel_state->active_crtcs) == 0)
3202 return true;
3203 else if (hweight32(intel_state->active_crtcs) > 1)
3204 return false;
3205
3206 /* Since we're now guaranteed to only have one active CRTC... */
3207 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003208 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003209 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003210
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003211 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003212 return false;
3213
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003214 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003215 struct skl_plane_wm *wm =
3216 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003217
Lyude656d1b82016-08-17 15:55:54 -04003218 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003219 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003220 continue;
3221
3222 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003223 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003224 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003225 { }
3226
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003227 latency = dev_priv->wm.skl_latency[level];
3228
3229 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003230 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003231 I915_FORMAT_MOD_X_TILED)
3232 latency += 15;
3233
Lyude656d1b82016-08-17 15:55:54 -04003234 /*
3235 * If any of the planes on this pipe don't enable wm levels
3236 * that incur memory latencies higher then 30µs we can't enable
3237 * the SAGV
3238 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003239 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003240 return false;
3241 }
3242
3243 return true;
3244}
3245
Damien Lespiaub9cec072014-11-04 17:06:43 +00003246static void
3247skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003248 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003249 struct skl_ddb_entry *alloc, /* out */
3250 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003251{
Matt Roperc107acf2016-05-12 07:06:01 -07003252 struct drm_atomic_state *state = cstate->base.state;
3253 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3254 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003255 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003256 unsigned int pipe_size, ddb_size;
3257 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003258
Matt Ropera6d3460e2016-05-12 07:06:04 -07003259 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003260 alloc->start = 0;
3261 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003262 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003263 return;
3264 }
3265
Matt Ropera6d3460e2016-05-12 07:06:04 -07003266 if (intel_state->active_pipe_changes)
3267 *num_active = hweight32(intel_state->active_crtcs);
3268 else
3269 *num_active = hweight32(dev_priv->active_crtcs);
3270
Deepak M6f3fff62016-09-15 15:01:10 +05303271 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3272 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003273
3274 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3275
Matt Roperc107acf2016-05-12 07:06:01 -07003276 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003277 * If the state doesn't change the active CRTC's, then there's
3278 * no need to recalculate; the existing pipe allocation limits
3279 * should remain unchanged. Note that we're safe from racing
3280 * commits since any racing commit that changes the active CRTC
3281 * list would need to grab _all_ crtc locks, including the one
3282 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003283 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003284 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003285 /*
3286 * alloc may be cleared by clear_intel_crtc_state,
3287 * copy from old state to be sure
3288 */
3289 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003290 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003291 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003292
3293 nth_active_pipe = hweight32(intel_state->active_crtcs &
3294 (drm_crtc_mask(for_crtc) - 1));
3295 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3296 alloc->start = nth_active_pipe * ddb_size / *num_active;
3297 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003298}
3299
Matt Roperc107acf2016-05-12 07:06:01 -07003300static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003301{
Matt Roperc107acf2016-05-12 07:06:01 -07003302 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003303 return 32;
3304
3305 return 8;
3306}
3307
Damien Lespiaua269c582014-11-04 17:06:49 +00003308static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3309{
3310 entry->start = reg & 0x3ff;
3311 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003312 if (entry->end)
3313 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003314}
3315
Damien Lespiau08db6652014-11-04 17:06:52 +00003316void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3317 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003318{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003319 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003320
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003321 memset(ddb, 0, sizeof(*ddb));
3322
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003323 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003324 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003325 enum plane_id plane_id;
3326 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003327
3328 power_domain = POWER_DOMAIN_PIPE(pipe);
3329 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003330 continue;
3331
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003332 for_each_plane_id_on_crtc(crtc, plane_id) {
3333 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003334
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003335 if (plane_id != PLANE_CURSOR)
3336 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3337 else
3338 val = I915_READ(CUR_BUF_CFG(pipe));
3339
3340 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3341 }
Imre Deak4d800032016-02-17 16:31:29 +02003342
3343 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003344 }
3345}
3346
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003347/*
3348 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3349 * The bspec defines downscale amount as:
3350 *
3351 * """
3352 * Horizontal down scale amount = maximum[1, Horizontal source size /
3353 * Horizontal destination size]
3354 * Vertical down scale amount = maximum[1, Vertical source size /
3355 * Vertical destination size]
3356 * Total down scale amount = Horizontal down scale amount *
3357 * Vertical down scale amount
3358 * """
3359 *
3360 * Return value is provided in 16.16 fixed point form to retain fractional part.
3361 * Caller should take care of dividing & rounding off the value.
3362 */
3363static uint32_t
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003364skl_plane_downscale_amount(const struct intel_crtc_state *cstate,
3365 const struct intel_plane_state *pstate)
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003366{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003367 struct intel_plane *plane = to_intel_plane(pstate->base.plane);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003368 uint32_t downscale_h, downscale_w;
3369 uint32_t src_w, src_h, dst_w, dst_h;
3370
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003371 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003372 return DRM_PLANE_HELPER_NO_SCALING;
3373
3374 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003375 if (plane->id == PLANE_CURSOR) {
3376 src_w = pstate->base.src_w;
3377 src_h = pstate->base.src_h;
3378 dst_w = pstate->base.crtc_w;
3379 dst_h = pstate->base.crtc_h;
3380 } else {
3381 src_w = drm_rect_width(&pstate->base.src);
3382 src_h = drm_rect_height(&pstate->base.src);
3383 dst_w = drm_rect_width(&pstate->base.dst);
3384 dst_h = drm_rect_height(&pstate->base.dst);
3385 }
3386
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003387 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003388 swap(dst_w, dst_h);
3389
3390 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3391 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3392
3393 /* Provide result in 16.16 fixed point */
3394 return (uint64_t)downscale_w * downscale_h >> 16;
3395}
3396
Damien Lespiaub9cec072014-11-04 17:06:43 +00003397static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003398skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3399 const struct drm_plane_state *pstate,
3400 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003401{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003402 struct intel_plane *plane = to_intel_plane(pstate->plane);
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003403 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003404 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003405 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003406 struct drm_framebuffer *fb;
3407 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003408
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003409 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003410 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003411
3412 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003413 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003414
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003415 if (plane->id == PLANE_CURSOR)
Matt Ropera1de91e2016-05-12 07:05:57 -07003416 return 0;
3417 if (y && format != DRM_FORMAT_NV12)
3418 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003419
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003420 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3421 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003422
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003423 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003424 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003425
3426 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003427 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003428 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003429 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003430 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003431 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003432 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003433 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003434 } else {
3435 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003436 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003437 }
3438
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003439 down_scale_amount = skl_plane_downscale_amount(cstate, intel_pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003440
3441 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003442}
3443
3444/*
3445 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3446 * a 8192x4096@32bpp framebuffer:
3447 * 3 * 4096 * 8192 * 4 < 2^32
3448 */
3449static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003450skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3451 unsigned *plane_data_rate,
3452 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003453{
Matt Roper9c74d822016-05-12 07:05:58 -07003454 struct drm_crtc_state *cstate = &intel_cstate->base;
3455 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003456 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003457 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003458 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003459
3460 if (WARN_ON(!state))
3461 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003462
Matt Ropera1de91e2016-05-12 07:05:57 -07003463 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003464 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003465 enum plane_id plane_id = to_intel_plane(plane)->id;
3466 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003467
Matt Ropera6d3460e2016-05-12 07:06:04 -07003468 /* packed/uv */
3469 rate = skl_plane_relative_data_rate(intel_cstate,
3470 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003471 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003472
3473 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003474
Matt Ropera6d3460e2016-05-12 07:06:04 -07003475 /* y-plane */
3476 rate = skl_plane_relative_data_rate(intel_cstate,
3477 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003478 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003479
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003480 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003481 }
3482
3483 return total_data_rate;
3484}
3485
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003486static uint16_t
3487skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3488 const int y)
3489{
3490 struct drm_framebuffer *fb = pstate->fb;
3491 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3492 uint32_t src_w, src_h;
3493 uint32_t min_scanlines = 8;
3494 uint8_t plane_bpp;
3495
3496 if (WARN_ON(!fb))
3497 return 0;
3498
3499 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003500 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003501 return 0;
3502
3503 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003504 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3505 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003506 return 8;
3507
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003508 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3509 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003510
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003511 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003512 swap(src_w, src_h);
3513
3514 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003515 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003516 src_w /= 2;
3517 src_h /= 2;
3518 }
3519
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003520 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003521 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003522 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003523 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003524
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003525 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003526 switch (plane_bpp) {
3527 case 1:
3528 min_scanlines = 32;
3529 break;
3530 case 2:
3531 min_scanlines = 16;
3532 break;
3533 case 4:
3534 min_scanlines = 8;
3535 break;
3536 case 8:
3537 min_scanlines = 4;
3538 break;
3539 default:
3540 WARN(1, "Unsupported pixel depth %u for rotation",
3541 plane_bpp);
3542 min_scanlines = 32;
3543 }
3544 }
3545
3546 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3547}
3548
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003549static void
3550skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3551 uint16_t *minimum, uint16_t *y_minimum)
3552{
3553 const struct drm_plane_state *pstate;
3554 struct drm_plane *plane;
3555
3556 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003557 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003558
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003559 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003560 continue;
3561
3562 if (!pstate->visible)
3563 continue;
3564
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003565 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
3566 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003567 }
3568
3569 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3570}
3571
Matt Roperc107acf2016-05-12 07:06:01 -07003572static int
Matt Roper024c9042015-09-24 15:53:11 -07003573skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003574 struct skl_ddb_allocation *ddb /* out */)
3575{
Matt Roperc107acf2016-05-12 07:06:01 -07003576 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003577 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003578 struct drm_device *dev = crtc->dev;
3579 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3580 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003581 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003582 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003583 uint16_t minimum[I915_MAX_PLANES] = {};
3584 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003585 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003586 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07003587 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003588 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3589 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003590
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003591 /* Clear the partitioning for disabled planes. */
3592 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3593 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3594
Matt Ropera6d3460e2016-05-12 07:06:04 -07003595 if (WARN_ON(!state))
3596 return 0;
3597
Matt Roperc107acf2016-05-12 07:06:01 -07003598 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003599 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003600 return 0;
3601 }
3602
Matt Ropera6d3460e2016-05-12 07:06:04 -07003603 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003604 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003605 if (alloc_size == 0) {
3606 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003607 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003608 }
3609
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003610 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003611
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003612 /*
3613 * 1. Allocate the mininum required blocks for each active plane
3614 * and allocate the cursor, it doesn't require extra allocation
3615 * proportional to the data rate.
3616 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003617
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003618 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
3619 alloc_size -= minimum[plane_id];
3620 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003621 }
3622
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003623 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3624 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3625
Damien Lespiaub9cec072014-11-04 17:06:43 +00003626 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003627 * 2. Distribute the remaining space in proportion to the amount of
3628 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003629 *
3630 * FIXME: we may not allocate every single block here.
3631 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003632 total_data_rate = skl_get_total_relative_data_rate(cstate,
3633 plane_data_rate,
3634 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003635 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003636 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003637
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003638 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003639 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003640 unsigned int data_rate, y_data_rate;
3641 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003642
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003643 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003644 continue;
3645
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003646 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003647
3648 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003649 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003650 * promote the expression to 64 bits to avoid overflowing, the
3651 * result is < available as data_rate / total_data_rate < 1
3652 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003653 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003654 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3655 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003656
Matt Roperc107acf2016-05-12 07:06:01 -07003657 /* Leave disabled planes at (0,0) */
3658 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003659 ddb->plane[pipe][plane_id].start = start;
3660 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003661 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003662
3663 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003664
3665 /*
3666 * allocation for y_plane part of planar format:
3667 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003668 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003669
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003670 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07003671 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3672 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003673
Matt Roperc107acf2016-05-12 07:06:01 -07003674 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003675 ddb->y_plane[pipe][plane_id].start = start;
3676 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003677 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003678
Matt Ropera1de91e2016-05-12 07:05:57 -07003679 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003680 }
3681
Matt Roperc107acf2016-05-12 07:06:01 -07003682 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003683}
3684
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003685/*
3686 * The max latency should be 257 (max the punit can code is 255 and we add 2us
Ville Syrjäläac484962016-01-20 21:05:26 +02003687 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003688 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3689 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3690*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303691static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
3692 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003693{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303694 uint32_t wm_intermediate_val;
3695 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003696
3697 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303698 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003699
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303700 wm_intermediate_val = latency * pixel_rate * cpp;
3701 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003702 return ret;
3703}
3704
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303705static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
3706 uint32_t pipe_htotal,
3707 uint32_t latency,
3708 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003709{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003710 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303711 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003712
3713 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303714 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003715
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003716 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303717 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
3718 pipe_htotal * 1000);
3719 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003720 return ret;
3721}
3722
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003723static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3724 struct intel_plane_state *pstate)
3725{
3726 uint64_t adjusted_pixel_rate;
3727 uint64_t downscale_amount;
3728 uint64_t pixel_rate;
3729
3730 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003731 if (WARN_ON(!intel_wm_plane_visible(cstate, pstate)))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003732 return 0;
3733
3734 /*
3735 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3736 * with additional adjustments for plane-specific scaling.
3737 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003738 adjusted_pixel_rate = cstate->pixel_rate;
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003739 downscale_amount = skl_plane_downscale_amount(cstate, pstate);
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003740
3741 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3742 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3743
3744 return pixel_rate;
3745}
3746
Matt Roper55994c22016-05-12 07:06:08 -07003747static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3748 struct intel_crtc_state *cstate,
3749 struct intel_plane_state *intel_pstate,
3750 uint16_t ddb_allocation,
3751 int level,
3752 uint16_t *out_blocks, /* out */
3753 uint8_t *out_lines, /* out */
3754 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003755{
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003756 struct intel_plane *plane = to_intel_plane(intel_pstate->base.plane);
Matt Roper33815fa2016-05-12 07:06:05 -07003757 struct drm_plane_state *pstate = &intel_pstate->base;
3758 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003759 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303760 uint_fixed_16_16_t method1, method2;
3761 uint_fixed_16_16_t plane_blocks_per_line;
3762 uint_fixed_16_16_t selected_result;
3763 uint32_t interm_pbpl;
3764 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003765 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02003766 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003767 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003768 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303769 uint_fixed_16_16_t y_tile_minimum;
3770 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003771 struct intel_atomic_state *state =
3772 to_intel_atomic_state(cstate->base.state);
3773 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303774 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003775
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003776 if (latency == 0 ||
3777 !intel_wm_plane_visible(cstate, intel_pstate)) {
Matt Roper55994c22016-05-12 07:06:08 -07003778 *enabled = false;
3779 return 0;
3780 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003781
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303782 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3783 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
3784 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
3785
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05303786 /* Display WA #1141: kbl. */
3787 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
3788 latency += 4;
3789
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303790 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003791 latency += 15;
3792
Ville Syrjälä93aa2a12017-03-14 17:10:50 +02003793 if (plane->id == PLANE_CURSOR) {
3794 width = intel_pstate->base.crtc_w;
3795 height = intel_pstate->base.crtc_h;
3796 } else {
3797 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3798 height = drm_rect_height(&intel_pstate->base.src) >> 16;
3799 }
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003800
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003801 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003802 swap(width, height);
3803
Ville Syrjälä353c8592016-12-14 23:30:57 +02003804 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003805 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3806
Dave Airlie61d0a042016-10-25 16:35:20 +10003807 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003808 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02003809 fb->format->cpp[1] :
3810 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003811
3812 switch (cpp) {
3813 case 1:
3814 y_min_scanlines = 16;
3815 break;
3816 case 2:
3817 y_min_scanlines = 8;
3818 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003819 case 4:
3820 y_min_scanlines = 4;
3821 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003822 default:
3823 MISSING_CASE(cpp);
3824 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003825 }
3826 } else {
3827 y_min_scanlines = 4;
3828 }
3829
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02003830 if (apply_memory_bw_wa)
3831 y_min_scanlines *= 2;
3832
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003833 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303834 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303835 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
3836 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003837 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303838 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303839 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303840 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
3841 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303842 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303843 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
3844 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003845 }
3846
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003847 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3848 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003849 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003850 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003851 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003852
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303853 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
3854 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003855
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303856 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303857 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003858 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003859 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3860 (plane_bytes_per_line / 512 < 1))
3861 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303862 else if ((ddb_allocation /
3863 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
3864 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003865 else
3866 selected_result = method1;
3867 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003868
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303869 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
3870 res_lines = DIV_ROUND_UP(selected_result.val,
3871 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003872
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003873 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303874 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303875 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003876 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003877 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003878 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003879 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003880 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003881
Matt Roper55994c22016-05-12 07:06:08 -07003882 if (res_blocks >= ddb_allocation || res_lines > 31) {
3883 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003884
3885 /*
3886 * If there are no valid level 0 watermarks, then we can't
3887 * support this display configuration.
3888 */
3889 if (level) {
3890 return 0;
3891 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003892 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07003893
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003894 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3895 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
3896 plane->base.id, plane->name,
3897 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07003898 return -EINVAL;
3899 }
Matt Roper55994c22016-05-12 07:06:08 -07003900 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003901
3902 *out_blocks = res_blocks;
3903 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003904 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003905
Matt Roper55994c22016-05-12 07:06:08 -07003906 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003907}
3908
Matt Roperf4a96752016-05-12 07:06:06 -07003909static int
3910skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3911 struct skl_ddb_allocation *ddb,
3912 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003913 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003914 int level,
3915 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003916{
Matt Roperf4a96752016-05-12 07:06:06 -07003917 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003918 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003919 struct drm_plane *plane = &intel_plane->base;
3920 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003921 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003922 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003923 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003924
3925 if (state)
3926 intel_pstate =
3927 intel_atomic_get_existing_plane_state(state,
3928 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003929
Matt Roperf4a96752016-05-12 07:06:06 -07003930 /*
Lyudea62163e2016-10-04 14:28:20 -04003931 * Note: If we start supporting multiple pending atomic commits against
3932 * the same planes/CRTC's in the future, plane->state will no longer be
3933 * the correct pre-state to use for the calculations here and we'll
3934 * need to change where we get the 'unchanged' plane data from.
3935 *
3936 * For now this is fine because we only allow one queued commit against
3937 * a CRTC. Even if the plane isn't modified by this transaction and we
3938 * don't have a plane lock, we still have the CRTC's lock, so we know
3939 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003940 */
Lyudea62163e2016-10-04 14:28:20 -04003941 if (!intel_pstate)
3942 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003943
Lyudea62163e2016-10-04 14:28:20 -04003944 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003945
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003946 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07003947
Lyudea62163e2016-10-04 14:28:20 -04003948 ret = skl_compute_plane_wm(dev_priv,
3949 cstate,
3950 intel_pstate,
3951 ddb_blocks,
3952 level,
3953 &result->plane_res_b,
3954 &result->plane_res_l,
3955 &result->plane_en);
3956 if (ret)
3957 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003958
3959 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003960}
3961
Damien Lespiau407b50f2014-11-04 17:06:57 +00003962static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003963skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003964{
Mahesh Kumara3a89862016-12-01 21:19:34 +05303965 struct drm_atomic_state *state = cstate->base.state;
3966 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003967 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05303968 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003969
Matt Roper024c9042015-09-24 15:53:11 -07003970 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003971 return 0;
3972
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003973 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003974
3975 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003976 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003977
Mahesh Kumara3a89862016-12-01 21:19:34 +05303978 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
3979 1000, pixel_rate);
3980
3981 /* Display WA #1135: bxt. */
3982 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
3983 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
3984
3985 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003986}
3987
Matt Roper024c9042015-09-24 15:53:11 -07003988static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003989 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003990{
Matt Roper024c9042015-09-24 15:53:11 -07003991 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003992 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003993
3994 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003995 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003996}
3997
Matt Roper55994c22016-05-12 07:06:08 -07003998static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3999 struct skl_ddb_allocation *ddb,
4000 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004001{
Matt Roper024c9042015-09-24 15:53:11 -07004002 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004003 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04004004 struct intel_plane *intel_plane;
4005 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004006 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07004007 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004008
Lyudea62163e2016-10-04 14:28:20 -04004009 /*
4010 * We'll only calculate watermarks for planes that are actually
4011 * enabled, so make sure all other planes are set as disabled.
4012 */
4013 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
4014
4015 for_each_intel_plane_mask(&dev_priv->drm,
4016 intel_plane,
4017 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004018 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04004019
4020 for (level = 0; level <= max_level; level++) {
4021 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
4022 intel_plane, level,
4023 &wm->wm[level]);
4024 if (ret)
4025 return ret;
4026 }
4027 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004028 }
Matt Roper024c9042015-09-24 15:53:11 -07004029 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004030
Matt Roper55994c22016-05-12 07:06:08 -07004031 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004032}
4033
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004034static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4035 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004036 const struct skl_ddb_entry *entry)
4037{
4038 if (entry->end)
4039 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4040 else
4041 I915_WRITE(reg, 0);
4042}
4043
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004044static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4045 i915_reg_t reg,
4046 const struct skl_wm_level *level)
4047{
4048 uint32_t val = 0;
4049
4050 if (level->plane_en) {
4051 val |= PLANE_WM_EN;
4052 val |= level->plane_res_b;
4053 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4054 }
4055
4056 I915_WRITE(reg, val);
4057}
4058
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004059static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4060 const struct skl_plane_wm *wm,
4061 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004062 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004063{
4064 struct drm_crtc *crtc = &intel_crtc->base;
4065 struct drm_device *dev = crtc->dev;
4066 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004067 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004068 enum pipe pipe = intel_crtc->pipe;
4069
4070 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004071 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004072 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004073 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004074 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004075 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004076
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004077 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4078 &ddb->plane[pipe][plane_id]);
4079 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4080 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004081}
4082
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004083static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4084 const struct skl_plane_wm *wm,
4085 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004086{
4087 struct drm_crtc *crtc = &intel_crtc->base;
4088 struct drm_device *dev = crtc->dev;
4089 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004090 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004091 enum pipe pipe = intel_crtc->pipe;
4092
4093 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004094 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4095 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004096 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004097 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004098
4099 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004100 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004101}
4102
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004103bool skl_wm_level_equals(const struct skl_wm_level *l1,
4104 const struct skl_wm_level *l2)
4105{
4106 if (l1->plane_en != l2->plane_en)
4107 return false;
4108
4109 /* If both planes aren't enabled, the rest shouldn't matter */
4110 if (!l1->plane_en)
4111 return true;
4112
4113 return (l1->plane_res_l == l2->plane_res_l &&
4114 l1->plane_res_b == l2->plane_res_b);
4115}
4116
Lyude27082492016-08-24 07:48:10 +02004117static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4118 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004119{
Lyude27082492016-08-24 07:48:10 +02004120 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004121}
4122
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004123bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4124 const struct skl_ddb_entry *ddb,
4125 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004126{
Lyudece0ba282016-09-15 10:46:35 -04004127 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004128
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004129 for (i = 0; i < I915_MAX_PIPES; i++)
4130 if (i != ignore && entries[i] &&
4131 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004132 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004133
Lyude27082492016-08-24 07:48:10 +02004134 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004135}
4136
Matt Roper55994c22016-05-12 07:06:08 -07004137static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004138 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004139 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004140 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004141 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004142{
Matt Roperf4a96752016-05-12 07:06:06 -07004143 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004144 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004145
Matt Roper55994c22016-05-12 07:06:08 -07004146 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4147 if (ret)
4148 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004149
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004150 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004151 *changed = false;
4152 else
4153 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004154
Matt Roper55994c22016-05-12 07:06:08 -07004155 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004156}
4157
Matt Roper9b613022016-06-27 16:42:44 -07004158static uint32_t
4159pipes_modified(struct drm_atomic_state *state)
4160{
4161 struct drm_crtc *crtc;
4162 struct drm_crtc_state *cstate;
4163 uint32_t i, ret = 0;
4164
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004165 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004166 ret |= drm_crtc_mask(crtc);
4167
4168 return ret;
4169}
4170
Jani Nikulabb7791b2016-10-04 12:29:17 +03004171static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004172skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4173{
4174 struct drm_atomic_state *state = cstate->base.state;
4175 struct drm_device *dev = state->dev;
4176 struct drm_crtc *crtc = cstate->base.crtc;
4177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4178 struct drm_i915_private *dev_priv = to_i915(dev);
4179 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4180 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4181 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4182 struct drm_plane_state *plane_state;
4183 struct drm_plane *plane;
4184 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004185
4186 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4187
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004188 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004189 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004190
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004191 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4192 &new_ddb->plane[pipe][plane_id]) &&
4193 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4194 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004195 continue;
4196
4197 plane_state = drm_atomic_get_plane_state(state, plane);
4198 if (IS_ERR(plane_state))
4199 return PTR_ERR(plane_state);
4200 }
4201
4202 return 0;
4203}
4204
Matt Roper98d39492016-05-12 07:06:03 -07004205static int
4206skl_compute_ddb(struct drm_atomic_state *state)
4207{
4208 struct drm_device *dev = state->dev;
4209 struct drm_i915_private *dev_priv = to_i915(dev);
4210 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4211 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004212 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004213 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004214 int ret;
4215
4216 /*
4217 * If this is our first atomic update following hardware readout,
4218 * we can't trust the DDB that the BIOS programmed for us. Let's
4219 * pretend that all pipes switched active status so that we'll
4220 * ensure a full DDB recompute.
4221 */
Matt Roper1b54a882016-06-17 13:42:18 -07004222 if (dev_priv->wm.distrust_bios_wm) {
4223 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4224 state->acquire_ctx);
4225 if (ret)
4226 return ret;
4227
Matt Roper98d39492016-05-12 07:06:03 -07004228 intel_state->active_pipe_changes = ~0;
4229
Matt Roper1b54a882016-06-17 13:42:18 -07004230 /*
4231 * We usually only initialize intel_state->active_crtcs if we
4232 * we're doing a modeset; make sure this field is always
4233 * initialized during the sanitization process that happens
4234 * on the first commit too.
4235 */
4236 if (!intel_state->modeset)
4237 intel_state->active_crtcs = dev_priv->active_crtcs;
4238 }
4239
Matt Roper98d39492016-05-12 07:06:03 -07004240 /*
4241 * If the modeset changes which CRTC's are active, we need to
4242 * recompute the DDB allocation for *all* active pipes, even
4243 * those that weren't otherwise being modified in any way by this
4244 * atomic commit. Due to the shrinking of the per-pipe allocations
4245 * when new active CRTC's are added, it's possible for a pipe that
4246 * we were already using and aren't changing at all here to suddenly
4247 * become invalid if its DDB needs exceeds its new allocation.
4248 *
4249 * Note that if we wind up doing a full DDB recompute, we can't let
4250 * any other display updates race with this transaction, so we need
4251 * to grab the lock on *all* CRTC's.
4252 */
Matt Roper734fa012016-05-12 15:11:40 -07004253 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004254 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004255 intel_state->wm_results.dirty_pipes = ~0;
4256 }
Matt Roper98d39492016-05-12 07:06:03 -07004257
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004258 /*
4259 * We're not recomputing for the pipes not included in the commit, so
4260 * make sure we start with the current state.
4261 */
4262 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4263
Matt Roper98d39492016-05-12 07:06:03 -07004264 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4265 struct intel_crtc_state *cstate;
4266
4267 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4268 if (IS_ERR(cstate))
4269 return PTR_ERR(cstate);
4270
Matt Roper734fa012016-05-12 15:11:40 -07004271 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004272 if (ret)
4273 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004274
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004275 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004276 if (ret)
4277 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004278 }
4279
4280 return 0;
4281}
4282
Matt Roper2722efb2016-08-17 15:55:55 -04004283static void
4284skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4285 struct skl_wm_values *src,
4286 enum pipe pipe)
4287{
Matt Roper2722efb2016-08-17 15:55:55 -04004288 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4289 sizeof(dst->ddb.y_plane[pipe]));
4290 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4291 sizeof(dst->ddb.plane[pipe]));
4292}
4293
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004294static void
4295skl_print_wm_changes(const struct drm_atomic_state *state)
4296{
4297 const struct drm_device *dev = state->dev;
4298 const struct drm_i915_private *dev_priv = to_i915(dev);
4299 const struct intel_atomic_state *intel_state =
4300 to_intel_atomic_state(state);
4301 const struct drm_crtc *crtc;
4302 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004303 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004304 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4305 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004306 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004307
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004308 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004309 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4310 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004311
Maarten Lankhorst75704982016-11-01 12:04:10 +01004312 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004313 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004314 const struct skl_ddb_entry *old, *new;
4315
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004316 old = &old_ddb->plane[pipe][plane_id];
4317 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004318
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004319 if (skl_ddb_entry_equal(old, new))
4320 continue;
4321
Maarten Lankhorst75704982016-11-01 12:04:10 +01004322 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4323 intel_plane->base.base.id,
4324 intel_plane->base.name,
4325 old->start, old->end,
4326 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004327 }
4328 }
4329}
4330
Matt Roper98d39492016-05-12 07:06:03 -07004331static int
4332skl_compute_wm(struct drm_atomic_state *state)
4333{
4334 struct drm_crtc *crtc;
4335 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004336 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4337 struct skl_wm_values *results = &intel_state->wm_results;
4338 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004339 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004340 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004341
4342 /*
4343 * If this transaction isn't actually touching any CRTC's, don't
4344 * bother with watermark calculation. Note that if we pass this
4345 * test, we're guaranteed to hold at least one CRTC state mutex,
4346 * which means we can safely use values like dev_priv->active_crtcs
4347 * since any racing commits that want to update them would need to
4348 * hold _all_ CRTC state mutexes.
4349 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004350 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004351 changed = true;
4352 if (!changed)
4353 return 0;
4354
Matt Roper734fa012016-05-12 15:11:40 -07004355 /* Clear all dirty flags */
4356 results->dirty_pipes = 0;
4357
Matt Roper98d39492016-05-12 07:06:03 -07004358 ret = skl_compute_ddb(state);
4359 if (ret)
4360 return ret;
4361
Matt Roper734fa012016-05-12 15:11:40 -07004362 /*
4363 * Calculate WM's for all pipes that are part of this transaction.
4364 * Note that the DDB allocation above may have added more CRTC's that
4365 * weren't otherwise being modified (and set bits in dirty_pipes) if
4366 * pipe allocations had to change.
4367 *
4368 * FIXME: Now that we're doing this in the atomic check phase, we
4369 * should allow skl_update_pipe_wm() to return failure in cases where
4370 * no suitable watermark values can be found.
4371 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004372 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004373 struct intel_crtc_state *intel_cstate =
4374 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004375 const struct skl_pipe_wm *old_pipe_wm =
4376 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004377
4378 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004379 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4380 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004381 if (ret)
4382 return ret;
4383
4384 if (changed)
4385 results->dirty_pipes |= drm_crtc_mask(crtc);
4386
4387 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4388 /* This pipe's WM's did not change */
4389 continue;
4390
4391 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004392 }
4393
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004394 skl_print_wm_changes(state);
4395
Matt Roper98d39492016-05-12 07:06:03 -07004396 return 0;
4397}
4398
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004399static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4400 struct intel_crtc_state *cstate)
4401{
4402 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4403 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4404 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004405 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004406 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004407 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004408
4409 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4410 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004411
4412 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004413
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004414 for_each_plane_id_on_crtc(crtc, plane_id) {
4415 if (plane_id != PLANE_CURSOR)
4416 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4417 ddb, plane_id);
4418 else
4419 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4420 ddb);
4421 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004422}
4423
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004424static void skl_initial_wm(struct intel_atomic_state *state,
4425 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004426{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004427 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004428 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004429 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004430 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004431 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004432 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004433
Ville Syrjälä432081b2016-10-31 22:37:03 +02004434 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004435 return;
4436
Matt Roper734fa012016-05-12 15:11:40 -07004437 mutex_lock(&dev_priv->wm.wm_mutex);
4438
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004439 if (cstate->base.active_changed)
4440 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004441
4442 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004443
4444 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004445}
4446
Ville Syrjäläd8905652016-01-14 14:53:35 +02004447static void ilk_compute_wm_config(struct drm_device *dev,
4448 struct intel_wm_config *config)
4449{
4450 struct intel_crtc *crtc;
4451
4452 /* Compute the currently _active_ config */
4453 for_each_intel_crtc(dev, crtc) {
4454 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4455
4456 if (!wm->pipe_enabled)
4457 continue;
4458
4459 config->sprites_enabled |= wm->sprites_enabled;
4460 config->sprites_scaled |= wm->sprites_scaled;
4461 config->num_pipes_active++;
4462 }
4463}
4464
Matt Ropered4a6a72016-02-23 17:20:13 -08004465static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004466{
Chris Wilson91c8a322016-07-05 10:40:23 +01004467 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004468 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004469 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004470 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004471 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004472 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004473
Ville Syrjäläd8905652016-01-14 14:53:35 +02004474 ilk_compute_wm_config(dev, &config);
4475
4476 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4477 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004478
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004479 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004480 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004481 config.num_pipes_active == 1 && config.sprites_enabled) {
4482 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4483 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004484
Imre Deak820c1982013-12-17 14:46:36 +02004485 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004486 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004487 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004488 }
4489
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004490 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004491 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004492
Imre Deak820c1982013-12-17 14:46:36 +02004493 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004494
Imre Deak820c1982013-12-17 14:46:36 +02004495 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004496}
4497
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004498static void ilk_initial_watermarks(struct intel_atomic_state *state,
4499 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004500{
Matt Ropered4a6a72016-02-23 17:20:13 -08004501 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4502 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004503
Matt Ropered4a6a72016-02-23 17:20:13 -08004504 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004505 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004506 ilk_program_watermarks(dev_priv);
4507 mutex_unlock(&dev_priv->wm.wm_mutex);
4508}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004509
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004510static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4511 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004512{
4513 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4514 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4515
4516 mutex_lock(&dev_priv->wm.wm_mutex);
4517 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004518 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004519 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004520 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004521 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004522}
4523
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004524static inline void skl_wm_level_from_reg_val(uint32_t val,
4525 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004526{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004527 level->plane_en = val & PLANE_WM_EN;
4528 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4529 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4530 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004531}
4532
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004533void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4534 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004535{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004536 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004537 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004538 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004539 int level, max_level;
4540 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004541 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004542
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004543 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004544
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004545 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4546 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004547
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004548 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004549 if (plane_id != PLANE_CURSOR)
4550 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004551 else
4552 val = I915_READ(CUR_WM(pipe, level));
4553
4554 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4555 }
4556
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004557 if (plane_id != PLANE_CURSOR)
4558 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004559 else
4560 val = I915_READ(CUR_WM_TRANS(pipe));
4561
4562 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4563 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004564
Matt Roper3ef00282015-03-09 10:19:24 -07004565 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004566 return;
4567
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004568 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004569}
4570
4571void skl_wm_get_hw_state(struct drm_device *dev)
4572{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004573 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004574 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004575 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004576 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004577 struct intel_crtc *intel_crtc;
4578 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004579
Damien Lespiaua269c582014-11-04 17:06:49 +00004580 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004581 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4582 intel_crtc = to_intel_crtc(crtc);
4583 cstate = to_intel_crtc_state(crtc->state);
4584
4585 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4586
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004587 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004588 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004589 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004590
Matt Roper279e99d2016-05-12 07:06:02 -07004591 if (dev_priv->active_crtcs) {
4592 /* Fully recompute DDB on first atomic commit */
4593 dev_priv->wm.distrust_bios_wm = true;
4594 } else {
4595 /* Easy/common case; just sanitize DDB now if everything off */
4596 memset(ddb, 0, sizeof(*ddb));
4597 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004598}
4599
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004600static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4601{
4602 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004603 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004604 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004605 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004606 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004607 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004608 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004609 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004610 [PIPE_A] = WM0_PIPEA_ILK,
4611 [PIPE_B] = WM0_PIPEB_ILK,
4612 [PIPE_C] = WM0_PIPEC_IVB,
4613 };
4614
4615 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004616 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004617 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004618
Ville Syrjälä15606532016-05-13 17:55:17 +03004619 memset(active, 0, sizeof(*active));
4620
Matt Roper3ef00282015-03-09 10:19:24 -07004621 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004622
4623 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004624 u32 tmp = hw->wm_pipe[pipe];
4625
4626 /*
4627 * For active pipes LP0 watermark is marked as
4628 * enabled, and LP1+ watermaks as disabled since
4629 * we can't really reverse compute them in case
4630 * multiple pipes are active.
4631 */
4632 active->wm[0].enable = true;
4633 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4634 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4635 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4636 active->linetime = hw->wm_linetime[pipe];
4637 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004638 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004639
4640 /*
4641 * For inactive pipes, all watermark levels
4642 * should be marked as enabled but zeroed,
4643 * which is what we'd compute them to.
4644 */
4645 for (level = 0; level <= max_level; level++)
4646 active->wm[level].enable = true;
4647 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004648
4649 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004650}
4651
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004652#define _FW_WM(value, plane) \
4653 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4654#define _FW_WM_VLV(value, plane) \
4655 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4656
4657static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4658 struct vlv_wm_values *wm)
4659{
4660 enum pipe pipe;
4661 uint32_t tmp;
4662
4663 for_each_pipe(dev_priv, pipe) {
4664 tmp = I915_READ(VLV_DDL(pipe));
4665
Ville Syrjälä1b313892016-11-28 19:37:08 +02004666 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004667 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004668 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004669 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004670 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004671 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004672 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004673 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4674 }
4675
4676 tmp = I915_READ(DSPFW1);
4677 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004678 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
4679 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
4680 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004681
4682 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004683 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
4684 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
4685 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004686
4687 tmp = I915_READ(DSPFW3);
4688 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4689
4690 if (IS_CHERRYVIEW(dev_priv)) {
4691 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004692 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4693 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004694
4695 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004696 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
4697 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004698
4699 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004700 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
4701 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004702
4703 tmp = I915_READ(DSPHOWM);
4704 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004705 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4706 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4707 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
4708 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4709 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4710 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4711 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4712 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4713 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004714 } else {
4715 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004716 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4717 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004718
4719 tmp = I915_READ(DSPHOWM);
4720 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004721 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4722 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4723 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4724 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4725 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4726 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004727 }
4728}
4729
4730#undef _FW_WM
4731#undef _FW_WM_VLV
4732
4733void vlv_wm_get_hw_state(struct drm_device *dev)
4734{
4735 struct drm_i915_private *dev_priv = to_i915(dev);
4736 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02004737 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004738 u32 val;
4739
4740 vlv_read_wm_values(dev_priv, wm);
4741
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004742 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4743 wm->level = VLV_WM_LEVEL_PM2;
4744
4745 if (IS_CHERRYVIEW(dev_priv)) {
4746 mutex_lock(&dev_priv->rps.hw_lock);
4747
4748 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4749 if (val & DSP_MAXFIFO_PM5_ENABLE)
4750 wm->level = VLV_WM_LEVEL_PM5;
4751
Ville Syrjälä58590c12015-09-08 21:05:12 +03004752 /*
4753 * If DDR DVFS is disabled in the BIOS, Punit
4754 * will never ack the request. So if that happens
4755 * assume we don't have to enable/disable DDR DVFS
4756 * dynamically. To test that just set the REQ_ACK
4757 * bit to poke the Punit, but don't change the
4758 * HIGH/LOW bits so that we don't actually change
4759 * the current state.
4760 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004761 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004762 val |= FORCE_DDR_FREQ_REQ_ACK;
4763 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4764
4765 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4766 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4767 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4768 "assuming DDR DVFS is disabled\n");
4769 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4770 } else {
4771 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4772 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4773 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4774 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004775
4776 mutex_unlock(&dev_priv->rps.hw_lock);
4777 }
4778
Ville Syrjäläff32c542017-03-02 19:14:57 +02004779 for_each_intel_crtc(dev, crtc) {
4780 struct intel_crtc_state *crtc_state =
4781 to_intel_crtc_state(crtc->base.state);
4782 struct vlv_wm_state *active = &crtc->wm.active.vlv;
4783 const struct vlv_fifo_state *fifo_state =
4784 &crtc_state->wm.vlv.fifo_state;
4785 enum pipe pipe = crtc->pipe;
4786 enum plane_id plane_id;
4787 int level;
4788
4789 vlv_get_fifo_size(crtc_state);
4790
4791 active->num_levels = wm->level + 1;
4792 active->cxsr = wm->cxsr;
4793
Ville Syrjäläff32c542017-03-02 19:14:57 +02004794 for (level = 0; level < active->num_levels; level++) {
4795 struct vlv_pipe_wm *raw =
4796 &crtc_state->wm.vlv.raw[level];
4797
4798 active->sr[level].plane = wm->sr.plane;
4799 active->sr[level].cursor = wm->sr.cursor;
4800
4801 for_each_plane_id_on_crtc(crtc, plane_id) {
4802 active->wm[level].plane[plane_id] =
4803 wm->pipe[pipe].plane[plane_id];
4804
4805 raw->plane[plane_id] =
4806 vlv_invert_wm_value(active->wm[level].plane[plane_id],
4807 fifo_state->plane[plane_id]);
4808 }
4809 }
4810
4811 for_each_plane_id_on_crtc(crtc, plane_id)
4812 vlv_raw_plane_wm_set(crtc_state, level,
4813 plane_id, USHRT_MAX);
4814 vlv_invalidate_wms(crtc, active, level);
4815
4816 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02004817 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02004818
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004819 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
Ville Syrjälä1b313892016-11-28 19:37:08 +02004820 pipe_name(pipe),
4821 wm->pipe[pipe].plane[PLANE_PRIMARY],
4822 wm->pipe[pipe].plane[PLANE_CURSOR],
4823 wm->pipe[pipe].plane[PLANE_SPRITE0],
4824 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02004825 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004826
4827 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4828 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4829}
4830
Ville Syrjälä602ae832017-03-02 19:15:02 +02004831void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
4832{
4833 struct intel_plane *plane;
4834 struct intel_crtc *crtc;
4835
4836 mutex_lock(&dev_priv->wm.wm_mutex);
4837
4838 for_each_intel_plane(&dev_priv->drm, plane) {
4839 struct intel_crtc *crtc =
4840 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
4841 struct intel_crtc_state *crtc_state =
4842 to_intel_crtc_state(crtc->base.state);
4843 struct intel_plane_state *plane_state =
4844 to_intel_plane_state(plane->base.state);
4845 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
4846 const struct vlv_fifo_state *fifo_state =
4847 &crtc_state->wm.vlv.fifo_state;
4848 enum plane_id plane_id = plane->id;
4849 int level;
4850
4851 if (plane_state->base.visible)
4852 continue;
4853
4854 for (level = 0; level < wm_state->num_levels; level++) {
4855 struct vlv_pipe_wm *raw =
4856 &crtc_state->wm.vlv.raw[level];
4857
4858 raw->plane[plane_id] = 0;
4859
4860 wm_state->wm[level].plane[plane_id] =
4861 vlv_invert_wm_value(raw->plane[plane_id],
4862 fifo_state->plane[plane_id]);
4863 }
4864 }
4865
4866 for_each_intel_crtc(&dev_priv->drm, crtc) {
4867 struct intel_crtc_state *crtc_state =
4868 to_intel_crtc_state(crtc->base.state);
4869
4870 crtc_state->wm.vlv.intermediate =
4871 crtc_state->wm.vlv.optimal;
4872 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4873 }
4874
4875 vlv_program_watermarks(dev_priv);
4876
4877 mutex_unlock(&dev_priv->wm.wm_mutex);
4878}
4879
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004880void ilk_wm_get_hw_state(struct drm_device *dev)
4881{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004882 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004883 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004884 struct drm_crtc *crtc;
4885
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004886 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004887 ilk_pipe_wm_get_hw_state(crtc);
4888
4889 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4890 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4891 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4892
4893 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004894 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004895 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4896 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4897 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004898
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004899 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004900 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4901 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004902 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004903 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4904 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004905
4906 hw->enable_fbc_wm =
4907 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4908}
4909
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004910/**
4911 * intel_update_watermarks - update FIFO watermark values based on current modes
4912 *
4913 * Calculate watermark values for the various WM regs based on current mode
4914 * and plane configuration.
4915 *
4916 * There are several cases to deal with here:
4917 * - normal (i.e. non-self-refresh)
4918 * - self-refresh (SR) mode
4919 * - lines are large relative to FIFO size (buffer can hold up to 2)
4920 * - lines are small relative to FIFO size (buffer can hold more than 2
4921 * lines), so need to account for TLB latency
4922 *
4923 * The normal calculation is:
4924 * watermark = dotclock * bytes per pixel * latency
4925 * where latency is platform & configuration dependent (we assume pessimal
4926 * values here).
4927 *
4928 * The SR calculation is:
4929 * watermark = (trunc(latency/line time)+1) * surface width *
4930 * bytes per pixel
4931 * where
4932 * line time = htotal / dotclock
4933 * surface width = hdisplay for normal plane and 64 for cursor
4934 * and latency is assumed to be high, as above.
4935 *
4936 * The final value programmed to the register should always be rounded up,
4937 * and include an extra 2 entries to account for clock crossings.
4938 *
4939 * We don't use the sprite, so we can ignore that. And on Crestline we have
4940 * to set the non-SR watermarks to 8.
4941 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004942void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004943{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004944 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004945
4946 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004947 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004948}
4949
Jani Nikulae2828912016-01-18 09:19:47 +02004950/*
Daniel Vetter92703882012-08-09 16:46:01 +02004951 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004952 */
4953DEFINE_SPINLOCK(mchdev_lock);
4954
4955/* Global for IPS driver to get at the current i915 device. Protected by
4956 * mchdev_lock. */
4957static struct drm_i915_private *i915_mch_dev;
4958
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004959bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004960{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004961 u16 rgvswctl;
4962
Chris Wilson67520412017-03-02 13:28:01 +00004963 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02004964
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004965 rgvswctl = I915_READ16(MEMSWCTL);
4966 if (rgvswctl & MEMCTL_CMD_STS) {
4967 DRM_DEBUG("gpu busy, RCS change rejected\n");
4968 return false; /* still busy with another command */
4969 }
4970
4971 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4972 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4973 I915_WRITE16(MEMSWCTL, rgvswctl);
4974 POSTING_READ16(MEMSWCTL);
4975
4976 rgvswctl |= MEMCTL_CMD_STS;
4977 I915_WRITE16(MEMSWCTL, rgvswctl);
4978
4979 return true;
4980}
4981
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004982static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004983{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004984 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004985 u8 fmax, fmin, fstart, vstart;
4986
Daniel Vetter92703882012-08-09 16:46:01 +02004987 spin_lock_irq(&mchdev_lock);
4988
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004989 rgvmodectl = I915_READ(MEMMODECTL);
4990
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004991 /* Enable temp reporting */
4992 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4993 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4994
4995 /* 100ms RC evaluation intervals */
4996 I915_WRITE(RCUPEI, 100000);
4997 I915_WRITE(RCDNEI, 100000);
4998
4999 /* Set max/min thresholds to 90ms and 80ms respectively */
5000 I915_WRITE(RCBMAXAVG, 90000);
5001 I915_WRITE(RCBMINAVG, 80000);
5002
5003 I915_WRITE(MEMIHYST, 1);
5004
5005 /* Set up min, max, and cur for interrupt handling */
5006 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
5007 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
5008 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
5009 MEMMODE_FSTART_SHIFT;
5010
Ville Syrjälä616847e2015-09-18 20:03:19 +03005011 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005012 PXVFREQ_PX_SHIFT;
5013
Daniel Vetter20e4d402012-08-08 23:35:39 +02005014 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
5015 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005016
Daniel Vetter20e4d402012-08-08 23:35:39 +02005017 dev_priv->ips.max_delay = fstart;
5018 dev_priv->ips.min_delay = fmin;
5019 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005020
5021 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
5022 fmax, fmin, fstart);
5023
5024 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
5025
5026 /*
5027 * Interrupts will be enabled in ironlake_irq_postinstall
5028 */
5029
5030 I915_WRITE(VIDSTART, vstart);
5031 POSTING_READ(VIDSTART);
5032
5033 rgvmodectl |= MEMMODE_SWMODE_EN;
5034 I915_WRITE(MEMMODECTL, rgvmodectl);
5035
Daniel Vetter92703882012-08-09 16:46:01 +02005036 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005037 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005038 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005039
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005040 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005041
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005042 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5043 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005044 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005045 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005046 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005047
5048 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005049}
5050
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005051static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005052{
Daniel Vetter92703882012-08-09 16:46:01 +02005053 u16 rgvswctl;
5054
5055 spin_lock_irq(&mchdev_lock);
5056
5057 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005058
5059 /* Ack interrupts, disable EFC interrupt */
5060 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5061 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5062 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5063 I915_WRITE(DEIIR, DE_PCU_EVENT);
5064 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5065
5066 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005067 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005068 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005069 rgvswctl |= MEMCTL_CMD_STS;
5070 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005071 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005072
Daniel Vetter92703882012-08-09 16:46:01 +02005073 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005074}
5075
Daniel Vetteracbe9472012-07-26 11:50:05 +02005076/* There's a funny hw issue where the hw returns all 0 when reading from
5077 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5078 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5079 * all limits and the gpu stuck at whatever frequency it is at atm).
5080 */
Akash Goel74ef1172015-03-06 11:07:19 +05305081static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005082{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005083 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005084
Daniel Vetter20b46e52012-07-26 11:16:14 +02005085 /* Only set the down limit when we've reached the lowest level to avoid
5086 * getting more interrupts, otherwise leave this clear. This prevents a
5087 * race in the hw when coming out of rc6: There's a tiny window where
5088 * the hw runs at the minimal clock before selecting the desired
5089 * frequency, if the down threshold expires in that window we will not
5090 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005091 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305092 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5093 if (val <= dev_priv->rps.min_freq_softlimit)
5094 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5095 } else {
5096 limits = dev_priv->rps.max_freq_softlimit << 24;
5097 if (val <= dev_priv->rps.min_freq_softlimit)
5098 limits |= dev_priv->rps.min_freq_softlimit << 16;
5099 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005100
5101 return limits;
5102}
5103
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005104static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5105{
5106 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305107 u32 threshold_up = 0, threshold_down = 0; /* in % */
5108 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005109
5110 new_power = dev_priv->rps.power;
5111 switch (dev_priv->rps.power) {
5112 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005113 if (val > dev_priv->rps.efficient_freq + 1 &&
5114 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005115 new_power = BETWEEN;
5116 break;
5117
5118 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005119 if (val <= dev_priv->rps.efficient_freq &&
5120 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005121 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005122 else if (val >= dev_priv->rps.rp0_freq &&
5123 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005124 new_power = HIGH_POWER;
5125 break;
5126
5127 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005128 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5129 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005130 new_power = BETWEEN;
5131 break;
5132 }
5133 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005134 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005135 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005136 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005137 new_power = HIGH_POWER;
5138 if (new_power == dev_priv->rps.power)
5139 return;
5140
5141 /* Note the units here are not exactly 1us, but 1280ns. */
5142 switch (new_power) {
5143 case LOW_POWER:
5144 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305145 ei_up = 16000;
5146 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005147
5148 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305149 ei_down = 32000;
5150 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005151 break;
5152
5153 case BETWEEN:
5154 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305155 ei_up = 13000;
5156 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005157
5158 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305159 ei_down = 32000;
5160 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005161 break;
5162
5163 case HIGH_POWER:
5164 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305165 ei_up = 10000;
5166 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005167
5168 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305169 ei_down = 32000;
5170 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005171 break;
5172 }
5173
Mika Kuoppala6067a272017-02-15 15:52:59 +02005174 /* When byt can survive without system hang with dynamic
5175 * sw freq adjustments, this restriction can be lifted.
5176 */
5177 if (IS_VALLEYVIEW(dev_priv))
5178 goto skip_hw_write;
5179
Akash Goel8a586432015-03-06 11:07:18 +05305180 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005181 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305182 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005183 GT_INTERVAL_FROM_US(dev_priv,
5184 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305185
5186 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005187 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305188 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005189 GT_INTERVAL_FROM_US(dev_priv,
5190 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305191
Chris Wilsona72b5622016-07-02 15:35:59 +01005192 I915_WRITE(GEN6_RP_CONTROL,
5193 GEN6_RP_MEDIA_TURBO |
5194 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5195 GEN6_RP_MEDIA_IS_GFX |
5196 GEN6_RP_ENABLE |
5197 GEN6_RP_UP_BUSY_AVG |
5198 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305199
Mika Kuoppala6067a272017-02-15 15:52:59 +02005200skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005201 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005202 dev_priv->rps.up_threshold = threshold_up;
5203 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005204 dev_priv->rps.last_adj = 0;
5205}
5206
Chris Wilson2876ce72014-03-28 08:03:34 +00005207static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5208{
5209 u32 mask = 0;
5210
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005211 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005212 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005213 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005214 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005215 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005216
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005217 mask &= dev_priv->pm_rps_events;
5218
Imre Deak59d02a12014-12-19 19:33:26 +02005219 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005220}
5221
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005222/* gen6_set_rps is called to update the frequency request, but should also be
5223 * called when the range (min_delay and max_delay) is modified so that we can
5224 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005225static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005226{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005227 /* min/max delay may still have been modified so be sure to
5228 * write the limits value.
5229 */
5230 if (val != dev_priv->rps.cur_freq) {
5231 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005232
Chris Wilsondc979972016-05-10 14:10:04 +01005233 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305234 I915_WRITE(GEN6_RPNSWREQ,
5235 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005236 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005237 I915_WRITE(GEN6_RPNSWREQ,
5238 HSW_FREQUENCY(val));
5239 else
5240 I915_WRITE(GEN6_RPNSWREQ,
5241 GEN6_FREQUENCY(val) |
5242 GEN6_OFFSET(0) |
5243 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005244 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005245
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005246 /* Make sure we continue to get interrupts
5247 * until we hit the minimum or maximum frequencies.
5248 */
Akash Goel74ef1172015-03-06 11:07:19 +05305249 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005250 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005251
Ben Widawskyb39fb292014-03-19 18:31:11 -07005252 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005253 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005254
5255 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005256}
5257
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005258static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005259{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005260 int err;
5261
Chris Wilsondc979972016-05-10 14:10:04 +01005262 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005263 "Odd GPU freq value\n"))
5264 val &= ~1;
5265
Deepak Scd25dd52015-07-10 18:31:40 +05305266 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5267
Chris Wilson8fb55192015-04-07 16:20:28 +01005268 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005269 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5270 if (err)
5271 return err;
5272
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005273 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005274 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005275
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005276 dev_priv->rps.cur_freq = val;
5277 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005278
5279 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005280}
5281
Deepak Sa7f6e232015-05-09 18:04:44 +05305282/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305283 *
5284 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305285 * 1. Forcewake Media well.
5286 * 2. Request idle freq.
5287 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305288*/
5289static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5290{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005291 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005292 int err;
Deepak S5549d252014-06-28 11:26:11 +05305293
Chris Wilsonaed242f2015-03-18 09:48:21 +00005294 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305295 return;
5296
Chris Wilsonc9efef72017-01-02 15:28:45 +00005297 /* The punit delays the write of the frequency and voltage until it
5298 * determines the GPU is awake. During normal usage we don't want to
5299 * waste power changing the frequency if the GPU is sleeping (rc6).
5300 * However, the GPU and driver is now idle and we do not want to delay
5301 * switching to minimum voltage (reducing power whilst idle) as we do
5302 * not expect to be woken in the near future and so must flush the
5303 * change by waking the device.
5304 *
5305 * We choose to take the media powerwell (either would do to trick the
5306 * punit into committing the voltage change) as that takes a lot less
5307 * power than the render powerwell.
5308 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305309 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005310 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305311 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005312
5313 if (err)
5314 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305315}
5316
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005317void gen6_rps_busy(struct drm_i915_private *dev_priv)
5318{
5319 mutex_lock(&dev_priv->rps.hw_lock);
5320 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005321 u8 freq;
5322
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005323 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005324 gen6_rps_reset_ei(dev_priv);
5325 I915_WRITE(GEN6_PMINTRMSK,
5326 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005327
Chris Wilsonc33d2472016-07-04 08:08:36 +01005328 gen6_enable_rps_interrupts(dev_priv);
5329
Chris Wilsonbd648182017-02-10 15:03:48 +00005330 /* Use the user's desired frequency as a guide, but for better
5331 * performance, jump directly to RPe as our starting frequency.
5332 */
5333 freq = max(dev_priv->rps.cur_freq,
5334 dev_priv->rps.efficient_freq);
5335
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005336 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005337 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005338 dev_priv->rps.min_freq_softlimit,
5339 dev_priv->rps.max_freq_softlimit)))
5340 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005341 }
5342 mutex_unlock(&dev_priv->rps.hw_lock);
5343}
5344
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005345void gen6_rps_idle(struct drm_i915_private *dev_priv)
5346{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005347 /* Flush our bottom-half so that it does not race with us
5348 * setting the idle frequency and so that it is bounded by
5349 * our rpm wakeref. And then disable the interrupts to stop any
5350 * futher RPS reclocking whilst we are asleep.
5351 */
5352 gen6_disable_rps_interrupts(dev_priv);
5353
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005354 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005355 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005356 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305357 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005358 else
Chris Wilsondc979972016-05-10 14:10:04 +01005359 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005360 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005361 I915_WRITE(GEN6_PMINTRMSK,
5362 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005363 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005364 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005365
Chris Wilson8d3afd72015-05-21 21:01:47 +01005366 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005367 while (!list_empty(&dev_priv->rps.clients))
5368 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005369 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005370}
5371
Chris Wilson1854d5c2015-04-07 16:20:32 +01005372void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005373 struct intel_rps_client *rps,
5374 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005375{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005376 /* This is intentionally racy! We peek at the state here, then
5377 * validate inside the RPS worker.
5378 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005379 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005380 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005381 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005382 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005383
Chris Wilsone61b9952015-04-27 13:41:24 +01005384 /* Force a RPS boost (and don't count it against the client) if
5385 * the GPU is severely congested.
5386 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005387 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005388 rps = NULL;
5389
Chris Wilson8d3afd72015-05-21 21:01:47 +01005390 spin_lock(&dev_priv->rps.client_lock);
5391 if (rps == NULL || list_empty(&rps->link)) {
5392 spin_lock_irq(&dev_priv->irq_lock);
5393 if (dev_priv->rps.interrupts_enabled) {
5394 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005395 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005396 }
5397 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005398
Chris Wilson2e1b8732015-04-27 13:41:22 +01005399 if (rps != NULL) {
5400 list_add(&rps->link, &dev_priv->rps.clients);
5401 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005402 } else
5403 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005404 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005405 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005406}
5407
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005408int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005409{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005410 int err;
5411
Chris Wilsoncfd1c482017-02-20 09:47:07 +00005412 lockdep_assert_held(&dev_priv->rps.hw_lock);
5413 GEM_BUG_ON(val > dev_priv->rps.max_freq);
5414 GEM_BUG_ON(val < dev_priv->rps.min_freq);
5415
Chris Wilson76e4e4b2017-02-20 09:47:08 +00005416 if (!dev_priv->rps.enabled) {
5417 dev_priv->rps.cur_freq = val;
5418 return 0;
5419 }
5420
Chris Wilsondc979972016-05-10 14:10:04 +01005421 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005422 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005423 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005424 err = gen6_set_rps(dev_priv, val);
5425
5426 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005427}
5428
Chris Wilsondc979972016-05-10 14:10:04 +01005429static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005430{
Zhe Wang20e49362014-11-04 17:07:05 +00005431 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005432 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005433}
5434
Chris Wilsondc979972016-05-10 14:10:04 +01005435static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305436{
Akash Goel2030d682016-04-23 00:05:45 +05305437 I915_WRITE(GEN6_RP_CONTROL, 0);
5438}
5439
Chris Wilsondc979972016-05-10 14:10:04 +01005440static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005441{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005442 I915_WRITE(GEN6_RC_CONTROL, 0);
5443 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305444 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005445}
5446
Chris Wilsondc979972016-05-10 14:10:04 +01005447static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305448{
Deepak S38807742014-05-23 21:00:15 +05305449 I915_WRITE(GEN6_RC_CONTROL, 0);
5450}
5451
Chris Wilsondc979972016-05-10 14:10:04 +01005452static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005453{
Deepak S98a2e5f2014-08-18 10:35:27 -07005454 /* we're doing forcewake before Disabling RC6,
5455 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005456 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005457
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005458 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005459
Mika Kuoppala59bad942015-01-16 11:34:40 +02005460 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005461}
5462
Chris Wilsondc979972016-05-10 14:10:04 +01005463static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005464{
Chris Wilsondc979972016-05-10 14:10:04 +01005465 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005466 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5467 mode = GEN6_RC_CTL_RC6_ENABLE;
5468 else
5469 mode = 0;
5470 }
Chris Wilsondc979972016-05-10 14:10:04 +01005471 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005472 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5473 "RC6 %s RC6p %s RC6pp %s\n",
5474 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5475 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5476 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005477
5478 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005479 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5480 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005481}
5482
Chris Wilsondc979972016-05-10 14:10:04 +01005483static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305484{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005485 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305486 bool enable_rc6 = true;
5487 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005488 u32 rc_ctl;
5489 int rc_sw_target;
5490
5491 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5492 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5493 RC_SW_TARGET_STATE_SHIFT;
5494 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5495 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5496 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5497 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5498 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305499
5500 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005501 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305502 enable_rc6 = false;
5503 }
5504
5505 /*
5506 * The exact context size is not known for BXT, so assume a page size
5507 * for this check.
5508 */
5509 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005510 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5511 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5512 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005513 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305514 enable_rc6 = false;
5515 }
5516
5517 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5518 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5519 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5520 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005521 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305522 enable_rc6 = false;
5523 }
5524
Imre Deakfc619842016-06-29 19:13:55 +03005525 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5526 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5527 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5528 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5529 enable_rc6 = false;
5530 }
5531
5532 if (!I915_READ(GEN6_GFXPAUSE)) {
5533 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5534 enable_rc6 = false;
5535 }
5536
5537 if (!I915_READ(GEN8_MISC_CTRL0)) {
5538 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305539 enable_rc6 = false;
5540 }
5541
5542 return enable_rc6;
5543}
5544
Chris Wilsondc979972016-05-10 14:10:04 +01005545int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005546{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005547 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005548 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005549 return 0;
5550
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305551 if (!enable_rc6)
5552 return 0;
5553
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005554 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305555 DRM_INFO("RC6 disabled by BIOS\n");
5556 return 0;
5557 }
5558
Daniel Vetter456470e2012-08-08 23:35:40 +02005559 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005560 if (enable_rc6 >= 0) {
5561 int mask;
5562
Chris Wilsondc979972016-05-10 14:10:04 +01005563 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005564 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5565 INTEL_RC6pp_ENABLE;
5566 else
5567 mask = INTEL_RC6_ENABLE;
5568
5569 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005570 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5571 "(requested %d, valid %d)\n",
5572 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005573
5574 return enable_rc6 & mask;
5575 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005576
Chris Wilsondc979972016-05-10 14:10:04 +01005577 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005578 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005579
5580 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005581}
5582
Chris Wilsondc979972016-05-10 14:10:04 +01005583static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005584{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005585 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005586
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005587 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005588 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005589 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005590 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5591 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5592 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5593 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005594 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005595 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5596 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5597 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5598 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005599 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005600 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005601
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005602 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005603 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005604 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005605 u32 ddcc_status = 0;
5606
5607 if (sandybridge_pcode_read(dev_priv,
5608 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5609 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005610 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005611 clamp_t(u8,
5612 ((ddcc_status >> 8) & 0xff),
5613 dev_priv->rps.min_freq,
5614 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005615 }
5616
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005617 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305618 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005619 * the natural hardware unit for SKL
5620 */
Akash Goelc5e06882015-06-29 14:50:19 +05305621 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5622 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5623 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5624 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5625 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5626 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005627}
5628
Chris Wilson3a45b052016-07-13 09:10:32 +01005629static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005630 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01005631{
5632 u8 freq = dev_priv->rps.cur_freq;
5633
5634 /* force a reset */
5635 dev_priv->rps.power = -1;
5636 dev_priv->rps.cur_freq = -1;
5637
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005638 if (set(dev_priv, freq))
5639 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01005640}
5641
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005642/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005643static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005644{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005645 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5646
Akash Goel0beb0592015-03-06 11:07:20 +05305647 /* Program defaults and thresholds for RPS*/
5648 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5649 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005650
Akash Goel0beb0592015-03-06 11:07:20 +05305651 /* 1 second timeout*/
5652 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5653 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5654
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005655 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005656
Akash Goel0beb0592015-03-06 11:07:20 +05305657 /* Leaning on the below call to gen6_set_rps to program/setup the
5658 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5659 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005660 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005661
5662 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5663}
5664
Chris Wilsondc979972016-05-10 14:10:04 +01005665static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005666{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005667 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305668 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005669 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005670
5671 /* 1a: Software RC state - RC0 */
5672 I915_WRITE(GEN6_RC_STATE, 0);
5673
5674 /* 1b: Get forcewake during program sequence. Although the driver
5675 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005676 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005677
5678 /* 2a: Disable RC states. */
5679 I915_WRITE(GEN6_RC_CONTROL, 0);
5680
5681 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305682
5683 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005684 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305685 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5686 else
5687 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005688 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5689 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305690 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005691 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305692
Dave Gordon1a3d1892016-05-13 15:36:30 +01005693 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305694 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5695
Zhe Wang20e49362014-11-04 17:07:05 +00005696 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005697
Zhe Wang38c23522015-01-20 12:23:04 +00005698 /* 2c: Program Coarse Power Gating Policies. */
5699 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5700 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5701
Zhe Wang20e49362014-11-04 17:07:05 +00005702 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005703 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005704 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005705 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00005706 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
5707 I915_WRITE(GEN6_RC_CONTROL,
5708 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00005709
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305710 /*
5711 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305712 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305713 */
Chris Wilsondc979972016-05-10 14:10:04 +01005714 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305715 I915_WRITE(GEN9_PG_ENABLE, 0);
5716 else
5717 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5718 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005719
Mika Kuoppala59bad942015-01-16 11:34:40 +02005720 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005721}
5722
Chris Wilsondc979972016-05-10 14:10:04 +01005723static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005724{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005725 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305726 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005727 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005728
5729 /* 1a: Software RC state - RC0 */
5730 I915_WRITE(GEN6_RC_STATE, 0);
5731
5732 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5733 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005734 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005735
5736 /* 2a: Disable RC states. */
5737 I915_WRITE(GEN6_RC_CONTROL, 0);
5738
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005739 /* 2b: Program RC6 thresholds.*/
5740 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5741 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5742 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305743 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005744 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005745 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005746 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005747 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5748 else
5749 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005750
5751 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005752 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005753 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005754 intel_print_rc6_info(dev_priv, rc6_mask);
5755 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005756 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5757 GEN7_RC_CTL_TO_MODE |
5758 rc6_mask);
5759 else
5760 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5761 GEN6_RC_CTL_EI_MODE(1) |
5762 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005763
5764 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005765 I915_WRITE(GEN6_RPNSWREQ,
5766 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5767 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5768 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005769 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5770 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005771
Daniel Vetter7526ed72014-09-29 15:07:19 +02005772 /* Docs recommend 900MHz, and 300 MHz respectively */
5773 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5774 dev_priv->rps.max_freq_softlimit << 24 |
5775 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005776
Daniel Vetter7526ed72014-09-29 15:07:19 +02005777 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5778 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5779 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5780 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005781
Daniel Vetter7526ed72014-09-29 15:07:19 +02005782 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005783
5784 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005785 I915_WRITE(GEN6_RP_CONTROL,
5786 GEN6_RP_MEDIA_TURBO |
5787 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5788 GEN6_RP_MEDIA_IS_GFX |
5789 GEN6_RP_ENABLE |
5790 GEN6_RP_UP_BUSY_AVG |
5791 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005792
Daniel Vetter7526ed72014-09-29 15:07:19 +02005793 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005794
Chris Wilson3a45b052016-07-13 09:10:32 +01005795 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005796
Mika Kuoppala59bad942015-01-16 11:34:40 +02005797 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005798}
5799
Chris Wilsondc979972016-05-10 14:10:04 +01005800static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005801{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005802 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305803 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005804 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005805 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005806 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005807 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005808
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005809 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005810
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005811 /* Here begins a magic sequence of register writes to enable
5812 * auto-downclocking.
5813 *
5814 * Perhaps there might be some value in exposing these to
5815 * userspace...
5816 */
5817 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005818
5819 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005820 gtfifodbg = I915_READ(GTFIFODBG);
5821 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005822 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5823 I915_WRITE(GTFIFODBG, gtfifodbg);
5824 }
5825
Mika Kuoppala59bad942015-01-16 11:34:40 +02005826 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005827
5828 /* disable the counters and set deterministic thresholds */
5829 I915_WRITE(GEN6_RC_CONTROL, 0);
5830
5831 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5832 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5833 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5834 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5835 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5836
Akash Goel3b3f1652016-10-13 22:44:48 +05305837 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005838 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005839
5840 I915_WRITE(GEN6_RC_SLEEP, 0);
5841 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005842 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005843 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5844 else
5845 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005846 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005847 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5848
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005849 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005850 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005851 if (rc6_mode & INTEL_RC6_ENABLE)
5852 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5853
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005854 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005855 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005856 if (rc6_mode & INTEL_RC6p_ENABLE)
5857 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005858
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005859 if (rc6_mode & INTEL_RC6pp_ENABLE)
5860 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5861 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005862
Chris Wilsondc979972016-05-10 14:10:04 +01005863 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005864
5865 I915_WRITE(GEN6_RC_CONTROL,
5866 rc6_mask |
5867 GEN6_RC_CTL_EI_MODE(1) |
5868 GEN6_RC_CTL_HW_ENABLE);
5869
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005870 /* Power down if completely idle for over 50ms */
5871 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005872 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005873
Chris Wilson3a45b052016-07-13 09:10:32 +01005874 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005875
Ben Widawsky31643d52012-09-26 10:34:01 -07005876 rc6vids = 0;
5877 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005878 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005879 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005880 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005881 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5882 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5883 rc6vids &= 0xffff00;
5884 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5885 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5886 if (ret)
5887 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5888 }
5889
Mika Kuoppala59bad942015-01-16 11:34:40 +02005890 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005891}
5892
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005893static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005894{
5895 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005896 unsigned int gpu_freq;
5897 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305898 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005899 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005900 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005901
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005902 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005903
Ben Widawskyeda79642013-10-07 17:15:48 -03005904 policy = cpufreq_cpu_get(0);
5905 if (policy) {
5906 max_ia_freq = policy->cpuinfo.max_freq;
5907 cpufreq_cpu_put(policy);
5908 } else {
5909 /*
5910 * Default to measured freq if none found, PCU will ensure we
5911 * don't go over
5912 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005913 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005914 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005915
5916 /* Convert from kHz to MHz */
5917 max_ia_freq /= 1000;
5918
Ben Widawsky153b4b952013-10-22 22:05:09 -07005919 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005920 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5921 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005922
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005923 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305924 /* Convert GT frequency to 50 HZ units */
5925 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5926 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5927 } else {
5928 min_gpu_freq = dev_priv->rps.min_freq;
5929 max_gpu_freq = dev_priv->rps.max_freq;
5930 }
5931
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005932 /*
5933 * For each potential GPU frequency, load a ring frequency we'd like
5934 * to use for memory access. We do this by specifying the IA frequency
5935 * the PCU should use as a reference to determine the ring frequency.
5936 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305937 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5938 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005939 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005940
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005941 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305942 /*
5943 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5944 * No floor required for ring frequency on SKL.
5945 */
5946 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005947 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005948 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5949 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005950 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005951 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005952 ring_freq = max(min_ring_freq, ring_freq);
5953 /* leave ia_freq as the default, chosen by cpufreq */
5954 } else {
5955 /* On older processors, there is no separate ring
5956 * clock domain, so in order to boost the bandwidth
5957 * of the ring, we need to upclock the CPU (ia_freq).
5958 *
5959 * For GPU frequencies less than 750MHz,
5960 * just use the lowest ring freq.
5961 */
5962 if (gpu_freq < min_freq)
5963 ia_freq = 800;
5964 else
5965 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5966 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5967 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005968
Ben Widawsky42c05262012-09-26 10:34:00 -07005969 sandybridge_pcode_write(dev_priv,
5970 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005971 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5972 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5973 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005974 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005975}
5976
Ville Syrjälä03af2042014-06-28 02:03:53 +03005977static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305978{
5979 u32 val, rp0;
5980
Jani Nikula5b5929c2015-10-07 11:17:46 +03005981 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305982
Imre Deak43b67992016-08-31 19:13:02 +03005983 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005984 case 8:
5985 /* (2 * 4) config */
5986 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5987 break;
5988 case 12:
5989 /* (2 * 6) config */
5990 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5991 break;
5992 case 16:
5993 /* (2 * 8) config */
5994 default:
5995 /* Setting (2 * 8) Min RP0 for any other combination */
5996 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5997 break;
Deepak S095acd52015-01-17 11:05:59 +05305998 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005999
6000 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
6001
Deepak S2b6b3a02014-05-27 15:59:30 +05306002 return rp0;
6003}
6004
6005static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6006{
6007 u32 val, rpe;
6008
6009 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
6010 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
6011
6012 return rpe;
6013}
6014
Deepak S7707df42014-07-12 18:46:14 +05306015static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
6016{
6017 u32 val, rp1;
6018
Jani Nikula5b5929c2015-10-07 11:17:46 +03006019 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
6020 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
6021
Deepak S7707df42014-07-12 18:46:14 +05306022 return rp1;
6023}
6024
Deepak S96676fe2016-08-12 18:46:41 +05306025static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
6026{
6027 u32 val, rpn;
6028
6029 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
6030 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
6031 FB_GFX_FREQ_FUSE_MASK);
6032
6033 return rpn;
6034}
6035
Deepak Sf8f2b002014-07-10 13:16:21 +05306036static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6037{
6038 u32 val, rp1;
6039
6040 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6041
6042 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6043
6044 return rp1;
6045}
6046
Ville Syrjälä03af2042014-06-28 02:03:53 +03006047static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006048{
6049 u32 val, rp0;
6050
Jani Nikula64936252013-05-22 15:36:20 +03006051 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006052
6053 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6054 /* Clamp to max */
6055 rp0 = min_t(u32, rp0, 0xea);
6056
6057 return rp0;
6058}
6059
6060static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6061{
6062 u32 val, rpe;
6063
Jani Nikula64936252013-05-22 15:36:20 +03006064 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006065 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006066 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006067 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6068
6069 return rpe;
6070}
6071
Ville Syrjälä03af2042014-06-28 02:03:53 +03006072static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006073{
Imre Deak36146032014-12-04 18:39:35 +02006074 u32 val;
6075
6076 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6077 /*
6078 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6079 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6080 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6081 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6082 * to make sure it matches what Punit accepts.
6083 */
6084 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006085}
6086
Imre Deakae484342014-03-31 15:10:44 +03006087/* Check that the pctx buffer wasn't move under us. */
6088static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6089{
6090 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6091
6092 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6093 dev_priv->vlv_pctx->stolen->start);
6094}
6095
Deepak S38807742014-05-23 21:00:15 +05306096
6097/* Check that the pcbr address is not empty. */
6098static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6099{
6100 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6101
6102 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6103}
6104
Chris Wilsondc979972016-05-10 14:10:04 +01006105static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306106{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006107 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006108 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306109 u32 pcbr;
6110 int pctx_size = 32*1024;
6111
Deepak S38807742014-05-23 21:00:15 +05306112 pcbr = I915_READ(VLV_PCBR);
6113 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006114 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306115 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006116 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306117
6118 pctx_paddr = (paddr & (~4095));
6119 I915_WRITE(VLV_PCBR, pctx_paddr);
6120 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006121
6122 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306123}
6124
Chris Wilsondc979972016-05-10 14:10:04 +01006125static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006126{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006127 struct drm_i915_gem_object *pctx;
6128 unsigned long pctx_paddr;
6129 u32 pcbr;
6130 int pctx_size = 24*1024;
6131
6132 pcbr = I915_READ(VLV_PCBR);
6133 if (pcbr) {
6134 /* BIOS set it up already, grab the pre-alloc'd space */
6135 int pcbr_offset;
6136
6137 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006138 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006139 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006140 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006141 pctx_size);
6142 goto out;
6143 }
6144
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006145 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6146
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006147 /*
6148 * From the Gunit register HAS:
6149 * The Gfx driver is expected to program this register and ensure
6150 * proper allocation within Gfx stolen memory. For example, this
6151 * register should be programmed such than the PCBR range does not
6152 * overlap with other ranges, such as the frame buffer, protected
6153 * memory, or any other relevant ranges.
6154 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006155 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006156 if (!pctx) {
6157 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006158 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006159 }
6160
6161 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6162 I915_WRITE(VLV_PCBR, pctx_paddr);
6163
6164out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006165 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006166 dev_priv->vlv_pctx = pctx;
6167}
6168
Chris Wilsondc979972016-05-10 14:10:04 +01006169static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006170{
Imre Deakae484342014-03-31 15:10:44 +03006171 if (WARN_ON(!dev_priv->vlv_pctx))
6172 return;
6173
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006174 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006175 dev_priv->vlv_pctx = NULL;
6176}
6177
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006178static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6179{
6180 dev_priv->rps.gpll_ref_freq =
6181 vlv_get_cck_clock(dev_priv, "GPLL ref",
6182 CCK_GPLL_CLOCK_CONTROL,
6183 dev_priv->czclk_freq);
6184
6185 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6186 dev_priv->rps.gpll_ref_freq);
6187}
6188
Chris Wilsondc979972016-05-10 14:10:04 +01006189static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006190{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006191 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006192
Chris Wilsondc979972016-05-10 14:10:04 +01006193 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006194
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006195 vlv_init_gpll_ref_freq(dev_priv);
6196
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006197 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6198 switch ((val >> 6) & 3) {
6199 case 0:
6200 case 1:
6201 dev_priv->mem_freq = 800;
6202 break;
6203 case 2:
6204 dev_priv->mem_freq = 1066;
6205 break;
6206 case 3:
6207 dev_priv->mem_freq = 1333;
6208 break;
6209 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006210 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006211
Imre Deak4e805192014-04-14 20:24:41 +03006212 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6213 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6214 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006215 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006216 dev_priv->rps.max_freq);
6217
6218 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6219 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006220 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006221 dev_priv->rps.efficient_freq);
6222
Deepak Sf8f2b002014-07-10 13:16:21 +05306223 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6224 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006225 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306226 dev_priv->rps.rp1_freq);
6227
Imre Deak4e805192014-04-14 20:24:41 +03006228 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6229 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006230 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006231 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006232}
6233
Chris Wilsondc979972016-05-10 14:10:04 +01006234static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306235{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006236 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306237
Chris Wilsondc979972016-05-10 14:10:04 +01006238 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306239
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006240 vlv_init_gpll_ref_freq(dev_priv);
6241
Ville Syrjäläa5805162015-05-26 20:42:30 +03006242 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006243 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006244 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006245
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006246 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006247 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006248 dev_priv->mem_freq = 2000;
6249 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006250 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006251 dev_priv->mem_freq = 1600;
6252 break;
6253 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006254 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006255
Deepak S2b6b3a02014-05-27 15:59:30 +05306256 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6257 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6258 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006259 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306260 dev_priv->rps.max_freq);
6261
6262 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6263 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006264 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306265 dev_priv->rps.efficient_freq);
6266
Deepak S7707df42014-07-12 18:46:14 +05306267 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6268 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006269 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306270 dev_priv->rps.rp1_freq);
6271
Deepak S96676fe2016-08-12 18:46:41 +05306272 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306273 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006274 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306275 dev_priv->rps.min_freq);
6276
Ville Syrjälä1c147622014-08-18 14:42:43 +03006277 WARN_ONCE((dev_priv->rps.max_freq |
6278 dev_priv->rps.efficient_freq |
6279 dev_priv->rps.rp1_freq |
6280 dev_priv->rps.min_freq) & 1,
6281 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306282}
6283
Chris Wilsondc979972016-05-10 14:10:04 +01006284static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006285{
Chris Wilsondc979972016-05-10 14:10:04 +01006286 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006287}
6288
Chris Wilsondc979972016-05-10 14:10:04 +01006289static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306290{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006291 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306292 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306293 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306294
6295 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6296
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006297 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6298 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306299 if (gtfifodbg) {
6300 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6301 gtfifodbg);
6302 I915_WRITE(GTFIFODBG, gtfifodbg);
6303 }
6304
6305 cherryview_check_pctx(dev_priv);
6306
6307 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6308 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006309 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306310
Ville Syrjälä160614a2015-01-19 13:50:47 +02006311 /* Disable RC states. */
6312 I915_WRITE(GEN6_RC_CONTROL, 0);
6313
Deepak S38807742014-05-23 21:00:15 +05306314 /* 2a: Program RC6 thresholds.*/
6315 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6316 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6317 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6318
Akash Goel3b3f1652016-10-13 22:44:48 +05306319 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006320 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306321 I915_WRITE(GEN6_RC_SLEEP, 0);
6322
Deepak Sf4f71c72015-03-28 15:23:35 +05306323 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6324 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306325
6326 /* allows RC6 residency counter to work */
6327 I915_WRITE(VLV_COUNTER_CONTROL,
6328 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6329 VLV_MEDIA_RC6_COUNT_EN |
6330 VLV_RENDER_RC6_COUNT_EN));
6331
6332 /* For now we assume BIOS is allocating and populating the PCBR */
6333 pcbr = I915_READ(VLV_PCBR);
6334
Deepak S38807742014-05-23 21:00:15 +05306335 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006336 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6337 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006338 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306339
6340 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6341
Deepak S2b6b3a02014-05-27 15:59:30 +05306342 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006343 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306344 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6345 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6346 I915_WRITE(GEN6_RP_UP_EI, 66000);
6347 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6348
6349 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6350
6351 /* 5: Enable RPS */
6352 I915_WRITE(GEN6_RP_CONTROL,
6353 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006354 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306355 GEN6_RP_ENABLE |
6356 GEN6_RP_UP_BUSY_AVG |
6357 GEN6_RP_DOWN_IDLE_AVG);
6358
Deepak S3ef62342015-04-29 08:36:24 +05306359 /* Setting Fixed Bias */
6360 val = VLV_OVERRIDE_EN |
6361 VLV_SOC_TDP_EN |
6362 CHV_BIAS_CPU_50_SOC_50;
6363 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6364
Deepak S2b6b3a02014-05-27 15:59:30 +05306365 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6366
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006367 /* RPS code assumes GPLL is used */
6368 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6369
Jani Nikula742f4912015-09-03 11:16:09 +03006370 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306371 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6372
Chris Wilson3a45b052016-07-13 09:10:32 +01006373 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306374
Mika Kuoppala59bad942015-01-16 11:34:40 +02006375 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306376}
6377
Chris Wilsondc979972016-05-10 14:10:04 +01006378static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006379{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006380 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306381 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006382 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006383
6384 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6385
Imre Deakae484342014-03-31 15:10:44 +03006386 valleyview_check_pctx(dev_priv);
6387
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006388 gtfifodbg = I915_READ(GTFIFODBG);
6389 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006390 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6391 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006392 I915_WRITE(GTFIFODBG, gtfifodbg);
6393 }
6394
Deepak Sc8d9a592013-11-23 14:55:42 +05306395 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006396 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006397
Ville Syrjälä160614a2015-01-19 13:50:47 +02006398 /* Disable RC states. */
6399 I915_WRITE(GEN6_RC_CONTROL, 0);
6400
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006401 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006402 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6403 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6404 I915_WRITE(GEN6_RP_UP_EI, 66000);
6405 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6406
6407 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6408
6409 I915_WRITE(GEN6_RP_CONTROL,
6410 GEN6_RP_MEDIA_TURBO |
6411 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6412 GEN6_RP_MEDIA_IS_GFX |
6413 GEN6_RP_ENABLE |
6414 GEN6_RP_UP_BUSY_AVG |
6415 GEN6_RP_DOWN_IDLE_CONT);
6416
6417 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6418 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6419 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6420
Akash Goel3b3f1652016-10-13 22:44:48 +05306421 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006422 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006423
Jesse Barnes2f0aa302013-11-15 09:32:11 -08006424 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006425
6426 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006427 I915_WRITE(VLV_COUNTER_CONTROL,
Mika Kuoppala6b7f6aa2017-03-15 18:12:59 +02006428 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6429 VLV_MEDIA_RC0_COUNT_EN |
Deepak S31685c22014-07-03 17:33:01 -04006430 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006431 VLV_MEDIA_RC6_COUNT_EN |
6432 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006433
Chris Wilsondc979972016-05-10 14:10:04 +01006434 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006435 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006436
Chris Wilsondc979972016-05-10 14:10:04 +01006437 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006438
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006439 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006440
Deepak S3ef62342015-04-29 08:36:24 +05306441 /* Setting Fixed Bias */
6442 val = VLV_OVERRIDE_EN |
6443 VLV_SOC_TDP_EN |
6444 VLV_BIAS_CPU_125_SOC_875;
6445 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6446
Jani Nikula64936252013-05-22 15:36:20 +03006447 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006448
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006449 /* RPS code assumes GPLL is used */
6450 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6451
Jani Nikula742f4912015-09-03 11:16:09 +03006452 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006453 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6454
Chris Wilson3a45b052016-07-13 09:10:32 +01006455 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006456
Mika Kuoppala59bad942015-01-16 11:34:40 +02006457 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006458}
6459
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006460static unsigned long intel_pxfreq(u32 vidfreq)
6461{
6462 unsigned long freq;
6463 int div = (vidfreq & 0x3f0000) >> 16;
6464 int post = (vidfreq & 0x3000) >> 12;
6465 int pre = (vidfreq & 0x7);
6466
6467 if (!pre)
6468 return 0;
6469
6470 freq = ((div * 133333) / ((1<<post) * pre));
6471
6472 return freq;
6473}
6474
Daniel Vettereb48eb02012-04-26 23:28:12 +02006475static const struct cparams {
6476 u16 i;
6477 u16 t;
6478 u16 m;
6479 u16 c;
6480} cparams[] = {
6481 { 1, 1333, 301, 28664 },
6482 { 1, 1066, 294, 24460 },
6483 { 1, 800, 294, 25192 },
6484 { 0, 1333, 276, 27605 },
6485 { 0, 1066, 276, 27605 },
6486 { 0, 800, 231, 23784 },
6487};
6488
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006489static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006490{
6491 u64 total_count, diff, ret;
6492 u32 count1, count2, count3, m = 0, c = 0;
6493 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6494 int i;
6495
Chris Wilson67520412017-03-02 13:28:01 +00006496 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006497
Daniel Vetter20e4d402012-08-08 23:35:39 +02006498 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006499
6500 /* Prevent division-by-zero if we are asking too fast.
6501 * Also, we don't get interesting results if we are polling
6502 * faster than once in 10ms, so just return the saved value
6503 * in such cases.
6504 */
6505 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006506 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006507
6508 count1 = I915_READ(DMIEC);
6509 count2 = I915_READ(DDREC);
6510 count3 = I915_READ(CSIEC);
6511
6512 total_count = count1 + count2 + count3;
6513
6514 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006515 if (total_count < dev_priv->ips.last_count1) {
6516 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006517 diff += total_count;
6518 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006519 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006520 }
6521
6522 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006523 if (cparams[i].i == dev_priv->ips.c_m &&
6524 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006525 m = cparams[i].m;
6526 c = cparams[i].c;
6527 break;
6528 }
6529 }
6530
6531 diff = div_u64(diff, diff1);
6532 ret = ((m * diff) + c);
6533 ret = div_u64(ret, 10);
6534
Daniel Vetter20e4d402012-08-08 23:35:39 +02006535 dev_priv->ips.last_count1 = total_count;
6536 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006537
Daniel Vetter20e4d402012-08-08 23:35:39 +02006538 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006539
6540 return ret;
6541}
6542
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006543unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6544{
6545 unsigned long val;
6546
Chris Wilsondc979972016-05-10 14:10:04 +01006547 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006548 return 0;
6549
6550 spin_lock_irq(&mchdev_lock);
6551
6552 val = __i915_chipset_val(dev_priv);
6553
6554 spin_unlock_irq(&mchdev_lock);
6555
6556 return val;
6557}
6558
Daniel Vettereb48eb02012-04-26 23:28:12 +02006559unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6560{
6561 unsigned long m, x, b;
6562 u32 tsfs;
6563
6564 tsfs = I915_READ(TSFS);
6565
6566 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6567 x = I915_READ8(TR1);
6568
6569 b = tsfs & TSFS_INTR_MASK;
6570
6571 return ((m * x) / 127) - b;
6572}
6573
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006574static int _pxvid_to_vd(u8 pxvid)
6575{
6576 if (pxvid == 0)
6577 return 0;
6578
6579 if (pxvid >= 8 && pxvid < 31)
6580 pxvid = 31;
6581
6582 return (pxvid + 2) * 125;
6583}
6584
6585static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006586{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006587 const int vd = _pxvid_to_vd(pxvid);
6588 const int vm = vd - 1125;
6589
Chris Wilsondc979972016-05-10 14:10:04 +01006590 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006591 return vm > 0 ? vm : 0;
6592
6593 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006594}
6595
Daniel Vetter02d71952012-08-09 16:44:54 +02006596static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006597{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006598 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006599 u32 count;
6600
Chris Wilson67520412017-03-02 13:28:01 +00006601 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006602
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006603 now = ktime_get_raw_ns();
6604 diffms = now - dev_priv->ips.last_time2;
6605 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006606
6607 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006608 if (!diffms)
6609 return;
6610
6611 count = I915_READ(GFXEC);
6612
Daniel Vetter20e4d402012-08-08 23:35:39 +02006613 if (count < dev_priv->ips.last_count2) {
6614 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006615 diff += count;
6616 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006617 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006618 }
6619
Daniel Vetter20e4d402012-08-08 23:35:39 +02006620 dev_priv->ips.last_count2 = count;
6621 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006622
6623 /* More magic constants... */
6624 diff = diff * 1181;
6625 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006626 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006627}
6628
Daniel Vetter02d71952012-08-09 16:44:54 +02006629void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6630{
Chris Wilsondc979972016-05-10 14:10:04 +01006631 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006632 return;
6633
Daniel Vetter92703882012-08-09 16:46:01 +02006634 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006635
6636 __i915_update_gfx_val(dev_priv);
6637
Daniel Vetter92703882012-08-09 16:46:01 +02006638 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006639}
6640
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006641static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006642{
6643 unsigned long t, corr, state1, corr2, state2;
6644 u32 pxvid, ext_v;
6645
Chris Wilson67520412017-03-02 13:28:01 +00006646 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006647
Ville Syrjälä616847e2015-09-18 20:03:19 +03006648 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006649 pxvid = (pxvid >> 24) & 0x7f;
6650 ext_v = pvid_to_extvid(dev_priv, pxvid);
6651
6652 state1 = ext_v;
6653
6654 t = i915_mch_val(dev_priv);
6655
6656 /* Revel in the empirically derived constants */
6657
6658 /* Correction factor in 1/100000 units */
6659 if (t > 80)
6660 corr = ((t * 2349) + 135940);
6661 else if (t >= 50)
6662 corr = ((t * 964) + 29317);
6663 else /* < 50 */
6664 corr = ((t * 301) + 1004);
6665
6666 corr = corr * ((150142 * state1) / 10000 - 78642);
6667 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006668 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006669
6670 state2 = (corr2 * state1) / 10000;
6671 state2 /= 100; /* convert to mW */
6672
Daniel Vetter02d71952012-08-09 16:44:54 +02006673 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006674
Daniel Vetter20e4d402012-08-08 23:35:39 +02006675 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006676}
6677
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006678unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6679{
6680 unsigned long val;
6681
Chris Wilsondc979972016-05-10 14:10:04 +01006682 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006683 return 0;
6684
6685 spin_lock_irq(&mchdev_lock);
6686
6687 val = __i915_gfx_val(dev_priv);
6688
6689 spin_unlock_irq(&mchdev_lock);
6690
6691 return val;
6692}
6693
Daniel Vettereb48eb02012-04-26 23:28:12 +02006694/**
6695 * i915_read_mch_val - return value for IPS use
6696 *
6697 * Calculate and return a value for the IPS driver to use when deciding whether
6698 * we have thermal and power headroom to increase CPU or GPU power budget.
6699 */
6700unsigned long i915_read_mch_val(void)
6701{
6702 struct drm_i915_private *dev_priv;
6703 unsigned long chipset_val, graphics_val, ret = 0;
6704
Daniel Vetter92703882012-08-09 16:46:01 +02006705 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006706 if (!i915_mch_dev)
6707 goto out_unlock;
6708 dev_priv = i915_mch_dev;
6709
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006710 chipset_val = __i915_chipset_val(dev_priv);
6711 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006712
6713 ret = chipset_val + graphics_val;
6714
6715out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006716 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006717
6718 return ret;
6719}
6720EXPORT_SYMBOL_GPL(i915_read_mch_val);
6721
6722/**
6723 * i915_gpu_raise - raise GPU frequency limit
6724 *
6725 * Raise the limit; IPS indicates we have thermal headroom.
6726 */
6727bool i915_gpu_raise(void)
6728{
6729 struct drm_i915_private *dev_priv;
6730 bool ret = true;
6731
Daniel Vetter92703882012-08-09 16:46:01 +02006732 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006733 if (!i915_mch_dev) {
6734 ret = false;
6735 goto out_unlock;
6736 }
6737 dev_priv = i915_mch_dev;
6738
Daniel Vetter20e4d402012-08-08 23:35:39 +02006739 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6740 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006741
6742out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006743 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006744
6745 return ret;
6746}
6747EXPORT_SYMBOL_GPL(i915_gpu_raise);
6748
6749/**
6750 * i915_gpu_lower - lower GPU frequency limit
6751 *
6752 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6753 * frequency maximum.
6754 */
6755bool i915_gpu_lower(void)
6756{
6757 struct drm_i915_private *dev_priv;
6758 bool ret = true;
6759
Daniel Vetter92703882012-08-09 16:46:01 +02006760 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006761 if (!i915_mch_dev) {
6762 ret = false;
6763 goto out_unlock;
6764 }
6765 dev_priv = i915_mch_dev;
6766
Daniel Vetter20e4d402012-08-08 23:35:39 +02006767 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6768 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006769
6770out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006771 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006772
6773 return ret;
6774}
6775EXPORT_SYMBOL_GPL(i915_gpu_lower);
6776
6777/**
6778 * i915_gpu_busy - indicate GPU business to IPS
6779 *
6780 * Tell the IPS driver whether or not the GPU is busy.
6781 */
6782bool i915_gpu_busy(void)
6783{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006784 bool ret = false;
6785
Daniel Vetter92703882012-08-09 16:46:01 +02006786 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006787 if (i915_mch_dev)
6788 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006789 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006790
6791 return ret;
6792}
6793EXPORT_SYMBOL_GPL(i915_gpu_busy);
6794
6795/**
6796 * i915_gpu_turbo_disable - disable graphics turbo
6797 *
6798 * Disable graphics turbo by resetting the max frequency and setting the
6799 * current frequency to the default.
6800 */
6801bool i915_gpu_turbo_disable(void)
6802{
6803 struct drm_i915_private *dev_priv;
6804 bool ret = true;
6805
Daniel Vetter92703882012-08-09 16:46:01 +02006806 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006807 if (!i915_mch_dev) {
6808 ret = false;
6809 goto out_unlock;
6810 }
6811 dev_priv = i915_mch_dev;
6812
Daniel Vetter20e4d402012-08-08 23:35:39 +02006813 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006814
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006815 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006816 ret = false;
6817
6818out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006819 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006820
6821 return ret;
6822}
6823EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6824
6825/**
6826 * Tells the intel_ips driver that the i915 driver is now loaded, if
6827 * IPS got loaded first.
6828 *
6829 * This awkward dance is so that neither module has to depend on the
6830 * other in order for IPS to do the appropriate communication of
6831 * GPU turbo limits to i915.
6832 */
6833static void
6834ips_ping_for_i915_load(void)
6835{
6836 void (*link)(void);
6837
6838 link = symbol_get(ips_link_to_i915_driver);
6839 if (link) {
6840 link();
6841 symbol_put(ips_link_to_i915_driver);
6842 }
6843}
6844
6845void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6846{
Daniel Vetter02d71952012-08-09 16:44:54 +02006847 /* We only register the i915 ips part with intel-ips once everything is
6848 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006849 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006850 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006851 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006852
6853 ips_ping_for_i915_load();
6854}
6855
6856void intel_gpu_ips_teardown(void)
6857{
Daniel Vetter92703882012-08-09 16:46:01 +02006858 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006859 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006860 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006861}
Deepak S76c3552f2014-01-30 23:08:16 +05306862
Chris Wilsondc979972016-05-10 14:10:04 +01006863static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006864{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006865 u32 lcfuse;
6866 u8 pxw[16];
6867 int i;
6868
6869 /* Disable to program */
6870 I915_WRITE(ECR, 0);
6871 POSTING_READ(ECR);
6872
6873 /* Program energy weights for various events */
6874 I915_WRITE(SDEW, 0x15040d00);
6875 I915_WRITE(CSIEW0, 0x007f0000);
6876 I915_WRITE(CSIEW1, 0x1e220004);
6877 I915_WRITE(CSIEW2, 0x04000004);
6878
6879 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006880 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006881 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006882 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006883
6884 /* Program P-state weights to account for frequency power adjustment */
6885 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006886 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006887 unsigned long freq = intel_pxfreq(pxvidfreq);
6888 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6889 PXVFREQ_PX_SHIFT;
6890 unsigned long val;
6891
6892 val = vid * vid;
6893 val *= (freq / 1000);
6894 val *= 255;
6895 val /= (127*127*900);
6896 if (val > 0xff)
6897 DRM_ERROR("bad pxval: %ld\n", val);
6898 pxw[i] = val;
6899 }
6900 /* Render standby states get 0 weight */
6901 pxw[14] = 0;
6902 pxw[15] = 0;
6903
6904 for (i = 0; i < 4; i++) {
6905 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6906 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006907 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006908 }
6909
6910 /* Adjust magic regs to magic values (more experimental results) */
6911 I915_WRITE(OGW0, 0);
6912 I915_WRITE(OGW1, 0);
6913 I915_WRITE(EG0, 0x00007f00);
6914 I915_WRITE(EG1, 0x0000000e);
6915 I915_WRITE(EG2, 0x000e0000);
6916 I915_WRITE(EG3, 0x68000300);
6917 I915_WRITE(EG4, 0x42000000);
6918 I915_WRITE(EG5, 0x00140031);
6919 I915_WRITE(EG6, 0);
6920 I915_WRITE(EG7, 0);
6921
6922 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006923 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006924
6925 /* Enable PMON + select events */
6926 I915_WRITE(ECR, 0x80000019);
6927
6928 lcfuse = I915_READ(LCFUSE02);
6929
Daniel Vetter20e4d402012-08-08 23:35:39 +02006930 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006931}
6932
Chris Wilsondc979972016-05-10 14:10:04 +01006933void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006934{
Imre Deakb268c692015-12-15 20:10:31 +02006935 /*
6936 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6937 * requirement.
6938 */
6939 if (!i915.enable_rc6) {
6940 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6941 intel_runtime_pm_get(dev_priv);
6942 }
Imre Deake6069ca2014-04-18 16:01:02 +03006943
Chris Wilsonb5163db2016-08-10 13:58:24 +01006944 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006945 mutex_lock(&dev_priv->rps.hw_lock);
6946
6947 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006948 if (IS_CHERRYVIEW(dev_priv))
6949 cherryview_init_gt_powersave(dev_priv);
6950 else if (IS_VALLEYVIEW(dev_priv))
6951 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006952 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006953 gen6_init_rps_frequencies(dev_priv);
6954
6955 /* Derive initial user preferences/limits from the hardware limits */
6956 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6957 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6958
6959 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6960 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6961
6962 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6963 dev_priv->rps.min_freq_softlimit =
6964 max_t(int,
6965 dev_priv->rps.efficient_freq,
6966 intel_freq_opcode(dev_priv, 450));
6967
Chris Wilson99ac9612016-07-13 09:10:34 +01006968 /* After setting max-softlimit, find the overclock max freq */
6969 if (IS_GEN6(dev_priv) ||
6970 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6971 u32 params = 0;
6972
6973 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6974 if (params & BIT(31)) { /* OC supported */
6975 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6976 (dev_priv->rps.max_freq & 0xff) * 50,
6977 (params & 0xff) * 50);
6978 dev_priv->rps.max_freq = params & 0xff;
6979 }
6980 }
6981
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006982 /* Finally allow us to boost to max by default */
6983 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6984
Chris Wilson773ea9a2016-07-13 09:10:33 +01006985 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006986 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006987
6988 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006989}
6990
Chris Wilsondc979972016-05-10 14:10:04 +01006991void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006992{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006993 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006994 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006995
6996 if (!i915.enable_rc6)
6997 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006998}
6999
Chris Wilson54b4f682016-07-21 21:16:19 +01007000/**
7001 * intel_suspend_gt_powersave - suspend PM work and helper threads
7002 * @dev_priv: i915 device
7003 *
7004 * We don't want to disable RC6 or other features here, we just want
7005 * to make sure any work we've queued has finished and won't bother
7006 * us while we're suspended.
7007 */
7008void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
7009{
7010 if (INTEL_GEN(dev_priv) < 6)
7011 return;
7012
7013 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
7014 intel_runtime_pm_put(dev_priv);
7015
7016 /* gen6_rps_idle() will be called later to disable interrupts */
7017}
7018
Chris Wilsonb7137e02016-07-13 09:10:37 +01007019void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
7020{
7021 dev_priv->rps.enabled = true; /* force disabling */
7022 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007023
7024 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07007025}
7026
Chris Wilsondc979972016-05-10 14:10:04 +01007027void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02007028{
Chris Wilsonb7137e02016-07-13 09:10:37 +01007029 if (!READ_ONCE(dev_priv->rps.enabled))
7030 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07007031
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007032 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007033
Chris Wilsonb7137e02016-07-13 09:10:37 +01007034 if (INTEL_GEN(dev_priv) >= 9) {
7035 gen9_disable_rc6(dev_priv);
7036 gen9_disable_rps(dev_priv);
7037 } else if (IS_CHERRYVIEW(dev_priv)) {
7038 cherryview_disable_rps(dev_priv);
7039 } else if (IS_VALLEYVIEW(dev_priv)) {
7040 valleyview_disable_rps(dev_priv);
7041 } else if (INTEL_GEN(dev_priv) >= 6) {
7042 gen6_disable_rps(dev_priv);
7043 } else if (IS_IRONLAKE_M(dev_priv)) {
7044 ironlake_disable_drps(dev_priv);
7045 }
7046
7047 dev_priv->rps.enabled = false;
7048 mutex_unlock(&dev_priv->rps.hw_lock);
7049}
7050
7051void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7052{
Chris Wilson54b4f682016-07-21 21:16:19 +01007053 /* We shouldn't be disabling as we submit, so this should be less
7054 * racy than it appears!
7055 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007056 if (READ_ONCE(dev_priv->rps.enabled))
7057 return;
7058
7059 /* Powersaving is controlled by the host when inside a VM */
7060 if (intel_vgpu_active(dev_priv))
7061 return;
7062
7063 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007064
Chris Wilsondc979972016-05-10 14:10:04 +01007065 if (IS_CHERRYVIEW(dev_priv)) {
7066 cherryview_enable_rps(dev_priv);
7067 } else if (IS_VALLEYVIEW(dev_priv)) {
7068 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007069 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007070 gen9_enable_rc6(dev_priv);
7071 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007072 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007073 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007074 } else if (IS_BROADWELL(dev_priv)) {
7075 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007076 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007077 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007078 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007079 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007080 } else if (IS_IRONLAKE_M(dev_priv)) {
7081 ironlake_enable_drps(dev_priv);
7082 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007083 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007084
7085 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7086 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7087
7088 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7089 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7090
Chris Wilson54b4f682016-07-21 21:16:19 +01007091 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007092 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007093}
Imre Deakc6df39b2014-04-14 20:24:29 +03007094
Chris Wilson54b4f682016-07-21 21:16:19 +01007095static void __intel_autoenable_gt_powersave(struct work_struct *work)
7096{
7097 struct drm_i915_private *dev_priv =
7098 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7099 struct intel_engine_cs *rcs;
7100 struct drm_i915_gem_request *req;
7101
7102 if (READ_ONCE(dev_priv->rps.enabled))
7103 goto out;
7104
Akash Goel3b3f1652016-10-13 22:44:48 +05307105 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007106 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007107 goto out;
7108
7109 if (!rcs->init_context)
7110 goto out;
7111
7112 mutex_lock(&dev_priv->drm.struct_mutex);
7113
7114 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7115 if (IS_ERR(req))
7116 goto unlock;
7117
7118 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7119 rcs->init_context(req);
7120
7121 /* Mark the device busy, calling intel_enable_gt_powersave() */
Chris Wilsone642c852017-03-17 11:47:09 +00007122 i915_add_request(req);
Chris Wilson54b4f682016-07-21 21:16:19 +01007123
7124unlock:
7125 mutex_unlock(&dev_priv->drm.struct_mutex);
7126out:
7127 intel_runtime_pm_put(dev_priv);
7128}
7129
7130void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7131{
7132 if (READ_ONCE(dev_priv->rps.enabled))
7133 return;
7134
7135 if (IS_IRONLAKE_M(dev_priv)) {
7136 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007137 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007138 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7139 /*
7140 * PCU communication is slow and this doesn't need to be
7141 * done at any specific time, so do this out of our fast path
7142 * to make resume and init faster.
7143 *
7144 * We depend on the HW RC6 power context save/restore
7145 * mechanism when entering D3 through runtime PM suspend. So
7146 * disable RPM until RPS/RC6 is properly setup. We can only
7147 * get here via the driver load/system resume/runtime resume
7148 * paths, so the _noresume version is enough (and in case of
7149 * runtime resume it's necessary).
7150 */
7151 if (queue_delayed_work(dev_priv->wq,
7152 &dev_priv->rps.autoenable_work,
7153 round_jiffies_up_relative(HZ)))
7154 intel_runtime_pm_get_noresume(dev_priv);
7155 }
7156}
7157
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007158static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007159{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007160 /*
7161 * On Ibex Peak and Cougar Point, we need to disable clock
7162 * gating for the panel power sequencer or it will fail to
7163 * start up when no ports are active.
7164 */
7165 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7166}
7167
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007168static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007169{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007170 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007171
Damien Lespiau055e3932014-08-18 13:49:10 +01007172 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007173 I915_WRITE(DSPCNTR(pipe),
7174 I915_READ(DSPCNTR(pipe)) |
7175 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007176
7177 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7178 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007179 }
7180}
7181
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007182static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007183{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007184 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7185 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7186 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7187
7188 /*
7189 * Don't touch WM1S_LP_EN here.
7190 * Doing so could cause underruns.
7191 */
7192}
7193
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007194static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007195{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007196 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007197
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007198 /*
7199 * Required for FBC
7200 * WaFbcDisableDpfcClockGating:ilk
7201 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007202 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7203 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7204 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007205
7206 I915_WRITE(PCH_3DCGDIS0,
7207 MARIUNIT_CLOCK_GATE_DISABLE |
7208 SVSMUNIT_CLOCK_GATE_DISABLE);
7209 I915_WRITE(PCH_3DCGDIS1,
7210 VFMUNIT_CLOCK_GATE_DISABLE);
7211
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007212 /*
7213 * According to the spec the following bits should be set in
7214 * order to enable memory self-refresh
7215 * The bit 22/21 of 0x42004
7216 * The bit 5 of 0x42020
7217 * The bit 15 of 0x45000
7218 */
7219 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7220 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7221 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007222 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007223 I915_WRITE(DISP_ARB_CTL,
7224 (I915_READ(DISP_ARB_CTL) |
7225 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007226
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007227 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007228
7229 /*
7230 * Based on the document from hardware guys the following bits
7231 * should be set unconditionally in order to enable FBC.
7232 * The bit 22 of 0x42000
7233 * The bit 22 of 0x42004
7234 * The bit 7,8,9 of 0x42020.
7235 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007236 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007237 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007238 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7239 I915_READ(ILK_DISPLAY_CHICKEN1) |
7240 ILK_FBCQ_DIS);
7241 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7242 I915_READ(ILK_DISPLAY_CHICKEN2) |
7243 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007244 }
7245
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007246 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7247
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007248 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7249 I915_READ(ILK_DISPLAY_CHICKEN2) |
7250 ILK_ELPIN_409_SELECT);
7251 I915_WRITE(_3D_CHICKEN2,
7252 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7253 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007254
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007255 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007256 I915_WRITE(CACHE_MODE_0,
7257 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007258
Akash Goel4e046322014-04-04 17:14:38 +05307259 /* WaDisable_RenderCache_OperationalFlush:ilk */
7260 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7261
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007262 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007263
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007264 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007265}
7266
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007267static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007268{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007269 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007270 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007271
7272 /*
7273 * On Ibex Peak and Cougar Point, we need to disable clock
7274 * gating for the panel power sequencer or it will fail to
7275 * start up when no ports are active.
7276 */
Jesse Barnescd664072013-10-02 10:34:19 -07007277 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7278 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7279 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007280 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7281 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007282 /* The below fixes the weird display corruption, a few pixels shifted
7283 * downward, on (only) LVDS of some HP laptops with IVY.
7284 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007285 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007286 val = I915_READ(TRANS_CHICKEN2(pipe));
7287 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7288 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007289 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007290 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007291 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7292 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7293 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007294 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7295 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007296 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007297 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007298 I915_WRITE(TRANS_CHICKEN1(pipe),
7299 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7300 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007301}
7302
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007303static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007304{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007305 uint32_t tmp;
7306
7307 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007308 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7309 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7310 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007311}
7312
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007313static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007314{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007315 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007316
Damien Lespiau231e54f2012-10-19 17:55:41 +01007317 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007318
7319 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7320 I915_READ(ILK_DISPLAY_CHICKEN2) |
7321 ILK_ELPIN_409_SELECT);
7322
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007323 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007324 I915_WRITE(_3D_CHICKEN,
7325 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7326
Akash Goel4e046322014-04-04 17:14:38 +05307327 /* WaDisable_RenderCache_OperationalFlush:snb */
7328 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7329
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007330 /*
7331 * BSpec recoomends 8x4 when MSAA is used,
7332 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007333 *
7334 * Note that PS/WM thread counts depend on the WIZ hashing
7335 * disable bit, which we don't touch here, but it's good
7336 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007337 */
7338 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007339 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007340
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007341 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007342
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007343 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007344 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007345
7346 I915_WRITE(GEN6_UCGCTL1,
7347 I915_READ(GEN6_UCGCTL1) |
7348 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7349 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7350
7351 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7352 * gating disable must be set. Failure to set it results in
7353 * flickering pixels due to Z write ordering failures after
7354 * some amount of runtime in the Mesa "fire" demo, and Unigine
7355 * Sanctuary and Tropics, and apparently anything else with
7356 * alpha test or pixel discard.
7357 *
7358 * According to the spec, bit 11 (RCCUNIT) must also be set,
7359 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007360 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007361 * WaDisableRCCUnitClockGating:snb
7362 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007363 */
7364 I915_WRITE(GEN6_UCGCTL2,
7365 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7366 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7367
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007368 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007369 I915_WRITE(_3D_CHICKEN3,
7370 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007371
7372 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007373 * Bspec says:
7374 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7375 * 3DSTATE_SF number of SF output attributes is more than 16."
7376 */
7377 I915_WRITE(_3D_CHICKEN3,
7378 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7379
7380 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007381 * According to the spec the following bits should be
7382 * set in order to enable memory self-refresh and fbc:
7383 * The bit21 and bit22 of 0x42000
7384 * The bit21 and bit22 of 0x42004
7385 * The bit5 and bit7 of 0x42020
7386 * The bit14 of 0x70180
7387 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007388 *
7389 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007390 */
7391 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7392 I915_READ(ILK_DISPLAY_CHICKEN1) |
7393 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7394 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7395 I915_READ(ILK_DISPLAY_CHICKEN2) |
7396 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007397 I915_WRITE(ILK_DSPCLK_GATE_D,
7398 I915_READ(ILK_DSPCLK_GATE_D) |
7399 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7400 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007401
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007402 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007403
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007404 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007405
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007406 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007407}
7408
7409static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7410{
7411 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7412
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007413 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007414 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007415 *
7416 * This actually overrides the dispatch
7417 * mode for all thread types.
7418 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007419 reg &= ~GEN7_FF_SCHED_MASK;
7420 reg |= GEN7_FF_TS_SCHED_HW;
7421 reg |= GEN7_FF_VS_SCHED_HW;
7422 reg |= GEN7_FF_DS_SCHED_HW;
7423
7424 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7425}
7426
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007427static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007428{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007429 /*
7430 * TODO: this bit should only be enabled when really needed, then
7431 * disabled when not needed anymore in order to save power.
7432 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007433 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007434 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7435 I915_READ(SOUTH_DSPCLK_GATE_D) |
7436 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007437
7438 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007439 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7440 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007441 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007442}
7443
Ville Syrjälä712bf362016-10-31 22:37:23 +02007444static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007445{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007446 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007447 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7448
7449 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7450 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7451 }
7452}
7453
Imre Deak450174f2016-05-03 15:54:21 +03007454static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7455 int general_prio_credits,
7456 int high_prio_credits)
7457{
7458 u32 misccpctl;
7459
7460 /* WaTempDisableDOPClkGating:bdw */
7461 misccpctl = I915_READ(GEN7_MISCCPCTL);
7462 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7463
7464 I915_WRITE(GEN8_L3SQCREG1,
7465 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7466 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7467
7468 /*
7469 * Wait at least 100 clocks before re-enabling clock gating.
7470 * See the definition of L3SQCREG1 in BSpec.
7471 */
7472 POSTING_READ(GEN8_L3SQCREG1);
7473 udelay(1);
7474 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7475}
7476
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007477static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007478{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007479 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007480
7481 /* WaDisableSDEUnitClockGating:kbl */
7482 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7483 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7484 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007485
7486 /* WaDisableGamClockGating:kbl */
7487 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7488 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7489 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007490
7491 /* WaFbcNukeOnHostModify:kbl */
7492 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7493 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007494}
7495
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007496static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007497{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007498 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007499
7500 /* WAC6entrylatency:skl */
7501 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7502 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007503
7504 /* WaFbcNukeOnHostModify:skl */
7505 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7506 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007507}
7508
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007509static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007510{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007511 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007512
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007513 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007514
Ben Widawskyab57fff2013-12-12 15:28:04 -08007515 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007516 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007517
Ben Widawskyab57fff2013-12-12 15:28:04 -08007518 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007519 I915_WRITE(CHICKEN_PAR1_1,
7520 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7521
Ben Widawskyab57fff2013-12-12 15:28:04 -08007522 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007523 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007524 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007525 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007526 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007527 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007528
Ben Widawskyab57fff2013-12-12 15:28:04 -08007529 /* WaVSRefCountFullforceMissDisable:bdw */
7530 /* WaDSRefCountFullforceMissDisable:bdw */
7531 I915_WRITE(GEN7_FF_THREAD_MODE,
7532 I915_READ(GEN7_FF_THREAD_MODE) &
7533 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007534
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007535 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7536 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007537
7538 /* WaDisableSDEUnitClockGating:bdw */
7539 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7540 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007541
Imre Deak450174f2016-05-03 15:54:21 +03007542 /* WaProgramL3SqcReg1Default:bdw */
7543 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007544
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007545 /*
7546 * WaGttCachingOffByDefault:bdw
7547 * GTT cache may not work with big pages, so if those
7548 * are ever enabled GTT cache may need to be disabled.
7549 */
7550 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7551
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007552 /* WaKVMNotificationOnConfigChange:bdw */
7553 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7554 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7555
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007556 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007557
7558 /* WaDisableDopClockGating:bdw
7559 *
7560 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7561 * clock gating.
7562 */
7563 I915_WRITE(GEN6_UCGCTL1,
7564 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007565}
7566
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007567static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007568{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007569 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007570
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007571 /* L3 caching of data atomics doesn't work -- disable it. */
7572 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7573 I915_WRITE(HSW_ROW_CHICKEN3,
7574 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7575
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007576 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007577 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7578 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7579 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7580
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007581 /* WaVSRefCountFullforceMissDisable:hsw */
7582 I915_WRITE(GEN7_FF_THREAD_MODE,
7583 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007584
Akash Goel4e046322014-04-04 17:14:38 +05307585 /* WaDisable_RenderCache_OperationalFlush:hsw */
7586 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7587
Chia-I Wufe27c602014-01-28 13:29:33 +08007588 /* enable HiZ Raw Stall Optimization */
7589 I915_WRITE(CACHE_MODE_0_GEN7,
7590 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7591
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007592 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007593 I915_WRITE(CACHE_MODE_1,
7594 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007595
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007596 /*
7597 * BSpec recommends 8x4 when MSAA is used,
7598 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007599 *
7600 * Note that PS/WM thread counts depend on the WIZ hashing
7601 * disable bit, which we don't touch here, but it's good
7602 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007603 */
7604 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007605 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007606
Kenneth Graunke94411592014-12-31 16:23:00 -08007607 /* WaSampleCChickenBitEnable:hsw */
7608 I915_WRITE(HALF_SLICE_CHICKEN3,
7609 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7610
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007611 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007612 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7613
Paulo Zanoni90a88642013-05-03 17:23:45 -03007614 /* WaRsPkgCStateDisplayPMReq:hsw */
7615 I915_WRITE(CHICKEN_PAR1_1,
7616 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007617
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007618 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007619}
7620
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007621static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007622{
Ben Widawsky20848222012-05-04 18:58:59 -07007623 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007624
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007625 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007626
Damien Lespiau231e54f2012-10-19 17:55:41 +01007627 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007628
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007629 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007630 I915_WRITE(_3D_CHICKEN3,
7631 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7632
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007633 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007634 I915_WRITE(IVB_CHICKEN3,
7635 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7636 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7637
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007638 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007639 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007640 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7641 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007642
Akash Goel4e046322014-04-04 17:14:38 +05307643 /* WaDisable_RenderCache_OperationalFlush:ivb */
7644 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7645
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007646 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007647 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7648 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7649
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007650 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007651 I915_WRITE(GEN7_L3CNTLREG1,
7652 GEN7_WA_FOR_GEN7_L3_CONTROL);
7653 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007654 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007655 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007656 I915_WRITE(GEN7_ROW_CHICKEN2,
7657 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007658 else {
7659 /* must write both registers */
7660 I915_WRITE(GEN7_ROW_CHICKEN2,
7661 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007662 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7663 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007664 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007665
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007666 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007667 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7668 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7669
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007670 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007671 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007672 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007673 */
7674 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007675 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007676
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007677 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007678 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7679 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7680 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7681
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007682 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007683
7684 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007685
Chris Wilson22721342014-03-04 09:41:43 +00007686 if (0) { /* causes HiZ corruption on ivb:gt1 */
7687 /* enable HiZ Raw Stall Optimization */
7688 I915_WRITE(CACHE_MODE_0_GEN7,
7689 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7690 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007691
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007692 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007693 I915_WRITE(CACHE_MODE_1,
7694 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007695
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007696 /*
7697 * BSpec recommends 8x4 when MSAA is used,
7698 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007699 *
7700 * Note that PS/WM thread counts depend on the WIZ hashing
7701 * disable bit, which we don't touch here, but it's good
7702 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007703 */
7704 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007705 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007706
Ben Widawsky20848222012-05-04 18:58:59 -07007707 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7708 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7709 snpcr |= GEN6_MBC_SNPCR_MED;
7710 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007711
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007712 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007713 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007714
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007715 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007716}
7717
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007718static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007719{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007720 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007721 I915_WRITE(_3D_CHICKEN3,
7722 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7723
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007724 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007725 I915_WRITE(IVB_CHICKEN3,
7726 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7727 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7728
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007729 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007730 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007731 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007732 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7733 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007734
Akash Goel4e046322014-04-04 17:14:38 +05307735 /* WaDisable_RenderCache_OperationalFlush:vlv */
7736 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7737
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007738 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007739 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7740 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7741
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007742 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007743 I915_WRITE(GEN7_ROW_CHICKEN2,
7744 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7745
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007746 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007747 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7748 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7749 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7750
Ville Syrjälä46680e02014-01-22 21:33:01 +02007751 gen7_setup_fixed_func_scheduler(dev_priv);
7752
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007753 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007754 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007755 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007756 */
7757 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007758 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007759
Akash Goelc98f5062014-03-24 23:00:07 +05307760 /* WaDisableL3Bank2xClockGate:vlv
7761 * Disabling L3 clock gating- MMIO 940c[25] = 1
7762 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7763 I915_WRITE(GEN7_UCGCTL4,
7764 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007765
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007766 /*
7767 * BSpec says this must be set, even though
7768 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7769 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007770 I915_WRITE(CACHE_MODE_1,
7771 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007772
7773 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007774 * BSpec recommends 8x4 when MSAA is used,
7775 * however in practice 16x4 seems fastest.
7776 *
7777 * Note that PS/WM thread counts depend on the WIZ hashing
7778 * disable bit, which we don't touch here, but it's good
7779 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7780 */
7781 I915_WRITE(GEN7_GT_MODE,
7782 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7783
7784 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007785 * WaIncreaseL3CreditsForVLVB0:vlv
7786 * This is the hardware default actually.
7787 */
7788 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7789
7790 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007791 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007792 * Disable clock gating on th GCFG unit to prevent a delay
7793 * in the reporting of vblank events.
7794 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007795 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007796}
7797
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007798static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007799{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007800 /* WaVSRefCountFullforceMissDisable:chv */
7801 /* WaDSRefCountFullforceMissDisable:chv */
7802 I915_WRITE(GEN7_FF_THREAD_MODE,
7803 I915_READ(GEN7_FF_THREAD_MODE) &
7804 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007805
7806 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7807 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7808 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007809
7810 /* WaDisableCSUnitClockGating:chv */
7811 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7812 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007813
7814 /* WaDisableSDEUnitClockGating:chv */
7815 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7816 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007817
7818 /*
Imre Deak450174f2016-05-03 15:54:21 +03007819 * WaProgramL3SqcReg1Default:chv
7820 * See gfxspecs/Related Documents/Performance Guide/
7821 * LSQC Setting Recommendations.
7822 */
7823 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7824
7825 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007826 * GTT cache may not work with big pages, so if those
7827 * are ever enabled GTT cache may need to be disabled.
7828 */
7829 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007830}
7831
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007832static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007833{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007834 uint32_t dspclk_gate;
7835
7836 I915_WRITE(RENCLK_GATE_D1, 0);
7837 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7838 GS_UNIT_CLOCK_GATE_DISABLE |
7839 CL_UNIT_CLOCK_GATE_DISABLE);
7840 I915_WRITE(RAMCLK_GATE_D, 0);
7841 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7842 OVRUNIT_CLOCK_GATE_DISABLE |
7843 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007844 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007845 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7846 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007847
7848 /* WaDisableRenderCachePipelinedFlush */
7849 I915_WRITE(CACHE_MODE_0,
7850 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007851
Akash Goel4e046322014-04-04 17:14:38 +05307852 /* WaDisable_RenderCache_OperationalFlush:g4x */
7853 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7854
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007855 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007856}
7857
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007858static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007859{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007860 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7861 I915_WRITE(RENCLK_GATE_D2, 0);
7862 I915_WRITE(DSPCLK_GATE_D, 0);
7863 I915_WRITE(RAMCLK_GATE_D, 0);
7864 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007865 I915_WRITE(MI_ARB_STATE,
7866 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307867
7868 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7869 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007870}
7871
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007872static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007873{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007874 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7875 I965_RCC_CLOCK_GATE_DISABLE |
7876 I965_RCPB_CLOCK_GATE_DISABLE |
7877 I965_ISC_CLOCK_GATE_DISABLE |
7878 I965_FBC_CLOCK_GATE_DISABLE);
7879 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007880 I915_WRITE(MI_ARB_STATE,
7881 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307882
7883 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7884 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007885}
7886
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007887static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007888{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007889 u32 dstate = I915_READ(D_STATE);
7890
7891 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7892 DSTATE_DOT_CLOCK_GATING;
7893 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007894
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007895 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007896 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007897
7898 /* IIR "flip pending" means done if this bit is set */
7899 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007900
7901 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007902 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007903
7904 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7905 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007906
7907 I915_WRITE(MI_ARB_STATE,
7908 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007909}
7910
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007911static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007912{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007913 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007914
7915 /* interrupts should cause a wake up from C3 */
7916 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7917 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007918
7919 I915_WRITE(MEM_MODE,
7920 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007921}
7922
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007923static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007924{
Ville Syrjälä10383922014-08-15 01:21:54 +03007925 I915_WRITE(MEM_MODE,
7926 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7927 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007928}
7929
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007930void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007931{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007932 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007933}
7934
Ville Syrjälä712bf362016-10-31 22:37:23 +02007935void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007936{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007937 if (HAS_PCH_LPT(dev_priv))
7938 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007939}
7940
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007941static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007942{
7943 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7944}
7945
7946/**
7947 * intel_init_clock_gating_hooks - setup the clock gating hooks
7948 * @dev_priv: device private
7949 *
7950 * Setup the hooks that configure which clocks of a given platform can be
7951 * gated and also apply various GT and display specific workarounds for these
7952 * platforms. Note that some GT specific workarounds are applied separately
7953 * when GPU contexts or batchbuffers start their execution.
7954 */
7955void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7956{
7957 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007958 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007959 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007960 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007961 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007962 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007963 else if (IS_GEMINILAKE(dev_priv))
7964 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007965 else if (IS_BROADWELL(dev_priv))
7966 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7967 else if (IS_CHERRYVIEW(dev_priv))
7968 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7969 else if (IS_HASWELL(dev_priv))
7970 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7971 else if (IS_IVYBRIDGE(dev_priv))
7972 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7973 else if (IS_VALLEYVIEW(dev_priv))
7974 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7975 else if (IS_GEN6(dev_priv))
7976 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7977 else if (IS_GEN5(dev_priv))
7978 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7979 else if (IS_G4X(dev_priv))
7980 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007981 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007982 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007983 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007984 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7985 else if (IS_GEN3(dev_priv))
7986 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7987 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7988 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7989 else if (IS_GEN2(dev_priv))
7990 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7991 else {
7992 MISSING_CASE(INTEL_DEVID(dev_priv));
7993 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7994 }
7995}
7996
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007997/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007998void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007999{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02008000 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008001
Daniel Vetterc921aba2012-04-26 23:28:17 +02008002 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008003 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008004 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008005 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02008006 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02008007
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008008 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008009 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008010 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01008011 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01008012 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07008013 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01008014 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008015 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03008016
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008017 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd6025442014-01-07 16:14:10 +02008018 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008019 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd6025442014-01-07 16:14:10 +02008020 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07008021 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08008022 dev_priv->display.compute_intermediate_wm =
8023 ilk_compute_intermediate_wm;
8024 dev_priv->display.initial_watermarks =
8025 ilk_initial_watermarks;
8026 dev_priv->display.optimize_watermarks =
8027 ilk_optimize_watermarks;
Ville Syrjäläbd6025442014-01-07 16:14:10 +02008028 } else {
8029 DRM_DEBUG_KMS("Failed to read display plane latency. "
8030 "Disable CxSR\n");
8031 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02008032 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02008033 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008034 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008035 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008036 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008037 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008038 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008039 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008040 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008041 dev_priv->is_ddr3,
8042 dev_priv->fsb_freq,
8043 dev_priv->mem_freq)) {
8044 DRM_INFO("failed to find known CxSR latency "
8045 "(found ddr%s fsb freq %d, mem freq %d), "
8046 "disabling CxSR\n",
8047 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8048 dev_priv->fsb_freq, dev_priv->mem_freq);
8049 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008050 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008051 dev_priv->display.update_wm = NULL;
8052 } else
8053 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008054 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008055 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008056 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008057 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008058 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008059 dev_priv->display.update_wm = i9xx_update_wm;
8060 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008061 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008062 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008063 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008064 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008065 } else {
8066 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008067 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008068 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008069 } else {
8070 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008071 }
8072}
8073
Lyude87660502016-08-17 15:55:53 -04008074static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8075{
8076 uint32_t flags =
8077 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8078
8079 switch (flags) {
8080 case GEN6_PCODE_SUCCESS:
8081 return 0;
8082 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8083 case GEN6_PCODE_ILLEGAL_CMD:
8084 return -ENXIO;
8085 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008086 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008087 return -EOVERFLOW;
8088 case GEN6_PCODE_TIMEOUT:
8089 return -ETIMEDOUT;
8090 default:
8091 MISSING_CASE(flags)
8092 return 0;
8093 }
8094}
8095
8096static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8097{
8098 uint32_t flags =
8099 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8100
8101 switch (flags) {
8102 case GEN6_PCODE_SUCCESS:
8103 return 0;
8104 case GEN6_PCODE_ILLEGAL_CMD:
8105 return -ENXIO;
8106 case GEN7_PCODE_TIMEOUT:
8107 return -ETIMEDOUT;
8108 case GEN7_PCODE_ILLEGAL_DATA:
8109 return -EINVAL;
8110 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8111 return -EOVERFLOW;
8112 default:
8113 MISSING_CASE(flags);
8114 return 0;
8115 }
8116}
8117
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008118int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008119{
Lyude87660502016-08-17 15:55:53 -04008120 int status;
8121
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008122 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008123
Chris Wilson3f5582d2016-06-30 15:32:45 +01008124 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8125 * use te fw I915_READ variants to reduce the amount of work
8126 * required when reading/writing.
8127 */
8128
8129 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008130 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8131 return -EAGAIN;
8132 }
8133
Chris Wilson3f5582d2016-06-30 15:32:45 +01008134 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8135 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8136 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008137
Chris Wilson3f5582d2016-06-30 15:32:45 +01008138 if (intel_wait_for_register_fw(dev_priv,
8139 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8140 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008141 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8142 return -ETIMEDOUT;
8143 }
8144
Chris Wilson3f5582d2016-06-30 15:32:45 +01008145 *val = I915_READ_FW(GEN6_PCODE_DATA);
8146 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008147
Lyude87660502016-08-17 15:55:53 -04008148 if (INTEL_GEN(dev_priv) > 6)
8149 status = gen7_check_mailbox_status(dev_priv);
8150 else
8151 status = gen6_check_mailbox_status(dev_priv);
8152
8153 if (status) {
8154 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8155 status);
8156 return status;
8157 }
8158
Ben Widawsky42c05262012-09-26 10:34:00 -07008159 return 0;
8160}
8161
Chris Wilson3f5582d2016-06-30 15:32:45 +01008162int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008163 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008164{
Lyude87660502016-08-17 15:55:53 -04008165 int status;
8166
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008167 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008168
Chris Wilson3f5582d2016-06-30 15:32:45 +01008169 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8170 * use te fw I915_READ variants to reduce the amount of work
8171 * required when reading/writing.
8172 */
8173
8174 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008175 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8176 return -EAGAIN;
8177 }
8178
Chris Wilson3f5582d2016-06-30 15:32:45 +01008179 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008180 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008181 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008182
Chris Wilson3f5582d2016-06-30 15:32:45 +01008183 if (intel_wait_for_register_fw(dev_priv,
8184 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8185 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008186 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8187 return -ETIMEDOUT;
8188 }
8189
Chris Wilson3f5582d2016-06-30 15:32:45 +01008190 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008191
Lyude87660502016-08-17 15:55:53 -04008192 if (INTEL_GEN(dev_priv) > 6)
8193 status = gen7_check_mailbox_status(dev_priv);
8194 else
8195 status = gen6_check_mailbox_status(dev_priv);
8196
8197 if (status) {
8198 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8199 status);
8200 return status;
8201 }
8202
Ben Widawsky42c05262012-09-26 10:34:00 -07008203 return 0;
8204}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008205
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008206static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8207 u32 request, u32 reply_mask, u32 reply,
8208 u32 *status)
8209{
8210 u32 val = request;
8211
8212 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8213
8214 return *status || ((val & reply_mask) == reply);
8215}
8216
8217/**
8218 * skl_pcode_request - send PCODE request until acknowledgment
8219 * @dev_priv: device private
8220 * @mbox: PCODE mailbox ID the request is targeted for
8221 * @request: request ID
8222 * @reply_mask: mask used to check for request acknowledgment
8223 * @reply: value used to check for request acknowledgment
8224 * @timeout_base_ms: timeout for polling with preemption enabled
8225 *
8226 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008227 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008228 * The request is acknowledged once the PCODE reply dword equals @reply after
8229 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008230 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008231 * preemption disabled.
8232 *
8233 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8234 * other error as reported by PCODE.
8235 */
8236int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8237 u32 reply_mask, u32 reply, int timeout_base_ms)
8238{
8239 u32 status;
8240 int ret;
8241
8242 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8243
8244#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8245 &status)
8246
8247 /*
8248 * Prime the PCODE by doing a request first. Normally it guarantees
8249 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8250 * _wait_for() doesn't guarantee when its passed condition is evaluated
8251 * first, so send the first request explicitly.
8252 */
8253 if (COND) {
8254 ret = 0;
8255 goto out;
8256 }
8257 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8258 if (!ret)
8259 goto out;
8260
8261 /*
8262 * The above can time out if the number of requests was low (2 in the
8263 * worst case) _and_ PCODE was busy for some reason even after a
8264 * (queued) request and @timeout_base_ms delay. As a workaround retry
8265 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008266 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008267 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008268 * requests, and for any quirks of the PCODE firmware that delays
8269 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008270 */
8271 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8272 WARN_ON_ONCE(timeout_base_ms > 3);
8273 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008274 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008275 preempt_enable();
8276
8277out:
8278 return ret ? ret : status;
8279#undef COND
8280}
8281
Ville Syrjälädd06f882014-11-10 22:55:12 +02008282static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8283{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008284 /*
8285 * N = val - 0xb7
8286 * Slow = Fast = GPLL ref * N
8287 */
8288 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008289}
8290
Fengguang Wub55dd642014-07-12 11:21:39 +02008291static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008292{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008293 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008294}
8295
Fengguang Wub55dd642014-07-12 11:21:39 +02008296static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308297{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008298 /*
8299 * N = val / 2
8300 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8301 */
8302 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308303}
8304
Fengguang Wub55dd642014-07-12 11:21:39 +02008305static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308306{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008307 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008308 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308309}
8310
Ville Syrjälä616bc822015-01-23 21:04:25 +02008311int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8312{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008313 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008314 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8315 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008316 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008317 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008318 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008319 return byt_gpu_freq(dev_priv, val);
8320 else
8321 return val * GT_FREQUENCY_MULTIPLIER;
8322}
8323
Ville Syrjälä616bc822015-01-23 21:04:25 +02008324int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8325{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008326 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008327 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8328 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008329 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008330 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008331 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008332 return byt_freq_opcode(dev_priv, val);
8333 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008334 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308335}
8336
Chris Wilson6ad790c2015-04-07 16:20:31 +01008337struct request_boost {
8338 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008339 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008340};
8341
8342static void __intel_rps_boost_work(struct work_struct *work)
8343{
8344 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008345 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008346
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008347 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008348 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008349
Chris Wilsone8a261e2016-07-20 13:31:49 +01008350 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008351 kfree(boost);
8352}
8353
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008354void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008355{
8356 struct request_boost *boost;
8357
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008358 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008359 return;
8360
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008361 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008362 return;
8363
Chris Wilson6ad790c2015-04-07 16:20:31 +01008364 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8365 if (boost == NULL)
8366 return;
8367
Chris Wilsone8a261e2016-07-20 13:31:49 +01008368 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008369
8370 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008371 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008372}
8373
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008374void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008375{
Daniel Vetterf742a552013-12-06 10:17:53 +01008376 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008377 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008378
Chris Wilson54b4f682016-07-21 21:16:19 +01008379 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8380 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008381 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008382
Paulo Zanoni33688d92014-03-07 20:08:19 -03008383 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008384 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008385}
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008386
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008387static u64 vlv_residency_raw(struct drm_i915_private *dev_priv,
8388 const i915_reg_t reg)
8389{
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008390 u32 lower, upper, tmp;
Chris Wilson71cc2b12017-03-24 16:54:18 +00008391 int loop = 2;
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008392
8393 /* The register accessed do not need forcewake. We borrow
8394 * uncore lock to prevent concurrent access to range reg.
8395 */
8396 spin_lock_irq(&dev_priv->uncore.lock);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008397
8398 /* vlv and chv residency counters are 40 bits in width.
8399 * With a control bit, we can choose between upper or lower
8400 * 32bit window into this counter.
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008401 *
8402 * Although we always use the counter in high-range mode elsewhere,
8403 * userspace may attempt to read the value before rc6 is initialised,
8404 * before we have set the default VLV_COUNTER_CONTROL value. So always
8405 * set the high bit to be safe.
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008406 */
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008407 I915_WRITE_FW(VLV_COUNTER_CONTROL,
8408 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008409 upper = I915_READ_FW(reg);
8410 do {
8411 tmp = upper;
8412
8413 I915_WRITE_FW(VLV_COUNTER_CONTROL,
8414 _MASKED_BIT_DISABLE(VLV_COUNT_RANGE_HIGH));
8415 lower = I915_READ_FW(reg);
8416
8417 I915_WRITE_FW(VLV_COUNTER_CONTROL,
8418 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH));
8419 upper = I915_READ_FW(reg);
Chris Wilson71cc2b12017-03-24 16:54:18 +00008420 } while (upper != tmp && --loop);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008421
Chris Wilsonfacbeca2017-03-17 12:59:18 +00008422 /* Everywhere else we always use VLV_COUNTER_CONTROL with the
8423 * VLV_COUNT_RANGE_HIGH bit set - so it is safe to leave it set
8424 * now.
8425 */
8426
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008427 spin_unlock_irq(&dev_priv->uncore.lock);
8428
8429 return lower | (u64)upper << 8;
8430}
8431
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02008432u64 intel_rc6_residency_us(struct drm_i915_private *dev_priv,
8433 const i915_reg_t reg)
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008434{
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008435 u64 time_hw, units, div;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008436
8437 if (!intel_enable_rc6())
8438 return 0;
8439
8440 intel_runtime_pm_get(dev_priv);
8441
8442 /* On VLV and CHV, residency time is in CZ units rather than 1.28us */
8443 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02008444 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008445 div = dev_priv->czclk_freq;
8446
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008447 time_hw = vlv_residency_raw(dev_priv, reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008448 } else if (IS_GEN9_LP(dev_priv)) {
Mika Kuoppalac5a0ad12017-03-15 17:43:00 +02008449 units = 1000;
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008450 div = 1200; /* 833.33ns */
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008451
8452 time_hw = I915_READ(reg);
8453 } else {
8454 units = 128000; /* 1.28us */
8455 div = 100000;
8456
8457 time_hw = I915_READ(reg);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008458 }
8459
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008460 intel_runtime_pm_put(dev_priv);
Mika Kuoppala47c21d92017-03-15 18:07:13 +02008461 return DIV_ROUND_UP_ULL(time_hw * units, div);
Mika Kuoppala135bafa2017-03-15 17:42:59 +02008462}