blob: 2ca38ae4421ed02656d9601f61f24a7fff18a1f5 [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 Deakd965e7ac2015-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äffc7a762016-10-31 22:37:21 +0200658static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300659{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200660 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300661
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200662 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200663 if (intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300664 if (enabled)
665 return NULL;
666 enabled = crtc;
667 }
668 }
669
670 return enabled;
671}
672
Ville Syrjälä432081b2016-10-31 22:37:03 +0200673static void pineview_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300674{
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200675 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200676 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300677 const struct cxsr_latency *latency;
678 u32 reg;
679 unsigned long wm;
680
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100681 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
682 dev_priv->is_ddr3,
683 dev_priv->fsb_freq,
684 dev_priv->mem_freq);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300685 if (!latency) {
686 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Imre Deak5209b1f2014-07-01 12:36:17 +0300687 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300688 return;
689 }
690
Ville Syrjäläffc7a762016-10-31 22:37:21 +0200691 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300692 if (crtc) {
Ville Syrjäläefc26112016-10-31 22:37:04 +0200693 const struct drm_display_mode *adjusted_mode =
694 &crtc->config->base.adjusted_mode;
695 const struct drm_framebuffer *fb =
696 crtc->base.primary->state->fb;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200697 int cpp = fb->format->cpp[0];
Ville Syrjälä7c5f93b2015-09-08 13:40:49 +0300698 int clock = adjusted_mode->crtc_clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300699
700 /* Display SR */
701 wm = intel_calculate_wm(clock, &pineview_display_wm,
702 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200703 cpp, latency->display_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300704 reg = I915_READ(DSPFW1);
705 reg &= ~DSPFW_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200706 reg |= FW_WM(wm, SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300707 I915_WRITE(DSPFW1, reg);
708 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
709
710 /* cursor SR */
711 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
712 pineview_display_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200713 cpp, latency->cursor_sr);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300714 reg = I915_READ(DSPFW3);
715 reg &= ~DSPFW_CURSOR_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200716 reg |= FW_WM(wm, CURSOR_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300717 I915_WRITE(DSPFW3, reg);
718
719 /* Display HPLL off SR */
720 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
721 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200722 cpp, latency->display_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300723 reg = I915_READ(DSPFW3);
724 reg &= ~DSPFW_HPLL_SR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200725 reg |= FW_WM(wm, HPLL_SR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300726 I915_WRITE(DSPFW3, reg);
727
728 /* cursor HPLL off SR */
729 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
730 pineview_display_hplloff_wm.fifo_size,
Ville Syrjäläac484962016-01-20 21:05:26 +0200731 cpp, latency->cursor_hpll_disable);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300732 reg = I915_READ(DSPFW3);
733 reg &= ~DSPFW_HPLL_CURSOR_MASK;
Ville Syrjäläf4998962015-03-10 17:02:21 +0200734 reg |= FW_WM(wm, HPLL_CURSOR);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300735 I915_WRITE(DSPFW3, reg);
736 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
737
Imre Deak5209b1f2014-07-01 12:36:17 +0300738 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300739 } else {
Imre Deak5209b1f2014-07-01 12:36:17 +0300740 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300741 }
742}
743
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200744static bool g4x_compute_wm0(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300745 int plane,
746 const struct intel_watermark_params *display,
747 int display_latency_ns,
748 const struct intel_watermark_params *cursor,
749 int cursor_latency_ns,
750 int *plane_wm,
751 int *cursor_wm)
752{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200753 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300754 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200755 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200756 int htotal, hdisplay, clock, cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300757 int line_time_us, line_count;
758 int entries, tlb_miss;
759
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200760 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200761 if (!intel_crtc_active(crtc)) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300762 *cursor_wm = cursor->guard_size;
763 *plane_wm = display->guard_size;
764 return false;
765 }
766
Ville Syrjäläefc26112016-10-31 22:37:04 +0200767 adjusted_mode = &crtc->config->base.adjusted_mode;
768 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100769 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800770 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200771 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200772 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300773
774 /* Use the small buffer method to calculate plane watermark */
Ville Syrjäläac484962016-01-20 21:05:26 +0200775 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300776 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
777 if (tlb_miss > 0)
778 entries += tlb_miss;
779 entries = DIV_ROUND_UP(entries, display->cacheline_size);
780 *plane_wm = entries + display->guard_size;
781 if (*plane_wm > (int)display->max_wm)
782 *plane_wm = display->max_wm;
783
784 /* Use the large buffer method to calculate cursor watermark */
Ville Syrjälä922044c2014-02-14 14:18:57 +0200785 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300786 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200787 entries = line_count * crtc->base.cursor->state->crtc_w * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300788 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
789 if (tlb_miss > 0)
790 entries += tlb_miss;
791 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
792 *cursor_wm = entries + cursor->guard_size;
793 if (*cursor_wm > (int)cursor->max_wm)
794 *cursor_wm = (int)cursor->max_wm;
795
796 return true;
797}
798
799/*
800 * Check the wm result.
801 *
802 * If any calculated watermark values is larger than the maximum value that
803 * can be programmed into the associated watermark register, that watermark
804 * must be disabled.
805 */
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200806static bool g4x_check_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300807 int display_wm, int cursor_wm,
808 const struct intel_watermark_params *display,
809 const struct intel_watermark_params *cursor)
810{
811 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
812 display_wm, cursor_wm);
813
814 if (display_wm > display->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100815 DRM_DEBUG_KMS("display watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300816 display_wm, display->max_wm);
817 return false;
818 }
819
820 if (cursor_wm > cursor->max_wm) {
Tvrtko Ursulinae9400c2016-10-13 11:09:25 +0100821 DRM_DEBUG_KMS("cursor watermark is too large(%d/%u), disabling\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300822 cursor_wm, cursor->max_wm);
823 return false;
824 }
825
826 if (!(display_wm || cursor_wm)) {
827 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
828 return false;
829 }
830
831 return true;
832}
833
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200834static bool g4x_compute_srwm(struct drm_i915_private *dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300835 int plane,
836 int latency_ns,
837 const struct intel_watermark_params *display,
838 const struct intel_watermark_params *cursor,
839 int *display_wm, int *cursor_wm)
840{
Ville Syrjäläefc26112016-10-31 22:37:04 +0200841 struct intel_crtc *crtc;
Ville Syrjälä4fe85902013-09-04 18:25:22 +0300842 const struct drm_display_mode *adjusted_mode;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200843 const struct drm_framebuffer *fb;
Ville Syrjäläac484962016-01-20 21:05:26 +0200844 int hdisplay, htotal, cpp, clock;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300845 unsigned long line_time_us;
846 int line_count, line_size;
847 int small, large;
848 int entries;
849
850 if (!latency_ns) {
851 *display_wm = *cursor_wm = 0;
852 return false;
853 }
854
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +0200855 crtc = intel_get_crtc_for_plane(dev_priv, plane);
Ville Syrjäläefc26112016-10-31 22:37:04 +0200856 adjusted_mode = &crtc->config->base.adjusted_mode;
857 fb = crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +0100858 clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -0800859 htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +0200860 hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +0200861 cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300862
Ville Syrjälä922044c2014-02-14 14:18:57 +0200863 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300864 line_count = (latency_ns / line_time_us + 1000) / 1000;
Ville Syrjäläac484962016-01-20 21:05:26 +0200865 line_size = hdisplay * cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300866
867 /* Use the minimum of the small and large buffer method for primary */
Ville Syrjäläac484962016-01-20 21:05:26 +0200868 small = ((clock * cpp / 1000) * latency_ns) / 1000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300869 large = line_count * line_size;
870
871 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
872 *display_wm = entries + display->guard_size;
873
874 /* calculate the self-refresh watermark for display cursor */
Ville Syrjäläefc26112016-10-31 22:37:04 +0200875 entries = line_count * cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300876 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
877 *cursor_wm = entries + cursor->guard_size;
878
Ville Syrjäläf0ce2312016-10-31 22:37:08 +0200879 return g4x_check_srwm(dev_priv,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -0300880 *display_wm, *cursor_wm,
881 display, cursor);
882}
883
Ville Syrjälä15665972015-03-10 16:16:28 +0200884#define FW_WM_VLV(value, plane) \
885 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
886
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200887static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200888 const struct vlv_wm_values *wm)
889{
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200890 enum pipe pipe;
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200891
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200892 for_each_pipe(dev_priv, pipe) {
Ville Syrjäläc137d662017-03-02 19:15:06 +0200893 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
894
Ville Syrjälä50f4cae2016-11-28 19:37:15 +0200895 I915_WRITE(VLV_DDL(pipe),
896 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
897 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
898 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
899 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
900 }
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200901
Ville Syrjälä6fe6a7f2016-11-28 19:37:14 +0200902 /*
903 * Zero the (unused) WM1 watermarks, and also clear all the
904 * high order bits so that there are no out of bounds values
905 * present in the registers during the reprogramming.
906 */
907 I915_WRITE(DSPHOWM, 0);
908 I915_WRITE(DSPHOWM1, 0);
909 I915_WRITE(DSPFW4, 0);
910 I915_WRITE(DSPFW5, 0);
911 I915_WRITE(DSPFW6, 0);
912
Ville Syrjäläae801522015-03-05 21:19:49 +0200913 I915_WRITE(DSPFW1,
Ville Syrjälä15665972015-03-10 16:16:28 +0200914 FW_WM(wm->sr.plane, SR) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200915 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
916 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
917 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200918 I915_WRITE(DSPFW2,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200919 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
920 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
921 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
Ville Syrjäläae801522015-03-05 21:19:49 +0200922 I915_WRITE(DSPFW3,
Ville Syrjälä15665972015-03-10 16:16:28 +0200923 FW_WM(wm->sr.cursor, CURSOR_SR));
Ville Syrjäläae801522015-03-05 21:19:49 +0200924
925 if (IS_CHERRYVIEW(dev_priv)) {
926 I915_WRITE(DSPFW7_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200927 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
928 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200929 I915_WRITE(DSPFW8_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200930 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
931 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
Ville Syrjäläae801522015-03-05 21:19:49 +0200932 I915_WRITE(DSPFW9_CHV,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200933 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
934 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200935 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200936 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200937 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
938 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
939 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
940 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
941 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
942 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
943 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
944 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
945 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200946 } else {
947 I915_WRITE(DSPFW7,
Ville Syrjälä1b313892016-11-28 19:37:08 +0200948 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
949 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
Ville Syrjäläae801522015-03-05 21:19:49 +0200950 I915_WRITE(DSPHOWM,
Ville Syrjälä15665972015-03-10 16:16:28 +0200951 FW_WM(wm->sr.plane >> 9, SR_HI) |
Ville Syrjälä1b313892016-11-28 19:37:08 +0200952 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
953 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
954 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
955 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
956 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
957 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
Ville Syrjäläae801522015-03-05 21:19:49 +0200958 }
959
960 POSTING_READ(DSPFW1);
Ville Syrjälä0018fda2015-03-05 21:19:45 +0200961}
962
Ville Syrjälä15665972015-03-10 16:16:28 +0200963#undef FW_WM_VLV
964
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300965/* latency must be in 0.1us units. */
966static unsigned int vlv_wm_method2(unsigned int pixel_rate,
967 unsigned int pipe_htotal,
968 unsigned int horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +0200969 unsigned int cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300970 unsigned int latency)
971{
972 unsigned int ret;
973
974 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +0200975 ret = (ret + 1) * horiz_pixels * cpp;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300976 ret = DIV_ROUND_UP(ret, 64);
977
978 return ret;
979}
980
Ville Syrjäläbb726512016-10-31 22:37:24 +0200981static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300982{
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300983 /* all latencies in usec */
984 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
985
Ville Syrjälä58590c12015-09-08 21:05:12 +0300986 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
987
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300988 if (IS_CHERRYVIEW(dev_priv)) {
989 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
990 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
Ville Syrjälä58590c12015-09-08 21:05:12 +0300991
992 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300993 }
994}
995
Ville Syrjäläe339d672016-11-28 19:37:17 +0200996static uint16_t vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
997 const struct intel_plane_state *plane_state,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +0300998 int level)
999{
Ville Syrjäläe339d672016-11-28 19:37:17 +02001000 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001001 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
Ville Syrjäläe339d672016-11-28 19:37:17 +02001002 const struct drm_display_mode *adjusted_mode =
1003 &crtc_state->base.adjusted_mode;
Ville Syrjäläac484962016-01-20 21:05:26 +02001004 int clock, htotal, cpp, width, wm;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001005
1006 if (dev_priv->wm.pri_latency[level] == 0)
1007 return USHRT_MAX;
1008
Ville Syrjäläe339d672016-11-28 19:37:17 +02001009 if (!plane_state->base.visible)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001010 return 0;
1011
Daniel Vetteref426c12017-01-04 11:41:10 +01001012 cpp = plane_state->base.fb->format->cpp[0];
Ville Syrjäläe339d672016-11-28 19:37:17 +02001013 clock = adjusted_mode->crtc_clock;
1014 htotal = adjusted_mode->crtc_htotal;
1015 width = crtc_state->pipe_src_w;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001016 if (WARN_ON(htotal == 0))
1017 htotal = 1;
1018
1019 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1020 /*
1021 * FIXME the formula gives values that are
1022 * too big for the cursor FIFO, and hence we
1023 * would never be able to use cursors. For
1024 * now just hardcode the watermark.
1025 */
1026 wm = 63;
1027 } else {
Ville Syrjäläac484962016-01-20 21:05:26 +02001028 wm = vlv_wm_method2(clock, htotal, width, cpp,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001029 dev_priv->wm.pri_latency[level] * 10);
1030 }
1031
1032 return min_t(int, wm, USHRT_MAX);
1033}
1034
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001035static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1036{
1037 return (active_planes & (BIT(PLANE_SPRITE0) |
1038 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1039}
1040
Ville Syrjälä5012e602017-03-02 19:14:56 +02001041static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001042{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001043 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001044 const struct vlv_pipe_wm *raw =
1045 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001046 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001047 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1048 int num_active_planes = hweight32(active_planes);
1049 const int fifo_size = 511;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001050 int fifo_extra, fifo_left = fifo_size;
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001051 int sprite0_fifo_extra = 0;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001052 unsigned int total_rate;
1053 enum plane_id plane_id;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001054
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001055 /*
1056 * When enabling sprite0 after sprite1 has already been enabled
1057 * we tend to get an underrun unless sprite0 already has some
1058 * FIFO space allcoated. Hence we always allocate at least one
1059 * cacheline for sprite0 whenever sprite1 is enabled.
1060 *
1061 * All other plane enable sequences appear immune to this problem.
1062 */
1063 if (vlv_need_sprite0_fifo_workaround(active_planes))
1064 sprite0_fifo_extra = 1;
1065
Ville Syrjälä5012e602017-03-02 19:14:56 +02001066 total_rate = raw->plane[PLANE_PRIMARY] +
1067 raw->plane[PLANE_SPRITE0] +
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001068 raw->plane[PLANE_SPRITE1] +
1069 sprite0_fifo_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001070
Ville Syrjälä5012e602017-03-02 19:14:56 +02001071 if (total_rate > fifo_size)
1072 return -EINVAL;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001073
Ville Syrjälä5012e602017-03-02 19:14:56 +02001074 if (total_rate == 0)
1075 total_rate = 1;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001076
Ville Syrjälä5012e602017-03-02 19:14:56 +02001077 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001078 unsigned int rate;
1079
Ville Syrjälä5012e602017-03-02 19:14:56 +02001080 if ((active_planes & BIT(plane_id)) == 0) {
1081 fifo_state->plane[plane_id] = 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001082 continue;
1083 }
1084
Ville Syrjälä5012e602017-03-02 19:14:56 +02001085 rate = raw->plane[plane_id];
1086 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1087 fifo_left -= fifo_state->plane[plane_id];
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001088 }
1089
Ville Syrjälä1a10ae62017-03-02 19:15:03 +02001090 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1091 fifo_left -= sprite0_fifo_extra;
1092
Ville Syrjälä5012e602017-03-02 19:14:56 +02001093 fifo_state->plane[PLANE_CURSOR] = 63;
1094
1095 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001096
1097 /* spread the remainder evenly */
Ville Syrjälä5012e602017-03-02 19:14:56 +02001098 for_each_plane_id_on_crtc(crtc, plane_id) {
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001099 int plane_extra;
1100
1101 if (fifo_left == 0)
1102 break;
1103
Ville Syrjälä5012e602017-03-02 19:14:56 +02001104 if ((active_planes & BIT(plane_id)) == 0)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001105 continue;
1106
1107 plane_extra = min(fifo_extra, fifo_left);
Ville Syrjälä5012e602017-03-02 19:14:56 +02001108 fifo_state->plane[plane_id] += plane_extra;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001109 fifo_left -= plane_extra;
1110 }
1111
Ville Syrjälä5012e602017-03-02 19:14:56 +02001112 WARN_ON(active_planes != 0 && fifo_left != 0);
1113
1114 /* give it all to the first plane if none are active */
1115 if (active_planes == 0) {
1116 WARN_ON(fifo_left != fifo_size);
1117 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1118 }
1119
1120 return 0;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001121}
1122
Ville Syrjäläff32c542017-03-02 19:14:57 +02001123static int vlv_num_wm_levels(struct drm_i915_private *dev_priv)
1124{
1125 return dev_priv->wm.max_level + 1;
1126}
1127
1128/* mark all levels starting from 'level' as invalid */
1129static void vlv_invalidate_wms(struct intel_crtc *crtc,
1130 struct vlv_wm_state *wm_state, int level)
1131{
1132 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1133
1134 for (; level < vlv_num_wm_levels(dev_priv); level++) {
1135 enum plane_id plane_id;
1136
1137 for_each_plane_id_on_crtc(crtc, plane_id)
1138 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1139
1140 wm_state->sr[level].cursor = USHRT_MAX;
1141 wm_state->sr[level].plane = USHRT_MAX;
1142 }
1143}
1144
Ville Syrjälä26cca0e2016-11-28 19:37:09 +02001145static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1146{
1147 if (wm > fifo_size)
1148 return USHRT_MAX;
1149 else
1150 return fifo_size - wm;
1151}
1152
Ville Syrjäläff32c542017-03-02 19:14:57 +02001153/*
1154 * Starting from 'level' set all higher
1155 * levels to 'value' in the "raw" watermarks.
1156 */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001157static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001158 int level, enum plane_id plane_id, u16 value)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001159{
Ville Syrjäläff32c542017-03-02 19:14:57 +02001160 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1161 int num_levels = vlv_num_wm_levels(dev_priv);
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001162 bool dirty = false;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001163
Ville Syrjäläff32c542017-03-02 19:14:57 +02001164 for (; level < num_levels; level++) {
1165 struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001166
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001167 dirty |= raw->plane[plane_id] != value;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001168 raw->plane[plane_id] = value;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001169 }
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001170
1171 return dirty;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001172}
1173
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001174static bool vlv_plane_wm_compute(struct intel_crtc_state *crtc_state,
Ville Syrjäläff32c542017-03-02 19:14:57 +02001175 const struct intel_plane_state *plane_state)
1176{
1177 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
1178 enum plane_id plane_id = plane->id;
1179 int num_levels = vlv_num_wm_levels(to_i915(plane->base.dev));
1180 int level;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001181 bool dirty = false;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001182
1183 if (!plane_state->base.visible) {
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001184 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1185 goto out;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001186 }
1187
1188 for (level = 0; level < num_levels; level++) {
1189 struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1190 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1191 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1192
Ville Syrjäläff32c542017-03-02 19:14:57 +02001193 if (wm > max_wm)
1194 break;
1195
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001196 dirty |= raw->plane[plane_id] != wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001197 raw->plane[plane_id] = wm;
1198 }
1199
1200 /* mark all higher levels as invalid */
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001201 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001202
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001203out:
1204 if (dirty)
1205 DRM_DEBUG_KMS("%s wms: [0]=%d,[1]=%d,[2]=%d\n",
1206 plane->base.name,
1207 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1208 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1209 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1210
1211 return dirty;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001212}
1213
1214static bool vlv_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1215 enum plane_id plane_id, int level)
1216{
1217 const struct vlv_pipe_wm *raw =
1218 &crtc_state->wm.vlv.raw[level];
1219 const struct vlv_fifo_state *fifo_state =
1220 &crtc_state->wm.vlv.fifo_state;
1221
1222 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1223}
1224
1225static bool vlv_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1226{
1227 return vlv_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1228 vlv_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1229 vlv_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1230 vlv_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1231}
1232
1233static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001234{
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001235 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001236 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001237 struct intel_atomic_state *state =
1238 to_intel_atomic_state(crtc_state->base.state);
Ville Syrjälä855c79f2017-03-02 19:14:54 +02001239 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001240 const struct vlv_fifo_state *fifo_state =
1241 &crtc_state->wm.vlv.fifo_state;
1242 int num_active_planes = hweight32(crtc_state->active_planes &
1243 ~BIT(PLANE_CURSOR));
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001244 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->base);
Ville Syrjäläff32c542017-03-02 19:14:57 +02001245 struct intel_plane_state *plane_state;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001246 struct intel_plane *plane;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001247 enum plane_id plane_id;
1248 int level, ret, i;
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001249 unsigned int dirty = 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001250
Ville Syrjäläff32c542017-03-02 19:14:57 +02001251 for_each_intel_plane_in_state(state, plane, plane_state, i) {
1252 const struct intel_plane_state *old_plane_state =
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001253 to_intel_plane_state(plane->base.state);
1254
Ville Syrjäläff32c542017-03-02 19:14:57 +02001255 if (plane_state->base.crtc != &crtc->base &&
1256 old_plane_state->base.crtc != &crtc->base)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001257 continue;
1258
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001259 if (vlv_plane_wm_compute(crtc_state, plane_state))
1260 dirty |= BIT(plane->id);
1261 }
1262
1263 /*
1264 * DSPARB registers may have been reset due to the
1265 * power well being turned off. Make sure we restore
1266 * them to a consistent state even if no primary/sprite
1267 * planes are initially active.
1268 */
1269 if (needs_modeset)
1270 crtc_state->fifo_changed = true;
1271
1272 if (!dirty)
1273 return 0;
1274
1275 /* cursor changes don't warrant a FIFO recompute */
1276 if (dirty & ~BIT(PLANE_CURSOR)) {
1277 const struct intel_crtc_state *old_crtc_state =
1278 to_intel_crtc_state(crtc->base.state);
1279 const struct vlv_fifo_state *old_fifo_state =
1280 &old_crtc_state->wm.vlv.fifo_state;
1281
1282 ret = vlv_compute_fifo(crtc_state);
1283 if (ret)
1284 return ret;
1285
1286 if (needs_modeset ||
1287 memcmp(old_fifo_state, fifo_state,
1288 sizeof(*fifo_state)) != 0)
1289 crtc_state->fifo_changed = true;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001290 }
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001291
Ville Syrjäläff32c542017-03-02 19:14:57 +02001292 /* initially allow all levels */
1293 wm_state->num_levels = vlv_num_wm_levels(dev_priv);
1294 /*
1295 * Note that enabling cxsr with no primary/sprite planes
1296 * enabled can wedge the pipe. Hence we only allow cxsr
1297 * with exactly one enabled primary/sprite plane.
1298 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001299 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001300
Ville Syrjälä5012e602017-03-02 19:14:56 +02001301 for (level = 0; level < wm_state->num_levels; level++) {
Ville Syrjäläff32c542017-03-02 19:14:57 +02001302 const struct vlv_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1303 const int sr_fifo_size = INTEL_INFO(dev_priv)->num_pipes * 512 - 1;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001304
Ville Syrjäläff32c542017-03-02 19:14:57 +02001305 if (!vlv_crtc_wm_is_valid(crtc_state, level))
1306 break;
Ville Syrjälä5012e602017-03-02 19:14:56 +02001307
Ville Syrjäläff32c542017-03-02 19:14:57 +02001308 for_each_plane_id_on_crtc(crtc, plane_id) {
1309 wm_state->wm[level].plane[plane_id] =
1310 vlv_invert_wm_value(raw->plane[plane_id],
1311 fifo_state->plane[plane_id]);
1312 }
1313
1314 wm_state->sr[level].plane =
1315 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
Ville Syrjälä5012e602017-03-02 19:14:56 +02001316 raw->plane[PLANE_SPRITE0],
Ville Syrjäläff32c542017-03-02 19:14:57 +02001317 raw->plane[PLANE_SPRITE1]),
1318 sr_fifo_size);
1319
1320 wm_state->sr[level].cursor =
1321 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1322 63);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001323 }
1324
Ville Syrjäläff32c542017-03-02 19:14:57 +02001325 if (level == 0)
1326 return -EINVAL;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001327
Ville Syrjäläff32c542017-03-02 19:14:57 +02001328 /* limit to only levels we can actually handle */
1329 wm_state->num_levels = level;
1330
1331 /* invalidate the higher levels */
1332 vlv_invalidate_wms(crtc, wm_state, level);
1333
1334 return 0;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001335}
1336
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001337#define VLV_FIFO(plane, value) \
1338 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1339
Ville Syrjäläff32c542017-03-02 19:14:57 +02001340static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1341 struct intel_crtc_state *crtc_state)
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001342{
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001343 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001344 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Ville Syrjälä814e7f02017-03-02 19:14:55 +02001345 const struct vlv_fifo_state *fifo_state =
1346 &crtc_state->wm.vlv.fifo_state;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001347 int sprite0_start, sprite1_start, fifo_size;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001348
Ville Syrjälä236c48e2017-03-02 19:14:58 +02001349 if (!crtc_state->fifo_changed)
1350 return;
1351
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001352 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1353 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1354 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001355
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02001356 WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
1357 WARN_ON(fifo_size != 511);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001358
Ville Syrjäläc137d662017-03-02 19:15:06 +02001359 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1360
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001361 /*
1362 * uncore.lock serves a double purpose here. It allows us to
1363 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1364 * it protects the DSPARB registers from getting clobbered by
1365 * parallel updates from multiple pipes.
1366 *
1367 * intel_pipe_update_start() has already disabled interrupts
1368 * for us, so a plain spin_lock() is sufficient here.
1369 */
1370 spin_lock(&dev_priv->uncore.lock);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001371
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001372 switch (crtc->pipe) {
1373 uint32_t dsparb, dsparb2, dsparb3;
1374 case PIPE_A:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001375 dsparb = I915_READ_FW(DSPARB);
1376 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001377
1378 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1379 VLV_FIFO(SPRITEB, 0xff));
1380 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1381 VLV_FIFO(SPRITEB, sprite1_start));
1382
1383 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1384 VLV_FIFO(SPRITEB_HI, 0x1));
1385 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1386 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1387
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001388 I915_WRITE_FW(DSPARB, dsparb);
1389 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001390 break;
1391 case PIPE_B:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001392 dsparb = I915_READ_FW(DSPARB);
1393 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001394
1395 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1396 VLV_FIFO(SPRITED, 0xff));
1397 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1398 VLV_FIFO(SPRITED, sprite1_start));
1399
1400 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1401 VLV_FIFO(SPRITED_HI, 0xff));
1402 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1403 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1404
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001405 I915_WRITE_FW(DSPARB, dsparb);
1406 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001407 break;
1408 case PIPE_C:
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001409 dsparb3 = I915_READ_FW(DSPARB3);
1410 dsparb2 = I915_READ_FW(DSPARB2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001411
1412 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1413 VLV_FIFO(SPRITEF, 0xff));
1414 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1415 VLV_FIFO(SPRITEF, sprite1_start));
1416
1417 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1418 VLV_FIFO(SPRITEF_HI, 0xff));
1419 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1420 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1421
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001422 I915_WRITE_FW(DSPARB3, dsparb3);
1423 I915_WRITE_FW(DSPARB2, dsparb2);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001424 break;
1425 default:
1426 break;
1427 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001428
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001429 POSTING_READ_FW(DSPARB);
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001430
Ville Syrjälä44e921d2017-03-09 17:44:34 +02001431 spin_unlock(&dev_priv->uncore.lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001432}
1433
1434#undef VLV_FIFO
1435
Ville Syrjälä4841da52017-03-02 19:14:59 +02001436static int vlv_compute_intermediate_wm(struct drm_device *dev,
1437 struct intel_crtc *crtc,
1438 struct intel_crtc_state *crtc_state)
1439{
1440 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
1441 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
1442 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
1443 int level;
1444
1445 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001446 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1447 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001448
1449 for (level = 0; level < intermediate->num_levels; level++) {
1450 enum plane_id plane_id;
1451
1452 for_each_plane_id_on_crtc(crtc, plane_id) {
1453 intermediate->wm[level].plane[plane_id] =
1454 min(optimal->wm[level].plane[plane_id],
1455 active->wm[level].plane[plane_id]);
1456 }
1457
1458 intermediate->sr[level].plane = min(optimal->sr[level].plane,
1459 active->sr[level].plane);
1460 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
1461 active->sr[level].cursor);
1462 }
1463
1464 vlv_invalidate_wms(crtc, intermediate, level);
1465
1466 /*
1467 * If our intermediate WM are identical to the final WM, then we can
1468 * omit the post-vblank programming; only update if it's different.
1469 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001470 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1471 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001472
1473 return 0;
1474}
1475
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001476static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001477 struct vlv_wm_values *wm)
1478{
1479 struct intel_crtc *crtc;
1480 int num_active_crtcs = 0;
1481
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001482 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001483 wm->cxsr = true;
1484
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001485 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001486 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001487
1488 if (!crtc->active)
1489 continue;
1490
1491 if (!wm_state->cxsr)
1492 wm->cxsr = false;
1493
1494 num_active_crtcs++;
1495 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1496 }
1497
1498 if (num_active_crtcs != 1)
1499 wm->cxsr = false;
1500
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001501 if (num_active_crtcs > 1)
1502 wm->level = VLV_WM_LEVEL_PM2;
1503
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001504 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001505 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001506 enum pipe pipe = crtc->pipe;
1507
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001508 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001509 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001510 wm->sr = wm_state->sr[wm->level];
1511
Ville Syrjälä1b313892016-11-28 19:37:08 +02001512 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
1513 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
1514 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
1515 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001516 }
1517}
1518
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001519static bool is_disabling(int old, int new, int threshold)
1520{
1521 return old >= threshold && new < threshold;
1522}
1523
1524static bool is_enabling(int old, int new, int threshold)
1525{
1526 return old < threshold && new >= threshold;
1527}
1528
Ville Syrjäläff32c542017-03-02 19:14:57 +02001529static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001530{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001531 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
1532 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001533
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001534 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001535
Ville Syrjäläff32c542017-03-02 19:14:57 +02001536 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001537 return;
1538
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001539 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001540 chv_set_memory_dvfs(dev_priv, false);
1541
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001542 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001543 chv_set_memory_pm5(dev_priv, false);
1544
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001545 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001546 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001547
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001548 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001549
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001550 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001551 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001552
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001553 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001554 chv_set_memory_pm5(dev_priv, true);
1555
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001556 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001557 chv_set_memory_dvfs(dev_priv, true);
1558
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001559 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001560}
1561
Ville Syrjäläff32c542017-03-02 19:14:57 +02001562static void vlv_initial_watermarks(struct intel_atomic_state *state,
1563 struct intel_crtc_state *crtc_state)
1564{
1565 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1566 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1567
1568 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02001569 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
1570 vlv_program_watermarks(dev_priv);
1571 mutex_unlock(&dev_priv->wm.wm_mutex);
1572}
1573
1574static void vlv_optimize_watermarks(struct intel_atomic_state *state,
1575 struct intel_crtc_state *crtc_state)
1576{
1577 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1578 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1579
1580 if (!crtc_state->wm.need_postvbl_update)
1581 return;
1582
1583 mutex_lock(&dev_priv->wm.wm_mutex);
1584 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001585 vlv_program_watermarks(dev_priv);
1586 mutex_unlock(&dev_priv->wm.wm_mutex);
1587}
1588
Ville Syrjäläae801522015-03-05 21:19:49 +02001589#define single_plane_enabled(mask) is_power_of_2(mask)
1590
Ville Syrjälä432081b2016-10-31 22:37:03 +02001591static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001592{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001593 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001594 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001595 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1596 int plane_sr, cursor_sr;
1597 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001598 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001599
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001600 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001601 &g4x_wm_info, pessimal_latency_ns,
1602 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001603 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001604 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001605
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001606 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001607 &g4x_wm_info, pessimal_latency_ns,
1608 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001609 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001610 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001611
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001612 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001613 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001614 sr_latency_ns,
1615 &g4x_wm_info,
1616 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001617 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001618 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001619 } else {
Imre Deak98584252014-06-13 14:54:20 +03001620 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001621 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001622 plane_sr = cursor_sr = 0;
1623 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001624
Ville Syrjäläa5043452014-06-28 02:04:18 +03001625 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1626 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001627 planea_wm, cursora_wm,
1628 planeb_wm, cursorb_wm,
1629 plane_sr, cursor_sr);
1630
1631 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001632 FW_WM(plane_sr, SR) |
1633 FW_WM(cursorb_wm, CURSORB) |
1634 FW_WM(planeb_wm, PLANEB) |
1635 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001636 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001637 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001638 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001639 /* HPLL off in SR has some issues on G4x... disable it */
1640 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001641 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001642 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001643
1644 if (cxsr_enabled)
1645 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001646}
1647
Ville Syrjälä432081b2016-10-31 22:37:03 +02001648static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001649{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001650 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001651 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001652 int srwm = 1;
1653 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001654 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001655
1656 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001657 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001658 if (crtc) {
1659 /* self-refresh has much higher latency */
1660 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001661 const struct drm_display_mode *adjusted_mode =
1662 &crtc->config->base.adjusted_mode;
1663 const struct drm_framebuffer *fb =
1664 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001665 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001666 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001667 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02001668 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001669 unsigned long line_time_us;
1670 int entries;
1671
Ville Syrjälä922044c2014-02-14 14:18:57 +02001672 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001673
1674 /* Use ns/us then divide to preserve precision */
1675 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001676 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001677 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1678 srwm = I965_FIFO_SIZE - entries;
1679 if (srwm < 0)
1680 srwm = 1;
1681 srwm &= 0x1ff;
1682 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1683 entries, srwm);
1684
1685 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001686 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001687 entries = DIV_ROUND_UP(entries,
1688 i965_cursor_wm_info.cacheline_size);
1689 cursor_sr = i965_cursor_wm_info.fifo_size -
1690 (entries + i965_cursor_wm_info.guard_size);
1691
1692 if (cursor_sr > i965_cursor_wm_info.max_wm)
1693 cursor_sr = i965_cursor_wm_info.max_wm;
1694
1695 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1696 "cursor %d\n", srwm, cursor_sr);
1697
Imre Deak98584252014-06-13 14:54:20 +03001698 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001699 } else {
Imre Deak98584252014-06-13 14:54:20 +03001700 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001701 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001702 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001703 }
1704
1705 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1706 srwm);
1707
1708 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001709 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1710 FW_WM(8, CURSORB) |
1711 FW_WM(8, PLANEB) |
1712 FW_WM(8, PLANEA));
1713 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1714 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001715 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001716 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001717
1718 if (cxsr_enabled)
1719 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001720}
1721
Ville Syrjäläf4998962015-03-10 17:02:21 +02001722#undef FW_WM
1723
Ville Syrjälä432081b2016-10-31 22:37:03 +02001724static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001725{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001726 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001727 const struct intel_watermark_params *wm_info;
1728 uint32_t fwater_lo;
1729 uint32_t fwater_hi;
1730 int cwm, srwm = 1;
1731 int fifo_size;
1732 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001733 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001734
Ville Syrjäläa9097be2016-10-31 22:37:20 +02001735 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001736 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001737 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001738 wm_info = &i915_wm_info;
1739 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001740 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001741
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001742 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001743 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001744 if (intel_crtc_active(crtc)) {
1745 const struct drm_display_mode *adjusted_mode =
1746 &crtc->config->base.adjusted_mode;
1747 const struct drm_framebuffer *fb =
1748 crtc->base.primary->state->fb;
1749 int cpp;
1750
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001751 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001752 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001753 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001754 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001755
Damien Lespiau241bfc32013-09-25 16:45:37 +01001756 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001757 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001758 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001759 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001760 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001761 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001762 if (planea_wm > (long)wm_info->max_wm)
1763 planea_wm = wm_info->max_wm;
1764 }
1765
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001766 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001767 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001768
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001769 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001770 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001771 if (intel_crtc_active(crtc)) {
1772 const struct drm_display_mode *adjusted_mode =
1773 &crtc->config->base.adjusted_mode;
1774 const struct drm_framebuffer *fb =
1775 crtc->base.primary->state->fb;
1776 int cpp;
1777
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001778 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001779 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001780 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001781 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001782
Damien Lespiau241bfc32013-09-25 16:45:37 +01001783 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001784 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001785 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001786 if (enabled == NULL)
1787 enabled = crtc;
1788 else
1789 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001790 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001791 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001792 if (planeb_wm > (long)wm_info->max_wm)
1793 planeb_wm = wm_info->max_wm;
1794 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001795
1796 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1797
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001798 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001799 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001800
Ville Syrjäläefc26112016-10-31 22:37:04 +02001801 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001802
1803 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001804 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001805 enabled = NULL;
1806 }
1807
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001808 /*
1809 * Overlay gets an aggressive default since video jitter is bad.
1810 */
1811 cwm = 2;
1812
1813 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001814 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001815
1816 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02001817 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001818 /* self-refresh has much higher latency */
1819 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001820 const struct drm_display_mode *adjusted_mode =
1821 &enabled->config->base.adjusted_mode;
1822 const struct drm_framebuffer *fb =
1823 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001824 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001825 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001826 int hdisplay = enabled->config->pipe_src_w;
1827 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001828 unsigned long line_time_us;
1829 int entries;
1830
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001831 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001832 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001833 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001834 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001835
Ville Syrjälä922044c2014-02-14 14:18:57 +02001836 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001837
1838 /* Use ns/us then divide to preserve precision */
1839 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001840 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001841 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1842 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1843 srwm = wm_info->fifo_size - entries;
1844 if (srwm < 0)
1845 srwm = 1;
1846
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001847 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001848 I915_WRITE(FW_BLC_SELF,
1849 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001850 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001851 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1852 }
1853
1854 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1855 planea_wm, planeb_wm, cwm, srwm);
1856
1857 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1858 fwater_hi = (cwm & 0x1f);
1859
1860 /* Set request length to 8 cachelines per fetch */
1861 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1862 fwater_hi = fwater_hi | (1 << 8);
1863
1864 I915_WRITE(FW_BLC, fwater_lo);
1865 I915_WRITE(FW_BLC2, fwater_hi);
1866
Imre Deak5209b1f2014-07-01 12:36:17 +03001867 if (enabled)
1868 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001869}
1870
Ville Syrjälä432081b2016-10-31 22:37:03 +02001871static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001872{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001873 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001874 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001875 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001876 uint32_t fwater_lo;
1877 int planea_wm;
1878
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001879 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001880 if (crtc == NULL)
1881 return;
1882
Ville Syrjäläefc26112016-10-31 22:37:04 +02001883 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001884 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001885 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001886 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001887 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001888 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1889 fwater_lo |= (3<<8) | planea_wm;
1890
1891 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1892
1893 I915_WRITE(FW_BLC, fwater_lo);
1894}
1895
Ville Syrjälä37126462013-08-01 16:18:55 +03001896/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001897static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001898{
1899 uint64_t ret;
1900
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001901 if (WARN(latency == 0, "Latency value missing\n"))
1902 return UINT_MAX;
1903
Ville Syrjäläac484962016-01-20 21:05:26 +02001904 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001905 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1906
1907 return ret;
1908}
1909
Ville Syrjälä37126462013-08-01 16:18:55 +03001910/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001911static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001912 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001913 uint32_t latency)
1914{
1915 uint32_t ret;
1916
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001917 if (WARN(latency == 0, "Latency value missing\n"))
1918 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001919 if (WARN_ON(!pipe_htotal))
1920 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001921
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001922 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001923 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001924 ret = DIV_ROUND_UP(ret, 64) + 2;
1925 return ret;
1926}
1927
Ville Syrjälä23297042013-07-05 11:57:17 +03001928static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001929 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001930{
Matt Roper15126882015-12-03 11:37:40 -08001931 /*
1932 * Neither of these should be possible since this function shouldn't be
1933 * called if the CRTC is off or the plane is invisible. But let's be
1934 * extra paranoid to avoid a potential divide-by-zero if we screw up
1935 * elsewhere in the driver.
1936 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001937 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001938 return 0;
1939 if (WARN_ON(!horiz_pixels))
1940 return 0;
1941
Ville Syrjäläac484962016-01-20 21:05:26 +02001942 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001943}
1944
Imre Deak820c1982013-12-17 14:46:36 +02001945struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001946 uint16_t pri;
1947 uint16_t spr;
1948 uint16_t cur;
1949 uint16_t fbc;
1950};
1951
Ville Syrjälä37126462013-08-01 16:18:55 +03001952/*
1953 * For both WM_PIPE and WM_LP.
1954 * mem_value must be in 0.1us units.
1955 */
Matt Roper7221fc32015-09-24 15:53:08 -07001956static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001957 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001958 uint32_t mem_value,
1959 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001960{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001961 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001962 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001963
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001964 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001965 return 0;
1966
Ville Syrjälä353c8592016-12-14 23:30:57 +02001967 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001968
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001969 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001970
1971 if (!is_lp)
1972 return method1;
1973
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001974 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07001975 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001976 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001977 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001978
1979 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001980}
1981
Ville Syrjälä37126462013-08-01 16:18:55 +03001982/*
1983 * For both WM_PIPE and WM_LP.
1984 * mem_value must be in 0.1us units.
1985 */
Matt Roper7221fc32015-09-24 15:53:08 -07001986static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001987 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001988 uint32_t mem_value)
1989{
1990 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001991 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001992
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001993 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001994 return 0;
1995
Ville Syrjälä353c8592016-12-14 23:30:57 +02001996 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001997
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001998 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
1999 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002000 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002001 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02002002 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002003 return min(method1, method2);
2004}
2005
Ville Syrjälä37126462013-08-01 16:18:55 +03002006/*
2007 * For both WM_PIPE and WM_LP.
2008 * mem_value must be in 0.1us units.
2009 */
Matt Roper7221fc32015-09-24 15:53:08 -07002010static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002011 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002012 uint32_t mem_value)
2013{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002014 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002015
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002016 /*
2017 * Treat cursor with fb as always visible since cursor updates
2018 * can happen faster than the vrefresh rate, and the current
2019 * watermark code doesn't handle that correctly. Cursor updates
2020 * which set/clear the fb or change the cursor size are going
2021 * to get throttled by intel_legacy_cursor_update() to work
2022 * around this problem with the watermark code.
2023 */
2024 if (!cstate->base.active || !pstate->base.fb)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002025 return 0;
2026
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002027 cpp = pstate->base.fb->format->cpp[0];
2028
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002029 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002030 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002031 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002032}
2033
Paulo Zanonicca32e92013-05-31 11:45:06 -03002034/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002035static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002036 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002037 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002038{
Ville Syrjälä83054942016-11-18 21:53:00 +02002039 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002040
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002041 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002042 return 0;
2043
Ville Syrjälä353c8592016-12-14 23:30:57 +02002044 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002045
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002046 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002047}
2048
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002049static unsigned int
2050ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002051{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002052 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002053 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002054 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002055 return 768;
2056 else
2057 return 512;
2058}
2059
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002060static unsigned int
2061ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2062 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002063{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002064 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002065 /* BDW primary/sprite plane watermarks */
2066 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002067 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002068 /* IVB/HSW primary/sprite plane watermarks */
2069 return level == 0 ? 127 : 1023;
2070 else if (!is_sprite)
2071 /* ILK/SNB primary plane watermarks */
2072 return level == 0 ? 127 : 511;
2073 else
2074 /* ILK/SNB sprite plane watermarks */
2075 return level == 0 ? 63 : 255;
2076}
2077
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002078static unsigned int
2079ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002080{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002081 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002082 return level == 0 ? 63 : 255;
2083 else
2084 return level == 0 ? 31 : 63;
2085}
2086
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002087static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002088{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002089 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002090 return 31;
2091 else
2092 return 15;
2093}
2094
Ville Syrjälä158ae642013-08-07 13:28:19 +03002095/* Calculate the maximum primary/sprite plane watermark */
2096static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2097 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002098 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002099 enum intel_ddb_partitioning ddb_partitioning,
2100 bool is_sprite)
2101{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002102 struct drm_i915_private *dev_priv = to_i915(dev);
2103 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002104
2105 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002106 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002107 return 0;
2108
2109 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002110 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002111 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002112
2113 /*
2114 * For some reason the non self refresh
2115 * FIFO size is only half of the self
2116 * refresh FIFO size on ILK/SNB.
2117 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002118 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002119 fifo_size /= 2;
2120 }
2121
Ville Syrjälä240264f2013-08-07 13:29:12 +03002122 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002123 /* level 0 is always calculated with 1:1 split */
2124 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2125 if (is_sprite)
2126 fifo_size *= 5;
2127 fifo_size /= 6;
2128 } else {
2129 fifo_size /= 2;
2130 }
2131 }
2132
2133 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002134 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002135}
2136
2137/* Calculate the maximum cursor plane watermark */
2138static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002139 int level,
2140 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002141{
2142 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002143 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002144 return 64;
2145
2146 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002147 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002148}
2149
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002150static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002151 int level,
2152 const struct intel_wm_config *config,
2153 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002154 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002155{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002156 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2157 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2158 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002159 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002160}
2161
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002162static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002163 int level,
2164 struct ilk_wm_maximums *max)
2165{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002166 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2167 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2168 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2169 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002170}
2171
Ville Syrjäläd9395652013-10-09 19:18:10 +03002172static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002173 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002174 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002175{
2176 bool ret;
2177
2178 /* already determined to be invalid? */
2179 if (!result->enable)
2180 return false;
2181
2182 result->enable = result->pri_val <= max->pri &&
2183 result->spr_val <= max->spr &&
2184 result->cur_val <= max->cur;
2185
2186 ret = result->enable;
2187
2188 /*
2189 * HACK until we can pre-compute everything,
2190 * and thus fail gracefully if LP0 watermarks
2191 * are exceeded...
2192 */
2193 if (level == 0 && !result->enable) {
2194 if (result->pri_val > max->pri)
2195 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2196 level, result->pri_val, max->pri);
2197 if (result->spr_val > max->spr)
2198 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2199 level, result->spr_val, max->spr);
2200 if (result->cur_val > max->cur)
2201 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2202 level, result->cur_val, max->cur);
2203
2204 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2205 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2206 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2207 result->enable = true;
2208 }
2209
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002210 return ret;
2211}
2212
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002213static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002214 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002215 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002216 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002217 struct intel_plane_state *pristate,
2218 struct intel_plane_state *sprstate,
2219 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002220 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002221{
2222 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2223 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2224 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2225
2226 /* WM1+ latency values stored in 0.5us units */
2227 if (level > 0) {
2228 pri_latency *= 5;
2229 spr_latency *= 5;
2230 cur_latency *= 5;
2231 }
2232
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002233 if (pristate) {
2234 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2235 pri_latency, level);
2236 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2237 }
2238
2239 if (sprstate)
2240 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2241
2242 if (curstate)
2243 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2244
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002245 result->enable = true;
2246}
2247
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002248static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002249hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002250{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002251 const struct intel_atomic_state *intel_state =
2252 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002253 const struct drm_display_mode *adjusted_mode =
2254 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002255 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002256
Matt Roperee91a152015-12-03 11:37:39 -08002257 if (!cstate->base.active)
2258 return 0;
2259 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2260 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002261 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002262 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002263
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002264 /* The WM are computed with base on how long it takes to fill a single
2265 * row at the given clock rate, multiplied by 8.
2266 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002267 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2268 adjusted_mode->crtc_clock);
2269 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002270 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002271
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002272 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2273 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002274}
2275
Ville Syrjäläbb726512016-10-31 22:37:24 +02002276static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2277 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002278{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002279 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002280 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002281 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002282 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002283
2284 /* read the first set of memory latencies[0:3] */
2285 val = 0; /* data0 to be programmed to 0 for first set */
2286 mutex_lock(&dev_priv->rps.hw_lock);
2287 ret = sandybridge_pcode_read(dev_priv,
2288 GEN9_PCODE_READ_MEM_LATENCY,
2289 &val);
2290 mutex_unlock(&dev_priv->rps.hw_lock);
2291
2292 if (ret) {
2293 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2294 return;
2295 }
2296
2297 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2298 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2299 GEN9_MEM_LATENCY_LEVEL_MASK;
2300 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2301 GEN9_MEM_LATENCY_LEVEL_MASK;
2302 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2303 GEN9_MEM_LATENCY_LEVEL_MASK;
2304
2305 /* read the second set of memory latencies[4:7] */
2306 val = 1; /* data0 to be programmed to 1 for second set */
2307 mutex_lock(&dev_priv->rps.hw_lock);
2308 ret = sandybridge_pcode_read(dev_priv,
2309 GEN9_PCODE_READ_MEM_LATENCY,
2310 &val);
2311 mutex_unlock(&dev_priv->rps.hw_lock);
2312 if (ret) {
2313 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2314 return;
2315 }
2316
2317 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2318 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2319 GEN9_MEM_LATENCY_LEVEL_MASK;
2320 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2321 GEN9_MEM_LATENCY_LEVEL_MASK;
2322 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2323 GEN9_MEM_LATENCY_LEVEL_MASK;
2324
Vandana Kannan367294b2014-11-04 17:06:46 +00002325 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002326 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2327 * need to be disabled. We make sure to sanitize the values out
2328 * of the punit to satisfy this requirement.
2329 */
2330 for (level = 1; level <= max_level; level++) {
2331 if (wm[level] == 0) {
2332 for (i = level + 1; i <= max_level; i++)
2333 wm[i] = 0;
2334 break;
2335 }
2336 }
2337
2338 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002339 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002340 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002341 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002342 * to add 2us to the various latency levels we retrieve from the
2343 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002344 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002345 if (wm[0] == 0) {
2346 wm[0] += 2;
2347 for (level = 1; level <= max_level; level++) {
2348 if (wm[level] == 0)
2349 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002350 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002351 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002352 }
2353
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002354 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002355 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2356
2357 wm[0] = (sskpd >> 56) & 0xFF;
2358 if (wm[0] == 0)
2359 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002360 wm[1] = (sskpd >> 4) & 0xFF;
2361 wm[2] = (sskpd >> 12) & 0xFF;
2362 wm[3] = (sskpd >> 20) & 0x1FF;
2363 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002364 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002365 uint32_t sskpd = I915_READ(MCH_SSKPD);
2366
2367 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2368 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2369 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2370 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002371 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002372 uint32_t mltr = I915_READ(MLTR_ILK);
2373
2374 /* ILK primary LP0 latency is 700 ns */
2375 wm[0] = 7;
2376 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2377 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002378 }
2379}
2380
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002381static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2382 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002383{
2384 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002385 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002386 wm[0] = 13;
2387}
2388
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002389static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2390 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002391{
2392 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002393 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002394 wm[0] = 13;
2395
2396 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002397 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002398 wm[3] *= 2;
2399}
2400
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002401int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002402{
2403 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002404 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002405 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002406 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002407 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002408 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002409 return 3;
2410 else
2411 return 2;
2412}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002413
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002414static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002415 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002416 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002417{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002418 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002419
2420 for (level = 0; level <= max_level; level++) {
2421 unsigned int latency = wm[level];
2422
2423 if (latency == 0) {
2424 DRM_ERROR("%s WM%d latency not provided\n",
2425 name, level);
2426 continue;
2427 }
2428
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002429 /*
2430 * - latencies are in us on gen9.
2431 * - before then, WM1+ latency values are in 0.5us units
2432 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002433 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002434 latency *= 10;
2435 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002436 latency *= 5;
2437
2438 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2439 name, level, wm[level],
2440 latency / 10, latency % 10);
2441 }
2442}
2443
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002444static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2445 uint16_t wm[5], uint16_t min)
2446{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002447 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002448
2449 if (wm[0] >= min)
2450 return false;
2451
2452 wm[0] = max(wm[0], min);
2453 for (level = 1; level <= max_level; level++)
2454 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2455
2456 return true;
2457}
2458
Ville Syrjäläbb726512016-10-31 22:37:24 +02002459static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002460{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002461 bool changed;
2462
2463 /*
2464 * The BIOS provided WM memory latency values are often
2465 * inadequate for high resolution displays. Adjust them.
2466 */
2467 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2468 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2469 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2470
2471 if (!changed)
2472 return;
2473
2474 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002475 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2476 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2477 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002478}
2479
Ville Syrjäläbb726512016-10-31 22:37:24 +02002480static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002481{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002482 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002483
2484 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2485 sizeof(dev_priv->wm.pri_latency));
2486 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2487 sizeof(dev_priv->wm.pri_latency));
2488
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002489 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002490 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002491
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002492 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2493 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2494 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002495
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002496 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002497 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002498}
2499
Ville Syrjäläbb726512016-10-31 22:37:24 +02002500static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002501{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002502 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002503 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002504}
2505
Matt Ropered4a6a72016-02-23 17:20:13 -08002506static bool ilk_validate_pipe_wm(struct drm_device *dev,
2507 struct intel_pipe_wm *pipe_wm)
2508{
2509 /* LP0 watermark maximums depend on this pipe alone */
2510 const struct intel_wm_config config = {
2511 .num_pipes_active = 1,
2512 .sprites_enabled = pipe_wm->sprites_enabled,
2513 .sprites_scaled = pipe_wm->sprites_scaled,
2514 };
2515 struct ilk_wm_maximums max;
2516
2517 /* LP0 watermarks always use 1/2 DDB partitioning */
2518 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2519
2520 /* At least LP0 must be valid */
2521 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2522 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2523 return false;
2524 }
2525
2526 return true;
2527}
2528
Matt Roper261a27d2015-10-08 15:28:25 -07002529/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002530static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002531{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002532 struct drm_atomic_state *state = cstate->base.state;
2533 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002534 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002535 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002536 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002537 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002538 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002539 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002540 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002541 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002542 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002543
Matt Ropere8f1f022016-05-12 07:05:55 -07002544 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002545
Matt Roper43d59ed2015-09-24 15:53:07 -07002546 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002547 struct intel_plane_state *ps;
2548
2549 ps = intel_atomic_get_existing_plane_state(state,
2550 intel_plane);
2551 if (!ps)
2552 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002553
2554 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002555 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002556 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002557 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002558 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002559 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002560 }
2561
Matt Ropered4a6a72016-02-23 17:20:13 -08002562 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002563 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002564 pipe_wm->sprites_enabled = sprstate->base.visible;
2565 pipe_wm->sprites_scaled = sprstate->base.visible &&
2566 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2567 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002568 }
2569
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002570 usable_level = max_level;
2571
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002572 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002573 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002574 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002575
2576 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002577 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002578 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002579
Matt Roper86c8bbb2015-09-24 15:53:16 -07002580 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002581 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2582
2583 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2584 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002585
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002586 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002587 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002588
Matt Ropered4a6a72016-02-23 17:20:13 -08002589 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002590 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002591
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002592 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002593
2594 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002595 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002596
Matt Roper86c8bbb2015-09-24 15:53:16 -07002597 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002598 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002599
2600 /*
2601 * Disable any watermark level that exceeds the
2602 * register maximums since such watermarks are
2603 * always invalid.
2604 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002605 if (level > usable_level)
2606 continue;
2607
2608 if (ilk_validate_wm_level(level, &max, wm))
2609 pipe_wm->wm[level] = *wm;
2610 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002611 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002612 }
2613
Matt Roper86c8bbb2015-09-24 15:53:16 -07002614 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002615}
2616
2617/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002618 * Build a set of 'intermediate' watermark values that satisfy both the old
2619 * state and the new state. These can be programmed to the hardware
2620 * immediately.
2621 */
2622static int ilk_compute_intermediate_wm(struct drm_device *dev,
2623 struct intel_crtc *intel_crtc,
2624 struct intel_crtc_state *newstate)
2625{
Matt Ropere8f1f022016-05-12 07:05:55 -07002626 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002627 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002628 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002629
2630 /*
2631 * Start with the final, target watermarks, then combine with the
2632 * currently active watermarks to get values that are safe both before
2633 * and after the vblank.
2634 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002635 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002636 a->pipe_enabled |= b->pipe_enabled;
2637 a->sprites_enabled |= b->sprites_enabled;
2638 a->sprites_scaled |= b->sprites_scaled;
2639
2640 for (level = 0; level <= max_level; level++) {
2641 struct intel_wm_level *a_wm = &a->wm[level];
2642 const struct intel_wm_level *b_wm = &b->wm[level];
2643
2644 a_wm->enable &= b_wm->enable;
2645 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2646 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2647 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2648 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2649 }
2650
2651 /*
2652 * We need to make sure that these merged watermark values are
2653 * actually a valid configuration themselves. If they're not,
2654 * there's no safe way to transition from the old state to
2655 * the new state, so we need to fail the atomic transaction.
2656 */
2657 if (!ilk_validate_pipe_wm(dev, a))
2658 return -EINVAL;
2659
2660 /*
2661 * If our intermediate WM are identical to the final WM, then we can
2662 * omit the post-vblank programming; only update if it's different.
2663 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002664 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
2665 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08002666
2667 return 0;
2668}
2669
2670/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002671 * Merge the watermarks from all active pipes for a specific level.
2672 */
2673static void ilk_merge_wm_level(struct drm_device *dev,
2674 int level,
2675 struct intel_wm_level *ret_wm)
2676{
2677 const struct intel_crtc *intel_crtc;
2678
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002679 ret_wm->enable = true;
2680
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002681 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002682 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002683 const struct intel_wm_level *wm = &active->wm[level];
2684
2685 if (!active->pipe_enabled)
2686 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002687
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002688 /*
2689 * The watermark values may have been used in the past,
2690 * so we must maintain them in the registers for some
2691 * time even if the level is now disabled.
2692 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002693 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002694 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002695
2696 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2697 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2698 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2699 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2700 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002701}
2702
2703/*
2704 * Merge all low power watermarks for all active pipes.
2705 */
2706static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002707 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002708 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002709 struct intel_pipe_wm *merged)
2710{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002711 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002712 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002713 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002714
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002715 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002716 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002717 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002718 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002719
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002720 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002721 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002722
2723 /* merge each WM1+ level */
2724 for (level = 1; level <= max_level; level++) {
2725 struct intel_wm_level *wm = &merged->wm[level];
2726
2727 ilk_merge_wm_level(dev, level, wm);
2728
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002729 if (level > last_enabled_level)
2730 wm->enable = false;
2731 else if (!ilk_validate_wm_level(level, max, wm))
2732 /* make sure all following levels get disabled */
2733 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002734
2735 /*
2736 * The spec says it is preferred to disable
2737 * FBC WMs instead of disabling a WM level.
2738 */
2739 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002740 if (wm->enable)
2741 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002742 wm->fbc_val = 0;
2743 }
2744 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002745
2746 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2747 /*
2748 * FIXME this is racy. FBC might get enabled later.
2749 * What we should check here is whether FBC can be
2750 * enabled sometime later.
2751 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002752 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002753 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002754 for (level = 2; level <= max_level; level++) {
2755 struct intel_wm_level *wm = &merged->wm[level];
2756
2757 wm->enable = false;
2758 }
2759 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002760}
2761
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002762static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2763{
2764 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2765 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2766}
2767
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002768/* The value we need to program into the WM_LPx latency field */
2769static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2770{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002771 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002772
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002773 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002774 return 2 * level;
2775 else
2776 return dev_priv->wm.pri_latency[level];
2777}
2778
Imre Deak820c1982013-12-17 14:46:36 +02002779static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002780 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002781 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002782 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002783{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002784 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002785 struct intel_crtc *intel_crtc;
2786 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002787
Ville Syrjälä0362c782013-10-09 19:17:57 +03002788 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002789 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002790
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002791 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002792 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002793 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002794
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002795 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002796
Ville Syrjälä0362c782013-10-09 19:17:57 +03002797 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002798
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002799 /*
2800 * Maintain the watermark values even if the level is
2801 * disabled. Doing otherwise could cause underruns.
2802 */
2803 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002804 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002805 (r->pri_val << WM1_LP_SR_SHIFT) |
2806 r->cur_val;
2807
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002808 if (r->enable)
2809 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2810
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002811 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002812 results->wm_lp[wm_lp - 1] |=
2813 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2814 else
2815 results->wm_lp[wm_lp - 1] |=
2816 r->fbc_val << WM1_LP_FBC_SHIFT;
2817
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002818 /*
2819 * Always set WM1S_LP_EN when spr_val != 0, even if the
2820 * level is disabled. Doing otherwise could cause underruns.
2821 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002822 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002823 WARN_ON(wm_lp != 1);
2824 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2825 } else
2826 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002827 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002828
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002829 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002830 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002831 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002832 const struct intel_wm_level *r =
2833 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002834
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002835 if (WARN_ON(!r->enable))
2836 continue;
2837
Matt Ropered4a6a72016-02-23 17:20:13 -08002838 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002839
2840 results->wm_pipe[pipe] =
2841 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2842 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2843 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002844 }
2845}
2846
Paulo Zanoni861f3382013-05-31 10:19:21 -03002847/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2848 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002849static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002850 struct intel_pipe_wm *r1,
2851 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002852{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002853 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002854 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002855
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002856 for (level = 1; level <= max_level; level++) {
2857 if (r1->wm[level].enable)
2858 level1 = level;
2859 if (r2->wm[level].enable)
2860 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002861 }
2862
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002863 if (level1 == level2) {
2864 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002865 return r2;
2866 else
2867 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002868 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002869 return r1;
2870 } else {
2871 return r2;
2872 }
2873}
2874
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002875/* dirty bits used to track which watermarks need changes */
2876#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2877#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2878#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2879#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2880#define WM_DIRTY_FBC (1 << 24)
2881#define WM_DIRTY_DDB (1 << 25)
2882
Damien Lespiau055e3932014-08-18 13:49:10 +01002883static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002884 const struct ilk_wm_values *old,
2885 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002886{
2887 unsigned int dirty = 0;
2888 enum pipe pipe;
2889 int wm_lp;
2890
Damien Lespiau055e3932014-08-18 13:49:10 +01002891 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002892 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2893 dirty |= WM_DIRTY_LINETIME(pipe);
2894 /* Must disable LP1+ watermarks too */
2895 dirty |= WM_DIRTY_LP_ALL;
2896 }
2897
2898 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2899 dirty |= WM_DIRTY_PIPE(pipe);
2900 /* Must disable LP1+ watermarks too */
2901 dirty |= WM_DIRTY_LP_ALL;
2902 }
2903 }
2904
2905 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2906 dirty |= WM_DIRTY_FBC;
2907 /* Must disable LP1+ watermarks too */
2908 dirty |= WM_DIRTY_LP_ALL;
2909 }
2910
2911 if (old->partitioning != new->partitioning) {
2912 dirty |= WM_DIRTY_DDB;
2913 /* Must disable LP1+ watermarks too */
2914 dirty |= WM_DIRTY_LP_ALL;
2915 }
2916
2917 /* LP1+ watermarks already deemed dirty, no need to continue */
2918 if (dirty & WM_DIRTY_LP_ALL)
2919 return dirty;
2920
2921 /* Find the lowest numbered LP1+ watermark in need of an update... */
2922 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2923 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2924 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2925 break;
2926 }
2927
2928 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2929 for (; wm_lp <= 3; wm_lp++)
2930 dirty |= WM_DIRTY_LP(wm_lp);
2931
2932 return dirty;
2933}
2934
Ville Syrjälä8553c182013-12-05 15:51:39 +02002935static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2936 unsigned int dirty)
2937{
Imre Deak820c1982013-12-17 14:46:36 +02002938 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002939 bool changed = false;
2940
2941 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2942 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2943 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2944 changed = true;
2945 }
2946 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2947 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2948 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2949 changed = true;
2950 }
2951 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2952 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2953 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2954 changed = true;
2955 }
2956
2957 /*
2958 * Don't touch WM1S_LP_EN here.
2959 * Doing so could cause underruns.
2960 */
2961
2962 return changed;
2963}
2964
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002965/*
2966 * The spec says we shouldn't write when we don't need, because every write
2967 * causes WMs to be re-evaluated, expending some power.
2968 */
Imre Deak820c1982013-12-17 14:46:36 +02002969static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2970 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002971{
Imre Deak820c1982013-12-17 14:46:36 +02002972 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002973 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002974 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002975
Damien Lespiau055e3932014-08-18 13:49:10 +01002976 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002977 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002978 return;
2979
Ville Syrjälä8553c182013-12-05 15:51:39 +02002980 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002981
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002982 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002983 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002984 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002985 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002986 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002987 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2988
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002989 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002990 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002991 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002992 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002993 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002994 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2995
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002996 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002997 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002998 val = I915_READ(WM_MISC);
2999 if (results->partitioning == INTEL_DDB_PART_1_2)
3000 val &= ~WM_MISC_DATA_PARTITION_5_6;
3001 else
3002 val |= WM_MISC_DATA_PARTITION_5_6;
3003 I915_WRITE(WM_MISC, val);
3004 } else {
3005 val = I915_READ(DISP_ARB_CTL2);
3006 if (results->partitioning == INTEL_DDB_PART_1_2)
3007 val &= ~DISP_DATA_PARTITION_5_6;
3008 else
3009 val |= DISP_DATA_PARTITION_5_6;
3010 I915_WRITE(DISP_ARB_CTL2, val);
3011 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003012 }
3013
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003014 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003015 val = I915_READ(DISP_ARB_CTL);
3016 if (results->enable_fbc_wm)
3017 val &= ~DISP_FBC_WM_DIS;
3018 else
3019 val |= DISP_FBC_WM_DIS;
3020 I915_WRITE(DISP_ARB_CTL, val);
3021 }
3022
Imre Deak954911e2013-12-17 14:46:34 +02003023 if (dirty & WM_DIRTY_LP(1) &&
3024 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3025 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3026
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003027 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003028 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3029 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3030 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3031 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3032 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003033
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003034 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003035 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003036 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003037 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd619b2013-12-05 15:51:33 +02003038 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003039 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003040
3041 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003042}
3043
Matt Ropered4a6a72016-02-23 17:20:13 -08003044bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003045{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003046 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003047
3048 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3049}
3050
Lyude656d1b82016-08-17 15:55:54 -04003051#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003052
Matt Roper024c9042015-09-24 15:53:11 -07003053/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003054 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3055 * so assume we'll always need it in order to avoid underruns.
3056 */
3057static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3058{
3059 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3060
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003061 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003062 return true;
3063
3064 return false;
3065}
3066
Paulo Zanoni56feca92016-09-22 18:00:28 -03003067static bool
3068intel_has_sagv(struct drm_i915_private *dev_priv)
3069{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003070 if (IS_KABYLAKE(dev_priv))
3071 return true;
3072
3073 if (IS_SKYLAKE(dev_priv) &&
3074 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3075 return true;
3076
3077 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003078}
3079
Lyude656d1b82016-08-17 15:55:54 -04003080/*
3081 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3082 * depending on power and performance requirements. The display engine access
3083 * to system memory is blocked during the adjustment time. Because of the
3084 * blocking time, having this enabled can cause full system hangs and/or pipe
3085 * underruns if we don't meet all of the following requirements:
3086 *
3087 * - <= 1 pipe enabled
3088 * - All planes can enable watermarks for latencies >= SAGV engine block time
3089 * - We're not using an interlaced display configuration
3090 */
3091int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003092intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003093{
3094 int ret;
3095
Paulo Zanoni56feca92016-09-22 18:00:28 -03003096 if (!intel_has_sagv(dev_priv))
3097 return 0;
3098
3099 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003100 return 0;
3101
3102 DRM_DEBUG_KMS("Enabling the SAGV\n");
3103 mutex_lock(&dev_priv->rps.hw_lock);
3104
3105 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3106 GEN9_SAGV_ENABLE);
3107
3108 /* We don't need to wait for the SAGV when enabling */
3109 mutex_unlock(&dev_priv->rps.hw_lock);
3110
3111 /*
3112 * Some skl systems, pre-release machines in particular,
3113 * don't actually have an SAGV.
3114 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003115 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003116 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003117 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003118 return 0;
3119 } else if (ret < 0) {
3120 DRM_ERROR("Failed to enable the SAGV\n");
3121 return ret;
3122 }
3123
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003124 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003125 return 0;
3126}
3127
Lyude656d1b82016-08-17 15:55:54 -04003128int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003129intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003130{
Imre Deakb3b8e992016-12-05 18:27:38 +02003131 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003132
Paulo Zanoni56feca92016-09-22 18:00:28 -03003133 if (!intel_has_sagv(dev_priv))
3134 return 0;
3135
3136 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003137 return 0;
3138
3139 DRM_DEBUG_KMS("Disabling the SAGV\n");
3140 mutex_lock(&dev_priv->rps.hw_lock);
3141
3142 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003143 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3144 GEN9_SAGV_DISABLE,
3145 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3146 1);
Lyude656d1b82016-08-17 15:55:54 -04003147 mutex_unlock(&dev_priv->rps.hw_lock);
3148
Lyude656d1b82016-08-17 15:55:54 -04003149 /*
3150 * Some skl systems, pre-release machines in particular,
3151 * don't actually have an SAGV.
3152 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003153 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003154 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003155 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003156 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003157 } else if (ret < 0) {
3158 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3159 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003160 }
3161
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003162 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003163 return 0;
3164}
3165
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003166bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003167{
3168 struct drm_device *dev = state->dev;
3169 struct drm_i915_private *dev_priv = to_i915(dev);
3170 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003171 struct intel_crtc *crtc;
3172 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003173 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003174 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003175 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003176
Paulo Zanoni56feca92016-09-22 18:00:28 -03003177 if (!intel_has_sagv(dev_priv))
3178 return false;
3179
Lyude656d1b82016-08-17 15:55:54 -04003180 /*
3181 * SKL workaround: bspec recommends we disable the SAGV when we have
3182 * more then one pipe enabled
3183 *
3184 * If there are no active CRTCs, no additional checks need be performed
3185 */
3186 if (hweight32(intel_state->active_crtcs) == 0)
3187 return true;
3188 else if (hweight32(intel_state->active_crtcs) > 1)
3189 return false;
3190
3191 /* Since we're now guaranteed to only have one active CRTC... */
3192 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003193 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003194 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003195
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003196 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003197 return false;
3198
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003199 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003200 struct skl_plane_wm *wm =
3201 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003202
Lyude656d1b82016-08-17 15:55:54 -04003203 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003204 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003205 continue;
3206
3207 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003208 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003209 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003210 { }
3211
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003212 latency = dev_priv->wm.skl_latency[level];
3213
3214 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003215 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003216 I915_FORMAT_MOD_X_TILED)
3217 latency += 15;
3218
Lyude656d1b82016-08-17 15:55:54 -04003219 /*
3220 * If any of the planes on this pipe don't enable wm levels
3221 * that incur memory latencies higher then 30µs we can't enable
3222 * the SAGV
3223 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003224 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003225 return false;
3226 }
3227
3228 return true;
3229}
3230
Damien Lespiaub9cec072014-11-04 17:06:43 +00003231static void
3232skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003233 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003234 struct skl_ddb_entry *alloc, /* out */
3235 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003236{
Matt Roperc107acf2016-05-12 07:06:01 -07003237 struct drm_atomic_state *state = cstate->base.state;
3238 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3239 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003240 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003241 unsigned int pipe_size, ddb_size;
3242 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003243
Matt Ropera6d3460e2016-05-12 07:06:04 -07003244 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003245 alloc->start = 0;
3246 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003247 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003248 return;
3249 }
3250
Matt Ropera6d3460e2016-05-12 07:06:04 -07003251 if (intel_state->active_pipe_changes)
3252 *num_active = hweight32(intel_state->active_crtcs);
3253 else
3254 *num_active = hweight32(dev_priv->active_crtcs);
3255
Deepak M6f3fff62016-09-15 15:01:10 +05303256 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3257 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003258
3259 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3260
Matt Roperc107acf2016-05-12 07:06:01 -07003261 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003262 * If the state doesn't change the active CRTC's, then there's
3263 * no need to recalculate; the existing pipe allocation limits
3264 * should remain unchanged. Note that we're safe from racing
3265 * commits since any racing commit that changes the active CRTC
3266 * list would need to grab _all_ crtc locks, including the one
3267 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003268 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003269 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003270 /*
3271 * alloc may be cleared by clear_intel_crtc_state,
3272 * copy from old state to be sure
3273 */
3274 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003275 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003276 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003277
3278 nth_active_pipe = hweight32(intel_state->active_crtcs &
3279 (drm_crtc_mask(for_crtc) - 1));
3280 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3281 alloc->start = nth_active_pipe * ddb_size / *num_active;
3282 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003283}
3284
Matt Roperc107acf2016-05-12 07:06:01 -07003285static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003286{
Matt Roperc107acf2016-05-12 07:06:01 -07003287 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003288 return 32;
3289
3290 return 8;
3291}
3292
Damien Lespiaua269c582014-11-04 17:06:49 +00003293static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3294{
3295 entry->start = reg & 0x3ff;
3296 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003297 if (entry->end)
3298 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003299}
3300
Damien Lespiau08db6652014-11-04 17:06:52 +00003301void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3302 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003303{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003304 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003305
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003306 memset(ddb, 0, sizeof(*ddb));
3307
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003308 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003309 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003310 enum plane_id plane_id;
3311 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003312
3313 power_domain = POWER_DOMAIN_PIPE(pipe);
3314 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003315 continue;
3316
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003317 for_each_plane_id_on_crtc(crtc, plane_id) {
3318 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003319
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003320 if (plane_id != PLANE_CURSOR)
3321 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3322 else
3323 val = I915_READ(CUR_BUF_CFG(pipe));
3324
3325 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3326 }
Imre Deak4d800032016-02-17 16:31:29 +02003327
3328 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003329 }
3330}
3331
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003332/*
3333 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3334 * The bspec defines downscale amount as:
3335 *
3336 * """
3337 * Horizontal down scale amount = maximum[1, Horizontal source size /
3338 * Horizontal destination size]
3339 * Vertical down scale amount = maximum[1, Vertical source size /
3340 * Vertical destination size]
3341 * Total down scale amount = Horizontal down scale amount *
3342 * Vertical down scale amount
3343 * """
3344 *
3345 * Return value is provided in 16.16 fixed point form to retain fractional part.
3346 * Caller should take care of dividing & rounding off the value.
3347 */
3348static uint32_t
3349skl_plane_downscale_amount(const struct intel_plane_state *pstate)
3350{
3351 uint32_t downscale_h, downscale_w;
3352 uint32_t src_w, src_h, dst_w, dst_h;
3353
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003354 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003355 return DRM_PLANE_HELPER_NO_SCALING;
3356
3357 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003358 src_w = drm_rect_width(&pstate->base.src);
3359 src_h = drm_rect_height(&pstate->base.src);
3360 dst_w = drm_rect_width(&pstate->base.dst);
3361 dst_h = drm_rect_height(&pstate->base.dst);
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003362 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003363 swap(dst_w, dst_h);
3364
3365 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3366 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3367
3368 /* Provide result in 16.16 fixed point */
3369 return (uint64_t)downscale_w * downscale_h >> 16;
3370}
3371
Damien Lespiaub9cec072014-11-04 17:06:43 +00003372static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003373skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3374 const struct drm_plane_state *pstate,
3375 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003376{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003377 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003378 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003379 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003380 struct drm_framebuffer *fb;
3381 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003382
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003383 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003384 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003385
3386 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003387 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003388
Matt Ropera1de91e2016-05-12 07:05:57 -07003389 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3390 return 0;
3391 if (y && format != DRM_FORMAT_NV12)
3392 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003393
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003394 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3395 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003396
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003397 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003398 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003399
3400 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003401 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003402 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003403 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003404 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003405 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003406 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003407 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003408 } else {
3409 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003410 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003411 }
3412
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003413 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3414
3415 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003416}
3417
3418/*
3419 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3420 * a 8192x4096@32bpp framebuffer:
3421 * 3 * 4096 * 8192 * 4 < 2^32
3422 */
3423static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003424skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3425 unsigned *plane_data_rate,
3426 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003427{
Matt Roper9c74d822016-05-12 07:05:58 -07003428 struct drm_crtc_state *cstate = &intel_cstate->base;
3429 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003430 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003431 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003432 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003433
3434 if (WARN_ON(!state))
3435 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003436
Matt Ropera1de91e2016-05-12 07:05:57 -07003437 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003438 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003439 enum plane_id plane_id = to_intel_plane(plane)->id;
3440 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003441
Matt Ropera6d3460e2016-05-12 07:06:04 -07003442 /* packed/uv */
3443 rate = skl_plane_relative_data_rate(intel_cstate,
3444 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003445 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003446
3447 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003448
Matt Ropera6d3460e2016-05-12 07:06:04 -07003449 /* y-plane */
3450 rate = skl_plane_relative_data_rate(intel_cstate,
3451 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003452 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003453
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003454 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003455 }
3456
3457 return total_data_rate;
3458}
3459
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003460static uint16_t
3461skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3462 const int y)
3463{
3464 struct drm_framebuffer *fb = pstate->fb;
3465 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3466 uint32_t src_w, src_h;
3467 uint32_t min_scanlines = 8;
3468 uint8_t plane_bpp;
3469
3470 if (WARN_ON(!fb))
3471 return 0;
3472
3473 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003474 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003475 return 0;
3476
3477 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003478 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3479 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003480 return 8;
3481
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003482 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3483 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003484
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003485 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003486 swap(src_w, src_h);
3487
3488 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003489 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003490 src_w /= 2;
3491 src_h /= 2;
3492 }
3493
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003494 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003495 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003496 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003497 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003498
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003499 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003500 switch (plane_bpp) {
3501 case 1:
3502 min_scanlines = 32;
3503 break;
3504 case 2:
3505 min_scanlines = 16;
3506 break;
3507 case 4:
3508 min_scanlines = 8;
3509 break;
3510 case 8:
3511 min_scanlines = 4;
3512 break;
3513 default:
3514 WARN(1, "Unsupported pixel depth %u for rotation",
3515 plane_bpp);
3516 min_scanlines = 32;
3517 }
3518 }
3519
3520 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3521}
3522
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003523static void
3524skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3525 uint16_t *minimum, uint16_t *y_minimum)
3526{
3527 const struct drm_plane_state *pstate;
3528 struct drm_plane *plane;
3529
3530 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003531 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003532
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003533 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003534 continue;
3535
3536 if (!pstate->visible)
3537 continue;
3538
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003539 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
3540 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003541 }
3542
3543 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3544}
3545
Matt Roperc107acf2016-05-12 07:06:01 -07003546static int
Matt Roper024c9042015-09-24 15:53:11 -07003547skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003548 struct skl_ddb_allocation *ddb /* out */)
3549{
Matt Roperc107acf2016-05-12 07:06:01 -07003550 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003551 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003552 struct drm_device *dev = crtc->dev;
3553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3554 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003555 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003556 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003557 uint16_t minimum[I915_MAX_PLANES] = {};
3558 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003559 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003560 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07003561 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003562 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3563 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003564
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003565 /* Clear the partitioning for disabled planes. */
3566 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3567 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3568
Matt Ropera6d3460e2016-05-12 07:06:04 -07003569 if (WARN_ON(!state))
3570 return 0;
3571
Matt Roperc107acf2016-05-12 07:06:01 -07003572 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003573 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003574 return 0;
3575 }
3576
Matt Ropera6d3460e2016-05-12 07:06:04 -07003577 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003578 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003579 if (alloc_size == 0) {
3580 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003581 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003582 }
3583
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003584 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003585
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003586 /*
3587 * 1. Allocate the mininum required blocks for each active plane
3588 * and allocate the cursor, it doesn't require extra allocation
3589 * proportional to the data rate.
3590 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003591
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003592 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
3593 alloc_size -= minimum[plane_id];
3594 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003595 }
3596
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003597 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3598 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3599
Damien Lespiaub9cec072014-11-04 17:06:43 +00003600 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003601 * 2. Distribute the remaining space in proportion to the amount of
3602 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003603 *
3604 * FIXME: we may not allocate every single block here.
3605 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003606 total_data_rate = skl_get_total_relative_data_rate(cstate,
3607 plane_data_rate,
3608 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003609 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003610 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003611
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003612 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003613 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003614 unsigned int data_rate, y_data_rate;
3615 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003616
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003617 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003618 continue;
3619
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003620 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003621
3622 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003623 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003624 * promote the expression to 64 bits to avoid overflowing, the
3625 * result is < available as data_rate / total_data_rate < 1
3626 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003627 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003628 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3629 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003630
Matt Roperc107acf2016-05-12 07:06:01 -07003631 /* Leave disabled planes at (0,0) */
3632 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003633 ddb->plane[pipe][plane_id].start = start;
3634 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003635 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003636
3637 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003638
3639 /*
3640 * allocation for y_plane part of planar format:
3641 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003642 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003643
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003644 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07003645 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3646 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003647
Matt Roperc107acf2016-05-12 07:06:01 -07003648 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003649 ddb->y_plane[pipe][plane_id].start = start;
3650 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003651 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003652
Matt Ropera1de91e2016-05-12 07:05:57 -07003653 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003654 }
3655
Matt Roperc107acf2016-05-12 07:06:01 -07003656 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003657}
3658
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003659/*
3660 * 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 +02003661 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003662 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3663 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3664*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303665static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
3666 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003667{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303668 uint32_t wm_intermediate_val;
3669 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003670
3671 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303672 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003673
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303674 wm_intermediate_val = latency * pixel_rate * cpp;
3675 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003676 return ret;
3677}
3678
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303679static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
3680 uint32_t pipe_htotal,
3681 uint32_t latency,
3682 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003683{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003684 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303685 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003686
3687 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303688 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003689
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003690 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303691 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
3692 pipe_htotal * 1000);
3693 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003694 return ret;
3695}
3696
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003697static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3698 struct intel_plane_state *pstate)
3699{
3700 uint64_t adjusted_pixel_rate;
3701 uint64_t downscale_amount;
3702 uint64_t pixel_rate;
3703
3704 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003705 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003706 return 0;
3707
3708 /*
3709 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3710 * with additional adjustments for plane-specific scaling.
3711 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003712 adjusted_pixel_rate = cstate->pixel_rate;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003713 downscale_amount = skl_plane_downscale_amount(pstate);
3714
3715 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3716 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3717
3718 return pixel_rate;
3719}
3720
Matt Roper55994c22016-05-12 07:06:08 -07003721static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3722 struct intel_crtc_state *cstate,
3723 struct intel_plane_state *intel_pstate,
3724 uint16_t ddb_allocation,
3725 int level,
3726 uint16_t *out_blocks, /* out */
3727 uint8_t *out_lines, /* out */
3728 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003729{
Matt Roper33815fa2016-05-12 07:06:05 -07003730 struct drm_plane_state *pstate = &intel_pstate->base;
3731 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003732 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303733 uint_fixed_16_16_t method1, method2;
3734 uint_fixed_16_16_t plane_blocks_per_line;
3735 uint_fixed_16_16_t selected_result;
3736 uint32_t interm_pbpl;
3737 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003738 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02003739 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003740 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003741 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303742 uint_fixed_16_16_t y_tile_minimum;
3743 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003744 struct intel_atomic_state *state =
3745 to_intel_atomic_state(cstate->base.state);
3746 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303747 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003748
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003749 if (latency == 0 || !cstate->base.active || !intel_pstate->base.visible) {
Matt Roper55994c22016-05-12 07:06:08 -07003750 *enabled = false;
3751 return 0;
3752 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003753
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303754 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3755 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
3756 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
3757
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05303758 /* Display WA #1141: kbl. */
3759 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
3760 latency += 4;
3761
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303762 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003763 latency += 15;
3764
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003765 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3766 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003767
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003768 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003769 swap(width, height);
3770
Ville Syrjälä353c8592016-12-14 23:30:57 +02003771 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003772 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3773
Dave Airlie61d0a042016-10-25 16:35:20 +10003774 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003775 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02003776 fb->format->cpp[1] :
3777 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003778
3779 switch (cpp) {
3780 case 1:
3781 y_min_scanlines = 16;
3782 break;
3783 case 2:
3784 y_min_scanlines = 8;
3785 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003786 case 4:
3787 y_min_scanlines = 4;
3788 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003789 default:
3790 MISSING_CASE(cpp);
3791 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003792 }
3793 } else {
3794 y_min_scanlines = 4;
3795 }
3796
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02003797 if (apply_memory_bw_wa)
3798 y_min_scanlines *= 2;
3799
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003800 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303801 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303802 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
3803 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003804 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303805 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303806 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303807 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
3808 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303809 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303810 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
3811 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003812 }
3813
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003814 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3815 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003816 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003817 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003818 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003819
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303820 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
3821 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003822
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303823 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303824 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003825 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003826 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3827 (plane_bytes_per_line / 512 < 1))
3828 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303829 else if ((ddb_allocation /
3830 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
3831 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003832 else
3833 selected_result = method1;
3834 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003835
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303836 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
3837 res_lines = DIV_ROUND_UP(selected_result.val,
3838 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003839
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003840 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303841 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303842 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003843 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003844 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003845 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003846 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003847 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003848
Matt Roper55994c22016-05-12 07:06:08 -07003849 if (res_blocks >= ddb_allocation || res_lines > 31) {
3850 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003851
3852 /*
3853 * If there are no valid level 0 watermarks, then we can't
3854 * support this display configuration.
3855 */
3856 if (level) {
3857 return 0;
3858 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003859 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07003860
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003861 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3862 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
3863 plane->base.id, plane->name,
3864 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07003865 return -EINVAL;
3866 }
Matt Roper55994c22016-05-12 07:06:08 -07003867 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003868
3869 *out_blocks = res_blocks;
3870 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003871 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003872
Matt Roper55994c22016-05-12 07:06:08 -07003873 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003874}
3875
Matt Roperf4a96752016-05-12 07:06:06 -07003876static int
3877skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3878 struct skl_ddb_allocation *ddb,
3879 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003880 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003881 int level,
3882 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003883{
Matt Roperf4a96752016-05-12 07:06:06 -07003884 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003885 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003886 struct drm_plane *plane = &intel_plane->base;
3887 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003888 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003889 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003890 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003891
3892 if (state)
3893 intel_pstate =
3894 intel_atomic_get_existing_plane_state(state,
3895 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003896
Matt Roperf4a96752016-05-12 07:06:06 -07003897 /*
Lyudea62163e2016-10-04 14:28:20 -04003898 * Note: If we start supporting multiple pending atomic commits against
3899 * the same planes/CRTC's in the future, plane->state will no longer be
3900 * the correct pre-state to use for the calculations here and we'll
3901 * need to change where we get the 'unchanged' plane data from.
3902 *
3903 * For now this is fine because we only allow one queued commit against
3904 * a CRTC. Even if the plane isn't modified by this transaction and we
3905 * don't have a plane lock, we still have the CRTC's lock, so we know
3906 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003907 */
Lyudea62163e2016-10-04 14:28:20 -04003908 if (!intel_pstate)
3909 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003910
Lyudea62163e2016-10-04 14:28:20 -04003911 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003912
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003913 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07003914
Lyudea62163e2016-10-04 14:28:20 -04003915 ret = skl_compute_plane_wm(dev_priv,
3916 cstate,
3917 intel_pstate,
3918 ddb_blocks,
3919 level,
3920 &result->plane_res_b,
3921 &result->plane_res_l,
3922 &result->plane_en);
3923 if (ret)
3924 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003925
3926 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003927}
3928
Damien Lespiau407b50f2014-11-04 17:06:57 +00003929static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003930skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003931{
Mahesh Kumara3a89862016-12-01 21:19:34 +05303932 struct drm_atomic_state *state = cstate->base.state;
3933 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003934 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05303935 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003936
Matt Roper024c9042015-09-24 15:53:11 -07003937 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003938 return 0;
3939
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003940 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003941
3942 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003943 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003944
Mahesh Kumara3a89862016-12-01 21:19:34 +05303945 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
3946 1000, pixel_rate);
3947
3948 /* Display WA #1135: bxt. */
3949 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
3950 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
3951
3952 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003953}
3954
Matt Roper024c9042015-09-24 15:53:11 -07003955static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003956 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003957{
Matt Roper024c9042015-09-24 15:53:11 -07003958 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003959 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003960
3961 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003962 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003963}
3964
Matt Roper55994c22016-05-12 07:06:08 -07003965static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3966 struct skl_ddb_allocation *ddb,
3967 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003968{
Matt Roper024c9042015-09-24 15:53:11 -07003969 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003970 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04003971 struct intel_plane *intel_plane;
3972 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003973 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07003974 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003975
Lyudea62163e2016-10-04 14:28:20 -04003976 /*
3977 * We'll only calculate watermarks for planes that are actually
3978 * enabled, so make sure all other planes are set as disabled.
3979 */
3980 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
3981
3982 for_each_intel_plane_mask(&dev_priv->drm,
3983 intel_plane,
3984 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003985 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04003986
3987 for (level = 0; level <= max_level; level++) {
3988 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3989 intel_plane, level,
3990 &wm->wm[level]);
3991 if (ret)
3992 return ret;
3993 }
3994 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003995 }
Matt Roper024c9042015-09-24 15:53:11 -07003996 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003997
Matt Roper55994c22016-05-12 07:06:08 -07003998 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003999}
4000
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004001static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
4002 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00004003 const struct skl_ddb_entry *entry)
4004{
4005 if (entry->end)
4006 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
4007 else
4008 I915_WRITE(reg, 0);
4009}
4010
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004011static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4012 i915_reg_t reg,
4013 const struct skl_wm_level *level)
4014{
4015 uint32_t val = 0;
4016
4017 if (level->plane_en) {
4018 val |= PLANE_WM_EN;
4019 val |= level->plane_res_b;
4020 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4021 }
4022
4023 I915_WRITE(reg, val);
4024}
4025
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004026static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4027 const struct skl_plane_wm *wm,
4028 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004029 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004030{
4031 struct drm_crtc *crtc = &intel_crtc->base;
4032 struct drm_device *dev = crtc->dev;
4033 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004034 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004035 enum pipe pipe = intel_crtc->pipe;
4036
4037 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004038 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004039 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004040 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004041 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004042 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004043
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004044 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4045 &ddb->plane[pipe][plane_id]);
4046 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4047 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004048}
4049
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004050static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4051 const struct skl_plane_wm *wm,
4052 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004053{
4054 struct drm_crtc *crtc = &intel_crtc->base;
4055 struct drm_device *dev = crtc->dev;
4056 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004057 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004058 enum pipe pipe = intel_crtc->pipe;
4059
4060 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004061 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4062 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004063 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004064 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004065
4066 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004067 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004068}
4069
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004070bool skl_wm_level_equals(const struct skl_wm_level *l1,
4071 const struct skl_wm_level *l2)
4072{
4073 if (l1->plane_en != l2->plane_en)
4074 return false;
4075
4076 /* If both planes aren't enabled, the rest shouldn't matter */
4077 if (!l1->plane_en)
4078 return true;
4079
4080 return (l1->plane_res_l == l2->plane_res_l &&
4081 l1->plane_res_b == l2->plane_res_b);
4082}
4083
Lyude27082492016-08-24 07:48:10 +02004084static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4085 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004086{
Lyude27082492016-08-24 07:48:10 +02004087 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004088}
4089
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004090bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4091 const struct skl_ddb_entry *ddb,
4092 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004093{
Lyudece0ba282016-09-15 10:46:35 -04004094 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004095
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004096 for (i = 0; i < I915_MAX_PIPES; i++)
4097 if (i != ignore && entries[i] &&
4098 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004099 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004100
Lyude27082492016-08-24 07:48:10 +02004101 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004102}
4103
Matt Roper55994c22016-05-12 07:06:08 -07004104static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004105 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004106 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004107 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004108 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004109{
Matt Roperf4a96752016-05-12 07:06:06 -07004110 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004111 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004112
Matt Roper55994c22016-05-12 07:06:08 -07004113 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4114 if (ret)
4115 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004116
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004117 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004118 *changed = false;
4119 else
4120 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004121
Matt Roper55994c22016-05-12 07:06:08 -07004122 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004123}
4124
Matt Roper9b613022016-06-27 16:42:44 -07004125static uint32_t
4126pipes_modified(struct drm_atomic_state *state)
4127{
4128 struct drm_crtc *crtc;
4129 struct drm_crtc_state *cstate;
4130 uint32_t i, ret = 0;
4131
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004132 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper9b613022016-06-27 16:42:44 -07004133 ret |= drm_crtc_mask(crtc);
4134
4135 return ret;
4136}
4137
Jani Nikulabb7791b2016-10-04 12:29:17 +03004138static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004139skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4140{
4141 struct drm_atomic_state *state = cstate->base.state;
4142 struct drm_device *dev = state->dev;
4143 struct drm_crtc *crtc = cstate->base.crtc;
4144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4145 struct drm_i915_private *dev_priv = to_i915(dev);
4146 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4147 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4148 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4149 struct drm_plane_state *plane_state;
4150 struct drm_plane *plane;
4151 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004152
4153 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4154
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004155 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004156 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004157
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004158 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4159 &new_ddb->plane[pipe][plane_id]) &&
4160 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4161 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004162 continue;
4163
4164 plane_state = drm_atomic_get_plane_state(state, plane);
4165 if (IS_ERR(plane_state))
4166 return PTR_ERR(plane_state);
4167 }
4168
4169 return 0;
4170}
4171
Matt Roper98d39492016-05-12 07:06:03 -07004172static int
4173skl_compute_ddb(struct drm_atomic_state *state)
4174{
4175 struct drm_device *dev = state->dev;
4176 struct drm_i915_private *dev_priv = to_i915(dev);
4177 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4178 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004179 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004180 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004181 int ret;
4182
4183 /*
4184 * If this is our first atomic update following hardware readout,
4185 * we can't trust the DDB that the BIOS programmed for us. Let's
4186 * pretend that all pipes switched active status so that we'll
4187 * ensure a full DDB recompute.
4188 */
Matt Roper1b54a882016-06-17 13:42:18 -07004189 if (dev_priv->wm.distrust_bios_wm) {
4190 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4191 state->acquire_ctx);
4192 if (ret)
4193 return ret;
4194
Matt Roper98d39492016-05-12 07:06:03 -07004195 intel_state->active_pipe_changes = ~0;
4196
Matt Roper1b54a882016-06-17 13:42:18 -07004197 /*
4198 * We usually only initialize intel_state->active_crtcs if we
4199 * we're doing a modeset; make sure this field is always
4200 * initialized during the sanitization process that happens
4201 * on the first commit too.
4202 */
4203 if (!intel_state->modeset)
4204 intel_state->active_crtcs = dev_priv->active_crtcs;
4205 }
4206
Matt Roper98d39492016-05-12 07:06:03 -07004207 /*
4208 * If the modeset changes which CRTC's are active, we need to
4209 * recompute the DDB allocation for *all* active pipes, even
4210 * those that weren't otherwise being modified in any way by this
4211 * atomic commit. Due to the shrinking of the per-pipe allocations
4212 * when new active CRTC's are added, it's possible for a pipe that
4213 * we were already using and aren't changing at all here to suddenly
4214 * become invalid if its DDB needs exceeds its new allocation.
4215 *
4216 * Note that if we wind up doing a full DDB recompute, we can't let
4217 * any other display updates race with this transaction, so we need
4218 * to grab the lock on *all* CRTC's.
4219 */
Matt Roper734fa012016-05-12 15:11:40 -07004220 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004221 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004222 intel_state->wm_results.dirty_pipes = ~0;
4223 }
Matt Roper98d39492016-05-12 07:06:03 -07004224
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004225 /*
4226 * We're not recomputing for the pipes not included in the commit, so
4227 * make sure we start with the current state.
4228 */
4229 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4230
Matt Roper98d39492016-05-12 07:06:03 -07004231 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4232 struct intel_crtc_state *cstate;
4233
4234 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4235 if (IS_ERR(cstate))
4236 return PTR_ERR(cstate);
4237
Matt Roper734fa012016-05-12 15:11:40 -07004238 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004239 if (ret)
4240 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004241
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004242 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004243 if (ret)
4244 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004245 }
4246
4247 return 0;
4248}
4249
Matt Roper2722efb2016-08-17 15:55:55 -04004250static void
4251skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4252 struct skl_wm_values *src,
4253 enum pipe pipe)
4254{
Matt Roper2722efb2016-08-17 15:55:55 -04004255 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4256 sizeof(dst->ddb.y_plane[pipe]));
4257 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4258 sizeof(dst->ddb.plane[pipe]));
4259}
4260
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004261static void
4262skl_print_wm_changes(const struct drm_atomic_state *state)
4263{
4264 const struct drm_device *dev = state->dev;
4265 const struct drm_i915_private *dev_priv = to_i915(dev);
4266 const struct intel_atomic_state *intel_state =
4267 to_intel_atomic_state(state);
4268 const struct drm_crtc *crtc;
4269 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004270 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004271 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4272 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004273 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004274
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004275 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004276 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4277 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004278
Maarten Lankhorst75704982016-11-01 12:04:10 +01004279 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004280 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004281 const struct skl_ddb_entry *old, *new;
4282
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004283 old = &old_ddb->plane[pipe][plane_id];
4284 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004285
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004286 if (skl_ddb_entry_equal(old, new))
4287 continue;
4288
Maarten Lankhorst75704982016-11-01 12:04:10 +01004289 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4290 intel_plane->base.base.id,
4291 intel_plane->base.name,
4292 old->start, old->end,
4293 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004294 }
4295 }
4296}
4297
Matt Roper98d39492016-05-12 07:06:03 -07004298static int
4299skl_compute_wm(struct drm_atomic_state *state)
4300{
4301 struct drm_crtc *crtc;
4302 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004303 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4304 struct skl_wm_values *results = &intel_state->wm_results;
4305 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004306 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004307 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004308
4309 /*
4310 * If this transaction isn't actually touching any CRTC's, don't
4311 * bother with watermark calculation. Note that if we pass this
4312 * test, we're guaranteed to hold at least one CRTC state mutex,
4313 * which means we can safely use values like dev_priv->active_crtcs
4314 * since any racing commits that want to update them would need to
4315 * hold _all_ CRTC state mutexes.
4316 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004317 for_each_new_crtc_in_state(state, crtc, cstate, i)
Matt Roper98d39492016-05-12 07:06:03 -07004318 changed = true;
4319 if (!changed)
4320 return 0;
4321
Matt Roper734fa012016-05-12 15:11:40 -07004322 /* Clear all dirty flags */
4323 results->dirty_pipes = 0;
4324
Matt Roper98d39492016-05-12 07:06:03 -07004325 ret = skl_compute_ddb(state);
4326 if (ret)
4327 return ret;
4328
Matt Roper734fa012016-05-12 15:11:40 -07004329 /*
4330 * Calculate WM's for all pipes that are part of this transaction.
4331 * Note that the DDB allocation above may have added more CRTC's that
4332 * weren't otherwise being modified (and set bits in dirty_pipes) if
4333 * pipe allocations had to change.
4334 *
4335 * FIXME: Now that we're doing this in the atomic check phase, we
4336 * should allow skl_update_pipe_wm() to return failure in cases where
4337 * no suitable watermark values can be found.
4338 */
Maarten Lankhorst6ebdb5a2017-03-09 15:52:03 +01004339 for_each_new_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004340 struct intel_crtc_state *intel_cstate =
4341 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004342 const struct skl_pipe_wm *old_pipe_wm =
4343 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004344
4345 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004346 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4347 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004348 if (ret)
4349 return ret;
4350
4351 if (changed)
4352 results->dirty_pipes |= drm_crtc_mask(crtc);
4353
4354 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4355 /* This pipe's WM's did not change */
4356 continue;
4357
4358 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004359 }
4360
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004361 skl_print_wm_changes(state);
4362
Matt Roper98d39492016-05-12 07:06:03 -07004363 return 0;
4364}
4365
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004366static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4367 struct intel_crtc_state *cstate)
4368{
4369 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4370 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4371 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004372 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004373 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004374 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004375
4376 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4377 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004378
4379 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004380
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004381 for_each_plane_id_on_crtc(crtc, plane_id) {
4382 if (plane_id != PLANE_CURSOR)
4383 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4384 ddb, plane_id);
4385 else
4386 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4387 ddb);
4388 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004389}
4390
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004391static void skl_initial_wm(struct intel_atomic_state *state,
4392 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004393{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004394 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004395 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004396 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004397 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004398 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004399 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004400
Ville Syrjälä432081b2016-10-31 22:37:03 +02004401 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004402 return;
4403
Matt Roper734fa012016-05-12 15:11:40 -07004404 mutex_lock(&dev_priv->wm.wm_mutex);
4405
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004406 if (cstate->base.active_changed)
4407 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004408
4409 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004410
4411 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004412}
4413
Ville Syrjäläd8905652016-01-14 14:53:35 +02004414static void ilk_compute_wm_config(struct drm_device *dev,
4415 struct intel_wm_config *config)
4416{
4417 struct intel_crtc *crtc;
4418
4419 /* Compute the currently _active_ config */
4420 for_each_intel_crtc(dev, crtc) {
4421 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4422
4423 if (!wm->pipe_enabled)
4424 continue;
4425
4426 config->sprites_enabled |= wm->sprites_enabled;
4427 config->sprites_scaled |= wm->sprites_scaled;
4428 config->num_pipes_active++;
4429 }
4430}
4431
Matt Ropered4a6a72016-02-23 17:20:13 -08004432static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004433{
Chris Wilson91c8a322016-07-05 10:40:23 +01004434 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004435 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004436 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004437 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004438 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004439 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004440
Ville Syrjäläd8905652016-01-14 14:53:35 +02004441 ilk_compute_wm_config(dev, &config);
4442
4443 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4444 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004445
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004446 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004447 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004448 config.num_pipes_active == 1 && config.sprites_enabled) {
4449 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4450 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004451
Imre Deak820c1982013-12-17 14:46:36 +02004452 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004453 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004454 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004455 }
4456
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004457 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004458 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004459
Imre Deak820c1982013-12-17 14:46:36 +02004460 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004461
Imre Deak820c1982013-12-17 14:46:36 +02004462 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004463}
4464
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004465static void ilk_initial_watermarks(struct intel_atomic_state *state,
4466 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004467{
Matt Ropered4a6a72016-02-23 17:20:13 -08004468 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4469 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004470
Matt Ropered4a6a72016-02-23 17:20:13 -08004471 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004472 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004473 ilk_program_watermarks(dev_priv);
4474 mutex_unlock(&dev_priv->wm.wm_mutex);
4475}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004476
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004477static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4478 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004479{
4480 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4481 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4482
4483 mutex_lock(&dev_priv->wm.wm_mutex);
4484 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004485 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004486 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004487 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004488 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004489}
4490
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004491static inline void skl_wm_level_from_reg_val(uint32_t val,
4492 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004493{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004494 level->plane_en = val & PLANE_WM_EN;
4495 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4496 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4497 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004498}
4499
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004500void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4501 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004502{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004503 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004505 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004506 int level, max_level;
4507 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004508 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004509
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004510 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004511
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004512 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4513 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004514
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004515 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004516 if (plane_id != PLANE_CURSOR)
4517 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004518 else
4519 val = I915_READ(CUR_WM(pipe, level));
4520
4521 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4522 }
4523
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004524 if (plane_id != PLANE_CURSOR)
4525 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004526 else
4527 val = I915_READ(CUR_WM_TRANS(pipe));
4528
4529 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4530 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004531
Matt Roper3ef00282015-03-09 10:19:24 -07004532 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004533 return;
4534
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004535 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004536}
4537
4538void skl_wm_get_hw_state(struct drm_device *dev)
4539{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004540 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004541 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004542 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004543 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004544 struct intel_crtc *intel_crtc;
4545 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004546
Damien Lespiaua269c582014-11-04 17:06:49 +00004547 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004548 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4549 intel_crtc = to_intel_crtc(crtc);
4550 cstate = to_intel_crtc_state(crtc->state);
4551
4552 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4553
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004554 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004555 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004556 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004557
Matt Roper279e99d2016-05-12 07:06:02 -07004558 if (dev_priv->active_crtcs) {
4559 /* Fully recompute DDB on first atomic commit */
4560 dev_priv->wm.distrust_bios_wm = true;
4561 } else {
4562 /* Easy/common case; just sanitize DDB now if everything off */
4563 memset(ddb, 0, sizeof(*ddb));
4564 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004565}
4566
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004567static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4568{
4569 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004570 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004571 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004572 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004573 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004574 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004575 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004576 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004577 [PIPE_A] = WM0_PIPEA_ILK,
4578 [PIPE_B] = WM0_PIPEB_ILK,
4579 [PIPE_C] = WM0_PIPEC_IVB,
4580 };
4581
4582 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004583 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004584 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004585
Ville Syrjälä15606532016-05-13 17:55:17 +03004586 memset(active, 0, sizeof(*active));
4587
Matt Roper3ef00282015-03-09 10:19:24 -07004588 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004589
4590 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004591 u32 tmp = hw->wm_pipe[pipe];
4592
4593 /*
4594 * For active pipes LP0 watermark is marked as
4595 * enabled, and LP1+ watermaks as disabled since
4596 * we can't really reverse compute them in case
4597 * multiple pipes are active.
4598 */
4599 active->wm[0].enable = true;
4600 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4601 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4602 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4603 active->linetime = hw->wm_linetime[pipe];
4604 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004605 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004606
4607 /*
4608 * For inactive pipes, all watermark levels
4609 * should be marked as enabled but zeroed,
4610 * which is what we'd compute them to.
4611 */
4612 for (level = 0; level <= max_level; level++)
4613 active->wm[level].enable = true;
4614 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004615
4616 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004617}
4618
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004619#define _FW_WM(value, plane) \
4620 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4621#define _FW_WM_VLV(value, plane) \
4622 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4623
4624static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4625 struct vlv_wm_values *wm)
4626{
4627 enum pipe pipe;
4628 uint32_t tmp;
4629
4630 for_each_pipe(dev_priv, pipe) {
4631 tmp = I915_READ(VLV_DDL(pipe));
4632
Ville Syrjälä1b313892016-11-28 19:37:08 +02004633 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004634 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004635 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004636 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004637 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004638 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004639 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004640 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4641 }
4642
4643 tmp = I915_READ(DSPFW1);
4644 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004645 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
4646 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
4647 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004648
4649 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004650 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
4651 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
4652 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004653
4654 tmp = I915_READ(DSPFW3);
4655 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4656
4657 if (IS_CHERRYVIEW(dev_priv)) {
4658 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004659 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4660 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004661
4662 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004663 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
4664 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004665
4666 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004667 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
4668 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004669
4670 tmp = I915_READ(DSPHOWM);
4671 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004672 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4673 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4674 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
4675 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4676 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4677 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4678 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4679 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4680 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004681 } else {
4682 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004683 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4684 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004685
4686 tmp = I915_READ(DSPHOWM);
4687 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004688 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4689 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4690 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4691 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4692 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4693 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004694 }
4695}
4696
4697#undef _FW_WM
4698#undef _FW_WM_VLV
4699
4700void vlv_wm_get_hw_state(struct drm_device *dev)
4701{
4702 struct drm_i915_private *dev_priv = to_i915(dev);
4703 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02004704 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004705 u32 val;
4706
4707 vlv_read_wm_values(dev_priv, wm);
4708
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004709 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4710 wm->level = VLV_WM_LEVEL_PM2;
4711
4712 if (IS_CHERRYVIEW(dev_priv)) {
4713 mutex_lock(&dev_priv->rps.hw_lock);
4714
4715 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4716 if (val & DSP_MAXFIFO_PM5_ENABLE)
4717 wm->level = VLV_WM_LEVEL_PM5;
4718
Ville Syrjälä58590c12015-09-08 21:05:12 +03004719 /*
4720 * If DDR DVFS is disabled in the BIOS, Punit
4721 * will never ack the request. So if that happens
4722 * assume we don't have to enable/disable DDR DVFS
4723 * dynamically. To test that just set the REQ_ACK
4724 * bit to poke the Punit, but don't change the
4725 * HIGH/LOW bits so that we don't actually change
4726 * the current state.
4727 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004728 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004729 val |= FORCE_DDR_FREQ_REQ_ACK;
4730 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4731
4732 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4733 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4734 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4735 "assuming DDR DVFS is disabled\n");
4736 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4737 } else {
4738 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4739 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4740 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4741 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004742
4743 mutex_unlock(&dev_priv->rps.hw_lock);
4744 }
4745
Ville Syrjäläff32c542017-03-02 19:14:57 +02004746 for_each_intel_crtc(dev, crtc) {
4747 struct intel_crtc_state *crtc_state =
4748 to_intel_crtc_state(crtc->base.state);
4749 struct vlv_wm_state *active = &crtc->wm.active.vlv;
4750 const struct vlv_fifo_state *fifo_state =
4751 &crtc_state->wm.vlv.fifo_state;
4752 enum pipe pipe = crtc->pipe;
4753 enum plane_id plane_id;
4754 int level;
4755
4756 vlv_get_fifo_size(crtc_state);
4757
4758 active->num_levels = wm->level + 1;
4759 active->cxsr = wm->cxsr;
4760
Ville Syrjäläff32c542017-03-02 19:14:57 +02004761 for (level = 0; level < active->num_levels; level++) {
4762 struct vlv_pipe_wm *raw =
4763 &crtc_state->wm.vlv.raw[level];
4764
4765 active->sr[level].plane = wm->sr.plane;
4766 active->sr[level].cursor = wm->sr.cursor;
4767
4768 for_each_plane_id_on_crtc(crtc, plane_id) {
4769 active->wm[level].plane[plane_id] =
4770 wm->pipe[pipe].plane[plane_id];
4771
4772 raw->plane[plane_id] =
4773 vlv_invert_wm_value(active->wm[level].plane[plane_id],
4774 fifo_state->plane[plane_id]);
4775 }
4776 }
4777
4778 for_each_plane_id_on_crtc(crtc, plane_id)
4779 vlv_raw_plane_wm_set(crtc_state, level,
4780 plane_id, USHRT_MAX);
4781 vlv_invalidate_wms(crtc, active, level);
4782
4783 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02004784 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02004785
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004786 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 +02004787 pipe_name(pipe),
4788 wm->pipe[pipe].plane[PLANE_PRIMARY],
4789 wm->pipe[pipe].plane[PLANE_CURSOR],
4790 wm->pipe[pipe].plane[PLANE_SPRITE0],
4791 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02004792 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004793
4794 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4795 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4796}
4797
Ville Syrjälä602ae832017-03-02 19:15:02 +02004798void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
4799{
4800 struct intel_plane *plane;
4801 struct intel_crtc *crtc;
4802
4803 mutex_lock(&dev_priv->wm.wm_mutex);
4804
4805 for_each_intel_plane(&dev_priv->drm, plane) {
4806 struct intel_crtc *crtc =
4807 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
4808 struct intel_crtc_state *crtc_state =
4809 to_intel_crtc_state(crtc->base.state);
4810 struct intel_plane_state *plane_state =
4811 to_intel_plane_state(plane->base.state);
4812 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
4813 const struct vlv_fifo_state *fifo_state =
4814 &crtc_state->wm.vlv.fifo_state;
4815 enum plane_id plane_id = plane->id;
4816 int level;
4817
4818 if (plane_state->base.visible)
4819 continue;
4820
4821 for (level = 0; level < wm_state->num_levels; level++) {
4822 struct vlv_pipe_wm *raw =
4823 &crtc_state->wm.vlv.raw[level];
4824
4825 raw->plane[plane_id] = 0;
4826
4827 wm_state->wm[level].plane[plane_id] =
4828 vlv_invert_wm_value(raw->plane[plane_id],
4829 fifo_state->plane[plane_id]);
4830 }
4831 }
4832
4833 for_each_intel_crtc(&dev_priv->drm, crtc) {
4834 struct intel_crtc_state *crtc_state =
4835 to_intel_crtc_state(crtc->base.state);
4836
4837 crtc_state->wm.vlv.intermediate =
4838 crtc_state->wm.vlv.optimal;
4839 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4840 }
4841
4842 vlv_program_watermarks(dev_priv);
4843
4844 mutex_unlock(&dev_priv->wm.wm_mutex);
4845}
4846
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004847void ilk_wm_get_hw_state(struct drm_device *dev)
4848{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004849 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004850 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004851 struct drm_crtc *crtc;
4852
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004853 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004854 ilk_pipe_wm_get_hw_state(crtc);
4855
4856 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4857 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4858 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4859
4860 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004861 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004862 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4863 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4864 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004865
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004866 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004867 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4868 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004869 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004870 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4871 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004872
4873 hw->enable_fbc_wm =
4874 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4875}
4876
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004877/**
4878 * intel_update_watermarks - update FIFO watermark values based on current modes
4879 *
4880 * Calculate watermark values for the various WM regs based on current mode
4881 * and plane configuration.
4882 *
4883 * There are several cases to deal with here:
4884 * - normal (i.e. non-self-refresh)
4885 * - self-refresh (SR) mode
4886 * - lines are large relative to FIFO size (buffer can hold up to 2)
4887 * - lines are small relative to FIFO size (buffer can hold more than 2
4888 * lines), so need to account for TLB latency
4889 *
4890 * The normal calculation is:
4891 * watermark = dotclock * bytes per pixel * latency
4892 * where latency is platform & configuration dependent (we assume pessimal
4893 * values here).
4894 *
4895 * The SR calculation is:
4896 * watermark = (trunc(latency/line time)+1) * surface width *
4897 * bytes per pixel
4898 * where
4899 * line time = htotal / dotclock
4900 * surface width = hdisplay for normal plane and 64 for cursor
4901 * and latency is assumed to be high, as above.
4902 *
4903 * The final value programmed to the register should always be rounded up,
4904 * and include an extra 2 entries to account for clock crossings.
4905 *
4906 * We don't use the sprite, so we can ignore that. And on Crestline we have
4907 * to set the non-SR watermarks to 8.
4908 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004909void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004910{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004911 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004912
4913 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004914 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004915}
4916
Jani Nikulae2828912016-01-18 09:19:47 +02004917/*
Daniel Vetter92703882012-08-09 16:46:01 +02004918 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004919 */
4920DEFINE_SPINLOCK(mchdev_lock);
4921
4922/* Global for IPS driver to get at the current i915 device. Protected by
4923 * mchdev_lock. */
4924static struct drm_i915_private *i915_mch_dev;
4925
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004926bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004927{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004928 u16 rgvswctl;
4929
Chris Wilson67520412017-03-02 13:28:01 +00004930 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02004931
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004932 rgvswctl = I915_READ16(MEMSWCTL);
4933 if (rgvswctl & MEMCTL_CMD_STS) {
4934 DRM_DEBUG("gpu busy, RCS change rejected\n");
4935 return false; /* still busy with another command */
4936 }
4937
4938 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4939 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4940 I915_WRITE16(MEMSWCTL, rgvswctl);
4941 POSTING_READ16(MEMSWCTL);
4942
4943 rgvswctl |= MEMCTL_CMD_STS;
4944 I915_WRITE16(MEMSWCTL, rgvswctl);
4945
4946 return true;
4947}
4948
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004949static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004950{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004951 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004952 u8 fmax, fmin, fstart, vstart;
4953
Daniel Vetter92703882012-08-09 16:46:01 +02004954 spin_lock_irq(&mchdev_lock);
4955
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004956 rgvmodectl = I915_READ(MEMMODECTL);
4957
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004958 /* Enable temp reporting */
4959 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4960 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4961
4962 /* 100ms RC evaluation intervals */
4963 I915_WRITE(RCUPEI, 100000);
4964 I915_WRITE(RCDNEI, 100000);
4965
4966 /* Set max/min thresholds to 90ms and 80ms respectively */
4967 I915_WRITE(RCBMAXAVG, 90000);
4968 I915_WRITE(RCBMINAVG, 80000);
4969
4970 I915_WRITE(MEMIHYST, 1);
4971
4972 /* Set up min, max, and cur for interrupt handling */
4973 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4974 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4975 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4976 MEMMODE_FSTART_SHIFT;
4977
Ville Syrjälä616847e2015-09-18 20:03:19 +03004978 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004979 PXVFREQ_PX_SHIFT;
4980
Daniel Vetter20e4d402012-08-08 23:35:39 +02004981 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4982 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004983
Daniel Vetter20e4d402012-08-08 23:35:39 +02004984 dev_priv->ips.max_delay = fstart;
4985 dev_priv->ips.min_delay = fmin;
4986 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004987
4988 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4989 fmax, fmin, fstart);
4990
4991 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4992
4993 /*
4994 * Interrupts will be enabled in ironlake_irq_postinstall
4995 */
4996
4997 I915_WRITE(VIDSTART, vstart);
4998 POSTING_READ(VIDSTART);
4999
5000 rgvmodectl |= MEMMODE_SWMODE_EN;
5001 I915_WRITE(MEMMODECTL, rgvmodectl);
5002
Daniel Vetter92703882012-08-09 16:46:01 +02005003 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005004 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005005 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005006
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005007 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005008
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005009 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5010 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005011 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005012 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005013 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005014
5015 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005016}
5017
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005018static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005019{
Daniel Vetter92703882012-08-09 16:46:01 +02005020 u16 rgvswctl;
5021
5022 spin_lock_irq(&mchdev_lock);
5023
5024 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005025
5026 /* Ack interrupts, disable EFC interrupt */
5027 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5028 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5029 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5030 I915_WRITE(DEIIR, DE_PCU_EVENT);
5031 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5032
5033 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005034 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005035 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005036 rgvswctl |= MEMCTL_CMD_STS;
5037 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005038 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005039
Daniel Vetter92703882012-08-09 16:46:01 +02005040 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005041}
5042
Daniel Vetteracbe9472012-07-26 11:50:05 +02005043/* There's a funny hw issue where the hw returns all 0 when reading from
5044 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5045 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5046 * all limits and the gpu stuck at whatever frequency it is at atm).
5047 */
Akash Goel74ef1172015-03-06 11:07:19 +05305048static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005049{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005050 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005051
Daniel Vetter20b46e52012-07-26 11:16:14 +02005052 /* Only set the down limit when we've reached the lowest level to avoid
5053 * getting more interrupts, otherwise leave this clear. This prevents a
5054 * race in the hw when coming out of rc6: There's a tiny window where
5055 * the hw runs at the minimal clock before selecting the desired
5056 * frequency, if the down threshold expires in that window we will not
5057 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005058 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305059 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5060 if (val <= dev_priv->rps.min_freq_softlimit)
5061 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5062 } else {
5063 limits = dev_priv->rps.max_freq_softlimit << 24;
5064 if (val <= dev_priv->rps.min_freq_softlimit)
5065 limits |= dev_priv->rps.min_freq_softlimit << 16;
5066 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005067
5068 return limits;
5069}
5070
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005071static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5072{
5073 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305074 u32 threshold_up = 0, threshold_down = 0; /* in % */
5075 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005076
5077 new_power = dev_priv->rps.power;
5078 switch (dev_priv->rps.power) {
5079 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005080 if (val > dev_priv->rps.efficient_freq + 1 &&
5081 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005082 new_power = BETWEEN;
5083 break;
5084
5085 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005086 if (val <= dev_priv->rps.efficient_freq &&
5087 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005088 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005089 else if (val >= dev_priv->rps.rp0_freq &&
5090 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005091 new_power = HIGH_POWER;
5092 break;
5093
5094 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005095 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5096 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005097 new_power = BETWEEN;
5098 break;
5099 }
5100 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005101 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005102 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005103 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005104 new_power = HIGH_POWER;
5105 if (new_power == dev_priv->rps.power)
5106 return;
5107
5108 /* Note the units here are not exactly 1us, but 1280ns. */
5109 switch (new_power) {
5110 case LOW_POWER:
5111 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305112 ei_up = 16000;
5113 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005114
5115 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305116 ei_down = 32000;
5117 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005118 break;
5119
5120 case BETWEEN:
5121 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305122 ei_up = 13000;
5123 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005124
5125 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305126 ei_down = 32000;
5127 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005128 break;
5129
5130 case HIGH_POWER:
5131 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305132 ei_up = 10000;
5133 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005134
5135 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305136 ei_down = 32000;
5137 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005138 break;
5139 }
5140
Mika Kuoppala6067a272017-02-15 15:52:59 +02005141 /* When byt can survive without system hang with dynamic
5142 * sw freq adjustments, this restriction can be lifted.
5143 */
5144 if (IS_VALLEYVIEW(dev_priv))
5145 goto skip_hw_write;
5146
Akash Goel8a586432015-03-06 11:07:18 +05305147 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005148 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305149 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005150 GT_INTERVAL_FROM_US(dev_priv,
5151 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305152
5153 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005154 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305155 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005156 GT_INTERVAL_FROM_US(dev_priv,
5157 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305158
Chris Wilsona72b5622016-07-02 15:35:59 +01005159 I915_WRITE(GEN6_RP_CONTROL,
5160 GEN6_RP_MEDIA_TURBO |
5161 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5162 GEN6_RP_MEDIA_IS_GFX |
5163 GEN6_RP_ENABLE |
5164 GEN6_RP_UP_BUSY_AVG |
5165 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305166
Mika Kuoppala6067a272017-02-15 15:52:59 +02005167skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005168 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005169 dev_priv->rps.up_threshold = threshold_up;
5170 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005171 dev_priv->rps.last_adj = 0;
5172}
5173
Chris Wilson2876ce72014-03-28 08:03:34 +00005174static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5175{
5176 u32 mask = 0;
5177
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005178 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005179 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005180 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005181 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005182 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005183
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005184 mask &= dev_priv->pm_rps_events;
5185
Imre Deak59d02a12014-12-19 19:33:26 +02005186 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005187}
5188
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005189/* gen6_set_rps is called to update the frequency request, but should also be
5190 * called when the range (min_delay and max_delay) is modified so that we can
5191 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005192static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005193{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005194 /* min/max delay may still have been modified so be sure to
5195 * write the limits value.
5196 */
5197 if (val != dev_priv->rps.cur_freq) {
5198 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005199
Chris Wilsondc979972016-05-10 14:10:04 +01005200 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305201 I915_WRITE(GEN6_RPNSWREQ,
5202 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005203 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005204 I915_WRITE(GEN6_RPNSWREQ,
5205 HSW_FREQUENCY(val));
5206 else
5207 I915_WRITE(GEN6_RPNSWREQ,
5208 GEN6_FREQUENCY(val) |
5209 GEN6_OFFSET(0) |
5210 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005211 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005212
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005213 /* Make sure we continue to get interrupts
5214 * until we hit the minimum or maximum frequencies.
5215 */
Akash Goel74ef1172015-03-06 11:07:19 +05305216 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005217 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005218
Ben Widawskyb39fb292014-03-19 18:31:11 -07005219 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005220 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005221
5222 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005223}
5224
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005225static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005226{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005227 int err;
5228
Chris Wilsondc979972016-05-10 14:10:04 +01005229 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005230 "Odd GPU freq value\n"))
5231 val &= ~1;
5232
Deepak Scd25dd52015-07-10 18:31:40 +05305233 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5234
Chris Wilson8fb55192015-04-07 16:20:28 +01005235 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005236 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5237 if (err)
5238 return err;
5239
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005240 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005241 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005242
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005243 dev_priv->rps.cur_freq = val;
5244 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005245
5246 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005247}
5248
Deepak Sa7f6e232015-05-09 18:04:44 +05305249/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305250 *
5251 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305252 * 1. Forcewake Media well.
5253 * 2. Request idle freq.
5254 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305255*/
5256static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5257{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005258 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005259 int err;
Deepak S5549d252014-06-28 11:26:11 +05305260
Chris Wilsonaed242f2015-03-18 09:48:21 +00005261 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305262 return;
5263
Chris Wilsonc9efef72017-01-02 15:28:45 +00005264 /* The punit delays the write of the frequency and voltage until it
5265 * determines the GPU is awake. During normal usage we don't want to
5266 * waste power changing the frequency if the GPU is sleeping (rc6).
5267 * However, the GPU and driver is now idle and we do not want to delay
5268 * switching to minimum voltage (reducing power whilst idle) as we do
5269 * not expect to be woken in the near future and so must flush the
5270 * change by waking the device.
5271 *
5272 * We choose to take the media powerwell (either would do to trick the
5273 * punit into committing the voltage change) as that takes a lot less
5274 * power than the render powerwell.
5275 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305276 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005277 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305278 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005279
5280 if (err)
5281 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305282}
5283
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005284void gen6_rps_busy(struct drm_i915_private *dev_priv)
5285{
5286 mutex_lock(&dev_priv->rps.hw_lock);
5287 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005288 u8 freq;
5289
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005290 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005291 gen6_rps_reset_ei(dev_priv);
5292 I915_WRITE(GEN6_PMINTRMSK,
5293 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005294
Chris Wilsonc33d2472016-07-04 08:08:36 +01005295 gen6_enable_rps_interrupts(dev_priv);
5296
Chris Wilsonbd648182017-02-10 15:03:48 +00005297 /* Use the user's desired frequency as a guide, but for better
5298 * performance, jump directly to RPe as our starting frequency.
5299 */
5300 freq = max(dev_priv->rps.cur_freq,
5301 dev_priv->rps.efficient_freq);
5302
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005303 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005304 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005305 dev_priv->rps.min_freq_softlimit,
5306 dev_priv->rps.max_freq_softlimit)))
5307 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005308 }
5309 mutex_unlock(&dev_priv->rps.hw_lock);
5310}
5311
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005312void gen6_rps_idle(struct drm_i915_private *dev_priv)
5313{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005314 /* Flush our bottom-half so that it does not race with us
5315 * setting the idle frequency and so that it is bounded by
5316 * our rpm wakeref. And then disable the interrupts to stop any
5317 * futher RPS reclocking whilst we are asleep.
5318 */
5319 gen6_disable_rps_interrupts(dev_priv);
5320
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005321 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005322 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005323 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305324 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005325 else
Chris Wilsondc979972016-05-10 14:10:04 +01005326 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005327 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005328 I915_WRITE(GEN6_PMINTRMSK,
5329 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005330 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005331 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005332
Chris Wilson8d3afd72015-05-21 21:01:47 +01005333 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005334 while (!list_empty(&dev_priv->rps.clients))
5335 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005336 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005337}
5338
Chris Wilson1854d5c2015-04-07 16:20:32 +01005339void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005340 struct intel_rps_client *rps,
5341 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005342{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005343 /* This is intentionally racy! We peek at the state here, then
5344 * validate inside the RPS worker.
5345 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005346 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005347 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005348 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005349 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005350
Chris Wilsone61b9952015-04-27 13:41:24 +01005351 /* Force a RPS boost (and don't count it against the client) if
5352 * the GPU is severely congested.
5353 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005354 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005355 rps = NULL;
5356
Chris Wilson8d3afd72015-05-21 21:01:47 +01005357 spin_lock(&dev_priv->rps.client_lock);
5358 if (rps == NULL || list_empty(&rps->link)) {
5359 spin_lock_irq(&dev_priv->irq_lock);
5360 if (dev_priv->rps.interrupts_enabled) {
5361 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005362 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005363 }
5364 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005365
Chris Wilson2e1b8732015-04-27 13:41:22 +01005366 if (rps != NULL) {
5367 list_add(&rps->link, &dev_priv->rps.clients);
5368 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005369 } else
5370 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005371 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005372 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005373}
5374
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005375int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005376{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005377 int err;
5378
Chris Wilsoncfd1c482017-02-20 09:47:07 +00005379 lockdep_assert_held(&dev_priv->rps.hw_lock);
5380 GEM_BUG_ON(val > dev_priv->rps.max_freq);
5381 GEM_BUG_ON(val < dev_priv->rps.min_freq);
5382
Chris Wilson76e4e4b2017-02-20 09:47:08 +00005383 if (!dev_priv->rps.enabled) {
5384 dev_priv->rps.cur_freq = val;
5385 return 0;
5386 }
5387
Chris Wilsondc979972016-05-10 14:10:04 +01005388 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005389 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005390 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005391 err = gen6_set_rps(dev_priv, val);
5392
5393 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005394}
5395
Chris Wilsondc979972016-05-10 14:10:04 +01005396static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005397{
Zhe Wang20e49362014-11-04 17:07:05 +00005398 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005399 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005400}
5401
Chris Wilsondc979972016-05-10 14:10:04 +01005402static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305403{
Akash Goel2030d682016-04-23 00:05:45 +05305404 I915_WRITE(GEN6_RP_CONTROL, 0);
5405}
5406
Chris Wilsondc979972016-05-10 14:10:04 +01005407static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005408{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005409 I915_WRITE(GEN6_RC_CONTROL, 0);
5410 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305411 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005412}
5413
Chris Wilsondc979972016-05-10 14:10:04 +01005414static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305415{
Deepak S38807742014-05-23 21:00:15 +05305416 I915_WRITE(GEN6_RC_CONTROL, 0);
5417}
5418
Chris Wilsondc979972016-05-10 14:10:04 +01005419static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005420{
Deepak S98a2e5f2014-08-18 10:35:27 -07005421 /* we're doing forcewake before Disabling RC6,
5422 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005423 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005424
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005425 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005426
Mika Kuoppala59bad942015-01-16 11:34:40 +02005427 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005428}
5429
Chris Wilsondc979972016-05-10 14:10:04 +01005430static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005431{
Chris Wilsondc979972016-05-10 14:10:04 +01005432 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005433 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5434 mode = GEN6_RC_CTL_RC6_ENABLE;
5435 else
5436 mode = 0;
5437 }
Chris Wilsondc979972016-05-10 14:10:04 +01005438 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005439 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5440 "RC6 %s RC6p %s RC6pp %s\n",
5441 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5442 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5443 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005444
5445 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005446 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5447 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005448}
5449
Chris Wilsondc979972016-05-10 14:10:04 +01005450static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305451{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005452 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305453 bool enable_rc6 = true;
5454 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005455 u32 rc_ctl;
5456 int rc_sw_target;
5457
5458 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5459 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5460 RC_SW_TARGET_STATE_SHIFT;
5461 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5462 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5463 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5464 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5465 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305466
5467 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005468 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305469 enable_rc6 = false;
5470 }
5471
5472 /*
5473 * The exact context size is not known for BXT, so assume a page size
5474 * for this check.
5475 */
5476 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005477 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5478 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5479 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005480 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305481 enable_rc6 = false;
5482 }
5483
5484 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5485 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5486 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5487 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005488 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305489 enable_rc6 = false;
5490 }
5491
Imre Deakfc619842016-06-29 19:13:55 +03005492 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5493 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5494 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5495 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5496 enable_rc6 = false;
5497 }
5498
5499 if (!I915_READ(GEN6_GFXPAUSE)) {
5500 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5501 enable_rc6 = false;
5502 }
5503
5504 if (!I915_READ(GEN8_MISC_CTRL0)) {
5505 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305506 enable_rc6 = false;
5507 }
5508
5509 return enable_rc6;
5510}
5511
Chris Wilsondc979972016-05-10 14:10:04 +01005512int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005513{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005514 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005515 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005516 return 0;
5517
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305518 if (!enable_rc6)
5519 return 0;
5520
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005521 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305522 DRM_INFO("RC6 disabled by BIOS\n");
5523 return 0;
5524 }
5525
Daniel Vetter456470e2012-08-08 23:35:40 +02005526 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005527 if (enable_rc6 >= 0) {
5528 int mask;
5529
Chris Wilsondc979972016-05-10 14:10:04 +01005530 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005531 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5532 INTEL_RC6pp_ENABLE;
5533 else
5534 mask = INTEL_RC6_ENABLE;
5535
5536 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005537 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5538 "(requested %d, valid %d)\n",
5539 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005540
5541 return enable_rc6 & mask;
5542 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005543
Chris Wilsondc979972016-05-10 14:10:04 +01005544 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005545 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005546
5547 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005548}
5549
Chris Wilsondc979972016-05-10 14:10:04 +01005550static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005551{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005552 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005553
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005554 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005555 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005556 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005557 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5558 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5559 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5560 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005561 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005562 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5563 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5564 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5565 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005566 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005567 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005568
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005569 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005570 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005571 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005572 u32 ddcc_status = 0;
5573
5574 if (sandybridge_pcode_read(dev_priv,
5575 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5576 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005577 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005578 clamp_t(u8,
5579 ((ddcc_status >> 8) & 0xff),
5580 dev_priv->rps.min_freq,
5581 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005582 }
5583
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005584 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305585 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005586 * the natural hardware unit for SKL
5587 */
Akash Goelc5e06882015-06-29 14:50:19 +05305588 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5589 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5590 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5591 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5592 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5593 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005594}
5595
Chris Wilson3a45b052016-07-13 09:10:32 +01005596static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005597 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01005598{
5599 u8 freq = dev_priv->rps.cur_freq;
5600
5601 /* force a reset */
5602 dev_priv->rps.power = -1;
5603 dev_priv->rps.cur_freq = -1;
5604
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005605 if (set(dev_priv, freq))
5606 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01005607}
5608
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005609/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005610static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005611{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005612 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5613
Akash Goel0beb0592015-03-06 11:07:20 +05305614 /* Program defaults and thresholds for RPS*/
5615 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5616 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005617
Akash Goel0beb0592015-03-06 11:07:20 +05305618 /* 1 second timeout*/
5619 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5620 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5621
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005622 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005623
Akash Goel0beb0592015-03-06 11:07:20 +05305624 /* Leaning on the below call to gen6_set_rps to program/setup the
5625 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5626 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005627 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005628
5629 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5630}
5631
Chris Wilsondc979972016-05-10 14:10:04 +01005632static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005633{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005634 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305635 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005636 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005637
5638 /* 1a: Software RC state - RC0 */
5639 I915_WRITE(GEN6_RC_STATE, 0);
5640
5641 /* 1b: Get forcewake during program sequence. Although the driver
5642 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005643 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005644
5645 /* 2a: Disable RC states. */
5646 I915_WRITE(GEN6_RC_CONTROL, 0);
5647
5648 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305649
5650 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005651 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305652 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5653 else
5654 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005655 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5656 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305657 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005658 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305659
Dave Gordon1a3d1892016-05-13 15:36:30 +01005660 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305661 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5662
Zhe Wang20e49362014-11-04 17:07:05 +00005663 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005664
Zhe Wang38c23522015-01-20 12:23:04 +00005665 /* 2c: Program Coarse Power Gating Policies. */
5666 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5667 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5668
Zhe Wang20e49362014-11-04 17:07:05 +00005669 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005670 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005671 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005672 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00005673 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
5674 I915_WRITE(GEN6_RC_CONTROL,
5675 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00005676
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305677 /*
5678 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305679 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305680 */
Chris Wilsondc979972016-05-10 14:10:04 +01005681 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305682 I915_WRITE(GEN9_PG_ENABLE, 0);
5683 else
5684 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5685 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005686
Mika Kuoppala59bad942015-01-16 11:34:40 +02005687 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005688}
5689
Chris Wilsondc979972016-05-10 14:10:04 +01005690static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005691{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005692 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305693 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005694 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005695
5696 /* 1a: Software RC state - RC0 */
5697 I915_WRITE(GEN6_RC_STATE, 0);
5698
5699 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5700 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005701 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005702
5703 /* 2a: Disable RC states. */
5704 I915_WRITE(GEN6_RC_CONTROL, 0);
5705
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005706 /* 2b: Program RC6 thresholds.*/
5707 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5708 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5709 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305710 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005711 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005712 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005713 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005714 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5715 else
5716 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005717
5718 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005719 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005720 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005721 intel_print_rc6_info(dev_priv, rc6_mask);
5722 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005723 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5724 GEN7_RC_CTL_TO_MODE |
5725 rc6_mask);
5726 else
5727 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5728 GEN6_RC_CTL_EI_MODE(1) |
5729 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005730
5731 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005732 I915_WRITE(GEN6_RPNSWREQ,
5733 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5734 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5735 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005736 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5737 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005738
Daniel Vetter7526ed72014-09-29 15:07:19 +02005739 /* Docs recommend 900MHz, and 300 MHz respectively */
5740 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5741 dev_priv->rps.max_freq_softlimit << 24 |
5742 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005743
Daniel Vetter7526ed72014-09-29 15:07:19 +02005744 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5745 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5746 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5747 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005748
Daniel Vetter7526ed72014-09-29 15:07:19 +02005749 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005750
5751 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005752 I915_WRITE(GEN6_RP_CONTROL,
5753 GEN6_RP_MEDIA_TURBO |
5754 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5755 GEN6_RP_MEDIA_IS_GFX |
5756 GEN6_RP_ENABLE |
5757 GEN6_RP_UP_BUSY_AVG |
5758 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005759
Daniel Vetter7526ed72014-09-29 15:07:19 +02005760 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005761
Chris Wilson3a45b052016-07-13 09:10:32 +01005762 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005763
Mika Kuoppala59bad942015-01-16 11:34:40 +02005764 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005765}
5766
Chris Wilsondc979972016-05-10 14:10:04 +01005767static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005768{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005769 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305770 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005771 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005772 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005773 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005774 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005775
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005776 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005777
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005778 /* Here begins a magic sequence of register writes to enable
5779 * auto-downclocking.
5780 *
5781 * Perhaps there might be some value in exposing these to
5782 * userspace...
5783 */
5784 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005785
5786 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005787 gtfifodbg = I915_READ(GTFIFODBG);
5788 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005789 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5790 I915_WRITE(GTFIFODBG, gtfifodbg);
5791 }
5792
Mika Kuoppala59bad942015-01-16 11:34:40 +02005793 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005794
5795 /* disable the counters and set deterministic thresholds */
5796 I915_WRITE(GEN6_RC_CONTROL, 0);
5797
5798 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5799 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5800 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5801 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5802 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5803
Akash Goel3b3f1652016-10-13 22:44:48 +05305804 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005805 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005806
5807 I915_WRITE(GEN6_RC_SLEEP, 0);
5808 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005809 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005810 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5811 else
5812 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005813 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005814 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5815
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005816 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005817 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005818 if (rc6_mode & INTEL_RC6_ENABLE)
5819 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5820
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005821 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005822 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005823 if (rc6_mode & INTEL_RC6p_ENABLE)
5824 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005825
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005826 if (rc6_mode & INTEL_RC6pp_ENABLE)
5827 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5828 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005829
Chris Wilsondc979972016-05-10 14:10:04 +01005830 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005831
5832 I915_WRITE(GEN6_RC_CONTROL,
5833 rc6_mask |
5834 GEN6_RC_CTL_EI_MODE(1) |
5835 GEN6_RC_CTL_HW_ENABLE);
5836
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005837 /* Power down if completely idle for over 50ms */
5838 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005839 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005840
Chris Wilson3a45b052016-07-13 09:10:32 +01005841 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005842
Ben Widawsky31643d52012-09-26 10:34:01 -07005843 rc6vids = 0;
5844 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005845 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005846 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005847 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005848 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5849 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5850 rc6vids &= 0xffff00;
5851 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5852 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5853 if (ret)
5854 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5855 }
5856
Mika Kuoppala59bad942015-01-16 11:34:40 +02005857 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005858}
5859
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005860static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005861{
5862 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005863 unsigned int gpu_freq;
5864 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305865 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005866 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005867 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005868
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005869 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005870
Ben Widawskyeda79642013-10-07 17:15:48 -03005871 policy = cpufreq_cpu_get(0);
5872 if (policy) {
5873 max_ia_freq = policy->cpuinfo.max_freq;
5874 cpufreq_cpu_put(policy);
5875 } else {
5876 /*
5877 * Default to measured freq if none found, PCU will ensure we
5878 * don't go over
5879 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005880 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005881 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005882
5883 /* Convert from kHz to MHz */
5884 max_ia_freq /= 1000;
5885
Ben Widawsky153b4b952013-10-22 22:05:09 -07005886 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005887 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5888 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005889
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005890 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305891 /* Convert GT frequency to 50 HZ units */
5892 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5893 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5894 } else {
5895 min_gpu_freq = dev_priv->rps.min_freq;
5896 max_gpu_freq = dev_priv->rps.max_freq;
5897 }
5898
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005899 /*
5900 * For each potential GPU frequency, load a ring frequency we'd like
5901 * to use for memory access. We do this by specifying the IA frequency
5902 * the PCU should use as a reference to determine the ring frequency.
5903 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305904 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5905 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005906 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005907
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005908 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305909 /*
5910 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5911 * No floor required for ring frequency on SKL.
5912 */
5913 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005914 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005915 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5916 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005917 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005918 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005919 ring_freq = max(min_ring_freq, ring_freq);
5920 /* leave ia_freq as the default, chosen by cpufreq */
5921 } else {
5922 /* On older processors, there is no separate ring
5923 * clock domain, so in order to boost the bandwidth
5924 * of the ring, we need to upclock the CPU (ia_freq).
5925 *
5926 * For GPU frequencies less than 750MHz,
5927 * just use the lowest ring freq.
5928 */
5929 if (gpu_freq < min_freq)
5930 ia_freq = 800;
5931 else
5932 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5933 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5934 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005935
Ben Widawsky42c05262012-09-26 10:34:00 -07005936 sandybridge_pcode_write(dev_priv,
5937 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005938 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5939 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5940 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005941 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005942}
5943
Ville Syrjälä03af2042014-06-28 02:03:53 +03005944static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305945{
5946 u32 val, rp0;
5947
Jani Nikula5b5929c2015-10-07 11:17:46 +03005948 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305949
Imre Deak43b67992016-08-31 19:13:02 +03005950 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005951 case 8:
5952 /* (2 * 4) config */
5953 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5954 break;
5955 case 12:
5956 /* (2 * 6) config */
5957 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5958 break;
5959 case 16:
5960 /* (2 * 8) config */
5961 default:
5962 /* Setting (2 * 8) Min RP0 for any other combination */
5963 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5964 break;
Deepak S095acd52015-01-17 11:05:59 +05305965 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005966
5967 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5968
Deepak S2b6b3a02014-05-27 15:59:30 +05305969 return rp0;
5970}
5971
5972static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5973{
5974 u32 val, rpe;
5975
5976 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5977 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5978
5979 return rpe;
5980}
5981
Deepak S7707df42014-07-12 18:46:14 +05305982static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5983{
5984 u32 val, rp1;
5985
Jani Nikula5b5929c2015-10-07 11:17:46 +03005986 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5987 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5988
Deepak S7707df42014-07-12 18:46:14 +05305989 return rp1;
5990}
5991
Deepak S96676fe2016-08-12 18:46:41 +05305992static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
5993{
5994 u32 val, rpn;
5995
5996 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
5997 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
5998 FB_GFX_FREQ_FUSE_MASK);
5999
6000 return rpn;
6001}
6002
Deepak Sf8f2b002014-07-10 13:16:21 +05306003static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
6004{
6005 u32 val, rp1;
6006
6007 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
6008
6009 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6010
6011 return rp1;
6012}
6013
Ville Syrjälä03af2042014-06-28 02:03:53 +03006014static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006015{
6016 u32 val, rp0;
6017
Jani Nikula64936252013-05-22 15:36:20 +03006018 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006019
6020 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6021 /* Clamp to max */
6022 rp0 = min_t(u32, rp0, 0xea);
6023
6024 return rp0;
6025}
6026
6027static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6028{
6029 u32 val, rpe;
6030
Jani Nikula64936252013-05-22 15:36:20 +03006031 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006032 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006033 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006034 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6035
6036 return rpe;
6037}
6038
Ville Syrjälä03af2042014-06-28 02:03:53 +03006039static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006040{
Imre Deak36146032014-12-04 18:39:35 +02006041 u32 val;
6042
6043 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6044 /*
6045 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6046 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6047 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6048 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6049 * to make sure it matches what Punit accepts.
6050 */
6051 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006052}
6053
Imre Deakae484342014-03-31 15:10:44 +03006054/* Check that the pctx buffer wasn't move under us. */
6055static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6056{
6057 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6058
6059 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6060 dev_priv->vlv_pctx->stolen->start);
6061}
6062
Deepak S38807742014-05-23 21:00:15 +05306063
6064/* Check that the pcbr address is not empty. */
6065static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6066{
6067 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6068
6069 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6070}
6071
Chris Wilsondc979972016-05-10 14:10:04 +01006072static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306073{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006074 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006075 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306076 u32 pcbr;
6077 int pctx_size = 32*1024;
6078
Deepak S38807742014-05-23 21:00:15 +05306079 pcbr = I915_READ(VLV_PCBR);
6080 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006081 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306082 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006083 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306084
6085 pctx_paddr = (paddr & (~4095));
6086 I915_WRITE(VLV_PCBR, pctx_paddr);
6087 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006088
6089 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306090}
6091
Chris Wilsondc979972016-05-10 14:10:04 +01006092static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006093{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006094 struct drm_i915_gem_object *pctx;
6095 unsigned long pctx_paddr;
6096 u32 pcbr;
6097 int pctx_size = 24*1024;
6098
6099 pcbr = I915_READ(VLV_PCBR);
6100 if (pcbr) {
6101 /* BIOS set it up already, grab the pre-alloc'd space */
6102 int pcbr_offset;
6103
6104 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006105 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006106 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006107 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006108 pctx_size);
6109 goto out;
6110 }
6111
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006112 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6113
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006114 /*
6115 * From the Gunit register HAS:
6116 * The Gfx driver is expected to program this register and ensure
6117 * proper allocation within Gfx stolen memory. For example, this
6118 * register should be programmed such than the PCBR range does not
6119 * overlap with other ranges, such as the frame buffer, protected
6120 * memory, or any other relevant ranges.
6121 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006122 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006123 if (!pctx) {
6124 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006125 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006126 }
6127
6128 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6129 I915_WRITE(VLV_PCBR, pctx_paddr);
6130
6131out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006132 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006133 dev_priv->vlv_pctx = pctx;
6134}
6135
Chris Wilsondc979972016-05-10 14:10:04 +01006136static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006137{
Imre Deakae484342014-03-31 15:10:44 +03006138 if (WARN_ON(!dev_priv->vlv_pctx))
6139 return;
6140
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006141 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006142 dev_priv->vlv_pctx = NULL;
6143}
6144
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006145static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6146{
6147 dev_priv->rps.gpll_ref_freq =
6148 vlv_get_cck_clock(dev_priv, "GPLL ref",
6149 CCK_GPLL_CLOCK_CONTROL,
6150 dev_priv->czclk_freq);
6151
6152 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6153 dev_priv->rps.gpll_ref_freq);
6154}
6155
Chris Wilsondc979972016-05-10 14:10:04 +01006156static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006157{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006158 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006159
Chris Wilsondc979972016-05-10 14:10:04 +01006160 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006161
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006162 vlv_init_gpll_ref_freq(dev_priv);
6163
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006164 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6165 switch ((val >> 6) & 3) {
6166 case 0:
6167 case 1:
6168 dev_priv->mem_freq = 800;
6169 break;
6170 case 2:
6171 dev_priv->mem_freq = 1066;
6172 break;
6173 case 3:
6174 dev_priv->mem_freq = 1333;
6175 break;
6176 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006177 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006178
Imre Deak4e805192014-04-14 20:24:41 +03006179 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6180 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6181 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006182 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006183 dev_priv->rps.max_freq);
6184
6185 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6186 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006187 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006188 dev_priv->rps.efficient_freq);
6189
Deepak Sf8f2b002014-07-10 13:16:21 +05306190 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6191 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006192 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306193 dev_priv->rps.rp1_freq);
6194
Imre Deak4e805192014-04-14 20:24:41 +03006195 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6196 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006197 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006198 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006199}
6200
Chris Wilsondc979972016-05-10 14:10:04 +01006201static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306202{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006203 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306204
Chris Wilsondc979972016-05-10 14:10:04 +01006205 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306206
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006207 vlv_init_gpll_ref_freq(dev_priv);
6208
Ville Syrjäläa5805162015-05-26 20:42:30 +03006209 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006210 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006211 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006212
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006213 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006214 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006215 dev_priv->mem_freq = 2000;
6216 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006217 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006218 dev_priv->mem_freq = 1600;
6219 break;
6220 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006221 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006222
Deepak S2b6b3a02014-05-27 15:59:30 +05306223 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6224 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6225 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006226 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306227 dev_priv->rps.max_freq);
6228
6229 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6230 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006231 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306232 dev_priv->rps.efficient_freq);
6233
Deepak S7707df42014-07-12 18:46:14 +05306234 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6235 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006236 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306237 dev_priv->rps.rp1_freq);
6238
Deepak S96676fe2016-08-12 18:46:41 +05306239 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306240 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006241 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306242 dev_priv->rps.min_freq);
6243
Ville Syrjälä1c147622014-08-18 14:42:43 +03006244 WARN_ONCE((dev_priv->rps.max_freq |
6245 dev_priv->rps.efficient_freq |
6246 dev_priv->rps.rp1_freq |
6247 dev_priv->rps.min_freq) & 1,
6248 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306249}
6250
Chris Wilsondc979972016-05-10 14:10:04 +01006251static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006252{
Chris Wilsondc979972016-05-10 14:10:04 +01006253 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006254}
6255
Chris Wilsondc979972016-05-10 14:10:04 +01006256static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306257{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006258 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306259 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306260 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306261
6262 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6263
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006264 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6265 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306266 if (gtfifodbg) {
6267 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6268 gtfifodbg);
6269 I915_WRITE(GTFIFODBG, gtfifodbg);
6270 }
6271
6272 cherryview_check_pctx(dev_priv);
6273
6274 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6275 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006276 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306277
Ville Syrjälä160614a2015-01-19 13:50:47 +02006278 /* Disable RC states. */
6279 I915_WRITE(GEN6_RC_CONTROL, 0);
6280
Deepak S38807742014-05-23 21:00:15 +05306281 /* 2a: Program RC6 thresholds.*/
6282 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6283 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6284 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6285
Akash Goel3b3f1652016-10-13 22:44:48 +05306286 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006287 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306288 I915_WRITE(GEN6_RC_SLEEP, 0);
6289
Deepak Sf4f71c72015-03-28 15:23:35 +05306290 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6291 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306292
6293 /* allows RC6 residency counter to work */
6294 I915_WRITE(VLV_COUNTER_CONTROL,
6295 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6296 VLV_MEDIA_RC6_COUNT_EN |
6297 VLV_RENDER_RC6_COUNT_EN));
6298
6299 /* For now we assume BIOS is allocating and populating the PCBR */
6300 pcbr = I915_READ(VLV_PCBR);
6301
Deepak S38807742014-05-23 21:00:15 +05306302 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006303 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6304 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006305 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306306
6307 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6308
Deepak S2b6b3a02014-05-27 15:59:30 +05306309 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006310 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306311 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6312 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6313 I915_WRITE(GEN6_RP_UP_EI, 66000);
6314 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6315
6316 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6317
6318 /* 5: Enable RPS */
6319 I915_WRITE(GEN6_RP_CONTROL,
6320 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006321 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306322 GEN6_RP_ENABLE |
6323 GEN6_RP_UP_BUSY_AVG |
6324 GEN6_RP_DOWN_IDLE_AVG);
6325
Deepak S3ef62342015-04-29 08:36:24 +05306326 /* Setting Fixed Bias */
6327 val = VLV_OVERRIDE_EN |
6328 VLV_SOC_TDP_EN |
6329 CHV_BIAS_CPU_50_SOC_50;
6330 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6331
Deepak S2b6b3a02014-05-27 15:59:30 +05306332 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6333
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006334 /* RPS code assumes GPLL is used */
6335 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6336
Jani Nikula742f4912015-09-03 11:16:09 +03006337 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306338 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6339
Chris Wilson3a45b052016-07-13 09:10:32 +01006340 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306341
Mika Kuoppala59bad942015-01-16 11:34:40 +02006342 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306343}
6344
Chris Wilsondc979972016-05-10 14:10:04 +01006345static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006346{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006347 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306348 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006349 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006350
6351 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6352
Imre Deakae484342014-03-31 15:10:44 +03006353 valleyview_check_pctx(dev_priv);
6354
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006355 gtfifodbg = I915_READ(GTFIFODBG);
6356 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006357 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6358 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006359 I915_WRITE(GTFIFODBG, gtfifodbg);
6360 }
6361
Deepak Sc8d9a592013-11-23 14:55:42 +05306362 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006363 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006364
Ville Syrjälä160614a2015-01-19 13:50:47 +02006365 /* Disable RC states. */
6366 I915_WRITE(GEN6_RC_CONTROL, 0);
6367
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006368 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006369 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6370 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6371 I915_WRITE(GEN6_RP_UP_EI, 66000);
6372 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6373
6374 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6375
6376 I915_WRITE(GEN6_RP_CONTROL,
6377 GEN6_RP_MEDIA_TURBO |
6378 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6379 GEN6_RP_MEDIA_IS_GFX |
6380 GEN6_RP_ENABLE |
6381 GEN6_RP_UP_BUSY_AVG |
6382 GEN6_RP_DOWN_IDLE_CONT);
6383
6384 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6385 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6386 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6387
Akash Goel3b3f1652016-10-13 22:44:48 +05306388 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006389 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006390
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08006391 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006392
6393 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006394 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04006395 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
6396 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006397 VLV_MEDIA_RC6_COUNT_EN |
6398 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006399
Chris Wilsondc979972016-05-10 14:10:04 +01006400 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006401 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006402
Chris Wilsondc979972016-05-10 14:10:04 +01006403 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006404
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006405 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006406
Deepak S3ef62342015-04-29 08:36:24 +05306407 /* Setting Fixed Bias */
6408 val = VLV_OVERRIDE_EN |
6409 VLV_SOC_TDP_EN |
6410 VLV_BIAS_CPU_125_SOC_875;
6411 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6412
Jani Nikula64936252013-05-22 15:36:20 +03006413 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006414
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006415 /* RPS code assumes GPLL is used */
6416 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6417
Jani Nikula742f4912015-09-03 11:16:09 +03006418 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006419 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6420
Chris Wilson3a45b052016-07-13 09:10:32 +01006421 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006422
Mika Kuoppala59bad942015-01-16 11:34:40 +02006423 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006424}
6425
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006426static unsigned long intel_pxfreq(u32 vidfreq)
6427{
6428 unsigned long freq;
6429 int div = (vidfreq & 0x3f0000) >> 16;
6430 int post = (vidfreq & 0x3000) >> 12;
6431 int pre = (vidfreq & 0x7);
6432
6433 if (!pre)
6434 return 0;
6435
6436 freq = ((div * 133333) / ((1<<post) * pre));
6437
6438 return freq;
6439}
6440
Daniel Vettereb48eb02012-04-26 23:28:12 +02006441static const struct cparams {
6442 u16 i;
6443 u16 t;
6444 u16 m;
6445 u16 c;
6446} cparams[] = {
6447 { 1, 1333, 301, 28664 },
6448 { 1, 1066, 294, 24460 },
6449 { 1, 800, 294, 25192 },
6450 { 0, 1333, 276, 27605 },
6451 { 0, 1066, 276, 27605 },
6452 { 0, 800, 231, 23784 },
6453};
6454
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006455static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006456{
6457 u64 total_count, diff, ret;
6458 u32 count1, count2, count3, m = 0, c = 0;
6459 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6460 int i;
6461
Chris Wilson67520412017-03-02 13:28:01 +00006462 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006463
Daniel Vetter20e4d402012-08-08 23:35:39 +02006464 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006465
6466 /* Prevent division-by-zero if we are asking too fast.
6467 * Also, we don't get interesting results if we are polling
6468 * faster than once in 10ms, so just return the saved value
6469 * in such cases.
6470 */
6471 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006472 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006473
6474 count1 = I915_READ(DMIEC);
6475 count2 = I915_READ(DDREC);
6476 count3 = I915_READ(CSIEC);
6477
6478 total_count = count1 + count2 + count3;
6479
6480 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006481 if (total_count < dev_priv->ips.last_count1) {
6482 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006483 diff += total_count;
6484 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006485 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006486 }
6487
6488 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006489 if (cparams[i].i == dev_priv->ips.c_m &&
6490 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006491 m = cparams[i].m;
6492 c = cparams[i].c;
6493 break;
6494 }
6495 }
6496
6497 diff = div_u64(diff, diff1);
6498 ret = ((m * diff) + c);
6499 ret = div_u64(ret, 10);
6500
Daniel Vetter20e4d402012-08-08 23:35:39 +02006501 dev_priv->ips.last_count1 = total_count;
6502 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006503
Daniel Vetter20e4d402012-08-08 23:35:39 +02006504 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006505
6506 return ret;
6507}
6508
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006509unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6510{
6511 unsigned long val;
6512
Chris Wilsondc979972016-05-10 14:10:04 +01006513 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006514 return 0;
6515
6516 spin_lock_irq(&mchdev_lock);
6517
6518 val = __i915_chipset_val(dev_priv);
6519
6520 spin_unlock_irq(&mchdev_lock);
6521
6522 return val;
6523}
6524
Daniel Vettereb48eb02012-04-26 23:28:12 +02006525unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6526{
6527 unsigned long m, x, b;
6528 u32 tsfs;
6529
6530 tsfs = I915_READ(TSFS);
6531
6532 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6533 x = I915_READ8(TR1);
6534
6535 b = tsfs & TSFS_INTR_MASK;
6536
6537 return ((m * x) / 127) - b;
6538}
6539
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006540static int _pxvid_to_vd(u8 pxvid)
6541{
6542 if (pxvid == 0)
6543 return 0;
6544
6545 if (pxvid >= 8 && pxvid < 31)
6546 pxvid = 31;
6547
6548 return (pxvid + 2) * 125;
6549}
6550
6551static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006552{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006553 const int vd = _pxvid_to_vd(pxvid);
6554 const int vm = vd - 1125;
6555
Chris Wilsondc979972016-05-10 14:10:04 +01006556 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006557 return vm > 0 ? vm : 0;
6558
6559 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006560}
6561
Daniel Vetter02d71952012-08-09 16:44:54 +02006562static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006563{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006564 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006565 u32 count;
6566
Chris Wilson67520412017-03-02 13:28:01 +00006567 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006568
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006569 now = ktime_get_raw_ns();
6570 diffms = now - dev_priv->ips.last_time2;
6571 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006572
6573 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006574 if (!diffms)
6575 return;
6576
6577 count = I915_READ(GFXEC);
6578
Daniel Vetter20e4d402012-08-08 23:35:39 +02006579 if (count < dev_priv->ips.last_count2) {
6580 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006581 diff += count;
6582 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006583 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006584 }
6585
Daniel Vetter20e4d402012-08-08 23:35:39 +02006586 dev_priv->ips.last_count2 = count;
6587 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006588
6589 /* More magic constants... */
6590 diff = diff * 1181;
6591 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006592 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006593}
6594
Daniel Vetter02d71952012-08-09 16:44:54 +02006595void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6596{
Chris Wilsondc979972016-05-10 14:10:04 +01006597 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006598 return;
6599
Daniel Vetter92703882012-08-09 16:46:01 +02006600 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006601
6602 __i915_update_gfx_val(dev_priv);
6603
Daniel Vetter92703882012-08-09 16:46:01 +02006604 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006605}
6606
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006607static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006608{
6609 unsigned long t, corr, state1, corr2, state2;
6610 u32 pxvid, ext_v;
6611
Chris Wilson67520412017-03-02 13:28:01 +00006612 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006613
Ville Syrjälä616847e2015-09-18 20:03:19 +03006614 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006615 pxvid = (pxvid >> 24) & 0x7f;
6616 ext_v = pvid_to_extvid(dev_priv, pxvid);
6617
6618 state1 = ext_v;
6619
6620 t = i915_mch_val(dev_priv);
6621
6622 /* Revel in the empirically derived constants */
6623
6624 /* Correction factor in 1/100000 units */
6625 if (t > 80)
6626 corr = ((t * 2349) + 135940);
6627 else if (t >= 50)
6628 corr = ((t * 964) + 29317);
6629 else /* < 50 */
6630 corr = ((t * 301) + 1004);
6631
6632 corr = corr * ((150142 * state1) / 10000 - 78642);
6633 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006634 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006635
6636 state2 = (corr2 * state1) / 10000;
6637 state2 /= 100; /* convert to mW */
6638
Daniel Vetter02d71952012-08-09 16:44:54 +02006639 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006640
Daniel Vetter20e4d402012-08-08 23:35:39 +02006641 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006642}
6643
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006644unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6645{
6646 unsigned long val;
6647
Chris Wilsondc979972016-05-10 14:10:04 +01006648 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006649 return 0;
6650
6651 spin_lock_irq(&mchdev_lock);
6652
6653 val = __i915_gfx_val(dev_priv);
6654
6655 spin_unlock_irq(&mchdev_lock);
6656
6657 return val;
6658}
6659
Daniel Vettereb48eb02012-04-26 23:28:12 +02006660/**
6661 * i915_read_mch_val - return value for IPS use
6662 *
6663 * Calculate and return a value for the IPS driver to use when deciding whether
6664 * we have thermal and power headroom to increase CPU or GPU power budget.
6665 */
6666unsigned long i915_read_mch_val(void)
6667{
6668 struct drm_i915_private *dev_priv;
6669 unsigned long chipset_val, graphics_val, ret = 0;
6670
Daniel Vetter92703882012-08-09 16:46:01 +02006671 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006672 if (!i915_mch_dev)
6673 goto out_unlock;
6674 dev_priv = i915_mch_dev;
6675
Chris Wilsonf531dcb22012-09-25 10:16:12 +01006676 chipset_val = __i915_chipset_val(dev_priv);
6677 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006678
6679 ret = chipset_val + graphics_val;
6680
6681out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006682 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006683
6684 return ret;
6685}
6686EXPORT_SYMBOL_GPL(i915_read_mch_val);
6687
6688/**
6689 * i915_gpu_raise - raise GPU frequency limit
6690 *
6691 * Raise the limit; IPS indicates we have thermal headroom.
6692 */
6693bool i915_gpu_raise(void)
6694{
6695 struct drm_i915_private *dev_priv;
6696 bool ret = true;
6697
Daniel Vetter92703882012-08-09 16:46:01 +02006698 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006699 if (!i915_mch_dev) {
6700 ret = false;
6701 goto out_unlock;
6702 }
6703 dev_priv = i915_mch_dev;
6704
Daniel Vetter20e4d402012-08-08 23:35:39 +02006705 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6706 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006707
6708out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006709 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006710
6711 return ret;
6712}
6713EXPORT_SYMBOL_GPL(i915_gpu_raise);
6714
6715/**
6716 * i915_gpu_lower - lower GPU frequency limit
6717 *
6718 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6719 * frequency maximum.
6720 */
6721bool i915_gpu_lower(void)
6722{
6723 struct drm_i915_private *dev_priv;
6724 bool ret = true;
6725
Daniel Vetter92703882012-08-09 16:46:01 +02006726 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006727 if (!i915_mch_dev) {
6728 ret = false;
6729 goto out_unlock;
6730 }
6731 dev_priv = i915_mch_dev;
6732
Daniel Vetter20e4d402012-08-08 23:35:39 +02006733 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6734 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006735
6736out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006737 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006738
6739 return ret;
6740}
6741EXPORT_SYMBOL_GPL(i915_gpu_lower);
6742
6743/**
6744 * i915_gpu_busy - indicate GPU business to IPS
6745 *
6746 * Tell the IPS driver whether or not the GPU is busy.
6747 */
6748bool i915_gpu_busy(void)
6749{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006750 bool ret = false;
6751
Daniel Vetter92703882012-08-09 16:46:01 +02006752 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006753 if (i915_mch_dev)
6754 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006755 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006756
6757 return ret;
6758}
6759EXPORT_SYMBOL_GPL(i915_gpu_busy);
6760
6761/**
6762 * i915_gpu_turbo_disable - disable graphics turbo
6763 *
6764 * Disable graphics turbo by resetting the max frequency and setting the
6765 * current frequency to the default.
6766 */
6767bool i915_gpu_turbo_disable(void)
6768{
6769 struct drm_i915_private *dev_priv;
6770 bool ret = true;
6771
Daniel Vetter92703882012-08-09 16:46:01 +02006772 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006773 if (!i915_mch_dev) {
6774 ret = false;
6775 goto out_unlock;
6776 }
6777 dev_priv = i915_mch_dev;
6778
Daniel Vetter20e4d402012-08-08 23:35:39 +02006779 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006780
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006781 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006782 ret = false;
6783
6784out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006785 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006786
6787 return ret;
6788}
6789EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6790
6791/**
6792 * Tells the intel_ips driver that the i915 driver is now loaded, if
6793 * IPS got loaded first.
6794 *
6795 * This awkward dance is so that neither module has to depend on the
6796 * other in order for IPS to do the appropriate communication of
6797 * GPU turbo limits to i915.
6798 */
6799static void
6800ips_ping_for_i915_load(void)
6801{
6802 void (*link)(void);
6803
6804 link = symbol_get(ips_link_to_i915_driver);
6805 if (link) {
6806 link();
6807 symbol_put(ips_link_to_i915_driver);
6808 }
6809}
6810
6811void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6812{
Daniel Vetter02d71952012-08-09 16:44:54 +02006813 /* We only register the i915 ips part with intel-ips once everything is
6814 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006815 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006816 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006817 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006818
6819 ips_ping_for_i915_load();
6820}
6821
6822void intel_gpu_ips_teardown(void)
6823{
Daniel Vetter92703882012-08-09 16:46:01 +02006824 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006825 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006826 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006827}
Deepak S76c3552f2014-01-30 23:08:16 +05306828
Chris Wilsondc979972016-05-10 14:10:04 +01006829static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006830{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006831 u32 lcfuse;
6832 u8 pxw[16];
6833 int i;
6834
6835 /* Disable to program */
6836 I915_WRITE(ECR, 0);
6837 POSTING_READ(ECR);
6838
6839 /* Program energy weights for various events */
6840 I915_WRITE(SDEW, 0x15040d00);
6841 I915_WRITE(CSIEW0, 0x007f0000);
6842 I915_WRITE(CSIEW1, 0x1e220004);
6843 I915_WRITE(CSIEW2, 0x04000004);
6844
6845 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006846 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006847 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006848 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006849
6850 /* Program P-state weights to account for frequency power adjustment */
6851 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006852 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006853 unsigned long freq = intel_pxfreq(pxvidfreq);
6854 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6855 PXVFREQ_PX_SHIFT;
6856 unsigned long val;
6857
6858 val = vid * vid;
6859 val *= (freq / 1000);
6860 val *= 255;
6861 val /= (127*127*900);
6862 if (val > 0xff)
6863 DRM_ERROR("bad pxval: %ld\n", val);
6864 pxw[i] = val;
6865 }
6866 /* Render standby states get 0 weight */
6867 pxw[14] = 0;
6868 pxw[15] = 0;
6869
6870 for (i = 0; i < 4; i++) {
6871 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6872 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006873 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006874 }
6875
6876 /* Adjust magic regs to magic values (more experimental results) */
6877 I915_WRITE(OGW0, 0);
6878 I915_WRITE(OGW1, 0);
6879 I915_WRITE(EG0, 0x00007f00);
6880 I915_WRITE(EG1, 0x0000000e);
6881 I915_WRITE(EG2, 0x000e0000);
6882 I915_WRITE(EG3, 0x68000300);
6883 I915_WRITE(EG4, 0x42000000);
6884 I915_WRITE(EG5, 0x00140031);
6885 I915_WRITE(EG6, 0);
6886 I915_WRITE(EG7, 0);
6887
6888 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006889 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006890
6891 /* Enable PMON + select events */
6892 I915_WRITE(ECR, 0x80000019);
6893
6894 lcfuse = I915_READ(LCFUSE02);
6895
Daniel Vetter20e4d402012-08-08 23:35:39 +02006896 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006897}
6898
Chris Wilsondc979972016-05-10 14:10:04 +01006899void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006900{
Imre Deakb268c692015-12-15 20:10:31 +02006901 /*
6902 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6903 * requirement.
6904 */
6905 if (!i915.enable_rc6) {
6906 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6907 intel_runtime_pm_get(dev_priv);
6908 }
Imre Deake6069ca2014-04-18 16:01:02 +03006909
Chris Wilsonb5163db2016-08-10 13:58:24 +01006910 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006911 mutex_lock(&dev_priv->rps.hw_lock);
6912
6913 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006914 if (IS_CHERRYVIEW(dev_priv))
6915 cherryview_init_gt_powersave(dev_priv);
6916 else if (IS_VALLEYVIEW(dev_priv))
6917 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006918 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006919 gen6_init_rps_frequencies(dev_priv);
6920
6921 /* Derive initial user preferences/limits from the hardware limits */
6922 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6923 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6924
6925 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6926 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6927
6928 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6929 dev_priv->rps.min_freq_softlimit =
6930 max_t(int,
6931 dev_priv->rps.efficient_freq,
6932 intel_freq_opcode(dev_priv, 450));
6933
Chris Wilson99ac9612016-07-13 09:10:34 +01006934 /* After setting max-softlimit, find the overclock max freq */
6935 if (IS_GEN6(dev_priv) ||
6936 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6937 u32 params = 0;
6938
6939 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6940 if (params & BIT(31)) { /* OC supported */
6941 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6942 (dev_priv->rps.max_freq & 0xff) * 50,
6943 (params & 0xff) * 50);
6944 dev_priv->rps.max_freq = params & 0xff;
6945 }
6946 }
6947
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006948 /* Finally allow us to boost to max by default */
6949 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6950
Chris Wilson773ea9a2016-07-13 09:10:33 +01006951 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006952 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006953
6954 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006955}
6956
Chris Wilsondc979972016-05-10 14:10:04 +01006957void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006958{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006959 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006960 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006961
6962 if (!i915.enable_rc6)
6963 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006964}
6965
Chris Wilson54b4f682016-07-21 21:16:19 +01006966/**
6967 * intel_suspend_gt_powersave - suspend PM work and helper threads
6968 * @dev_priv: i915 device
6969 *
6970 * We don't want to disable RC6 or other features here, we just want
6971 * to make sure any work we've queued has finished and won't bother
6972 * us while we're suspended.
6973 */
6974void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
6975{
6976 if (INTEL_GEN(dev_priv) < 6)
6977 return;
6978
6979 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
6980 intel_runtime_pm_put(dev_priv);
6981
6982 /* gen6_rps_idle() will be called later to disable interrupts */
6983}
6984
Chris Wilsonb7137e02016-07-13 09:10:37 +01006985void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
6986{
6987 dev_priv->rps.enabled = true; /* force disabling */
6988 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006989
6990 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006991}
6992
Chris Wilsondc979972016-05-10 14:10:04 +01006993void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006994{
Chris Wilsonb7137e02016-07-13 09:10:37 +01006995 if (!READ_ONCE(dev_priv->rps.enabled))
6996 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006997
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006998 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006999
Chris Wilsonb7137e02016-07-13 09:10:37 +01007000 if (INTEL_GEN(dev_priv) >= 9) {
7001 gen9_disable_rc6(dev_priv);
7002 gen9_disable_rps(dev_priv);
7003 } else if (IS_CHERRYVIEW(dev_priv)) {
7004 cherryview_disable_rps(dev_priv);
7005 } else if (IS_VALLEYVIEW(dev_priv)) {
7006 valleyview_disable_rps(dev_priv);
7007 } else if (INTEL_GEN(dev_priv) >= 6) {
7008 gen6_disable_rps(dev_priv);
7009 } else if (IS_IRONLAKE_M(dev_priv)) {
7010 ironlake_disable_drps(dev_priv);
7011 }
7012
7013 dev_priv->rps.enabled = false;
7014 mutex_unlock(&dev_priv->rps.hw_lock);
7015}
7016
7017void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7018{
Chris Wilson54b4f682016-07-21 21:16:19 +01007019 /* We shouldn't be disabling as we submit, so this should be less
7020 * racy than it appears!
7021 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007022 if (READ_ONCE(dev_priv->rps.enabled))
7023 return;
7024
7025 /* Powersaving is controlled by the host when inside a VM */
7026 if (intel_vgpu_active(dev_priv))
7027 return;
7028
7029 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007030
Chris Wilsondc979972016-05-10 14:10:04 +01007031 if (IS_CHERRYVIEW(dev_priv)) {
7032 cherryview_enable_rps(dev_priv);
7033 } else if (IS_VALLEYVIEW(dev_priv)) {
7034 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007035 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007036 gen9_enable_rc6(dev_priv);
7037 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007038 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007039 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007040 } else if (IS_BROADWELL(dev_priv)) {
7041 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007042 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007043 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007044 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007045 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007046 } else if (IS_IRONLAKE_M(dev_priv)) {
7047 ironlake_enable_drps(dev_priv);
7048 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007049 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007050
7051 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7052 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7053
7054 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7055 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7056
Chris Wilson54b4f682016-07-21 21:16:19 +01007057 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007058 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007059}
Imre Deakc6df39b2014-04-14 20:24:29 +03007060
Chris Wilson54b4f682016-07-21 21:16:19 +01007061static void __intel_autoenable_gt_powersave(struct work_struct *work)
7062{
7063 struct drm_i915_private *dev_priv =
7064 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7065 struct intel_engine_cs *rcs;
7066 struct drm_i915_gem_request *req;
7067
7068 if (READ_ONCE(dev_priv->rps.enabled))
7069 goto out;
7070
Akash Goel3b3f1652016-10-13 22:44:48 +05307071 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007072 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007073 goto out;
7074
7075 if (!rcs->init_context)
7076 goto out;
7077
7078 mutex_lock(&dev_priv->drm.struct_mutex);
7079
7080 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7081 if (IS_ERR(req))
7082 goto unlock;
7083
7084 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7085 rcs->init_context(req);
7086
7087 /* Mark the device busy, calling intel_enable_gt_powersave() */
7088 i915_add_request_no_flush(req);
7089
7090unlock:
7091 mutex_unlock(&dev_priv->drm.struct_mutex);
7092out:
7093 intel_runtime_pm_put(dev_priv);
7094}
7095
7096void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7097{
7098 if (READ_ONCE(dev_priv->rps.enabled))
7099 return;
7100
7101 if (IS_IRONLAKE_M(dev_priv)) {
7102 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007103 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007104 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7105 /*
7106 * PCU communication is slow and this doesn't need to be
7107 * done at any specific time, so do this out of our fast path
7108 * to make resume and init faster.
7109 *
7110 * We depend on the HW RC6 power context save/restore
7111 * mechanism when entering D3 through runtime PM suspend. So
7112 * disable RPM until RPS/RC6 is properly setup. We can only
7113 * get here via the driver load/system resume/runtime resume
7114 * paths, so the _noresume version is enough (and in case of
7115 * runtime resume it's necessary).
7116 */
7117 if (queue_delayed_work(dev_priv->wq,
7118 &dev_priv->rps.autoenable_work,
7119 round_jiffies_up_relative(HZ)))
7120 intel_runtime_pm_get_noresume(dev_priv);
7121 }
7122}
7123
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007124static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007125{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007126 /*
7127 * On Ibex Peak and Cougar Point, we need to disable clock
7128 * gating for the panel power sequencer or it will fail to
7129 * start up when no ports are active.
7130 */
7131 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7132}
7133
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007134static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007135{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007136 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007137
Damien Lespiau055e3932014-08-18 13:49:10 +01007138 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007139 I915_WRITE(DSPCNTR(pipe),
7140 I915_READ(DSPCNTR(pipe)) |
7141 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007142
7143 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7144 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007145 }
7146}
7147
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007148static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007149{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007150 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7151 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7152 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7153
7154 /*
7155 * Don't touch WM1S_LP_EN here.
7156 * Doing so could cause underruns.
7157 */
7158}
7159
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007160static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007161{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007162 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007163
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007164 /*
7165 * Required for FBC
7166 * WaFbcDisableDpfcClockGating:ilk
7167 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007168 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7169 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7170 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007171
7172 I915_WRITE(PCH_3DCGDIS0,
7173 MARIUNIT_CLOCK_GATE_DISABLE |
7174 SVSMUNIT_CLOCK_GATE_DISABLE);
7175 I915_WRITE(PCH_3DCGDIS1,
7176 VFMUNIT_CLOCK_GATE_DISABLE);
7177
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007178 /*
7179 * According to the spec the following bits should be set in
7180 * order to enable memory self-refresh
7181 * The bit 22/21 of 0x42004
7182 * The bit 5 of 0x42020
7183 * The bit 15 of 0x45000
7184 */
7185 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7186 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7187 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007188 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007189 I915_WRITE(DISP_ARB_CTL,
7190 (I915_READ(DISP_ARB_CTL) |
7191 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007192
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007193 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007194
7195 /*
7196 * Based on the document from hardware guys the following bits
7197 * should be set unconditionally in order to enable FBC.
7198 * The bit 22 of 0x42000
7199 * The bit 22 of 0x42004
7200 * The bit 7,8,9 of 0x42020.
7201 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007202 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007203 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007204 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7205 I915_READ(ILK_DISPLAY_CHICKEN1) |
7206 ILK_FBCQ_DIS);
7207 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7208 I915_READ(ILK_DISPLAY_CHICKEN2) |
7209 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007210 }
7211
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007212 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7213
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007214 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7215 I915_READ(ILK_DISPLAY_CHICKEN2) |
7216 ILK_ELPIN_409_SELECT);
7217 I915_WRITE(_3D_CHICKEN2,
7218 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7219 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007220
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007221 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007222 I915_WRITE(CACHE_MODE_0,
7223 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007224
Akash Goel4e046322014-04-04 17:14:38 +05307225 /* WaDisable_RenderCache_OperationalFlush:ilk */
7226 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7227
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007228 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007229
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007230 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007231}
7232
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007233static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007234{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007235 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007236 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007237
7238 /*
7239 * On Ibex Peak and Cougar Point, we need to disable clock
7240 * gating for the panel power sequencer or it will fail to
7241 * start up when no ports are active.
7242 */
Jesse Barnescd664072013-10-02 10:34:19 -07007243 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7244 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7245 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007246 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7247 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007248 /* The below fixes the weird display corruption, a few pixels shifted
7249 * downward, on (only) LVDS of some HP laptops with IVY.
7250 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007251 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007252 val = I915_READ(TRANS_CHICKEN2(pipe));
7253 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7254 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007255 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007256 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007257 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7258 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7259 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007260 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7261 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007262 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007263 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007264 I915_WRITE(TRANS_CHICKEN1(pipe),
7265 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7266 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007267}
7268
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007269static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007270{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007271 uint32_t tmp;
7272
7273 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007274 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7275 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7276 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007277}
7278
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007279static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007280{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007281 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007282
Damien Lespiau231e54f2012-10-19 17:55:41 +01007283 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007284
7285 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7286 I915_READ(ILK_DISPLAY_CHICKEN2) |
7287 ILK_ELPIN_409_SELECT);
7288
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007289 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007290 I915_WRITE(_3D_CHICKEN,
7291 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7292
Akash Goel4e046322014-04-04 17:14:38 +05307293 /* WaDisable_RenderCache_OperationalFlush:snb */
7294 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7295
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007296 /*
7297 * BSpec recoomends 8x4 when MSAA is used,
7298 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007299 *
7300 * Note that PS/WM thread counts depend on the WIZ hashing
7301 * disable bit, which we don't touch here, but it's good
7302 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007303 */
7304 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007305 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007306
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007307 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007308
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007309 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007310 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007311
7312 I915_WRITE(GEN6_UCGCTL1,
7313 I915_READ(GEN6_UCGCTL1) |
7314 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7315 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7316
7317 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7318 * gating disable must be set. Failure to set it results in
7319 * flickering pixels due to Z write ordering failures after
7320 * some amount of runtime in the Mesa "fire" demo, and Unigine
7321 * Sanctuary and Tropics, and apparently anything else with
7322 * alpha test or pixel discard.
7323 *
7324 * According to the spec, bit 11 (RCCUNIT) must also be set,
7325 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007326 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007327 * WaDisableRCCUnitClockGating:snb
7328 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007329 */
7330 I915_WRITE(GEN6_UCGCTL2,
7331 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7332 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7333
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007334 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007335 I915_WRITE(_3D_CHICKEN3,
7336 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007337
7338 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007339 * Bspec says:
7340 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7341 * 3DSTATE_SF number of SF output attributes is more than 16."
7342 */
7343 I915_WRITE(_3D_CHICKEN3,
7344 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7345
7346 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007347 * According to the spec the following bits should be
7348 * set in order to enable memory self-refresh and fbc:
7349 * The bit21 and bit22 of 0x42000
7350 * The bit21 and bit22 of 0x42004
7351 * The bit5 and bit7 of 0x42020
7352 * The bit14 of 0x70180
7353 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007354 *
7355 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007356 */
7357 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7358 I915_READ(ILK_DISPLAY_CHICKEN1) |
7359 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7360 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7361 I915_READ(ILK_DISPLAY_CHICKEN2) |
7362 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007363 I915_WRITE(ILK_DSPCLK_GATE_D,
7364 I915_READ(ILK_DSPCLK_GATE_D) |
7365 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7366 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007367
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007368 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007369
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007370 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007371
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007372 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007373}
7374
7375static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7376{
7377 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7378
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007379 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007380 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007381 *
7382 * This actually overrides the dispatch
7383 * mode for all thread types.
7384 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007385 reg &= ~GEN7_FF_SCHED_MASK;
7386 reg |= GEN7_FF_TS_SCHED_HW;
7387 reg |= GEN7_FF_VS_SCHED_HW;
7388 reg |= GEN7_FF_DS_SCHED_HW;
7389
7390 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7391}
7392
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007393static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007394{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007395 /*
7396 * TODO: this bit should only be enabled when really needed, then
7397 * disabled when not needed anymore in order to save power.
7398 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007399 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007400 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7401 I915_READ(SOUTH_DSPCLK_GATE_D) |
7402 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007403
7404 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007405 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7406 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007407 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007408}
7409
Ville Syrjälä712bf362016-10-31 22:37:23 +02007410static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007411{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007412 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007413 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7414
7415 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7416 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7417 }
7418}
7419
Imre Deak450174f2016-05-03 15:54:21 +03007420static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7421 int general_prio_credits,
7422 int high_prio_credits)
7423{
7424 u32 misccpctl;
7425
7426 /* WaTempDisableDOPClkGating:bdw */
7427 misccpctl = I915_READ(GEN7_MISCCPCTL);
7428 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7429
7430 I915_WRITE(GEN8_L3SQCREG1,
7431 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7432 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7433
7434 /*
7435 * Wait at least 100 clocks before re-enabling clock gating.
7436 * See the definition of L3SQCREG1 in BSpec.
7437 */
7438 POSTING_READ(GEN8_L3SQCREG1);
7439 udelay(1);
7440 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7441}
7442
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007443static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007444{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007445 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007446
7447 /* WaDisableSDEUnitClockGating:kbl */
7448 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7449 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7450 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007451
7452 /* WaDisableGamClockGating:kbl */
7453 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7454 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7455 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007456
7457 /* WaFbcNukeOnHostModify:kbl */
7458 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7459 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007460}
7461
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007462static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007463{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007464 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007465
7466 /* WAC6entrylatency:skl */
7467 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7468 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007469
7470 /* WaFbcNukeOnHostModify:skl */
7471 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7472 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007473}
7474
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007475static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007476{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007477 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007478
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007479 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007480
Ben Widawskyab57fff2013-12-12 15:28:04 -08007481 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007482 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007483
Ben Widawskyab57fff2013-12-12 15:28:04 -08007484 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007485 I915_WRITE(CHICKEN_PAR1_1,
7486 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7487
Ben Widawskyab57fff2013-12-12 15:28:04 -08007488 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007489 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007490 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007491 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007492 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007493 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007494
Ben Widawskyab57fff2013-12-12 15:28:04 -08007495 /* WaVSRefCountFullforceMissDisable:bdw */
7496 /* WaDSRefCountFullforceMissDisable:bdw */
7497 I915_WRITE(GEN7_FF_THREAD_MODE,
7498 I915_READ(GEN7_FF_THREAD_MODE) &
7499 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007500
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007501 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7502 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007503
7504 /* WaDisableSDEUnitClockGating:bdw */
7505 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7506 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007507
Imre Deak450174f2016-05-03 15:54:21 +03007508 /* WaProgramL3SqcReg1Default:bdw */
7509 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007510
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007511 /*
7512 * WaGttCachingOffByDefault:bdw
7513 * GTT cache may not work with big pages, so if those
7514 * are ever enabled GTT cache may need to be disabled.
7515 */
7516 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7517
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007518 /* WaKVMNotificationOnConfigChange:bdw */
7519 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7520 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7521
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007522 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007523
7524 /* WaDisableDopClockGating:bdw
7525 *
7526 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7527 * clock gating.
7528 */
7529 I915_WRITE(GEN6_UCGCTL1,
7530 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007531}
7532
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007533static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007534{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007535 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007536
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007537 /* L3 caching of data atomics doesn't work -- disable it. */
7538 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7539 I915_WRITE(HSW_ROW_CHICKEN3,
7540 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7541
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007542 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007543 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7544 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7545 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7546
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007547 /* WaVSRefCountFullforceMissDisable:hsw */
7548 I915_WRITE(GEN7_FF_THREAD_MODE,
7549 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007550
Akash Goel4e046322014-04-04 17:14:38 +05307551 /* WaDisable_RenderCache_OperationalFlush:hsw */
7552 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7553
Chia-I Wufe27c602014-01-28 13:29:33 +08007554 /* enable HiZ Raw Stall Optimization */
7555 I915_WRITE(CACHE_MODE_0_GEN7,
7556 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7557
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007558 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007559 I915_WRITE(CACHE_MODE_1,
7560 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007561
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007562 /*
7563 * BSpec recommends 8x4 when MSAA is used,
7564 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007565 *
7566 * Note that PS/WM thread counts depend on the WIZ hashing
7567 * disable bit, which we don't touch here, but it's good
7568 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007569 */
7570 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007571 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007572
Kenneth Graunke94411592014-12-31 16:23:00 -08007573 /* WaSampleCChickenBitEnable:hsw */
7574 I915_WRITE(HALF_SLICE_CHICKEN3,
7575 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7576
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007577 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007578 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7579
Paulo Zanoni90a88642013-05-03 17:23:45 -03007580 /* WaRsPkgCStateDisplayPMReq:hsw */
7581 I915_WRITE(CHICKEN_PAR1_1,
7582 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007583
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007584 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007585}
7586
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007587static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007588{
Ben Widawsky20848222012-05-04 18:58:59 -07007589 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007590
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007591 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007592
Damien Lespiau231e54f2012-10-19 17:55:41 +01007593 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007594
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007595 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007596 I915_WRITE(_3D_CHICKEN3,
7597 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7598
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007599 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007600 I915_WRITE(IVB_CHICKEN3,
7601 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7602 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7603
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007604 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007605 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007606 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7607 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007608
Akash Goel4e046322014-04-04 17:14:38 +05307609 /* WaDisable_RenderCache_OperationalFlush:ivb */
7610 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7611
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007612 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007613 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7614 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7615
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007616 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007617 I915_WRITE(GEN7_L3CNTLREG1,
7618 GEN7_WA_FOR_GEN7_L3_CONTROL);
7619 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007620 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007621 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007622 I915_WRITE(GEN7_ROW_CHICKEN2,
7623 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007624 else {
7625 /* must write both registers */
7626 I915_WRITE(GEN7_ROW_CHICKEN2,
7627 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007628 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7629 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007630 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007631
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007632 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007633 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7634 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7635
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007636 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007637 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007638 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007639 */
7640 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007641 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007642
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007643 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007644 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7645 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7646 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7647
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007648 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007649
7650 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007651
Chris Wilson22721342014-03-04 09:41:43 +00007652 if (0) { /* causes HiZ corruption on ivb:gt1 */
7653 /* enable HiZ Raw Stall Optimization */
7654 I915_WRITE(CACHE_MODE_0_GEN7,
7655 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7656 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007657
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007658 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007659 I915_WRITE(CACHE_MODE_1,
7660 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007661
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007662 /*
7663 * BSpec recommends 8x4 when MSAA is used,
7664 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007665 *
7666 * Note that PS/WM thread counts depend on the WIZ hashing
7667 * disable bit, which we don't touch here, but it's good
7668 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007669 */
7670 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007671 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007672
Ben Widawsky20848222012-05-04 18:58:59 -07007673 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7674 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7675 snpcr |= GEN6_MBC_SNPCR_MED;
7676 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007677
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007678 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007679 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007680
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007681 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007682}
7683
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007684static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007685{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007686 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007687 I915_WRITE(_3D_CHICKEN3,
7688 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7689
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007690 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007691 I915_WRITE(IVB_CHICKEN3,
7692 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7693 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7694
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007695 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007696 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007697 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007698 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7699 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007700
Akash Goel4e046322014-04-04 17:14:38 +05307701 /* WaDisable_RenderCache_OperationalFlush:vlv */
7702 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7703
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007704 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007705 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7706 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7707
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007708 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007709 I915_WRITE(GEN7_ROW_CHICKEN2,
7710 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7711
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007712 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007713 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7714 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7715 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7716
Ville Syrjälä46680e02014-01-22 21:33:01 +02007717 gen7_setup_fixed_func_scheduler(dev_priv);
7718
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007719 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007720 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007721 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007722 */
7723 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007724 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007725
Akash Goelc98f5062014-03-24 23:00:07 +05307726 /* WaDisableL3Bank2xClockGate:vlv
7727 * Disabling L3 clock gating- MMIO 940c[25] = 1
7728 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7729 I915_WRITE(GEN7_UCGCTL4,
7730 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007731
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007732 /*
7733 * BSpec says this must be set, even though
7734 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7735 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007736 I915_WRITE(CACHE_MODE_1,
7737 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007738
7739 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007740 * BSpec recommends 8x4 when MSAA is used,
7741 * however in practice 16x4 seems fastest.
7742 *
7743 * Note that PS/WM thread counts depend on the WIZ hashing
7744 * disable bit, which we don't touch here, but it's good
7745 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7746 */
7747 I915_WRITE(GEN7_GT_MODE,
7748 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7749
7750 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007751 * WaIncreaseL3CreditsForVLVB0:vlv
7752 * This is the hardware default actually.
7753 */
7754 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7755
7756 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007757 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007758 * Disable clock gating on th GCFG unit to prevent a delay
7759 * in the reporting of vblank events.
7760 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007761 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007762}
7763
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007764static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007765{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007766 /* WaVSRefCountFullforceMissDisable:chv */
7767 /* WaDSRefCountFullforceMissDisable:chv */
7768 I915_WRITE(GEN7_FF_THREAD_MODE,
7769 I915_READ(GEN7_FF_THREAD_MODE) &
7770 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007771
7772 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7773 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7774 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007775
7776 /* WaDisableCSUnitClockGating:chv */
7777 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7778 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007779
7780 /* WaDisableSDEUnitClockGating:chv */
7781 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7782 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007783
7784 /*
Imre Deak450174f2016-05-03 15:54:21 +03007785 * WaProgramL3SqcReg1Default:chv
7786 * See gfxspecs/Related Documents/Performance Guide/
7787 * LSQC Setting Recommendations.
7788 */
7789 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7790
7791 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007792 * GTT cache may not work with big pages, so if those
7793 * are ever enabled GTT cache may need to be disabled.
7794 */
7795 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007796}
7797
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007798static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007799{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007800 uint32_t dspclk_gate;
7801
7802 I915_WRITE(RENCLK_GATE_D1, 0);
7803 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7804 GS_UNIT_CLOCK_GATE_DISABLE |
7805 CL_UNIT_CLOCK_GATE_DISABLE);
7806 I915_WRITE(RAMCLK_GATE_D, 0);
7807 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7808 OVRUNIT_CLOCK_GATE_DISABLE |
7809 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007810 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007811 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7812 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007813
7814 /* WaDisableRenderCachePipelinedFlush */
7815 I915_WRITE(CACHE_MODE_0,
7816 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007817
Akash Goel4e046322014-04-04 17:14:38 +05307818 /* WaDisable_RenderCache_OperationalFlush:g4x */
7819 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7820
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007821 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007822}
7823
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007824static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007825{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007826 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7827 I915_WRITE(RENCLK_GATE_D2, 0);
7828 I915_WRITE(DSPCLK_GATE_D, 0);
7829 I915_WRITE(RAMCLK_GATE_D, 0);
7830 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007831 I915_WRITE(MI_ARB_STATE,
7832 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307833
7834 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7835 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007836}
7837
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007838static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007839{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007840 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7841 I965_RCC_CLOCK_GATE_DISABLE |
7842 I965_RCPB_CLOCK_GATE_DISABLE |
7843 I965_ISC_CLOCK_GATE_DISABLE |
7844 I965_FBC_CLOCK_GATE_DISABLE);
7845 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007846 I915_WRITE(MI_ARB_STATE,
7847 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307848
7849 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7850 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007851}
7852
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007853static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007854{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007855 u32 dstate = I915_READ(D_STATE);
7856
7857 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7858 DSTATE_DOT_CLOCK_GATING;
7859 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007860
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007861 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007862 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007863
7864 /* IIR "flip pending" means done if this bit is set */
7865 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007866
7867 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007868 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007869
7870 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7871 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007872
7873 I915_WRITE(MI_ARB_STATE,
7874 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007875}
7876
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007877static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007878{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007879 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007880
7881 /* interrupts should cause a wake up from C3 */
7882 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7883 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007884
7885 I915_WRITE(MEM_MODE,
7886 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007887}
7888
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007889static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007890{
Ville Syrjälä10383922014-08-15 01:21:54 +03007891 I915_WRITE(MEM_MODE,
7892 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7893 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007894}
7895
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007896void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007897{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007898 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007899}
7900
Ville Syrjälä712bf362016-10-31 22:37:23 +02007901void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007902{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007903 if (HAS_PCH_LPT(dev_priv))
7904 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007905}
7906
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007907static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007908{
7909 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7910}
7911
7912/**
7913 * intel_init_clock_gating_hooks - setup the clock gating hooks
7914 * @dev_priv: device private
7915 *
7916 * Setup the hooks that configure which clocks of a given platform can be
7917 * gated and also apply various GT and display specific workarounds for these
7918 * platforms. Note that some GT specific workarounds are applied separately
7919 * when GPU contexts or batchbuffers start their execution.
7920 */
7921void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7922{
7923 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007924 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007925 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007926 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007927 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007928 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007929 else if (IS_GEMINILAKE(dev_priv))
7930 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007931 else if (IS_BROADWELL(dev_priv))
7932 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7933 else if (IS_CHERRYVIEW(dev_priv))
7934 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7935 else if (IS_HASWELL(dev_priv))
7936 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7937 else if (IS_IVYBRIDGE(dev_priv))
7938 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7939 else if (IS_VALLEYVIEW(dev_priv))
7940 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7941 else if (IS_GEN6(dev_priv))
7942 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7943 else if (IS_GEN5(dev_priv))
7944 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7945 else if (IS_G4X(dev_priv))
7946 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007947 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007948 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007949 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007950 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7951 else if (IS_GEN3(dev_priv))
7952 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7953 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7954 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7955 else if (IS_GEN2(dev_priv))
7956 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7957 else {
7958 MISSING_CASE(INTEL_DEVID(dev_priv));
7959 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7960 }
7961}
7962
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007963/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007964void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007965{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007966 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007967
Daniel Vetterc921aba2012-04-26 23:28:17 +02007968 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007969 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007970 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007971 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007972 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007973
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007974 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007975 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007976 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01007977 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01007978 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007979 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007980 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007981 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007982
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007983 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007984 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007985 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007986 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007987 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007988 dev_priv->display.compute_intermediate_wm =
7989 ilk_compute_intermediate_wm;
7990 dev_priv->display.initial_watermarks =
7991 ilk_initial_watermarks;
7992 dev_priv->display.optimize_watermarks =
7993 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007994 } else {
7995 DRM_DEBUG_KMS("Failed to read display plane latency. "
7996 "Disable CxSR\n");
7997 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007998 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007999 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02008000 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008001 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008002 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02008003 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02008004 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02008005 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01008006 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008007 dev_priv->is_ddr3,
8008 dev_priv->fsb_freq,
8009 dev_priv->mem_freq)) {
8010 DRM_INFO("failed to find known CxSR latency "
8011 "(found ddr%s fsb freq %d, mem freq %d), "
8012 "disabling CxSR\n",
8013 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8014 dev_priv->fsb_freq, dev_priv->mem_freq);
8015 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008016 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008017 dev_priv->display.update_wm = NULL;
8018 } else
8019 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008020 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008021 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008022 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008023 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008024 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008025 dev_priv->display.update_wm = i9xx_update_wm;
8026 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008027 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008028 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008029 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008030 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008031 } else {
8032 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008033 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008034 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008035 } else {
8036 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008037 }
8038}
8039
Lyude87660502016-08-17 15:55:53 -04008040static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8041{
8042 uint32_t flags =
8043 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8044
8045 switch (flags) {
8046 case GEN6_PCODE_SUCCESS:
8047 return 0;
8048 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8049 case GEN6_PCODE_ILLEGAL_CMD:
8050 return -ENXIO;
8051 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008052 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008053 return -EOVERFLOW;
8054 case GEN6_PCODE_TIMEOUT:
8055 return -ETIMEDOUT;
8056 default:
8057 MISSING_CASE(flags)
8058 return 0;
8059 }
8060}
8061
8062static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8063{
8064 uint32_t flags =
8065 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8066
8067 switch (flags) {
8068 case GEN6_PCODE_SUCCESS:
8069 return 0;
8070 case GEN6_PCODE_ILLEGAL_CMD:
8071 return -ENXIO;
8072 case GEN7_PCODE_TIMEOUT:
8073 return -ETIMEDOUT;
8074 case GEN7_PCODE_ILLEGAL_DATA:
8075 return -EINVAL;
8076 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8077 return -EOVERFLOW;
8078 default:
8079 MISSING_CASE(flags);
8080 return 0;
8081 }
8082}
8083
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008084int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008085{
Lyude87660502016-08-17 15:55:53 -04008086 int status;
8087
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008088 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008089
Chris Wilson3f5582d2016-06-30 15:32:45 +01008090 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8091 * use te fw I915_READ variants to reduce the amount of work
8092 * required when reading/writing.
8093 */
8094
8095 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008096 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8097 return -EAGAIN;
8098 }
8099
Chris Wilson3f5582d2016-06-30 15:32:45 +01008100 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8101 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8102 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008103
Chris Wilson3f5582d2016-06-30 15:32:45 +01008104 if (intel_wait_for_register_fw(dev_priv,
8105 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8106 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008107 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8108 return -ETIMEDOUT;
8109 }
8110
Chris Wilson3f5582d2016-06-30 15:32:45 +01008111 *val = I915_READ_FW(GEN6_PCODE_DATA);
8112 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008113
Lyude87660502016-08-17 15:55:53 -04008114 if (INTEL_GEN(dev_priv) > 6)
8115 status = gen7_check_mailbox_status(dev_priv);
8116 else
8117 status = gen6_check_mailbox_status(dev_priv);
8118
8119 if (status) {
8120 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8121 status);
8122 return status;
8123 }
8124
Ben Widawsky42c05262012-09-26 10:34:00 -07008125 return 0;
8126}
8127
Chris Wilson3f5582d2016-06-30 15:32:45 +01008128int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008129 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008130{
Lyude87660502016-08-17 15:55:53 -04008131 int status;
8132
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008133 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008134
Chris Wilson3f5582d2016-06-30 15:32:45 +01008135 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8136 * use te fw I915_READ variants to reduce the amount of work
8137 * required when reading/writing.
8138 */
8139
8140 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008141 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8142 return -EAGAIN;
8143 }
8144
Chris Wilson3f5582d2016-06-30 15:32:45 +01008145 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008146 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008147 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008148
Chris Wilson3f5582d2016-06-30 15:32:45 +01008149 if (intel_wait_for_register_fw(dev_priv,
8150 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8151 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008152 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8153 return -ETIMEDOUT;
8154 }
8155
Chris Wilson3f5582d2016-06-30 15:32:45 +01008156 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008157
Lyude87660502016-08-17 15:55:53 -04008158 if (INTEL_GEN(dev_priv) > 6)
8159 status = gen7_check_mailbox_status(dev_priv);
8160 else
8161 status = gen6_check_mailbox_status(dev_priv);
8162
8163 if (status) {
8164 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8165 status);
8166 return status;
8167 }
8168
Ben Widawsky42c05262012-09-26 10:34:00 -07008169 return 0;
8170}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008171
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008172static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8173 u32 request, u32 reply_mask, u32 reply,
8174 u32 *status)
8175{
8176 u32 val = request;
8177
8178 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8179
8180 return *status || ((val & reply_mask) == reply);
8181}
8182
8183/**
8184 * skl_pcode_request - send PCODE request until acknowledgment
8185 * @dev_priv: device private
8186 * @mbox: PCODE mailbox ID the request is targeted for
8187 * @request: request ID
8188 * @reply_mask: mask used to check for request acknowledgment
8189 * @reply: value used to check for request acknowledgment
8190 * @timeout_base_ms: timeout for polling with preemption enabled
8191 *
8192 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008193 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008194 * The request is acknowledged once the PCODE reply dword equals @reply after
8195 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008196 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008197 * preemption disabled.
8198 *
8199 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8200 * other error as reported by PCODE.
8201 */
8202int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8203 u32 reply_mask, u32 reply, int timeout_base_ms)
8204{
8205 u32 status;
8206 int ret;
8207
8208 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8209
8210#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8211 &status)
8212
8213 /*
8214 * Prime the PCODE by doing a request first. Normally it guarantees
8215 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8216 * _wait_for() doesn't guarantee when its passed condition is evaluated
8217 * first, so send the first request explicitly.
8218 */
8219 if (COND) {
8220 ret = 0;
8221 goto out;
8222 }
8223 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8224 if (!ret)
8225 goto out;
8226
8227 /*
8228 * The above can time out if the number of requests was low (2 in the
8229 * worst case) _and_ PCODE was busy for some reason even after a
8230 * (queued) request and @timeout_base_ms delay. As a workaround retry
8231 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008232 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008233 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008234 * requests, and for any quirks of the PCODE firmware that delays
8235 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008236 */
8237 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8238 WARN_ON_ONCE(timeout_base_ms > 3);
8239 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008240 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008241 preempt_enable();
8242
8243out:
8244 return ret ? ret : status;
8245#undef COND
8246}
8247
Ville Syrjälädd06f882014-11-10 22:55:12 +02008248static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8249{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008250 /*
8251 * N = val - 0xb7
8252 * Slow = Fast = GPLL ref * N
8253 */
8254 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008255}
8256
Fengguang Wub55dd642014-07-12 11:21:39 +02008257static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008258{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008259 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008260}
8261
Fengguang Wub55dd642014-07-12 11:21:39 +02008262static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308263{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008264 /*
8265 * N = val / 2
8266 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8267 */
8268 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308269}
8270
Fengguang Wub55dd642014-07-12 11:21:39 +02008271static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308272{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008273 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008274 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308275}
8276
Ville Syrjälä616bc822015-01-23 21:04:25 +02008277int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8278{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008279 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008280 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8281 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008282 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008283 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008284 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008285 return byt_gpu_freq(dev_priv, val);
8286 else
8287 return val * GT_FREQUENCY_MULTIPLIER;
8288}
8289
Ville Syrjälä616bc822015-01-23 21:04:25 +02008290int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8291{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008292 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008293 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8294 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008295 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008296 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008297 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008298 return byt_freq_opcode(dev_priv, val);
8299 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008300 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308301}
8302
Chris Wilson6ad790c2015-04-07 16:20:31 +01008303struct request_boost {
8304 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008305 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008306};
8307
8308static void __intel_rps_boost_work(struct work_struct *work)
8309{
8310 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008311 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008312
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008313 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008314 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008315
Chris Wilsone8a261e2016-07-20 13:31:49 +01008316 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008317 kfree(boost);
8318}
8319
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008320void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008321{
8322 struct request_boost *boost;
8323
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008324 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008325 return;
8326
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008327 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008328 return;
8329
Chris Wilson6ad790c2015-04-07 16:20:31 +01008330 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8331 if (boost == NULL)
8332 return;
8333
Chris Wilsone8a261e2016-07-20 13:31:49 +01008334 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008335
8336 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008337 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008338}
8339
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008340void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008341{
Daniel Vetterf742a552013-12-06 10:17:53 +01008342 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008343 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008344
Chris Wilson54b4f682016-07-21 21:16:19 +01008345 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8346 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008347 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008348
Paulo Zanoni33688d92014-03-07 20:08:19 -03008349 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008350 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008351}