blob: 4db3a792c6c24dcc56461109dec0b97071c64494 [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ä467a14d2016-12-05 16:13:28 +02001361 spin_lock(&dev_priv->wm.dsparb_lock);
1362
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001363 switch (crtc->pipe) {
1364 uint32_t dsparb, dsparb2, dsparb3;
1365 case PIPE_A:
1366 dsparb = I915_READ(DSPARB);
1367 dsparb2 = I915_READ(DSPARB2);
1368
1369 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1370 VLV_FIFO(SPRITEB, 0xff));
1371 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1372 VLV_FIFO(SPRITEB, sprite1_start));
1373
1374 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1375 VLV_FIFO(SPRITEB_HI, 0x1));
1376 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1377 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1378
1379 I915_WRITE(DSPARB, dsparb);
1380 I915_WRITE(DSPARB2, dsparb2);
1381 break;
1382 case PIPE_B:
1383 dsparb = I915_READ(DSPARB);
1384 dsparb2 = I915_READ(DSPARB2);
1385
1386 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1387 VLV_FIFO(SPRITED, 0xff));
1388 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1389 VLV_FIFO(SPRITED, sprite1_start));
1390
1391 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1392 VLV_FIFO(SPRITED_HI, 0xff));
1393 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1394 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1395
1396 I915_WRITE(DSPARB, dsparb);
1397 I915_WRITE(DSPARB2, dsparb2);
1398 break;
1399 case PIPE_C:
1400 dsparb3 = I915_READ(DSPARB3);
1401 dsparb2 = I915_READ(DSPARB2);
1402
1403 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1404 VLV_FIFO(SPRITEF, 0xff));
1405 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1406 VLV_FIFO(SPRITEF, sprite1_start));
1407
1408 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1409 VLV_FIFO(SPRITEF_HI, 0xff));
1410 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1411 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1412
1413 I915_WRITE(DSPARB3, dsparb3);
1414 I915_WRITE(DSPARB2, dsparb2);
1415 break;
1416 default:
1417 break;
1418 }
Ville Syrjälä467a14d2016-12-05 16:13:28 +02001419
1420 POSTING_READ(DSPARB);
1421
1422 spin_unlock(&dev_priv->wm.dsparb_lock);
Ville Syrjälä54f1b6e2015-06-24 22:00:05 +03001423}
1424
1425#undef VLV_FIFO
1426
Ville Syrjälä4841da52017-03-02 19:14:59 +02001427static int vlv_compute_intermediate_wm(struct drm_device *dev,
1428 struct intel_crtc *crtc,
1429 struct intel_crtc_state *crtc_state)
1430{
1431 struct vlv_wm_state *intermediate = &crtc_state->wm.vlv.intermediate;
1432 const struct vlv_wm_state *optimal = &crtc_state->wm.vlv.optimal;
1433 const struct vlv_wm_state *active = &crtc->wm.active.vlv;
1434 int level;
1435
1436 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001437 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1438 !crtc_state->disable_cxsr;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001439
1440 for (level = 0; level < intermediate->num_levels; level++) {
1441 enum plane_id plane_id;
1442
1443 for_each_plane_id_on_crtc(crtc, plane_id) {
1444 intermediate->wm[level].plane[plane_id] =
1445 min(optimal->wm[level].plane[plane_id],
1446 active->wm[level].plane[plane_id]);
1447 }
1448
1449 intermediate->sr[level].plane = min(optimal->sr[level].plane,
1450 active->sr[level].plane);
1451 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
1452 active->sr[level].cursor);
1453 }
1454
1455 vlv_invalidate_wms(crtc, intermediate, level);
1456
1457 /*
1458 * If our intermediate WM are identical to the final WM, then we can
1459 * omit the post-vblank programming; only update if it's different.
1460 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02001461 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1462 crtc_state->wm.need_postvbl_update = true;
Ville Syrjälä4841da52017-03-02 19:14:59 +02001463
1464 return 0;
1465}
1466
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001467static void vlv_merge_wm(struct drm_i915_private *dev_priv,
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001468 struct vlv_wm_values *wm)
1469{
1470 struct intel_crtc *crtc;
1471 int num_active_crtcs = 0;
1472
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001473 wm->level = dev_priv->wm.max_level;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001474 wm->cxsr = true;
1475
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001476 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001477 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001478
1479 if (!crtc->active)
1480 continue;
1481
1482 if (!wm_state->cxsr)
1483 wm->cxsr = false;
1484
1485 num_active_crtcs++;
1486 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1487 }
1488
1489 if (num_active_crtcs != 1)
1490 wm->cxsr = false;
1491
Ville Syrjälä6f9c7842015-06-24 22:00:08 +03001492 if (num_active_crtcs > 1)
1493 wm->level = VLV_WM_LEVEL_PM2;
1494
Ville Syrjälä7c951c02016-11-28 19:37:10 +02001495 for_each_intel_crtc(&dev_priv->drm, crtc) {
Ville Syrjälä7eb49412017-03-02 19:14:53 +02001496 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001497 enum pipe pipe = crtc->pipe;
1498
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001499 wm->pipe[pipe] = wm_state->wm[wm->level];
Ville Syrjäläff32c542017-03-02 19:14:57 +02001500 if (crtc->active && wm->cxsr)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001501 wm->sr = wm_state->sr[wm->level];
1502
Ville Syrjälä1b313892016-11-28 19:37:08 +02001503 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
1504 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
1505 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
1506 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001507 }
1508}
1509
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001510static bool is_disabling(int old, int new, int threshold)
1511{
1512 return old >= threshold && new < threshold;
1513}
1514
1515static bool is_enabling(int old, int new, int threshold)
1516{
1517 return old < threshold && new >= threshold;
1518}
1519
Ville Syrjäläff32c542017-03-02 19:14:57 +02001520static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001521{
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001522 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
1523 struct vlv_wm_values new_wm = {};
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001524
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001525 vlv_merge_wm(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001526
Ville Syrjäläff32c542017-03-02 19:14:57 +02001527 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001528 return;
1529
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001530 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001531 chv_set_memory_dvfs(dev_priv, false);
1532
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001533 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001534 chv_set_memory_pm5(dev_priv, false);
1535
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001536 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001537 _intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001538
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001539 vlv_write_wm_values(dev_priv, &new_wm);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001540
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001541 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
Ville Syrjälä3d90e642016-11-28 19:37:11 +02001542 _intel_set_memory_cxsr(dev_priv, true);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001543
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001544 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001545 chv_set_memory_pm5(dev_priv, true);
1546
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001547 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03001548 chv_set_memory_dvfs(dev_priv, true);
1549
Ville Syrjäläfa292a42016-11-28 19:37:16 +02001550 *old_wm = new_wm;
Ville Syrjälä3c2777f2014-06-26 17:03:06 +03001551}
1552
Ville Syrjäläff32c542017-03-02 19:14:57 +02001553static void vlv_initial_watermarks(struct intel_atomic_state *state,
1554 struct intel_crtc_state *crtc_state)
1555{
1556 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1557 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1558
1559 mutex_lock(&dev_priv->wm.wm_mutex);
Ville Syrjälä4841da52017-03-02 19:14:59 +02001560 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
1561 vlv_program_watermarks(dev_priv);
1562 mutex_unlock(&dev_priv->wm.wm_mutex);
1563}
1564
1565static void vlv_optimize_watermarks(struct intel_atomic_state *state,
1566 struct intel_crtc_state *crtc_state)
1567{
1568 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1569 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
1570
1571 if (!crtc_state->wm.need_postvbl_update)
1572 return;
1573
1574 mutex_lock(&dev_priv->wm.wm_mutex);
1575 intel_crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
Ville Syrjäläff32c542017-03-02 19:14:57 +02001576 vlv_program_watermarks(dev_priv);
1577 mutex_unlock(&dev_priv->wm.wm_mutex);
1578}
1579
Ville Syrjäläae801522015-03-05 21:19:49 +02001580#define single_plane_enabled(mask) is_power_of_2(mask)
1581
Ville Syrjälä432081b2016-10-31 22:37:03 +02001582static void g4x_update_wm(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001583{
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001584 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001585 static const int sr_latency_ns = 12000;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001586 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1587 int plane_sr, cursor_sr;
1588 unsigned int enabled = 0;
Imre Deak98584252014-06-13 14:54:20 +03001589 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001590
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001591 if (g4x_compute_wm0(dev_priv, PIPE_A,
Chris Wilson5aef6002014-09-03 11:56:07 +01001592 &g4x_wm_info, pessimal_latency_ns,
1593 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001594 &planea_wm, &cursora_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001595 enabled |= 1 << PIPE_A;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001596
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001597 if (g4x_compute_wm0(dev_priv, PIPE_B,
Chris Wilson5aef6002014-09-03 11:56:07 +01001598 &g4x_wm_info, pessimal_latency_ns,
1599 &g4x_cursor_wm_info, pessimal_latency_ns,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001600 &planeb_wm, &cursorb_wm))
Ville Syrjälä51cea1f2013-03-21 13:10:44 +02001601 enabled |= 1 << PIPE_B;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001602
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001603 if (single_plane_enabled(enabled) &&
Ville Syrjäläf0ce2312016-10-31 22:37:08 +02001604 g4x_compute_srwm(dev_priv, ffs(enabled) - 1,
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001605 sr_latency_ns,
1606 &g4x_wm_info,
1607 &g4x_cursor_wm_info,
Chris Wilson52bd02d2012-12-07 10:43:24 +00001608 &plane_sr, &cursor_sr)) {
Imre Deak98584252014-06-13 14:54:20 +03001609 cxsr_enabled = true;
Chris Wilson52bd02d2012-12-07 10:43:24 +00001610 } else {
Imre Deak98584252014-06-13 14:54:20 +03001611 cxsr_enabled = false;
Imre Deak5209b1f2014-07-01 12:36:17 +03001612 intel_set_memory_cxsr(dev_priv, false);
Chris Wilson52bd02d2012-12-07 10:43:24 +00001613 plane_sr = cursor_sr = 0;
1614 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001615
Ville Syrjäläa5043452014-06-28 02:04:18 +03001616 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1617 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001618 planea_wm, cursora_wm,
1619 planeb_wm, cursorb_wm,
1620 plane_sr, cursor_sr);
1621
1622 I915_WRITE(DSPFW1,
Ville Syrjäläf4998962015-03-10 17:02:21 +02001623 FW_WM(plane_sr, SR) |
1624 FW_WM(cursorb_wm, CURSORB) |
1625 FW_WM(planeb_wm, PLANEB) |
1626 FW_WM(planea_wm, PLANEA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001627 I915_WRITE(DSPFW2,
Chris Wilson8c919b22012-12-04 16:33:19 +00001628 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001629 FW_WM(cursora_wm, CURSORA));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001630 /* HPLL off in SR has some issues on G4x... disable it */
1631 I915_WRITE(DSPFW3,
Chris Wilson8c919b22012-12-04 16:33:19 +00001632 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
Ville Syrjäläf4998962015-03-10 17:02:21 +02001633 FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001634
1635 if (cxsr_enabled)
1636 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001637}
1638
Ville Syrjälä432081b2016-10-31 22:37:03 +02001639static void i965_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001640{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001641 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001642 struct intel_crtc *crtc;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001643 int srwm = 1;
1644 int cursor_sr = 16;
Imre Deak98584252014-06-13 14:54:20 +03001645 bool cxsr_enabled;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001646
1647 /* Calc sr entries for one plane configs */
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001648 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001649 if (crtc) {
1650 /* self-refresh has much higher latency */
1651 static const int sr_latency_ns = 12000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001652 const struct drm_display_mode *adjusted_mode =
1653 &crtc->config->base.adjusted_mode;
1654 const struct drm_framebuffer *fb =
1655 crtc->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001656 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001657 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001658 int hdisplay = crtc->config->pipe_src_w;
Ville Syrjälä353c8592016-12-14 23:30:57 +02001659 int cpp = fb->format->cpp[0];
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001660 unsigned long line_time_us;
1661 int entries;
1662
Ville Syrjälä922044c2014-02-14 14:18:57 +02001663 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001664
1665 /* Use ns/us then divide to preserve precision */
1666 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001667 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001668 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1669 srwm = I965_FIFO_SIZE - entries;
1670 if (srwm < 0)
1671 srwm = 1;
1672 srwm &= 0x1ff;
1673 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1674 entries, srwm);
1675
1676 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläefc26112016-10-31 22:37:04 +02001677 cpp * crtc->base.cursor->state->crtc_w;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001678 entries = DIV_ROUND_UP(entries,
1679 i965_cursor_wm_info.cacheline_size);
1680 cursor_sr = i965_cursor_wm_info.fifo_size -
1681 (entries + i965_cursor_wm_info.guard_size);
1682
1683 if (cursor_sr > i965_cursor_wm_info.max_wm)
1684 cursor_sr = i965_cursor_wm_info.max_wm;
1685
1686 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1687 "cursor %d\n", srwm, cursor_sr);
1688
Imre Deak98584252014-06-13 14:54:20 +03001689 cxsr_enabled = true;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001690 } else {
Imre Deak98584252014-06-13 14:54:20 +03001691 cxsr_enabled = false;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001692 /* Turn off self refresh if both pipes are enabled */
Imre Deak5209b1f2014-07-01 12:36:17 +03001693 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001694 }
1695
1696 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1697 srwm);
1698
1699 /* 965 has limitations... */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001700 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1701 FW_WM(8, CURSORB) |
1702 FW_WM(8, PLANEB) |
1703 FW_WM(8, PLANEA));
1704 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1705 FW_WM(8, PLANEC_OLD));
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001706 /* update cursor SR watermark */
Ville Syrjäläf4998962015-03-10 17:02:21 +02001707 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
Imre Deak98584252014-06-13 14:54:20 +03001708
1709 if (cxsr_enabled)
1710 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001711}
1712
Ville Syrjäläf4998962015-03-10 17:02:21 +02001713#undef FW_WM
1714
Ville Syrjälä432081b2016-10-31 22:37:03 +02001715static void i9xx_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001716{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001717 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001718 const struct intel_watermark_params *wm_info;
1719 uint32_t fwater_lo;
1720 uint32_t fwater_hi;
1721 int cwm, srwm = 1;
1722 int fifo_size;
1723 int planea_wm, planeb_wm;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001724 struct intel_crtc *crtc, *enabled = NULL;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001725
Ville Syrjäläa9097be2016-10-31 22:37:20 +02001726 if (IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001727 wm_info = &i945_wm_info;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001728 else if (!IS_GEN2(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001729 wm_info = &i915_wm_info;
1730 else
Ville Syrjälä9d539102014-08-15 01:21:53 +03001731 wm_info = &i830_a_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001732
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001733 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 0);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001734 crtc = intel_get_crtc_for_plane(dev_priv, 0);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001735 if (intel_crtc_active(crtc)) {
1736 const struct drm_display_mode *adjusted_mode =
1737 &crtc->config->base.adjusted_mode;
1738 const struct drm_framebuffer *fb =
1739 crtc->base.primary->state->fb;
1740 int cpp;
1741
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001742 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001743 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001744 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001745 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001746
Damien Lespiau241bfc32013-09-25 16:45:37 +01001747 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001748 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001749 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001750 enabled = crtc;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001751 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001752 planea_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001753 if (planea_wm > (long)wm_info->max_wm)
1754 planea_wm = wm_info->max_wm;
1755 }
1756
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001757 if (IS_GEN2(dev_priv))
Ville Syrjälä9d539102014-08-15 01:21:53 +03001758 wm_info = &i830_bc_wm_info;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001759
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001760 fifo_size = dev_priv->display.get_fifo_size(dev_priv, 1);
Ville Syrjäläb91eb5c2016-10-31 22:37:09 +02001761 crtc = intel_get_crtc_for_plane(dev_priv, 1);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001762 if (intel_crtc_active(crtc)) {
1763 const struct drm_display_mode *adjusted_mode =
1764 &crtc->config->base.adjusted_mode;
1765 const struct drm_framebuffer *fb =
1766 crtc->base.primary->state->fb;
1767 int cpp;
1768
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01001769 if (IS_GEN2(dev_priv))
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001770 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001771 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001772 cpp = fb->format->cpp[0];
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001773
Damien Lespiau241bfc32013-09-25 16:45:37 +01001774 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Chris Wilsonb9e0bda2012-10-22 12:32:15 +01001775 wm_info, fifo_size, cpp,
Chris Wilson5aef6002014-09-03 11:56:07 +01001776 pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001777 if (enabled == NULL)
1778 enabled = crtc;
1779 else
1780 enabled = NULL;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001781 } else {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001782 planeb_wm = fifo_size - wm_info->guard_size;
Ville Syrjälä9d539102014-08-15 01:21:53 +03001783 if (planeb_wm > (long)wm_info->max_wm)
1784 planeb_wm = wm_info->max_wm;
1785 }
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001786
1787 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1788
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001789 if (IS_I915GM(dev_priv) && enabled) {
Matt Roper2ff8fde2014-07-08 07:50:07 -07001790 struct drm_i915_gem_object *obj;
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001791
Ville Syrjäläefc26112016-10-31 22:37:04 +02001792 obj = intel_fb_obj(enabled->base.primary->state->fb);
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001793
1794 /* self-refresh seems busted with untiled */
Chris Wilson3e510a82016-08-05 10:14:23 +01001795 if (!i915_gem_object_is_tiled(obj))
Daniel Vetter2ab1bc92014-04-07 08:54:21 +02001796 enabled = NULL;
1797 }
1798
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001799 /*
1800 * Overlay gets an aggressive default since video jitter is bad.
1801 */
1802 cwm = 2;
1803
1804 /* Play safe and disable self-refresh before adjusting watermarks. */
Imre Deak5209b1f2014-07-01 12:36:17 +03001805 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001806
1807 /* Calc sr entries for one plane configs */
Ville Syrjälä03427fc2016-10-31 22:37:18 +02001808 if (HAS_FW_BLC(dev_priv) && enabled) {
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001809 /* self-refresh has much higher latency */
1810 static const int sr_latency_ns = 6000;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001811 const struct drm_display_mode *adjusted_mode =
1812 &enabled->config->base.adjusted_mode;
1813 const struct drm_framebuffer *fb =
1814 enabled->base.primary->state->fb;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001815 int clock = adjusted_mode->crtc_clock;
Jesse Barnesfec8cba2013-11-27 11:10:26 -08001816 int htotal = adjusted_mode->crtc_htotal;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001817 int hdisplay = enabled->config->pipe_src_w;
1818 int cpp;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001819 unsigned long line_time_us;
1820 int entries;
1821
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001822 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001823 cpp = 4;
Ville Syrjäläefc26112016-10-31 22:37:04 +02001824 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02001825 cpp = fb->format->cpp[0];
Ville Syrjälä2d1b5052016-07-29 17:57:01 +03001826
Ville Syrjälä922044c2014-02-14 14:18:57 +02001827 line_time_us = max(htotal * 1000 / clock, 1);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001828
1829 /* Use ns/us then divide to preserve precision */
1830 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Ville Syrjäläac484962016-01-20 21:05:26 +02001831 cpp * hdisplay;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001832 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1833 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1834 srwm = wm_info->fifo_size - entries;
1835 if (srwm < 0)
1836 srwm = 1;
1837
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001838 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001839 I915_WRITE(FW_BLC_SELF,
1840 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
Ville Syrjäläacb91352016-07-29 17:57:02 +03001841 else
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001842 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1843 }
1844
1845 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1846 planea_wm, planeb_wm, cwm, srwm);
1847
1848 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1849 fwater_hi = (cwm & 0x1f);
1850
1851 /* Set request length to 8 cachelines per fetch */
1852 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1853 fwater_hi = fwater_hi | (1 << 8);
1854
1855 I915_WRITE(FW_BLC, fwater_lo);
1856 I915_WRITE(FW_BLC2, fwater_hi);
1857
Imre Deak5209b1f2014-07-01 12:36:17 +03001858 if (enabled)
1859 intel_set_memory_cxsr(dev_priv, true);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001860}
1861
Ville Syrjälä432081b2016-10-31 22:37:03 +02001862static void i845_update_wm(struct intel_crtc *unused_crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001863{
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001864 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
Ville Syrjäläefc26112016-10-31 22:37:04 +02001865 struct intel_crtc *crtc;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001866 const struct drm_display_mode *adjusted_mode;
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001867 uint32_t fwater_lo;
1868 int planea_wm;
1869
Ville Syrjäläffc7a762016-10-31 22:37:21 +02001870 crtc = single_enabled_crtc(dev_priv);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001871 if (crtc == NULL)
1872 return;
1873
Ville Syrjäläefc26112016-10-31 22:37:04 +02001874 adjusted_mode = &crtc->config->base.adjusted_mode;
Damien Lespiau241bfc32013-09-25 16:45:37 +01001875 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
Daniel Vetterfeb56b92013-12-14 20:38:30 -02001876 &i845_wm_info,
Ville Syrjäläef0f5e92016-10-31 22:37:17 +02001877 dev_priv->display.get_fifo_size(dev_priv, 0),
Chris Wilson5aef6002014-09-03 11:56:07 +01001878 4, pessimal_latency_ns);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03001879 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1880 fwater_lo |= (3<<8) | planea_wm;
1881
1882 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1883
1884 I915_WRITE(FW_BLC, fwater_lo);
1885}
1886
Ville Syrjälä37126462013-08-01 16:18:55 +03001887/* latency must be in 0.1us units. */
Ville Syrjäläac484962016-01-20 21:05:26 +02001888static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001889{
1890 uint64_t ret;
1891
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001892 if (WARN(latency == 0, "Latency value missing\n"))
1893 return UINT_MAX;
1894
Ville Syrjäläac484962016-01-20 21:05:26 +02001895 ret = (uint64_t) pixel_rate * cpp * latency;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001896 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1897
1898 return ret;
1899}
1900
Ville Syrjälä37126462013-08-01 16:18:55 +03001901/* latency must be in 0.1us units. */
Ville Syrjälä23297042013-07-05 11:57:17 +03001902static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
Ville Syrjäläac484962016-01-20 21:05:26 +02001903 uint32_t horiz_pixels, uint8_t cpp,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001904 uint32_t latency)
1905{
1906 uint32_t ret;
1907
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001908 if (WARN(latency == 0, "Latency value missing\n"))
1909 return UINT_MAX;
Matt Roper15126882015-12-03 11:37:40 -08001910 if (WARN_ON(!pipe_htotal))
1911 return UINT_MAX;
Ville Syrjälä3312ba62013-08-01 16:18:53 +03001912
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001913 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
Ville Syrjäläac484962016-01-20 21:05:26 +02001914 ret = (ret + 1) * horiz_pixels * cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001915 ret = DIV_ROUND_UP(ret, 64) + 2;
1916 return ret;
1917}
1918
Ville Syrjälä23297042013-07-05 11:57:17 +03001919static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
Ville Syrjäläac484962016-01-20 21:05:26 +02001920 uint8_t cpp)
Paulo Zanonicca32e92013-05-31 11:45:06 -03001921{
Matt Roper15126882015-12-03 11:37:40 -08001922 /*
1923 * Neither of these should be possible since this function shouldn't be
1924 * called if the CRTC is off or the plane is invisible. But let's be
1925 * extra paranoid to avoid a potential divide-by-zero if we screw up
1926 * elsewhere in the driver.
1927 */
Ville Syrjäläac484962016-01-20 21:05:26 +02001928 if (WARN_ON(!cpp))
Matt Roper15126882015-12-03 11:37:40 -08001929 return 0;
1930 if (WARN_ON(!horiz_pixels))
1931 return 0;
1932
Ville Syrjäläac484962016-01-20 21:05:26 +02001933 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001934}
1935
Imre Deak820c1982013-12-17 14:46:36 +02001936struct ilk_wm_maximums {
Paulo Zanonicca32e92013-05-31 11:45:06 -03001937 uint16_t pri;
1938 uint16_t spr;
1939 uint16_t cur;
1940 uint16_t fbc;
1941};
1942
Ville Syrjälä37126462013-08-01 16:18:55 +03001943/*
1944 * For both WM_PIPE and WM_LP.
1945 * mem_value must be in 0.1us units.
1946 */
Matt Roper7221fc32015-09-24 15:53:08 -07001947static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001948 const struct intel_plane_state *pstate,
Paulo Zanonicca32e92013-05-31 11:45:06 -03001949 uint32_t mem_value,
1950 bool is_lp)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001951{
Paulo Zanonicca32e92013-05-31 11:45:06 -03001952 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001953 int cpp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03001954
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001955 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001956 return 0;
1957
Ville Syrjälä353c8592016-12-14 23:30:57 +02001958 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001959
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001960 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001961
1962 if (!is_lp)
1963 return method1;
1964
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001965 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07001966 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001967 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001968 cpp, mem_value);
Paulo Zanonicca32e92013-05-31 11:45:06 -03001969
1970 return min(method1, method2);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001971}
1972
Ville Syrjälä37126462013-08-01 16:18:55 +03001973/*
1974 * For both WM_PIPE and WM_LP.
1975 * mem_value must be in 0.1us units.
1976 */
Matt Roper7221fc32015-09-24 15:53:08 -07001977static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07001978 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001979 uint32_t mem_value)
1980{
1981 uint32_t method1, method2;
Ville Syrjälä83054942016-11-18 21:53:00 +02001982 int cpp;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001983
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001984 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001985 return 0;
1986
Ville Syrjälä353c8592016-12-14 23:30:57 +02001987 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02001988
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02001989 method1 = ilk_wm_method1(cstate->pixel_rate, cpp, mem_value);
1990 method2 = ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07001991 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjälä936e71e2016-07-26 19:06:59 +03001992 drm_rect_width(&pstate->base.dst),
Ville Syrjäläac484962016-01-20 21:05:26 +02001993 cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03001994 return min(method1, method2);
1995}
1996
Ville Syrjälä37126462013-08-01 16:18:55 +03001997/*
1998 * For both WM_PIPE and WM_LP.
1999 * mem_value must be in 0.1us units.
2000 */
Matt Roper7221fc32015-09-24 15:53:08 -07002001static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002002 const struct intel_plane_state *pstate,
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002003 uint32_t mem_value)
2004{
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002005 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002006
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002007 /*
2008 * Treat cursor with fb as always visible since cursor updates
2009 * can happen faster than the vrefresh rate, and the current
2010 * watermark code doesn't handle that correctly. Cursor updates
2011 * which set/clear the fb or change the cursor size are going
2012 * to get throttled by intel_legacy_cursor_update() to work
2013 * around this problem with the watermark code.
2014 */
2015 if (!cstate->base.active || !pstate->base.fb)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002016 return 0;
2017
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002018 cpp = pstate->base.fb->format->cpp[0];
2019
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02002020 return ilk_wm_method2(cstate->pixel_rate,
Matt Roper7221fc32015-09-24 15:53:08 -07002021 cstate->base.adjusted_mode.crtc_htotal,
Ville Syrjäläa5509ab2017-02-17 17:01:59 +02002022 pstate->base.crtc_w, cpp, mem_value);
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002023}
2024
Paulo Zanonicca32e92013-05-31 11:45:06 -03002025/* Only for WM_LP. */
Matt Roper7221fc32015-09-24 15:53:08 -07002026static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
Matt Roper43d59ed2015-09-24 15:53:07 -07002027 const struct intel_plane_state *pstate,
Ville Syrjälä1fda9882013-07-05 11:57:19 +03002028 uint32_t pri_val)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002029{
Ville Syrjälä83054942016-11-18 21:53:00 +02002030 int cpp;
Matt Roper43d59ed2015-09-24 15:53:07 -07002031
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002032 if (!cstate->base.active || !pstate->base.visible)
Paulo Zanonicca32e92013-05-31 11:45:06 -03002033 return 0;
2034
Ville Syrjälä353c8592016-12-14 23:30:57 +02002035 cpp = pstate->base.fb->format->cpp[0];
Ville Syrjälä83054942016-11-18 21:53:00 +02002036
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002037 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->base.dst), cpp);
Paulo Zanonicca32e92013-05-31 11:45:06 -03002038}
2039
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002040static unsigned int
2041ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002042{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002043 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002044 return 3072;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002045 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002046 return 768;
2047 else
2048 return 512;
2049}
2050
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002051static unsigned int
2052ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2053 int level, bool is_sprite)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002054{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002055 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002056 /* BDW primary/sprite plane watermarks */
2057 return level == 0 ? 255 : 2047;
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002058 else if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002059 /* IVB/HSW primary/sprite plane watermarks */
2060 return level == 0 ? 127 : 1023;
2061 else if (!is_sprite)
2062 /* ILK/SNB primary plane watermarks */
2063 return level == 0 ? 127 : 511;
2064 else
2065 /* ILK/SNB sprite plane watermarks */
2066 return level == 0 ? 63 : 255;
2067}
2068
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002069static unsigned int
2070ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002071{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002072 if (INTEL_GEN(dev_priv) >= 7)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002073 return level == 0 ? 63 : 255;
2074 else
2075 return level == 0 ? 31 : 63;
2076}
2077
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002078static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002079{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002080 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä4e975082014-03-07 18:32:11 +02002081 return 31;
2082 else
2083 return 15;
2084}
2085
Ville Syrjälä158ae642013-08-07 13:28:19 +03002086/* Calculate the maximum primary/sprite plane watermark */
2087static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
2088 int level,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002089 const struct intel_wm_config *config,
Ville Syrjälä158ae642013-08-07 13:28:19 +03002090 enum intel_ddb_partitioning ddb_partitioning,
2091 bool is_sprite)
2092{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002093 struct drm_i915_private *dev_priv = to_i915(dev);
2094 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002095
2096 /* if sprites aren't enabled, sprites get nothing */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002097 if (is_sprite && !config->sprites_enabled)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002098 return 0;
2099
2100 /* HSW allows LP1+ watermarks even with multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002101 if (level == 0 || config->num_pipes_active > 1) {
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002102 fifo_size /= INTEL_INFO(dev_priv)->num_pipes;
Ville Syrjälä158ae642013-08-07 13:28:19 +03002103
2104 /*
2105 * For some reason the non self refresh
2106 * FIFO size is only half of the self
2107 * refresh FIFO size on ILK/SNB.
2108 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002109 if (INTEL_GEN(dev_priv) <= 6)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002110 fifo_size /= 2;
2111 }
2112
Ville Syrjälä240264f2013-08-07 13:29:12 +03002113 if (config->sprites_enabled) {
Ville Syrjälä158ae642013-08-07 13:28:19 +03002114 /* level 0 is always calculated with 1:1 split */
2115 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2116 if (is_sprite)
2117 fifo_size *= 5;
2118 fifo_size /= 6;
2119 } else {
2120 fifo_size /= 2;
2121 }
2122 }
2123
2124 /* clamp to max that the registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002125 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002126}
2127
2128/* Calculate the maximum cursor plane watermark */
2129static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
Ville Syrjälä240264f2013-08-07 13:29:12 +03002130 int level,
2131 const struct intel_wm_config *config)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002132{
2133 /* HSW LP1+ watermarks w/ multiple pipes */
Ville Syrjälä240264f2013-08-07 13:29:12 +03002134 if (level > 0 && config->num_pipes_active > 1)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002135 return 64;
2136
2137 /* otherwise just report max that registers can hold */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002138 return ilk_cursor_wm_reg_max(to_i915(dev), level);
Ville Syrjälä158ae642013-08-07 13:28:19 +03002139}
2140
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002141static void ilk_compute_wm_maximums(const struct drm_device *dev,
Ville Syrjälä34982fe2013-10-09 19:18:09 +03002142 int level,
2143 const struct intel_wm_config *config,
2144 enum intel_ddb_partitioning ddb_partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002145 struct ilk_wm_maximums *max)
Ville Syrjälä158ae642013-08-07 13:28:19 +03002146{
Ville Syrjälä240264f2013-08-07 13:29:12 +03002147 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
2148 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
2149 max->cur = ilk_cursor_wm_max(dev, level, config);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002150 max->fbc = ilk_fbc_wm_reg_max(to_i915(dev));
Ville Syrjälä158ae642013-08-07 13:28:19 +03002151}
2152
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002153static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002154 int level,
2155 struct ilk_wm_maximums *max)
2156{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002157 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2158 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2159 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2160 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002161}
2162
Ville Syrjäläd9395652013-10-09 19:18:10 +03002163static bool ilk_validate_wm_level(int level,
Imre Deak820c1982013-12-17 14:46:36 +02002164 const struct ilk_wm_maximums *max,
Ville Syrjäläd9395652013-10-09 19:18:10 +03002165 struct intel_wm_level *result)
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002166{
2167 bool ret;
2168
2169 /* already determined to be invalid? */
2170 if (!result->enable)
2171 return false;
2172
2173 result->enable = result->pri_val <= max->pri &&
2174 result->spr_val <= max->spr &&
2175 result->cur_val <= max->cur;
2176
2177 ret = result->enable;
2178
2179 /*
2180 * HACK until we can pre-compute everything,
2181 * and thus fail gracefully if LP0 watermarks
2182 * are exceeded...
2183 */
2184 if (level == 0 && !result->enable) {
2185 if (result->pri_val > max->pri)
2186 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2187 level, result->pri_val, max->pri);
2188 if (result->spr_val > max->spr)
2189 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2190 level, result->spr_val, max->spr);
2191 if (result->cur_val > max->cur)
2192 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2193 level, result->cur_val, max->cur);
2194
2195 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
2196 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
2197 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
2198 result->enable = true;
2199 }
2200
Ville Syrjäläa9786a12013-08-07 13:24:47 +03002201 return ret;
2202}
2203
Damien Lespiaud34ff9c2014-01-06 19:17:23 +00002204static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
Matt Roper43d59ed2015-09-24 15:53:07 -07002205 const struct intel_crtc *intel_crtc,
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002206 int level,
Matt Roper7221fc32015-09-24 15:53:08 -07002207 struct intel_crtc_state *cstate,
Matt Roper86c8bbb2015-09-24 15:53:16 -07002208 struct intel_plane_state *pristate,
2209 struct intel_plane_state *sprstate,
2210 struct intel_plane_state *curstate,
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002211 struct intel_wm_level *result)
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002212{
2213 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2214 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2215 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2216
2217 /* WM1+ latency values stored in 0.5us units */
2218 if (level > 0) {
2219 pri_latency *= 5;
2220 spr_latency *= 5;
2221 cur_latency *= 5;
2222 }
2223
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002224 if (pristate) {
2225 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2226 pri_latency, level);
2227 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2228 }
2229
2230 if (sprstate)
2231 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2232
2233 if (curstate)
2234 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2235
Ville Syrjälä6f5ddd12013-08-06 22:24:02 +03002236 result->enable = true;
2237}
2238
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002239static uint32_t
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002240hsw_compute_linetime_wm(const struct intel_crtc_state *cstate)
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002241{
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002242 const struct intel_atomic_state *intel_state =
2243 to_intel_atomic_state(cstate->base.state);
Matt Roperee91a152015-12-03 11:37:39 -08002244 const struct drm_display_mode *adjusted_mode =
2245 &cstate->base.adjusted_mode;
Paulo Zanoni85a02de2013-05-03 17:23:43 -03002246 u32 linetime, ips_linetime;
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002247
Matt Roperee91a152015-12-03 11:37:39 -08002248 if (!cstate->base.active)
2249 return 0;
2250 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2251 return 0;
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002252 if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002253 return 0;
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002254
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002255 /* The WM are computed with base on how long it takes to fill a single
2256 * row at the given clock rate, multiplied by 8.
2257 * */
Ville Syrjälä124abe02015-09-08 13:40:45 +03002258 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2259 adjusted_mode->crtc_clock);
2260 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
Ville Syrjäläbb0f4aa2017-01-20 20:21:59 +02002261 intel_state->cdclk.logical.cdclk);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002262
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002263 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2264 PIPE_WM_LINETIME_TIME(linetime);
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03002265}
2266
Ville Syrjäläbb726512016-10-31 22:37:24 +02002267static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2268 uint16_t wm[8])
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002269{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002270 if (IS_GEN9(dev_priv)) {
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002271 uint32_t val;
Vandana Kannan4f947382014-11-04 17:06:47 +00002272 int ret, i;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002273 int level, max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002274
2275 /* read the first set of memory latencies[0:3] */
2276 val = 0; /* data0 to be programmed to 0 for first set */
2277 mutex_lock(&dev_priv->rps.hw_lock);
2278 ret = sandybridge_pcode_read(dev_priv,
2279 GEN9_PCODE_READ_MEM_LATENCY,
2280 &val);
2281 mutex_unlock(&dev_priv->rps.hw_lock);
2282
2283 if (ret) {
2284 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2285 return;
2286 }
2287
2288 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2289 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2290 GEN9_MEM_LATENCY_LEVEL_MASK;
2291 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2292 GEN9_MEM_LATENCY_LEVEL_MASK;
2293 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2294 GEN9_MEM_LATENCY_LEVEL_MASK;
2295
2296 /* read the second set of memory latencies[4:7] */
2297 val = 1; /* data0 to be programmed to 1 for second set */
2298 mutex_lock(&dev_priv->rps.hw_lock);
2299 ret = sandybridge_pcode_read(dev_priv,
2300 GEN9_PCODE_READ_MEM_LATENCY,
2301 &val);
2302 mutex_unlock(&dev_priv->rps.hw_lock);
2303 if (ret) {
2304 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2305 return;
2306 }
2307
2308 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2309 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2310 GEN9_MEM_LATENCY_LEVEL_MASK;
2311 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2312 GEN9_MEM_LATENCY_LEVEL_MASK;
2313 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2314 GEN9_MEM_LATENCY_LEVEL_MASK;
2315
Vandana Kannan367294b2014-11-04 17:06:46 +00002316 /*
Paulo Zanoni0727e402016-09-22 18:00:30 -03002317 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2318 * need to be disabled. We make sure to sanitize the values out
2319 * of the punit to satisfy this requirement.
2320 */
2321 for (level = 1; level <= max_level; level++) {
2322 if (wm[level] == 0) {
2323 for (i = level + 1; i <= max_level; i++)
2324 wm[i] = 0;
2325 break;
2326 }
2327 }
2328
2329 /*
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02002330 * WaWmMemoryReadLatency:skl,glk
Damien Lespiau6f972352015-02-09 19:33:07 +00002331 *
Vandana Kannan367294b2014-11-04 17:06:46 +00002332 * punit doesn't take into account the read latency so we need
Paulo Zanoni0727e402016-09-22 18:00:30 -03002333 * to add 2us to the various latency levels we retrieve from the
2334 * punit when level 0 response data us 0us.
Vandana Kannan367294b2014-11-04 17:06:46 +00002335 */
Paulo Zanoni0727e402016-09-22 18:00:30 -03002336 if (wm[0] == 0) {
2337 wm[0] += 2;
2338 for (level = 1; level <= max_level; level++) {
2339 if (wm[level] == 0)
2340 break;
Vandana Kannan367294b2014-11-04 17:06:46 +00002341 wm[level] += 2;
Vandana Kannan4f947382014-11-04 17:06:47 +00002342 }
Paulo Zanoni0727e402016-09-22 18:00:30 -03002343 }
2344
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002345 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002346 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2347
2348 wm[0] = (sskpd >> 56) & 0xFF;
2349 if (wm[0] == 0)
2350 wm[0] = sskpd & 0xF;
Ville Syrjäläe5d50192013-07-05 11:57:22 +03002351 wm[1] = (sskpd >> 4) & 0xFF;
2352 wm[2] = (sskpd >> 12) & 0xFF;
2353 wm[3] = (sskpd >> 20) & 0x1FF;
2354 wm[4] = (sskpd >> 32) & 0x1FF;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002355 } else if (INTEL_GEN(dev_priv) >= 6) {
Ville Syrjälä63cf9a12013-07-05 11:57:23 +03002356 uint32_t sskpd = I915_READ(MCH_SSKPD);
2357
2358 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2359 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2360 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2361 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
Ville Syrjäläbb726512016-10-31 22:37:24 +02002362 } else if (INTEL_GEN(dev_priv) >= 5) {
Ville Syrjälä3a88d0a2013-08-01 16:18:49 +03002363 uint32_t mltr = I915_READ(MLTR_ILK);
2364
2365 /* ILK primary LP0 latency is 700 ns */
2366 wm[0] = 7;
2367 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2368 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
Ville Syrjälä12b134d2013-07-05 11:57:21 +03002369 }
2370}
2371
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002372static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2373 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002374{
2375 /* ILK sprite LP0 latency is 1300 ns */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002376 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002377 wm[0] = 13;
2378}
2379
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002380static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2381 uint16_t wm[5])
Ville Syrjälä53615a52013-08-01 16:18:50 +03002382{
2383 /* ILK cursor LP0 latency is 1300 ns */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002384 if (IS_GEN5(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002385 wm[0] = 13;
2386
2387 /* WaDoubleCursorLP3Latency:ivb */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002388 if (IS_IVYBRIDGE(dev_priv))
Ville Syrjälä53615a52013-08-01 16:18:50 +03002389 wm[3] *= 2;
2390}
2391
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002392int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002393{
2394 /* how many WM levels are we expecting */
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002395 if (INTEL_GEN(dev_priv) >= 9)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002396 return 7;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002397 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002398 return 4;
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002399 else if (INTEL_GEN(dev_priv) >= 6)
Ville Syrjäläad0d6dc2013-08-30 14:30:25 +03002400 return 3;
2401 else
2402 return 2;
2403}
Daniel Vetter7526ed72014-09-29 15:07:19 +02002404
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002405static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002406 const char *name,
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002407 const uint16_t wm[8])
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002408{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002409 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002410
2411 for (level = 0; level <= max_level; level++) {
2412 unsigned int latency = wm[level];
2413
2414 if (latency == 0) {
2415 DRM_ERROR("%s WM%d latency not provided\n",
2416 name, level);
2417 continue;
2418 }
2419
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002420 /*
2421 * - latencies are in us on gen9.
2422 * - before then, WM1+ latency values are in 0.5us units
2423 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002424 if (IS_GEN9(dev_priv))
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002425 latency *= 10;
2426 else if (level > 0)
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002427 latency *= 5;
2428
2429 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2430 name, level, wm[level],
2431 latency / 10, latency % 10);
2432 }
2433}
2434
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002435static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2436 uint16_t wm[5], uint16_t min)
2437{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002438 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002439
2440 if (wm[0] >= min)
2441 return false;
2442
2443 wm[0] = max(wm[0], min);
2444 for (level = 1; level <= max_level; level++)
2445 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2446
2447 return true;
2448}
2449
Ville Syrjäläbb726512016-10-31 22:37:24 +02002450static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002451{
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002452 bool changed;
2453
2454 /*
2455 * The BIOS provided WM memory latency values are often
2456 * inadequate for high resolution displays. Adjust them.
2457 */
2458 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2459 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2460 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2461
2462 if (!changed)
2463 return;
2464
2465 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002466 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2467 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2468 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002469}
2470
Ville Syrjäläbb726512016-10-31 22:37:24 +02002471static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
Ville Syrjälä53615a52013-08-01 16:18:50 +03002472{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002473 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002474
2475 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2476 sizeof(dev_priv->wm.pri_latency));
2477 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2478 sizeof(dev_priv->wm.pri_latency));
2479
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002480 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002481 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
Ville Syrjälä26ec9712013-08-01 16:18:52 +03002482
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002483 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
2484 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
2485 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
Ville Syrjäläe95a2f72014-05-08 15:09:19 +03002486
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002487 if (IS_GEN6(dev_priv))
Ville Syrjäläbb726512016-10-31 22:37:24 +02002488 snb_wm_latency_quirk(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03002489}
2490
Ville Syrjäläbb726512016-10-31 22:37:24 +02002491static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002492{
Ville Syrjäläbb726512016-10-31 22:37:24 +02002493 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002494 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
Pradeep Bhat2af30a52014-11-04 17:06:38 +00002495}
2496
Matt Ropered4a6a72016-02-23 17:20:13 -08002497static bool ilk_validate_pipe_wm(struct drm_device *dev,
2498 struct intel_pipe_wm *pipe_wm)
2499{
2500 /* LP0 watermark maximums depend on this pipe alone */
2501 const struct intel_wm_config config = {
2502 .num_pipes_active = 1,
2503 .sprites_enabled = pipe_wm->sprites_enabled,
2504 .sprites_scaled = pipe_wm->sprites_scaled,
2505 };
2506 struct ilk_wm_maximums max;
2507
2508 /* LP0 watermarks always use 1/2 DDB partitioning */
2509 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2510
2511 /* At least LP0 must be valid */
2512 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2513 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2514 return false;
2515 }
2516
2517 return true;
2518}
2519
Matt Roper261a27d2015-10-08 15:28:25 -07002520/* Compute new watermarks for the pipe */
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002521static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
Matt Roper261a27d2015-10-08 15:28:25 -07002522{
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002523 struct drm_atomic_state *state = cstate->base.state;
2524 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Matt Roper86c8bbb2015-09-24 15:53:16 -07002525 struct intel_pipe_wm *pipe_wm;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002526 struct drm_device *dev = state->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01002527 const struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper43d59ed2015-09-24 15:53:07 -07002528 struct intel_plane *intel_plane;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002529 struct intel_plane_state *pristate = NULL;
Matt Roper43d59ed2015-09-24 15:53:07 -07002530 struct intel_plane_state *sprstate = NULL;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002531 struct intel_plane_state *curstate = NULL;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002532 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
Imre Deak820c1982013-12-17 14:46:36 +02002533 struct ilk_wm_maximums max;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002534
Matt Ropere8f1f022016-05-12 07:05:55 -07002535 pipe_wm = &cstate->wm.ilk.optimal;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002536
Matt Roper43d59ed2015-09-24 15:53:07 -07002537 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002538 struct intel_plane_state *ps;
2539
2540 ps = intel_atomic_get_existing_plane_state(state,
2541 intel_plane);
2542 if (!ps)
2543 continue;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002544
2545 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002546 pristate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002547 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002548 sprstate = ps;
Matt Roper86c8bbb2015-09-24 15:53:16 -07002549 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002550 curstate = ps;
Matt Roper43d59ed2015-09-24 15:53:07 -07002551 }
2552
Matt Ropered4a6a72016-02-23 17:20:13 -08002553 pipe_wm->pipe_enabled = cstate->base.active;
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002554 if (sprstate) {
Ville Syrjälä936e71e2016-07-26 19:06:59 +03002555 pipe_wm->sprites_enabled = sprstate->base.visible;
2556 pipe_wm->sprites_scaled = sprstate->base.visible &&
2557 (drm_rect_width(&sprstate->base.dst) != drm_rect_width(&sprstate->base.src) >> 16 ||
2558 drm_rect_height(&sprstate->base.dst) != drm_rect_height(&sprstate->base.src) >> 16);
Maarten Lankhorste3bddde2016-03-01 11:07:22 +01002559 }
2560
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002561 usable_level = max_level;
2562
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002563 /* ILK/SNB: LP2+ watermarks only w/o sprites */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002564 if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002565 usable_level = 1;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002566
2567 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
Matt Ropered4a6a72016-02-23 17:20:13 -08002568 if (pipe_wm->sprites_scaled)
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002569 usable_level = 0;
Ville Syrjälä7b39a0b2013-12-05 15:51:30 +02002570
Matt Roper86c8bbb2015-09-24 15:53:16 -07002571 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002572 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2573
2574 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2575 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002576
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002577 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjälä532f7a72016-04-29 17:31:17 +03002578 pipe_wm->linetime = hsw_compute_linetime_wm(cstate);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002579
Matt Ropered4a6a72016-02-23 17:20:13 -08002580 if (!ilk_validate_pipe_wm(dev, pipe_wm))
Maarten Lankhorst1a426d62016-03-02 12:36:03 +01002581 return -EINVAL;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002582
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002583 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002584
2585 for (level = 1; level <= max_level; level++) {
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002586 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002587
Matt Roper86c8bbb2015-09-24 15:53:16 -07002588 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002589 pristate, sprstate, curstate, wm);
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002590
2591 /*
2592 * Disable any watermark level that exceeds the
2593 * register maximums since such watermarks are
2594 * always invalid.
2595 */
Maarten Lankhorst71f0a622016-03-08 10:57:16 +01002596 if (level > usable_level)
2597 continue;
2598
2599 if (ilk_validate_wm_level(level, &max, wm))
2600 pipe_wm->wm[level] = *wm;
2601 else
Maarten Lankhorstd81f04c2016-03-02 12:38:06 +01002602 usable_level = level;
Ville Syrjäläa3cb4042014-04-28 15:44:56 +03002603 }
2604
Matt Roper86c8bbb2015-09-24 15:53:16 -07002605 return 0;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002606}
2607
2608/*
Matt Ropered4a6a72016-02-23 17:20:13 -08002609 * Build a set of 'intermediate' watermark values that satisfy both the old
2610 * state and the new state. These can be programmed to the hardware
2611 * immediately.
2612 */
2613static int ilk_compute_intermediate_wm(struct drm_device *dev,
2614 struct intel_crtc *intel_crtc,
2615 struct intel_crtc_state *newstate)
2616{
Matt Ropere8f1f022016-05-12 07:05:55 -07002617 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08002618 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002619 int level, max_level = ilk_wm_max_level(to_i915(dev));
Matt Ropered4a6a72016-02-23 17:20:13 -08002620
2621 /*
2622 * Start with the final, target watermarks, then combine with the
2623 * currently active watermarks to get values that are safe both before
2624 * and after the vblank.
2625 */
Matt Ropere8f1f022016-05-12 07:05:55 -07002626 *a = newstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08002627 a->pipe_enabled |= b->pipe_enabled;
2628 a->sprites_enabled |= b->sprites_enabled;
2629 a->sprites_scaled |= b->sprites_scaled;
2630
2631 for (level = 0; level <= max_level; level++) {
2632 struct intel_wm_level *a_wm = &a->wm[level];
2633 const struct intel_wm_level *b_wm = &b->wm[level];
2634
2635 a_wm->enable &= b_wm->enable;
2636 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2637 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2638 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2639 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2640 }
2641
2642 /*
2643 * We need to make sure that these merged watermark values are
2644 * actually a valid configuration themselves. If they're not,
2645 * there's no safe way to transition from the old state to
2646 * the new state, so we need to fail the atomic transaction.
2647 */
2648 if (!ilk_validate_pipe_wm(dev, a))
2649 return -EINVAL;
2650
2651 /*
2652 * If our intermediate WM are identical to the final WM, then we can
2653 * omit the post-vblank programming; only update if it's different.
2654 */
Ville Syrjälä5eeb7982017-03-02 19:15:00 +02002655 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
2656 newstate->wm.need_postvbl_update = true;
Matt Ropered4a6a72016-02-23 17:20:13 -08002657
2658 return 0;
2659}
2660
2661/*
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002662 * Merge the watermarks from all active pipes for a specific level.
2663 */
2664static void ilk_merge_wm_level(struct drm_device *dev,
2665 int level,
2666 struct intel_wm_level *ret_wm)
2667{
2668 const struct intel_crtc *intel_crtc;
2669
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002670 ret_wm->enable = true;
2671
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002672 for_each_intel_crtc(dev, intel_crtc) {
Matt Ropered4a6a72016-02-23 17:20:13 -08002673 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
Ville Syrjäläfe392ef2014-03-07 18:32:10 +02002674 const struct intel_wm_level *wm = &active->wm[level];
2675
2676 if (!active->pipe_enabled)
2677 continue;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002678
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002679 /*
2680 * The watermark values may have been used in the past,
2681 * so we must maintain them in the registers for some
2682 * time even if the level is now disabled.
2683 */
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002684 if (!wm->enable)
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002685 ret_wm->enable = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002686
2687 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2688 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2689 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2690 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2691 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002692}
2693
2694/*
2695 * Merge all low power watermarks for all active pipes.
2696 */
2697static void ilk_wm_merge(struct drm_device *dev,
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002698 const struct intel_wm_config *config,
Imre Deak820c1982013-12-17 14:46:36 +02002699 const struct ilk_wm_maximums *max,
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002700 struct intel_pipe_wm *merged)
2701{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002702 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002703 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002704 int last_enabled_level = max_level;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002705
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002706 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01002707 if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002708 config->num_pipes_active > 1)
Ville Syrjälä1204d5b2016-04-01 21:53:18 +03002709 last_enabled_level = 0;
Ville Syrjälä0ba22e22013-12-05 15:51:34 +02002710
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002711 /* ILK: FBC WM must be disabled always */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002712 merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002713
2714 /* merge each WM1+ level */
2715 for (level = 1; level <= max_level; level++) {
2716 struct intel_wm_level *wm = &merged->wm[level];
2717
2718 ilk_merge_wm_level(dev, level, wm);
2719
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002720 if (level > last_enabled_level)
2721 wm->enable = false;
2722 else if (!ilk_validate_wm_level(level, max, wm))
2723 /* make sure all following levels get disabled */
2724 last_enabled_level = level - 1;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002725
2726 /*
2727 * The spec says it is preferred to disable
2728 * FBC WMs instead of disabling a WM level.
2729 */
2730 if (wm->fbc_val > max->fbc) {
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002731 if (wm->enable)
2732 merged->fbc_wm_enabled = false;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002733 wm->fbc_val = 0;
2734 }
2735 }
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002736
2737 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2738 /*
2739 * FIXME this is racy. FBC might get enabled later.
2740 * What we should check here is whether FBC can be
2741 * enabled sometime later.
2742 */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002743 if (IS_GEN5(dev_priv) && !merged->fbc_wm_enabled &&
Paulo Zanoni0e631ad2015-10-14 17:45:36 -03002744 intel_fbc_is_active(dev_priv)) {
Ville Syrjälä6c8b6c22013-12-05 15:51:35 +02002745 for (level = 2; level <= max_level; level++) {
2746 struct intel_wm_level *wm = &merged->wm[level];
2747
2748 wm->enable = false;
2749 }
2750 }
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002751}
2752
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002753static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2754{
2755 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2756 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2757}
2758
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002759/* The value we need to program into the WM_LPx latency field */
2760static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2761{
Chris Wilsonfac5e232016-07-04 11:34:36 +01002762 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002763
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002764 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002765 return 2 * level;
2766 else
2767 return dev_priv->wm.pri_latency[level];
2768}
2769
Imre Deak820c1982013-12-17 14:46:36 +02002770static void ilk_compute_wm_results(struct drm_device *dev,
Ville Syrjälä0362c782013-10-09 19:17:57 +03002771 const struct intel_pipe_wm *merged,
Ville Syrjälä609cede2013-10-09 19:18:03 +03002772 enum intel_ddb_partitioning partitioning,
Imre Deak820c1982013-12-17 14:46:36 +02002773 struct ilk_wm_values *results)
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002774{
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002775 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002776 struct intel_crtc *intel_crtc;
2777 int level, wm_lp;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002778
Ville Syrjälä0362c782013-10-09 19:17:57 +03002779 results->enable_fbc_wm = merged->fbc_wm_enabled;
Ville Syrjälä609cede2013-10-09 19:18:03 +03002780 results->partitioning = partitioning;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002781
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002782 /* LP1+ register values */
Paulo Zanonicca32e92013-05-31 11:45:06 -03002783 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
Ville Syrjälä1fd527c2013-08-06 22:24:05 +03002784 const struct intel_wm_level *r;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002785
Ville Syrjäläb380ca32013-10-09 19:18:01 +03002786 level = ilk_wm_lp_to_level(wm_lp, merged);
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002787
Ville Syrjälä0362c782013-10-09 19:17:57 +03002788 r = &merged->wm[level];
Paulo Zanonicca32e92013-05-31 11:45:06 -03002789
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002790 /*
2791 * Maintain the watermark values even if the level is
2792 * disabled. Doing otherwise could cause underruns.
2793 */
2794 results->wm_lp[wm_lp - 1] =
Ville Syrjäläa68d68e2013-12-05 15:51:29 +02002795 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
Ville Syrjälä416f4722013-11-02 21:07:46 -07002796 (r->pri_val << WM1_LP_SR_SHIFT) |
2797 r->cur_val;
2798
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002799 if (r->enable)
2800 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2801
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002802 if (INTEL_GEN(dev_priv) >= 8)
Ville Syrjälä416f4722013-11-02 21:07:46 -07002803 results->wm_lp[wm_lp - 1] |=
2804 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2805 else
2806 results->wm_lp[wm_lp - 1] |=
2807 r->fbc_val << WM1_LP_FBC_SHIFT;
2808
Ville Syrjäläd52fea52014-04-28 15:44:57 +03002809 /*
2810 * Always set WM1S_LP_EN when spr_val != 0, even if the
2811 * level is disabled. Doing otherwise could cause underruns.
2812 */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00002813 if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002814 WARN_ON(wm_lp != 1);
2815 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2816 } else
2817 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
Paulo Zanonicca32e92013-05-31 11:45:06 -03002818 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002819
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002820 /* LP0 register values */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01002821 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002822 enum pipe pipe = intel_crtc->pipe;
Matt Ropered4a6a72016-02-23 17:20:13 -08002823 const struct intel_wm_level *r =
2824 &intel_crtc->wm.active.ilk.wm[0];
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03002825
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002826 if (WARN_ON(!r->enable))
2827 continue;
2828
Matt Ropered4a6a72016-02-23 17:20:13 -08002829 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
Ville Syrjälä0b2ae6d2013-10-09 19:17:55 +03002830
2831 results->wm_pipe[pipe] =
2832 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2833 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2834 r->cur_val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002835 }
2836}
2837
Paulo Zanoni861f3382013-05-31 10:19:21 -03002838/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2839 * case both are at the same level. Prefer r1 in case they're the same. */
Imre Deak820c1982013-12-17 14:46:36 +02002840static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002841 struct intel_pipe_wm *r1,
2842 struct intel_pipe_wm *r2)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002843{
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01002844 int level, max_level = ilk_wm_max_level(to_i915(dev));
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002845 int level1 = 0, level2 = 0;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002846
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002847 for (level = 1; level <= max_level; level++) {
2848 if (r1->wm[level].enable)
2849 level1 = level;
2850 if (r2->wm[level].enable)
2851 level2 = level;
Paulo Zanoni861f3382013-05-31 10:19:21 -03002852 }
2853
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002854 if (level1 == level2) {
2855 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
Paulo Zanoni861f3382013-05-31 10:19:21 -03002856 return r2;
2857 else
2858 return r1;
Ville Syrjälä198a1e92013-10-09 19:17:58 +03002859 } else if (level1 > level2) {
Paulo Zanoni861f3382013-05-31 10:19:21 -03002860 return r1;
2861 } else {
2862 return r2;
2863 }
2864}
2865
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002866/* dirty bits used to track which watermarks need changes */
2867#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2868#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2869#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2870#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2871#define WM_DIRTY_FBC (1 << 24)
2872#define WM_DIRTY_DDB (1 << 25)
2873
Damien Lespiau055e3932014-08-18 13:49:10 +01002874static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
Imre Deak820c1982013-12-17 14:46:36 +02002875 const struct ilk_wm_values *old,
2876 const struct ilk_wm_values *new)
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002877{
2878 unsigned int dirty = 0;
2879 enum pipe pipe;
2880 int wm_lp;
2881
Damien Lespiau055e3932014-08-18 13:49:10 +01002882 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002883 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2884 dirty |= WM_DIRTY_LINETIME(pipe);
2885 /* Must disable LP1+ watermarks too */
2886 dirty |= WM_DIRTY_LP_ALL;
2887 }
2888
2889 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2890 dirty |= WM_DIRTY_PIPE(pipe);
2891 /* Must disable LP1+ watermarks too */
2892 dirty |= WM_DIRTY_LP_ALL;
2893 }
2894 }
2895
2896 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2897 dirty |= WM_DIRTY_FBC;
2898 /* Must disable LP1+ watermarks too */
2899 dirty |= WM_DIRTY_LP_ALL;
2900 }
2901
2902 if (old->partitioning != new->partitioning) {
2903 dirty |= WM_DIRTY_DDB;
2904 /* Must disable LP1+ watermarks too */
2905 dirty |= WM_DIRTY_LP_ALL;
2906 }
2907
2908 /* LP1+ watermarks already deemed dirty, no need to continue */
2909 if (dirty & WM_DIRTY_LP_ALL)
2910 return dirty;
2911
2912 /* Find the lowest numbered LP1+ watermark in need of an update... */
2913 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2914 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2915 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2916 break;
2917 }
2918
2919 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2920 for (; wm_lp <= 3; wm_lp++)
2921 dirty |= WM_DIRTY_LP(wm_lp);
2922
2923 return dirty;
2924}
2925
Ville Syrjälä8553c182013-12-05 15:51:39 +02002926static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2927 unsigned int dirty)
2928{
Imre Deak820c1982013-12-17 14:46:36 +02002929 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä8553c182013-12-05 15:51:39 +02002930 bool changed = false;
2931
2932 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2933 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2934 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2935 changed = true;
2936 }
2937 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2938 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2939 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2940 changed = true;
2941 }
2942 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2943 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2944 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2945 changed = true;
2946 }
2947
2948 /*
2949 * Don't touch WM1S_LP_EN here.
2950 * Doing so could cause underruns.
2951 */
2952
2953 return changed;
2954}
2955
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002956/*
2957 * The spec says we shouldn't write when we don't need, because every write
2958 * causes WMs to be re-evaluated, expending some power.
2959 */
Imre Deak820c1982013-12-17 14:46:36 +02002960static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2961 struct ilk_wm_values *results)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002962{
Imre Deak820c1982013-12-17 14:46:36 +02002963 struct ilk_wm_values *previous = &dev_priv->wm.hw;
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002964 unsigned int dirty;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002965 uint32_t val;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002966
Damien Lespiau055e3932014-08-18 13:49:10 +01002967 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002968 if (!dirty)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002969 return;
2970
Ville Syrjälä8553c182013-12-05 15:51:39 +02002971 _ilk_disable_lp_wm(dev_priv, dirty);
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02002972
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002973 if (dirty & WM_DIRTY_PIPE(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002974 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002975 if (dirty & WM_DIRTY_PIPE(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002976 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002977 if (dirty & WM_DIRTY_PIPE(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002978 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2979
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002980 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002981 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002982 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002983 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002984 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
Paulo Zanoni801bcff2013-05-31 10:08:35 -03002985 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2986
Ville Syrjälä49a687c2013-10-11 19:39:52 +03002987 if (dirty & WM_DIRTY_DDB) {
Tvrtko Ursulin86527442016-10-13 11:03:00 +01002988 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
Ville Syrjäläac9545f2013-12-05 15:51:28 +02002989 val = I915_READ(WM_MISC);
2990 if (results->partitioning == INTEL_DDB_PART_1_2)
2991 val &= ~WM_MISC_DATA_PARTITION_5_6;
2992 else
2993 val |= WM_MISC_DATA_PARTITION_5_6;
2994 I915_WRITE(WM_MISC, val);
2995 } else {
2996 val = I915_READ(DISP_ARB_CTL2);
2997 if (results->partitioning == INTEL_DDB_PART_1_2)
2998 val &= ~DISP_DATA_PARTITION_5_6;
2999 else
3000 val |= DISP_DATA_PARTITION_5_6;
3001 I915_WRITE(DISP_ARB_CTL2, val);
3002 }
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03003003 }
3004
Ville Syrjälä49a687c2013-10-11 19:39:52 +03003005 if (dirty & WM_DIRTY_FBC) {
Paulo Zanonicca32e92013-05-31 11:45:06 -03003006 val = I915_READ(DISP_ARB_CTL);
3007 if (results->enable_fbc_wm)
3008 val &= ~DISP_FBC_WM_DIS;
3009 else
3010 val |= DISP_FBC_WM_DIS;
3011 I915_WRITE(DISP_ARB_CTL, val);
3012 }
3013
Imre Deak954911e2013-12-17 14:46:34 +02003014 if (dirty & WM_DIRTY_LP(1) &&
3015 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3016 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
3017
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00003018 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä6cef2b8a2013-12-05 15:51:32 +02003019 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3020 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
3021 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3022 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
3023 }
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003024
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003025 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003026 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003027 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003028 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
Ville Syrjäläfacd6192013-12-05 15:51:33 +02003029 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003030 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
Ville Syrjälä609cede2013-10-09 19:18:03 +03003031
3032 dev_priv->wm.hw = *results;
Paulo Zanoni801bcff2013-05-31 10:08:35 -03003033}
3034
Matt Ropered4a6a72016-02-23 17:20:13 -08003035bool ilk_disable_lp_wm(struct drm_device *dev)
Ville Syrjälä8553c182013-12-05 15:51:39 +02003036{
Chris Wilsonfac5e232016-07-04 11:34:36 +01003037 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjälä8553c182013-12-05 15:51:39 +02003038
3039 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3040}
3041
Lyude656d1b82016-08-17 15:55:54 -04003042#define SKL_SAGV_BLOCK_TIME 30 /* µs */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003043
Matt Roper024c9042015-09-24 15:53:11 -07003044/*
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003045 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3046 * so assume we'll always need it in order to avoid underruns.
3047 */
3048static bool skl_needs_memory_bw_wa(struct intel_atomic_state *state)
3049{
3050 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3051
Rodrigo Vivib976dc52017-01-23 10:32:37 -08003052 if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv))
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003053 return true;
3054
3055 return false;
3056}
3057
Paulo Zanoni56feca92016-09-22 18:00:28 -03003058static bool
3059intel_has_sagv(struct drm_i915_private *dev_priv)
3060{
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003061 if (IS_KABYLAKE(dev_priv))
3062 return true;
3063
3064 if (IS_SKYLAKE(dev_priv) &&
3065 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED)
3066 return true;
3067
3068 return false;
Paulo Zanoni56feca92016-09-22 18:00:28 -03003069}
3070
Lyude656d1b82016-08-17 15:55:54 -04003071/*
3072 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3073 * depending on power and performance requirements. The display engine access
3074 * to system memory is blocked during the adjustment time. Because of the
3075 * blocking time, having this enabled can cause full system hangs and/or pipe
3076 * underruns if we don't meet all of the following requirements:
3077 *
3078 * - <= 1 pipe enabled
3079 * - All planes can enable watermarks for latencies >= SAGV engine block time
3080 * - We're not using an interlaced display configuration
3081 */
3082int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003083intel_enable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003084{
3085 int ret;
3086
Paulo Zanoni56feca92016-09-22 18:00:28 -03003087 if (!intel_has_sagv(dev_priv))
3088 return 0;
3089
3090 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
Lyude656d1b82016-08-17 15:55:54 -04003091 return 0;
3092
3093 DRM_DEBUG_KMS("Enabling the SAGV\n");
3094 mutex_lock(&dev_priv->rps.hw_lock);
3095
3096 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3097 GEN9_SAGV_ENABLE);
3098
3099 /* We don't need to wait for the SAGV when enabling */
3100 mutex_unlock(&dev_priv->rps.hw_lock);
3101
3102 /*
3103 * Some skl systems, pre-release machines in particular,
3104 * don't actually have an SAGV.
3105 */
Paulo Zanoni6e3100e2016-09-22 18:00:29 -03003106 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003107 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003108 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003109 return 0;
3110 } else if (ret < 0) {
3111 DRM_ERROR("Failed to enable the SAGV\n");
3112 return ret;
3113 }
3114
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003115 dev_priv->sagv_status = I915_SAGV_ENABLED;
Lyude656d1b82016-08-17 15:55:54 -04003116 return 0;
3117}
3118
Lyude656d1b82016-08-17 15:55:54 -04003119int
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003120intel_disable_sagv(struct drm_i915_private *dev_priv)
Lyude656d1b82016-08-17 15:55:54 -04003121{
Imre Deakb3b8e992016-12-05 18:27:38 +02003122 int ret;
Lyude656d1b82016-08-17 15:55:54 -04003123
Paulo Zanoni56feca92016-09-22 18:00:28 -03003124 if (!intel_has_sagv(dev_priv))
3125 return 0;
3126
3127 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
Lyude656d1b82016-08-17 15:55:54 -04003128 return 0;
3129
3130 DRM_DEBUG_KMS("Disabling the SAGV\n");
3131 mutex_lock(&dev_priv->rps.hw_lock);
3132
3133 /* bspec says to keep retrying for at least 1 ms */
Imre Deakb3b8e992016-12-05 18:27:38 +02003134 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3135 GEN9_SAGV_DISABLE,
3136 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3137 1);
Lyude656d1b82016-08-17 15:55:54 -04003138 mutex_unlock(&dev_priv->rps.hw_lock);
3139
Lyude656d1b82016-08-17 15:55:54 -04003140 /*
3141 * Some skl systems, pre-release machines in particular,
3142 * don't actually have an SAGV.
3143 */
Imre Deakb3b8e992016-12-05 18:27:38 +02003144 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
Lyude656d1b82016-08-17 15:55:54 -04003145 DRM_DEBUG_DRIVER("No SAGV found on system, ignoring\n");
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003146 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
Lyude656d1b82016-08-17 15:55:54 -04003147 return 0;
Imre Deakb3b8e992016-12-05 18:27:38 +02003148 } else if (ret < 0) {
3149 DRM_ERROR("Failed to disable the SAGV (%d)\n", ret);
3150 return ret;
Lyude656d1b82016-08-17 15:55:54 -04003151 }
3152
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003153 dev_priv->sagv_status = I915_SAGV_DISABLED;
Lyude656d1b82016-08-17 15:55:54 -04003154 return 0;
3155}
3156
Paulo Zanoni16dcdc42016-09-22 18:00:27 -03003157bool intel_can_enable_sagv(struct drm_atomic_state *state)
Lyude656d1b82016-08-17 15:55:54 -04003158{
3159 struct drm_device *dev = state->dev;
3160 struct drm_i915_private *dev_priv = to_i915(dev);
3161 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003162 struct intel_crtc *crtc;
3163 struct intel_plane *plane;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003164 struct intel_crtc_state *cstate;
Lyude656d1b82016-08-17 15:55:54 -04003165 enum pipe pipe;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003166 int level, latency;
Lyude656d1b82016-08-17 15:55:54 -04003167
Paulo Zanoni56feca92016-09-22 18:00:28 -03003168 if (!intel_has_sagv(dev_priv))
3169 return false;
3170
Lyude656d1b82016-08-17 15:55:54 -04003171 /*
3172 * SKL workaround: bspec recommends we disable the SAGV when we have
3173 * more then one pipe enabled
3174 *
3175 * If there are no active CRTCs, no additional checks need be performed
3176 */
3177 if (hweight32(intel_state->active_crtcs) == 0)
3178 return true;
3179 else if (hweight32(intel_state->active_crtcs) > 1)
3180 return false;
3181
3182 /* Since we're now guaranteed to only have one active CRTC... */
3183 pipe = ffs(intel_state->active_crtcs) - 1;
Ville Syrjälä98187832016-10-31 22:37:10 +02003184 crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003185 cstate = to_intel_crtc_state(crtc->base.state);
Lyude656d1b82016-08-17 15:55:54 -04003186
Paulo Zanonic89cadd2016-10-10 17:30:59 -03003187 if (crtc->base.state->adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Lyude656d1b82016-08-17 15:55:54 -04003188 return false;
3189
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003190 for_each_intel_plane_on_crtc(dev, crtc, plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003191 struct skl_plane_wm *wm =
3192 &cstate->wm.skl.optimal.planes[plane->id];
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003193
Lyude656d1b82016-08-17 15:55:54 -04003194 /* Skip this plane if it's not enabled */
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003195 if (!wm->wm[0].plane_en)
Lyude656d1b82016-08-17 15:55:54 -04003196 continue;
3197
3198 /* Find the highest enabled wm level for this plane */
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003199 for (level = ilk_wm_max_level(dev_priv);
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02003200 !wm->wm[level].plane_en; --level)
Lyude656d1b82016-08-17 15:55:54 -04003201 { }
3202
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003203 latency = dev_priv->wm.skl_latency[level];
3204
3205 if (skl_needs_memory_bw_wa(intel_state) &&
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003206 plane->base.state->fb->modifier ==
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003207 I915_FORMAT_MOD_X_TILED)
3208 latency += 15;
3209
Lyude656d1b82016-08-17 15:55:54 -04003210 /*
3211 * If any of the planes on this pipe don't enable wm levels
3212 * that incur memory latencies higher then 30µs we can't enable
3213 * the SAGV
3214 */
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003215 if (latency < SKL_SAGV_BLOCK_TIME)
Lyude656d1b82016-08-17 15:55:54 -04003216 return false;
3217 }
3218
3219 return true;
3220}
3221
Damien Lespiaub9cec072014-11-04 17:06:43 +00003222static void
3223skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
Matt Roper024c9042015-09-24 15:53:11 -07003224 const struct intel_crtc_state *cstate,
Matt Roperc107acf2016-05-12 07:06:01 -07003225 struct skl_ddb_entry *alloc, /* out */
3226 int *num_active /* out */)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003227{
Matt Roperc107acf2016-05-12 07:06:01 -07003228 struct drm_atomic_state *state = cstate->base.state;
3229 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
3230 struct drm_i915_private *dev_priv = to_i915(dev);
Matt Roper024c9042015-09-24 15:53:11 -07003231 struct drm_crtc *for_crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003232 unsigned int pipe_size, ddb_size;
3233 int nth_active_pipe;
Matt Roperc107acf2016-05-12 07:06:01 -07003234
Matt Ropera6d3460e2016-05-12 07:06:04 -07003235 if (WARN_ON(!state) || !cstate->base.active) {
Damien Lespiaub9cec072014-11-04 17:06:43 +00003236 alloc->start = 0;
3237 alloc->end = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003238 *num_active = hweight32(dev_priv->active_crtcs);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003239 return;
3240 }
3241
Matt Ropera6d3460e2016-05-12 07:06:04 -07003242 if (intel_state->active_pipe_changes)
3243 *num_active = hweight32(intel_state->active_crtcs);
3244 else
3245 *num_active = hweight32(dev_priv->active_crtcs);
3246
Deepak M6f3fff62016-09-15 15:01:10 +05303247 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
3248 WARN_ON(ddb_size == 0);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003249
3250 ddb_size -= 4; /* 4 blocks for bypass path allocation */
3251
Matt Roperc107acf2016-05-12 07:06:01 -07003252 /*
Matt Ropera6d3460e2016-05-12 07:06:04 -07003253 * If the state doesn't change the active CRTC's, then there's
3254 * no need to recalculate; the existing pipe allocation limits
3255 * should remain unchanged. Note that we're safe from racing
3256 * commits since any racing commit that changes the active CRTC
3257 * list would need to grab _all_ crtc locks, including the one
3258 * we currently hold.
Matt Roperc107acf2016-05-12 07:06:01 -07003259 */
Matt Ropera6d3460e2016-05-12 07:06:04 -07003260 if (!intel_state->active_pipe_changes) {
Maarten Lankhorst512b5522016-11-08 13:55:34 +01003261 /*
3262 * alloc may be cleared by clear_intel_crtc_state,
3263 * copy from old state to be sure
3264 */
3265 *alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003266 return;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003267 }
Matt Ropera6d3460e2016-05-12 07:06:04 -07003268
3269 nth_active_pipe = hweight32(intel_state->active_crtcs &
3270 (drm_crtc_mask(for_crtc) - 1));
3271 pipe_size = ddb_size / hweight32(intel_state->active_crtcs);
3272 alloc->start = nth_active_pipe * ddb_size / *num_active;
3273 alloc->end = alloc->start + pipe_size;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003274}
3275
Matt Roperc107acf2016-05-12 07:06:01 -07003276static unsigned int skl_cursor_allocation(int num_active)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003277{
Matt Roperc107acf2016-05-12 07:06:01 -07003278 if (num_active == 1)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003279 return 32;
3280
3281 return 8;
3282}
3283
Damien Lespiaua269c582014-11-04 17:06:49 +00003284static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
3285{
3286 entry->start = reg & 0x3ff;
3287 entry->end = (reg >> 16) & 0x3ff;
Damien Lespiau16160e32014-11-04 17:06:53 +00003288 if (entry->end)
3289 entry->end += 1;
Damien Lespiaua269c582014-11-04 17:06:49 +00003290}
3291
Damien Lespiau08db6652014-11-04 17:06:52 +00003292void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
3293 struct skl_ddb_allocation *ddb /* out */)
Damien Lespiaua269c582014-11-04 17:06:49 +00003294{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003295 struct intel_crtc *crtc;
Damien Lespiaua269c582014-11-04 17:06:49 +00003296
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003297 memset(ddb, 0, sizeof(*ddb));
3298
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003299 for_each_intel_crtc(&dev_priv->drm, crtc) {
Imre Deak4d800032016-02-17 16:31:29 +02003300 enum intel_display_power_domain power_domain;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003301 enum plane_id plane_id;
3302 enum pipe pipe = crtc->pipe;
Imre Deak4d800032016-02-17 16:31:29 +02003303
3304 power_domain = POWER_DOMAIN_PIPE(pipe);
3305 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
Maarten Lankhorstb10f1b22015-10-22 13:56:34 +02003306 continue;
3307
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003308 for_each_plane_id_on_crtc(crtc, plane_id) {
3309 u32 val;
Damien Lespiaua269c582014-11-04 17:06:49 +00003310
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003311 if (plane_id != PLANE_CURSOR)
3312 val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
3313 else
3314 val = I915_READ(CUR_BUF_CFG(pipe));
3315
3316 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane_id], val);
3317 }
Imre Deak4d800032016-02-17 16:31:29 +02003318
3319 intel_display_power_put(dev_priv, power_domain);
Damien Lespiaua269c582014-11-04 17:06:49 +00003320 }
3321}
3322
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003323/*
3324 * Determines the downscale amount of a plane for the purposes of watermark calculations.
3325 * The bspec defines downscale amount as:
3326 *
3327 * """
3328 * Horizontal down scale amount = maximum[1, Horizontal source size /
3329 * Horizontal destination size]
3330 * Vertical down scale amount = maximum[1, Vertical source size /
3331 * Vertical destination size]
3332 * Total down scale amount = Horizontal down scale amount *
3333 * Vertical down scale amount
3334 * """
3335 *
3336 * Return value is provided in 16.16 fixed point form to retain fractional part.
3337 * Caller should take care of dividing & rounding off the value.
3338 */
3339static uint32_t
3340skl_plane_downscale_amount(const struct intel_plane_state *pstate)
3341{
3342 uint32_t downscale_h, downscale_w;
3343 uint32_t src_w, src_h, dst_w, dst_h;
3344
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003345 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003346 return DRM_PLANE_HELPER_NO_SCALING;
3347
3348 /* n.b., src is 16.16 fixed point, dst is whole integer */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003349 src_w = drm_rect_width(&pstate->base.src);
3350 src_h = drm_rect_height(&pstate->base.src);
3351 dst_w = drm_rect_width(&pstate->base.dst);
3352 dst_h = drm_rect_height(&pstate->base.dst);
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003353 if (drm_rotation_90_or_270(pstate->base.rotation))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003354 swap(dst_w, dst_h);
3355
3356 downscale_h = max(src_h / dst_h, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3357 downscale_w = max(src_w / dst_w, (uint32_t)DRM_PLANE_HELPER_NO_SCALING);
3358
3359 /* Provide result in 16.16 fixed point */
3360 return (uint64_t)downscale_w * downscale_h >> 16;
3361}
3362
Damien Lespiaub9cec072014-11-04 17:06:43 +00003363static unsigned int
Matt Roper024c9042015-09-24 15:53:11 -07003364skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
3365 const struct drm_plane_state *pstate,
3366 int y)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003367{
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003368 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003369 uint32_t down_scale_amount, data_rate;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003370 uint32_t width = 0, height = 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003371 struct drm_framebuffer *fb;
3372 u32 format;
Matt Ropera1de91e2016-05-12 07:05:57 -07003373
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003374 if (!intel_pstate->base.visible)
Matt Ropera1de91e2016-05-12 07:05:57 -07003375 return 0;
Ville Syrjälä83054942016-11-18 21:53:00 +02003376
3377 fb = pstate->fb;
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003378 format = fb->format->format;
Ville Syrjälä83054942016-11-18 21:53:00 +02003379
Matt Ropera1de91e2016-05-12 07:05:57 -07003380 if (pstate->plane->type == DRM_PLANE_TYPE_CURSOR)
3381 return 0;
3382 if (y && format != DRM_FORMAT_NV12)
3383 return 0;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003384
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003385 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3386 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003387
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003388 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003389 swap(width, height);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003390
3391 /* for planar format */
Matt Ropera1de91e2016-05-12 07:05:57 -07003392 if (format == DRM_FORMAT_NV12) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003393 if (y) /* y-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003394 data_rate = width * height *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003395 fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003396 else /* uv-plane data rate */
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003397 data_rate = (width / 2) * (height / 2) *
Ville Syrjälä353c8592016-12-14 23:30:57 +02003398 fb->format->cpp[1];
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003399 } else {
3400 /* for packed formats */
Ville Syrjälä353c8592016-12-14 23:30:57 +02003401 data_rate = width * height * fb->format->cpp[0];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003402 }
3403
Kumar, Mahesh8d19d7d2016-05-19 15:03:01 -07003404 down_scale_amount = skl_plane_downscale_amount(intel_pstate);
3405
3406 return (uint64_t)data_rate * down_scale_amount >> 16;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003407}
3408
3409/*
3410 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
3411 * a 8192x4096@32bpp framebuffer:
3412 * 3 * 4096 * 8192 * 4 < 2^32
3413 */
3414static unsigned int
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003415skl_get_total_relative_data_rate(struct intel_crtc_state *intel_cstate,
3416 unsigned *plane_data_rate,
3417 unsigned *plane_y_data_rate)
Damien Lespiaub9cec072014-11-04 17:06:43 +00003418{
Matt Roper9c74d822016-05-12 07:05:58 -07003419 struct drm_crtc_state *cstate = &intel_cstate->base;
3420 struct drm_atomic_state *state = cstate->state;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003421 struct drm_plane *plane;
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003422 const struct drm_plane_state *pstate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003423 unsigned int total_data_rate = 0;
Matt Ropera6d3460e2016-05-12 07:06:04 -07003424
3425 if (WARN_ON(!state))
3426 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003427
Matt Ropera1de91e2016-05-12 07:05:57 -07003428 /* Calculate and cache data rate for each plane */
Maarten Lankhorstc8fe32c2016-10-26 15:41:29 +02003429 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, cstate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003430 enum plane_id plane_id = to_intel_plane(plane)->id;
3431 unsigned int rate;
Matt Roper024c9042015-09-24 15:53:11 -07003432
Matt Ropera6d3460e2016-05-12 07:06:04 -07003433 /* packed/uv */
3434 rate = skl_plane_relative_data_rate(intel_cstate,
3435 pstate, 0);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003436 plane_data_rate[plane_id] = rate;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003437
3438 total_data_rate += rate;
Matt Roper9c74d822016-05-12 07:05:58 -07003439
Matt Ropera6d3460e2016-05-12 07:06:04 -07003440 /* y-plane */
3441 rate = skl_plane_relative_data_rate(intel_cstate,
3442 pstate, 1);
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003443 plane_y_data_rate[plane_id] = rate;
Matt Ropera1de91e2016-05-12 07:05:57 -07003444
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003445 total_data_rate += rate;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003446 }
3447
3448 return total_data_rate;
3449}
3450
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003451static uint16_t
3452skl_ddb_min_alloc(const struct drm_plane_state *pstate,
3453 const int y)
3454{
3455 struct drm_framebuffer *fb = pstate->fb;
3456 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
3457 uint32_t src_w, src_h;
3458 uint32_t min_scanlines = 8;
3459 uint8_t plane_bpp;
3460
3461 if (WARN_ON(!fb))
3462 return 0;
3463
3464 /* For packed formats, no y-plane, return 0 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003465 if (y && fb->format->format != DRM_FORMAT_NV12)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003466 return 0;
3467
3468 /* For Non Y-tile return 8-blocks */
Ville Syrjäläbae781b2016-11-16 13:33:16 +02003469 if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
3470 fb->modifier != I915_FORMAT_MOD_Yf_TILED)
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003471 return 8;
3472
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003473 src_w = drm_rect_width(&intel_pstate->base.src) >> 16;
3474 src_h = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003475
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003476 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003477 swap(src_w, src_h);
3478
3479 /* Halve UV plane width and height for NV12 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003480 if (fb->format->format == DRM_FORMAT_NV12 && !y) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003481 src_w /= 2;
3482 src_h /= 2;
3483 }
3484
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003485 if (fb->format->format == DRM_FORMAT_NV12 && !y)
Ville Syrjälä353c8592016-12-14 23:30:57 +02003486 plane_bpp = fb->format->cpp[1];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003487 else
Ville Syrjälä353c8592016-12-14 23:30:57 +02003488 plane_bpp = fb->format->cpp[0];
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003489
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003490 if (drm_rotation_90_or_270(pstate->rotation)) {
Kumar, Maheshcbcfd142016-05-31 09:58:59 -07003491 switch (plane_bpp) {
3492 case 1:
3493 min_scanlines = 32;
3494 break;
3495 case 2:
3496 min_scanlines = 16;
3497 break;
3498 case 4:
3499 min_scanlines = 8;
3500 break;
3501 case 8:
3502 min_scanlines = 4;
3503 break;
3504 default:
3505 WARN(1, "Unsupported pixel depth %u for rotation",
3506 plane_bpp);
3507 min_scanlines = 32;
3508 }
3509 }
3510
3511 return DIV_ROUND_UP((4 * src_w * plane_bpp), 512) * min_scanlines/4 + 3;
3512}
3513
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003514static void
3515skl_ddb_calc_min(const struct intel_crtc_state *cstate, int num_active,
3516 uint16_t *minimum, uint16_t *y_minimum)
3517{
3518 const struct drm_plane_state *pstate;
3519 struct drm_plane *plane;
3520
3521 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, &cstate->base) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003522 enum plane_id plane_id = to_intel_plane(plane)->id;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003523
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003524 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003525 continue;
3526
3527 if (!pstate->visible)
3528 continue;
3529
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003530 minimum[plane_id] = skl_ddb_min_alloc(pstate, 0);
3531 y_minimum[plane_id] = skl_ddb_min_alloc(pstate, 1);
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003532 }
3533
3534 minimum[PLANE_CURSOR] = skl_cursor_allocation(num_active);
3535}
3536
Matt Roperc107acf2016-05-12 07:06:01 -07003537static int
Matt Roper024c9042015-09-24 15:53:11 -07003538skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
Damien Lespiaub9cec072014-11-04 17:06:43 +00003539 struct skl_ddb_allocation *ddb /* out */)
3540{
Matt Roperc107acf2016-05-12 07:06:01 -07003541 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003542 struct drm_crtc *crtc = cstate->base.crtc;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003543 struct drm_device *dev = crtc->dev;
3544 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3545 enum pipe pipe = intel_crtc->pipe;
Lyudece0ba282016-09-15 10:46:35 -04003546 struct skl_ddb_entry *alloc = &cstate->wm.skl.ddb;
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003547 uint16_t alloc_size, start;
Maarten Lankhorstfefdd812016-10-26 15:41:33 +02003548 uint16_t minimum[I915_MAX_PLANES] = {};
3549 uint16_t y_minimum[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003550 unsigned int total_data_rate;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003551 enum plane_id plane_id;
Matt Roperc107acf2016-05-12 07:06:01 -07003552 int num_active;
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003553 unsigned plane_data_rate[I915_MAX_PLANES] = {};
3554 unsigned plane_y_data_rate[I915_MAX_PLANES] = {};
Damien Lespiaub9cec072014-11-04 17:06:43 +00003555
Paulo Zanoni5a920b82016-10-04 14:37:32 -03003556 /* Clear the partitioning for disabled planes. */
3557 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
3558 memset(ddb->y_plane[pipe], 0, sizeof(ddb->y_plane[pipe]));
3559
Matt Ropera6d3460e2016-05-12 07:06:04 -07003560 if (WARN_ON(!state))
3561 return 0;
3562
Matt Roperc107acf2016-05-12 07:06:01 -07003563 if (!cstate->base.active) {
Lyudece0ba282016-09-15 10:46:35 -04003564 alloc->start = alloc->end = 0;
Matt Roperc107acf2016-05-12 07:06:01 -07003565 return 0;
3566 }
3567
Matt Ropera6d3460e2016-05-12 07:06:04 -07003568 skl_ddb_get_pipe_allocation_limits(dev, cstate, alloc, &num_active);
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003569 alloc_size = skl_ddb_entry_size(alloc);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003570 if (alloc_size == 0) {
3571 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
Matt Roperc107acf2016-05-12 07:06:01 -07003572 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003573 }
3574
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003575 skl_ddb_calc_min(cstate, num_active, minimum, y_minimum);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003576
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003577 /*
3578 * 1. Allocate the mininum required blocks for each active plane
3579 * and allocate the cursor, it doesn't require extra allocation
3580 * proportional to the data rate.
3581 */
Damien Lespiaub9cec072014-11-04 17:06:43 +00003582
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003583 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
3584 alloc_size -= minimum[plane_id];
3585 alloc_size -= y_minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003586 }
3587
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003588 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - minimum[PLANE_CURSOR];
3589 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
3590
Damien Lespiaub9cec072014-11-04 17:06:43 +00003591 /*
Damien Lespiau80958152015-02-09 13:35:10 +00003592 * 2. Distribute the remaining space in proportion to the amount of
3593 * data each plane needs to fetch from memory.
Damien Lespiaub9cec072014-11-04 17:06:43 +00003594 *
3595 * FIXME: we may not allocate every single block here.
3596 */
Maarten Lankhorst1e6ee542016-10-26 15:41:32 +02003597 total_data_rate = skl_get_total_relative_data_rate(cstate,
3598 plane_data_rate,
3599 plane_y_data_rate);
Matt Ropera1de91e2016-05-12 07:05:57 -07003600 if (total_data_rate == 0)
Matt Roperc107acf2016-05-12 07:06:01 -07003601 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003602
Damien Lespiau34bb56a2014-11-04 17:07:01 +00003603 start = alloc->start;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003604 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003605 unsigned int data_rate, y_data_rate;
3606 uint16_t plane_blocks, y_plane_blocks = 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003607
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003608 if (plane_id == PLANE_CURSOR)
Maarten Lankhorst49845a72016-10-26 15:41:34 +02003609 continue;
3610
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003611 data_rate = plane_data_rate[plane_id];
Damien Lespiaub9cec072014-11-04 17:06:43 +00003612
3613 /*
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003614 * allocation for (packed formats) or (uv-plane part of planar format):
Damien Lespiaub9cec072014-11-04 17:06:43 +00003615 * promote the expression to 64 bits to avoid overflowing, the
3616 * result is < available as data_rate / total_data_rate < 1
3617 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003618 plane_blocks = minimum[plane_id];
Damien Lespiau80958152015-02-09 13:35:10 +00003619 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3620 total_data_rate);
Damien Lespiaub9cec072014-11-04 17:06:43 +00003621
Matt Roperc107acf2016-05-12 07:06:01 -07003622 /* Leave disabled planes at (0,0) */
3623 if (data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003624 ddb->plane[pipe][plane_id].start = start;
3625 ddb->plane[pipe][plane_id].end = start + plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003626 }
Damien Lespiaub9cec072014-11-04 17:06:43 +00003627
3628 start += plane_blocks;
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003629
3630 /*
3631 * allocation for y_plane part of planar format:
3632 */
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003633 y_data_rate = plane_y_data_rate[plane_id];
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003634
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003635 y_plane_blocks = y_minimum[plane_id];
Matt Ropera1de91e2016-05-12 07:05:57 -07003636 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3637 total_data_rate);
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003638
Matt Roperc107acf2016-05-12 07:06:01 -07003639 if (y_data_rate) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003640 ddb->y_plane[pipe][plane_id].start = start;
3641 ddb->y_plane[pipe][plane_id].end = start + y_plane_blocks;
Matt Roperc107acf2016-05-12 07:06:01 -07003642 }
Chandra Konduru2cd601c2015-04-27 15:47:37 -07003643
Matt Ropera1de91e2016-05-12 07:05:57 -07003644 start += y_plane_blocks;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003645 }
3646
Matt Roperc107acf2016-05-12 07:06:01 -07003647 return 0;
Damien Lespiaub9cec072014-11-04 17:06:43 +00003648}
3649
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003650/*
3651 * 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 +02003652 * for the read latency) and cpp should always be <= 8, so that
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003653 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3654 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3655*/
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303656static uint_fixed_16_16_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp,
3657 uint32_t latency)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003658{
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303659 uint32_t wm_intermediate_val;
3660 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003661
3662 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303663 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003664
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303665 wm_intermediate_val = latency * pixel_rate * cpp;
3666 ret = fixed_16_16_div_round_up_u64(wm_intermediate_val, 1000 * 512);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003667 return ret;
3668}
3669
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303670static uint_fixed_16_16_t skl_wm_method2(uint32_t pixel_rate,
3671 uint32_t pipe_htotal,
3672 uint32_t latency,
3673 uint_fixed_16_16_t plane_blocks_per_line)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003674{
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003675 uint32_t wm_intermediate_val;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303676 uint_fixed_16_16_t ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003677
3678 if (latency == 0)
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303679 return FP_16_16_MAX;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003680
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003681 wm_intermediate_val = latency * pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303682 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
3683 pipe_htotal * 1000);
3684 ret = mul_u32_fixed_16_16(wm_intermediate_val, plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003685 return ret;
3686}
3687
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003688static uint32_t skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *cstate,
3689 struct intel_plane_state *pstate)
3690{
3691 uint64_t adjusted_pixel_rate;
3692 uint64_t downscale_amount;
3693 uint64_t pixel_rate;
3694
3695 /* Shouldn't reach here on disabled planes... */
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003696 if (WARN_ON(!pstate->base.visible))
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003697 return 0;
3698
3699 /*
3700 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
3701 * with additional adjustments for plane-specific scaling.
3702 */
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003703 adjusted_pixel_rate = cstate->pixel_rate;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003704 downscale_amount = skl_plane_downscale_amount(pstate);
3705
3706 pixel_rate = adjusted_pixel_rate * downscale_amount >> 16;
3707 WARN_ON(pixel_rate != clamp_t(uint32_t, pixel_rate, 0, ~0));
3708
3709 return pixel_rate;
3710}
3711
Matt Roper55994c22016-05-12 07:06:08 -07003712static int skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3713 struct intel_crtc_state *cstate,
3714 struct intel_plane_state *intel_pstate,
3715 uint16_t ddb_allocation,
3716 int level,
3717 uint16_t *out_blocks, /* out */
3718 uint8_t *out_lines, /* out */
3719 bool *enabled /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003720{
Matt Roper33815fa2016-05-12 07:06:05 -07003721 struct drm_plane_state *pstate = &intel_pstate->base;
3722 struct drm_framebuffer *fb = pstate->fb;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003723 uint32_t latency = dev_priv->wm.skl_latency[level];
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303724 uint_fixed_16_16_t method1, method2;
3725 uint_fixed_16_16_t plane_blocks_per_line;
3726 uint_fixed_16_16_t selected_result;
3727 uint32_t interm_pbpl;
3728 uint32_t plane_bytes_per_line;
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003729 uint32_t res_blocks, res_lines;
Ville Syrjäläac484962016-01-20 21:05:26 +02003730 uint8_t cpp;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003731 uint32_t width = 0, height = 0;
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003732 uint32_t plane_pixel_rate;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303733 uint_fixed_16_16_t y_tile_minimum;
3734 uint32_t y_min_scanlines;
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003735 struct intel_atomic_state *state =
3736 to_intel_atomic_state(cstate->base.state);
3737 bool apply_memory_bw_wa = skl_needs_memory_bw_wa(state);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303738 bool y_tiled, x_tiled;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003739
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003740 if (latency == 0 || !cstate->base.active || !intel_pstate->base.visible) {
Matt Roper55994c22016-05-12 07:06:08 -07003741 *enabled = false;
3742 return 0;
3743 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003744
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303745 y_tiled = fb->modifier == I915_FORMAT_MOD_Y_TILED ||
3746 fb->modifier == I915_FORMAT_MOD_Yf_TILED;
3747 x_tiled = fb->modifier == I915_FORMAT_MOD_X_TILED;
3748
Mahesh Kumar4b7b2332016-12-01 21:19:35 +05303749 /* Display WA #1141: kbl. */
3750 if (IS_KABYLAKE(dev_priv) && dev_priv->ipc_enabled)
3751 latency += 4;
3752
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303753 if (apply_memory_bw_wa && x_tiled)
Paulo Zanoniee3d5322016-10-11 15:25:38 -03003754 latency += 15;
3755
Ville Syrjälä936e71e2016-07-26 19:06:59 +03003756 width = drm_rect_width(&intel_pstate->base.src) >> 16;
3757 height = drm_rect_height(&intel_pstate->base.src) >> 16;
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003758
Ville Syrjäläbd2ef252016-09-26 19:30:46 +03003759 if (drm_rotation_90_or_270(pstate->rotation))
Kumar, Mahesha280f7d2016-04-06 08:26:39 -07003760 swap(width, height);
3761
Ville Syrjälä353c8592016-12-14 23:30:57 +02003762 cpp = fb->format->cpp[0];
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003763 plane_pixel_rate = skl_adjusted_plane_pixel_rate(cstate, intel_pstate);
3764
Dave Airlie61d0a042016-10-25 16:35:20 +10003765 if (drm_rotation_90_or_270(pstate->rotation)) {
Ville Syrjälä438b74a2016-12-14 23:32:55 +02003766 int cpp = (fb->format->format == DRM_FORMAT_NV12) ?
Ville Syrjälä353c8592016-12-14 23:30:57 +02003767 fb->format->cpp[1] :
3768 fb->format->cpp[0];
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003769
3770 switch (cpp) {
3771 case 1:
3772 y_min_scanlines = 16;
3773 break;
3774 case 2:
3775 y_min_scanlines = 8;
3776 break;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003777 case 4:
3778 y_min_scanlines = 4;
3779 break;
Paulo Zanoni86a462b2016-09-22 18:00:35 -03003780 default:
3781 MISSING_CASE(cpp);
3782 return -EINVAL;
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003783 }
3784 } else {
3785 y_min_scanlines = 4;
3786 }
3787
Paulo Zanoni2ef32de2016-11-08 18:22:11 -02003788 if (apply_memory_bw_wa)
3789 y_min_scanlines *= 2;
3790
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003791 plane_bytes_per_line = width * cpp;
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303792 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303793 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line *
3794 y_min_scanlines, 512);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003795 plane_blocks_per_line =
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303796 fixed_16_16_div_round_up(interm_pbpl, y_min_scanlines);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303797 } else if (x_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303798 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512);
3799 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303800 } else {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303801 interm_pbpl = DIV_ROUND_UP(plane_bytes_per_line, 512) + 1;
3802 plane_blocks_per_line = u32_to_fixed_16_16(interm_pbpl);
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003803 }
3804
Kumar, Mahesh9c2f7a92016-05-16 15:52:00 -07003805 method1 = skl_wm_method1(plane_pixel_rate, cpp, latency);
3806 method2 = skl_wm_method2(plane_pixel_rate,
Matt Roper024c9042015-09-24 15:53:11 -07003807 cstate->base.adjusted_mode.crtc_htotal,
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003808 latency,
Paulo Zanoni7a1a8ae2016-09-22 18:00:32 -03003809 plane_blocks_per_line);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003810
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303811 y_tile_minimum = mul_u32_fixed_16_16(y_min_scanlines,
3812 plane_blocks_per_line);
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003813
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303814 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303815 selected_result = max_fixed_16_16(method2, y_tile_minimum);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003816 } else {
Paulo Zanonif1db3ea2016-09-22 18:00:34 -03003817 if ((cpp * cstate->base.adjusted_mode.crtc_htotal / 512 < 1) &&
3818 (plane_bytes_per_line / 512 < 1))
3819 selected_result = method2;
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303820 else if ((ddb_allocation /
3821 fixed_16_16_to_u32_round_up(plane_blocks_per_line)) >= 1)
3822 selected_result = min_fixed_16_16(method1, method2);
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003823 else
3824 selected_result = method1;
3825 }
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003826
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303827 res_blocks = fixed_16_16_to_u32_round_up(selected_result) + 1;
3828 res_lines = DIV_ROUND_UP(selected_result.val,
3829 plane_blocks_per_line.val);
Damien Lespiaue6d66172014-11-04 17:06:55 +00003830
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003831 if (level >= 1 && level <= 7) {
Mahesh Kumaref8a4fb2016-12-01 21:19:33 +05303832 if (y_tiled) {
Mahesh Kumarb95320b2016-12-01 21:19:37 +05303833 res_blocks += fixed_16_16_to_u32_round_up(y_tile_minimum);
Paulo Zanoni1186fa82016-09-22 18:00:31 -03003834 res_lines += y_min_scanlines;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003835 } else {
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003836 res_blocks++;
Paulo Zanoni75676ed2016-09-22 18:00:33 -03003837 }
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +00003838 }
Tvrtko Ursulind4c2aa62015-02-27 11:15:22 +00003839
Matt Roper55994c22016-05-12 07:06:08 -07003840 if (res_blocks >= ddb_allocation || res_lines > 31) {
3841 *enabled = false;
Matt Roper6b6bada2016-05-12 07:06:10 -07003842
3843 /*
3844 * If there are no valid level 0 watermarks, then we can't
3845 * support this display configuration.
3846 */
3847 if (level) {
3848 return 0;
3849 } else {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003850 struct drm_plane *plane = pstate->plane;
Matt Roper6b6bada2016-05-12 07:06:10 -07003851
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003852 DRM_DEBUG_KMS("Requested display configuration exceeds system watermark limitations\n");
3853 DRM_DEBUG_KMS("[PLANE:%d:%s] blocks required = %u/%u, lines required = %u/31\n",
3854 plane->base.id, plane->name,
3855 res_blocks, ddb_allocation, res_lines);
Matt Roper6b6bada2016-05-12 07:06:10 -07003856 return -EINVAL;
3857 }
Matt Roper55994c22016-05-12 07:06:08 -07003858 }
Damien Lespiaue6d66172014-11-04 17:06:55 +00003859
3860 *out_blocks = res_blocks;
3861 *out_lines = res_lines;
Matt Roper55994c22016-05-12 07:06:08 -07003862 *enabled = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003863
Matt Roper55994c22016-05-12 07:06:08 -07003864 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003865}
3866
Matt Roperf4a96752016-05-12 07:06:06 -07003867static int
3868skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3869 struct skl_ddb_allocation *ddb,
3870 struct intel_crtc_state *cstate,
Lyudea62163e2016-10-04 14:28:20 -04003871 struct intel_plane *intel_plane,
Matt Roperf4a96752016-05-12 07:06:06 -07003872 int level,
3873 struct skl_wm_level *result)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003874{
Matt Roperf4a96752016-05-12 07:06:06 -07003875 struct drm_atomic_state *state = cstate->base.state;
Matt Roper024c9042015-09-24 15:53:11 -07003876 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Lyudea62163e2016-10-04 14:28:20 -04003877 struct drm_plane *plane = &intel_plane->base;
3878 struct intel_plane_state *intel_pstate = NULL;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003879 uint16_t ddb_blocks;
Matt Roper024c9042015-09-24 15:53:11 -07003880 enum pipe pipe = intel_crtc->pipe;
Matt Roper55994c22016-05-12 07:06:08 -07003881 int ret;
Lyudea62163e2016-10-04 14:28:20 -04003882
3883 if (state)
3884 intel_pstate =
3885 intel_atomic_get_existing_plane_state(state,
3886 intel_plane);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003887
Matt Roperf4a96752016-05-12 07:06:06 -07003888 /*
Lyudea62163e2016-10-04 14:28:20 -04003889 * Note: If we start supporting multiple pending atomic commits against
3890 * the same planes/CRTC's in the future, plane->state will no longer be
3891 * the correct pre-state to use for the calculations here and we'll
3892 * need to change where we get the 'unchanged' plane data from.
3893 *
3894 * For now this is fine because we only allow one queued commit against
3895 * a CRTC. Even if the plane isn't modified by this transaction and we
3896 * don't have a plane lock, we still have the CRTC's lock, so we know
3897 * that no other transactions are racing with us to update it.
Matt Roperf4a96752016-05-12 07:06:06 -07003898 */
Lyudea62163e2016-10-04 14:28:20 -04003899 if (!intel_pstate)
3900 intel_pstate = to_intel_plane_state(plane->state);
Matt Roperf4a96752016-05-12 07:06:06 -07003901
Lyudea62163e2016-10-04 14:28:20 -04003902 WARN_ON(!intel_pstate->base.fb);
Matt Roper024c9042015-09-24 15:53:11 -07003903
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003904 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][intel_plane->id]);
Matt Roperf4a96752016-05-12 07:06:06 -07003905
Lyudea62163e2016-10-04 14:28:20 -04003906 ret = skl_compute_plane_wm(dev_priv,
3907 cstate,
3908 intel_pstate,
3909 ddb_blocks,
3910 level,
3911 &result->plane_res_b,
3912 &result->plane_res_l,
3913 &result->plane_en);
3914 if (ret)
3915 return ret;
Matt Roperf4a96752016-05-12 07:06:06 -07003916
3917 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003918}
3919
Damien Lespiau407b50f2014-11-04 17:06:57 +00003920static uint32_t
Matt Roper024c9042015-09-24 15:53:11 -07003921skl_compute_linetime_wm(struct intel_crtc_state *cstate)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003922{
Mahesh Kumara3a89862016-12-01 21:19:34 +05303923 struct drm_atomic_state *state = cstate->base.state;
3924 struct drm_i915_private *dev_priv = to_i915(state->dev);
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003925 uint32_t pixel_rate;
Mahesh Kumara3a89862016-12-01 21:19:34 +05303926 uint32_t linetime_wm;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003927
Matt Roper024c9042015-09-24 15:53:11 -07003928 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003929 return 0;
3930
Ville Syrjäläa7d1b3f2017-01-26 21:50:31 +02003931 pixel_rate = cstate->pixel_rate;
Paulo Zanoni30d1b5f2016-10-07 17:28:58 -03003932
3933 if (WARN_ON(pixel_rate == 0))
Mika Kuoppala661abfc2015-07-16 19:36:51 +03003934 return 0;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003935
Mahesh Kumara3a89862016-12-01 21:19:34 +05303936 linetime_wm = DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal *
3937 1000, pixel_rate);
3938
3939 /* Display WA #1135: bxt. */
3940 if (IS_BROXTON(dev_priv) && dev_priv->ipc_enabled)
3941 linetime_wm = DIV_ROUND_UP(linetime_wm, 2);
3942
3943 return linetime_wm;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003944}
3945
Matt Roper024c9042015-09-24 15:53:11 -07003946static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
Damien Lespiau9414f562014-11-04 17:06:58 +00003947 struct skl_wm_level *trans_wm /* out */)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003948{
Matt Roper024c9042015-09-24 15:53:11 -07003949 if (!cstate->base.active)
Damien Lespiau407b50f2014-11-04 17:06:57 +00003950 return;
Damien Lespiau9414f562014-11-04 17:06:58 +00003951
3952 /* Until we know more, just disable transition WMs */
Lyudea62163e2016-10-04 14:28:20 -04003953 trans_wm->plane_en = false;
Damien Lespiau407b50f2014-11-04 17:06:57 +00003954}
3955
Matt Roper55994c22016-05-12 07:06:08 -07003956static int skl_build_pipe_wm(struct intel_crtc_state *cstate,
3957 struct skl_ddb_allocation *ddb,
3958 struct skl_pipe_wm *pipe_wm)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003959{
Matt Roper024c9042015-09-24 15:53:11 -07003960 struct drm_device *dev = cstate->base.crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01003961 const struct drm_i915_private *dev_priv = to_i915(dev);
Lyudea62163e2016-10-04 14:28:20 -04003962 struct intel_plane *intel_plane;
3963 struct skl_plane_wm *wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01003964 int level, max_level = ilk_wm_max_level(dev_priv);
Matt Roper55994c22016-05-12 07:06:08 -07003965 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003966
Lyudea62163e2016-10-04 14:28:20 -04003967 /*
3968 * We'll only calculate watermarks for planes that are actually
3969 * enabled, so make sure all other planes are set as disabled.
3970 */
3971 memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
3972
3973 for_each_intel_plane_mask(&dev_priv->drm,
3974 intel_plane,
3975 cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02003976 wm = &pipe_wm->planes[intel_plane->id];
Lyudea62163e2016-10-04 14:28:20 -04003977
3978 for (level = 0; level <= max_level; level++) {
3979 ret = skl_compute_wm_level(dev_priv, ddb, cstate,
3980 intel_plane, level,
3981 &wm->wm[level]);
3982 if (ret)
3983 return ret;
3984 }
3985 skl_compute_transition_wm(cstate, &wm->trans_wm);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003986 }
Matt Roper024c9042015-09-24 15:53:11 -07003987 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003988
Matt Roper55994c22016-05-12 07:06:08 -07003989 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00003990}
3991
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003992static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3993 i915_reg_t reg,
Damien Lespiau16160e32014-11-04 17:06:53 +00003994 const struct skl_ddb_entry *entry)
3995{
3996 if (entry->end)
3997 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3998 else
3999 I915_WRITE(reg, 0);
4000}
4001
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004002static void skl_write_wm_level(struct drm_i915_private *dev_priv,
4003 i915_reg_t reg,
4004 const struct skl_wm_level *level)
4005{
4006 uint32_t val = 0;
4007
4008 if (level->plane_en) {
4009 val |= PLANE_WM_EN;
4010 val |= level->plane_res_b;
4011 val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
4012 }
4013
4014 I915_WRITE(reg, val);
4015}
4016
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004017static void skl_write_plane_wm(struct intel_crtc *intel_crtc,
4018 const struct skl_plane_wm *wm,
4019 const struct skl_ddb_allocation *ddb,
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004020 enum plane_id plane_id)
Lyude62e0fb82016-08-22 12:50:08 -04004021{
4022 struct drm_crtc *crtc = &intel_crtc->base;
4023 struct drm_device *dev = crtc->dev;
4024 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004025 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004026 enum pipe pipe = intel_crtc->pipe;
4027
4028 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004029 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004030 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004031 }
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004032 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004033 &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004034
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004035 skl_ddb_entry_write(dev_priv, PLANE_BUF_CFG(pipe, plane_id),
4036 &ddb->plane[pipe][plane_id]);
4037 skl_ddb_entry_write(dev_priv, PLANE_NV12_BUF_CFG(pipe, plane_id),
4038 &ddb->y_plane[pipe][plane_id]);
Lyude62e0fb82016-08-22 12:50:08 -04004039}
4040
Ville Syrjäläd9348de2016-11-22 22:21:53 +02004041static void skl_write_cursor_wm(struct intel_crtc *intel_crtc,
4042 const struct skl_plane_wm *wm,
4043 const struct skl_ddb_allocation *ddb)
Lyude62e0fb82016-08-22 12:50:08 -04004044{
4045 struct drm_crtc *crtc = &intel_crtc->base;
4046 struct drm_device *dev = crtc->dev;
4047 struct drm_i915_private *dev_priv = to_i915(dev);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004048 int level, max_level = ilk_wm_max_level(dev_priv);
Lyude62e0fb82016-08-22 12:50:08 -04004049 enum pipe pipe = intel_crtc->pipe;
4050
4051 for (level = 0; level <= max_level; level++) {
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004052 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
4053 &wm->wm[level]);
Lyude62e0fb82016-08-22 12:50:08 -04004054 }
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004055 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
Lyude27082492016-08-24 07:48:10 +02004056
4057 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004058 &ddb->plane[pipe][PLANE_CURSOR]);
Lyude62e0fb82016-08-22 12:50:08 -04004059}
4060
cpaul@redhat.com45ece232016-10-14 17:31:56 -04004061bool skl_wm_level_equals(const struct skl_wm_level *l1,
4062 const struct skl_wm_level *l2)
4063{
4064 if (l1->plane_en != l2->plane_en)
4065 return false;
4066
4067 /* If both planes aren't enabled, the rest shouldn't matter */
4068 if (!l1->plane_en)
4069 return true;
4070
4071 return (l1->plane_res_l == l2->plane_res_l &&
4072 l1->plane_res_b == l2->plane_res_b);
4073}
4074
Lyude27082492016-08-24 07:48:10 +02004075static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
4076 const struct skl_ddb_entry *b)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004077{
Lyude27082492016-08-24 07:48:10 +02004078 return a->start < b->end && b->start < a->end;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004079}
4080
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004081bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry **entries,
4082 const struct skl_ddb_entry *ddb,
4083 int ignore)
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004084{
Lyudece0ba282016-09-15 10:46:35 -04004085 int i;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004086
Maarten Lankhorst5eff5032016-11-08 13:55:35 +01004087 for (i = 0; i < I915_MAX_PIPES; i++)
4088 if (i != ignore && entries[i] &&
4089 skl_ddb_entries_overlap(ddb, entries[i]))
Lyude27082492016-08-24 07:48:10 +02004090 return true;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004091
Lyude27082492016-08-24 07:48:10 +02004092 return false;
Damien Lespiau0e8fb7b2014-11-04 17:07:02 +00004093}
4094
Matt Roper55994c22016-05-12 07:06:08 -07004095static int skl_update_pipe_wm(struct drm_crtc_state *cstate,
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004096 const struct skl_pipe_wm *old_pipe_wm,
Matt Roper55994c22016-05-12 07:06:08 -07004097 struct skl_pipe_wm *pipe_wm, /* out */
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004098 struct skl_ddb_allocation *ddb, /* out */
Matt Roper55994c22016-05-12 07:06:08 -07004099 bool *changed /* out */)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004100{
Matt Roperf4a96752016-05-12 07:06:06 -07004101 struct intel_crtc_state *intel_cstate = to_intel_crtc_state(cstate);
Matt Roper55994c22016-05-12 07:06:08 -07004102 int ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004103
Matt Roper55994c22016-05-12 07:06:08 -07004104 ret = skl_build_pipe_wm(intel_cstate, ddb, pipe_wm);
4105 if (ret)
4106 return ret;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004107
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004108 if (!memcmp(old_pipe_wm, pipe_wm, sizeof(*pipe_wm)))
Matt Roper55994c22016-05-12 07:06:08 -07004109 *changed = false;
4110 else
4111 *changed = true;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004112
Matt Roper55994c22016-05-12 07:06:08 -07004113 return 0;
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004114}
4115
Matt Roper9b613022016-06-27 16:42:44 -07004116static uint32_t
4117pipes_modified(struct drm_atomic_state *state)
4118{
4119 struct drm_crtc *crtc;
4120 struct drm_crtc_state *cstate;
4121 uint32_t i, ret = 0;
4122
4123 for_each_crtc_in_state(state, crtc, cstate, i)
4124 ret |= drm_crtc_mask(crtc);
4125
4126 return ret;
4127}
4128
Jani Nikulabb7791b2016-10-04 12:29:17 +03004129static int
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004130skl_ddb_add_affected_planes(struct intel_crtc_state *cstate)
4131{
4132 struct drm_atomic_state *state = cstate->base.state;
4133 struct drm_device *dev = state->dev;
4134 struct drm_crtc *crtc = cstate->base.crtc;
4135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4136 struct drm_i915_private *dev_priv = to_i915(dev);
4137 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4138 struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
4139 struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
4140 struct drm_plane_state *plane_state;
4141 struct drm_plane *plane;
4142 enum pipe pipe = intel_crtc->pipe;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004143
4144 WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
4145
Maarten Lankhorst220b0962016-10-26 15:41:30 +02004146 drm_for_each_plane_mask(plane, dev, cstate->base.plane_mask) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004147 enum plane_id plane_id = to_intel_plane(plane)->id;
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004148
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004149 if (skl_ddb_entry_equal(&cur_ddb->plane[pipe][plane_id],
4150 &new_ddb->plane[pipe][plane_id]) &&
4151 skl_ddb_entry_equal(&cur_ddb->y_plane[pipe][plane_id],
4152 &new_ddb->y_plane[pipe][plane_id]))
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004153 continue;
4154
4155 plane_state = drm_atomic_get_plane_state(state, plane);
4156 if (IS_ERR(plane_state))
4157 return PTR_ERR(plane_state);
4158 }
4159
4160 return 0;
4161}
4162
Matt Roper98d39492016-05-12 07:06:03 -07004163static int
4164skl_compute_ddb(struct drm_atomic_state *state)
4165{
4166 struct drm_device *dev = state->dev;
4167 struct drm_i915_private *dev_priv = to_i915(dev);
4168 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4169 struct intel_crtc *intel_crtc;
Matt Roper734fa012016-05-12 15:11:40 -07004170 struct skl_ddb_allocation *ddb = &intel_state->wm_results.ddb;
Matt Roper9b613022016-06-27 16:42:44 -07004171 uint32_t realloc_pipes = pipes_modified(state);
Matt Roper98d39492016-05-12 07:06:03 -07004172 int ret;
4173
4174 /*
4175 * If this is our first atomic update following hardware readout,
4176 * we can't trust the DDB that the BIOS programmed for us. Let's
4177 * pretend that all pipes switched active status so that we'll
4178 * ensure a full DDB recompute.
4179 */
Matt Roper1b54a882016-06-17 13:42:18 -07004180 if (dev_priv->wm.distrust_bios_wm) {
4181 ret = drm_modeset_lock(&dev->mode_config.connection_mutex,
4182 state->acquire_ctx);
4183 if (ret)
4184 return ret;
4185
Matt Roper98d39492016-05-12 07:06:03 -07004186 intel_state->active_pipe_changes = ~0;
4187
Matt Roper1b54a882016-06-17 13:42:18 -07004188 /*
4189 * We usually only initialize intel_state->active_crtcs if we
4190 * we're doing a modeset; make sure this field is always
4191 * initialized during the sanitization process that happens
4192 * on the first commit too.
4193 */
4194 if (!intel_state->modeset)
4195 intel_state->active_crtcs = dev_priv->active_crtcs;
4196 }
4197
Matt Roper98d39492016-05-12 07:06:03 -07004198 /*
4199 * If the modeset changes which CRTC's are active, we need to
4200 * recompute the DDB allocation for *all* active pipes, even
4201 * those that weren't otherwise being modified in any way by this
4202 * atomic commit. Due to the shrinking of the per-pipe allocations
4203 * when new active CRTC's are added, it's possible for a pipe that
4204 * we were already using and aren't changing at all here to suddenly
4205 * become invalid if its DDB needs exceeds its new allocation.
4206 *
4207 * Note that if we wind up doing a full DDB recompute, we can't let
4208 * any other display updates race with this transaction, so we need
4209 * to grab the lock on *all* CRTC's.
4210 */
Matt Roper734fa012016-05-12 15:11:40 -07004211 if (intel_state->active_pipe_changes) {
Matt Roper98d39492016-05-12 07:06:03 -07004212 realloc_pipes = ~0;
Matt Roper734fa012016-05-12 15:11:40 -07004213 intel_state->wm_results.dirty_pipes = ~0;
4214 }
Matt Roper98d39492016-05-12 07:06:03 -07004215
Paulo Zanoni5a920b82016-10-04 14:37:32 -03004216 /*
4217 * We're not recomputing for the pipes not included in the commit, so
4218 * make sure we start with the current state.
4219 */
4220 memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
4221
Matt Roper98d39492016-05-12 07:06:03 -07004222 for_each_intel_crtc_mask(dev, intel_crtc, realloc_pipes) {
4223 struct intel_crtc_state *cstate;
4224
4225 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
4226 if (IS_ERR(cstate))
4227 return PTR_ERR(cstate);
4228
Matt Roper734fa012016-05-12 15:11:40 -07004229 ret = skl_allocate_pipe_ddb(cstate, ddb);
Matt Roper98d39492016-05-12 07:06:03 -07004230 if (ret)
4231 return ret;
Lyude05a76d32016-08-17 15:55:57 -04004232
Paulo Zanoni7f60e202016-09-29 16:36:48 -03004233 ret = skl_ddb_add_affected_planes(cstate);
Lyude05a76d32016-08-17 15:55:57 -04004234 if (ret)
4235 return ret;
Matt Roper98d39492016-05-12 07:06:03 -07004236 }
4237
4238 return 0;
4239}
4240
Matt Roper2722efb2016-08-17 15:55:55 -04004241static void
4242skl_copy_wm_for_pipe(struct skl_wm_values *dst,
4243 struct skl_wm_values *src,
4244 enum pipe pipe)
4245{
Matt Roper2722efb2016-08-17 15:55:55 -04004246 memcpy(dst->ddb.y_plane[pipe], src->ddb.y_plane[pipe],
4247 sizeof(dst->ddb.y_plane[pipe]));
4248 memcpy(dst->ddb.plane[pipe], src->ddb.plane[pipe],
4249 sizeof(dst->ddb.plane[pipe]));
4250}
4251
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004252static void
4253skl_print_wm_changes(const struct drm_atomic_state *state)
4254{
4255 const struct drm_device *dev = state->dev;
4256 const struct drm_i915_private *dev_priv = to_i915(dev);
4257 const struct intel_atomic_state *intel_state =
4258 to_intel_atomic_state(state);
4259 const struct drm_crtc *crtc;
4260 const struct drm_crtc_state *cstate;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004261 const struct intel_plane *intel_plane;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004262 const struct skl_ddb_allocation *old_ddb = &dev_priv->wm.skl_hw.ddb;
4263 const struct skl_ddb_allocation *new_ddb = &intel_state->wm_results.ddb;
Maarten Lankhorst75704982016-11-01 12:04:10 +01004264 int i;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004265
4266 for_each_crtc_in_state(state, crtc, cstate, i) {
Maarten Lankhorst75704982016-11-01 12:04:10 +01004267 const struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4268 enum pipe pipe = intel_crtc->pipe;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004269
Maarten Lankhorst75704982016-11-01 12:04:10 +01004270 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004271 enum plane_id plane_id = intel_plane->id;
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004272 const struct skl_ddb_entry *old, *new;
4273
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004274 old = &old_ddb->plane[pipe][plane_id];
4275 new = &new_ddb->plane[pipe][plane_id];
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004276
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004277 if (skl_ddb_entry_equal(old, new))
4278 continue;
4279
Maarten Lankhorst75704982016-11-01 12:04:10 +01004280 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] ddb (%d - %d) -> (%d - %d)\n",
4281 intel_plane->base.base.id,
4282 intel_plane->base.name,
4283 old->start, old->end,
4284 new->start, new->end);
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004285 }
4286 }
4287}
4288
Matt Roper98d39492016-05-12 07:06:03 -07004289static int
4290skl_compute_wm(struct drm_atomic_state *state)
4291{
4292 struct drm_crtc *crtc;
4293 struct drm_crtc_state *cstate;
Matt Roper734fa012016-05-12 15:11:40 -07004294 struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
4295 struct skl_wm_values *results = &intel_state->wm_results;
4296 struct skl_pipe_wm *pipe_wm;
Matt Roper98d39492016-05-12 07:06:03 -07004297 bool changed = false;
Matt Roper734fa012016-05-12 15:11:40 -07004298 int ret, i;
Matt Roper98d39492016-05-12 07:06:03 -07004299
4300 /*
4301 * If this transaction isn't actually touching any CRTC's, don't
4302 * bother with watermark calculation. Note that if we pass this
4303 * test, we're guaranteed to hold at least one CRTC state mutex,
4304 * which means we can safely use values like dev_priv->active_crtcs
4305 * since any racing commits that want to update them would need to
4306 * hold _all_ CRTC state mutexes.
4307 */
4308 for_each_crtc_in_state(state, crtc, cstate, i)
4309 changed = true;
4310 if (!changed)
4311 return 0;
4312
Matt Roper734fa012016-05-12 15:11:40 -07004313 /* Clear all dirty flags */
4314 results->dirty_pipes = 0;
4315
Matt Roper98d39492016-05-12 07:06:03 -07004316 ret = skl_compute_ddb(state);
4317 if (ret)
4318 return ret;
4319
Matt Roper734fa012016-05-12 15:11:40 -07004320 /*
4321 * Calculate WM's for all pipes that are part of this transaction.
4322 * Note that the DDB allocation above may have added more CRTC's that
4323 * weren't otherwise being modified (and set bits in dirty_pipes) if
4324 * pipe allocations had to change.
4325 *
4326 * FIXME: Now that we're doing this in the atomic check phase, we
4327 * should allow skl_update_pipe_wm() to return failure in cases where
4328 * no suitable watermark values can be found.
4329 */
4330 for_each_crtc_in_state(state, crtc, cstate, i) {
Matt Roper734fa012016-05-12 15:11:40 -07004331 struct intel_crtc_state *intel_cstate =
4332 to_intel_crtc_state(cstate);
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004333 const struct skl_pipe_wm *old_pipe_wm =
4334 &to_intel_crtc_state(crtc->state)->wm.skl.optimal;
Matt Roper734fa012016-05-12 15:11:40 -07004335
4336 pipe_wm = &intel_cstate->wm.skl.optimal;
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004337 ret = skl_update_pipe_wm(cstate, old_pipe_wm, pipe_wm,
4338 &results->ddb, &changed);
Matt Roper734fa012016-05-12 15:11:40 -07004339 if (ret)
4340 return ret;
4341
4342 if (changed)
4343 results->dirty_pipes |= drm_crtc_mask(crtc);
4344
4345 if ((results->dirty_pipes & drm_crtc_mask(crtc)) == 0)
4346 /* This pipe's WM's did not change */
4347 continue;
4348
4349 intel_cstate->update_wm_pre = true;
Matt Roper734fa012016-05-12 15:11:40 -07004350 }
4351
cpaul@redhat.com413fc532016-10-14 17:31:54 -04004352 skl_print_wm_changes(state);
4353
Matt Roper98d39492016-05-12 07:06:03 -07004354 return 0;
4355}
4356
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004357static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
4358 struct intel_crtc_state *cstate)
4359{
4360 struct intel_crtc *crtc = to_intel_crtc(cstate->base.crtc);
4361 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
4362 struct skl_pipe_wm *pipe_wm = &cstate->wm.skl.optimal;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004363 const struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004364 enum pipe pipe = crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004365 enum plane_id plane_id;
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004366
4367 if (!(state->wm_results.dirty_pipes & drm_crtc_mask(&crtc->base)))
4368 return;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004369
4370 I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004371
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004372 for_each_plane_id_on_crtc(crtc, plane_id) {
4373 if (plane_id != PLANE_CURSOR)
4374 skl_write_plane_wm(crtc, &pipe_wm->planes[plane_id],
4375 ddb, plane_id);
4376 else
4377 skl_write_cursor_wm(crtc, &pipe_wm->planes[plane_id],
4378 ddb);
4379 }
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004380}
4381
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004382static void skl_initial_wm(struct intel_atomic_state *state,
4383 struct intel_crtc_state *cstate)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004384{
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004385 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjälä432081b2016-10-31 22:37:03 +02004386 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004387 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004388 struct skl_wm_values *results = &state->wm_results;
Matt Roper2722efb2016-08-17 15:55:55 -04004389 struct skl_wm_values *hw_vals = &dev_priv->wm.skl_hw;
Lyude27082492016-08-24 07:48:10 +02004390 enum pipe pipe = intel_crtc->pipe;
Bob Paauweadda50b2015-07-21 10:42:53 -07004391
Ville Syrjälä432081b2016-10-31 22:37:03 +02004392 if ((results->dirty_pipes & drm_crtc_mask(&intel_crtc->base)) == 0)
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004393 return;
4394
Matt Roper734fa012016-05-12 15:11:40 -07004395 mutex_lock(&dev_priv->wm.wm_mutex);
4396
Maarten Lankhorste62929b2016-11-08 13:55:33 +01004397 if (cstate->base.active_changed)
4398 skl_atomic_update_crtc_wm(state, cstate);
Lyude27082492016-08-24 07:48:10 +02004399
4400 skl_copy_wm_for_pipe(hw_vals, results, pipe);
Matt Roper734fa012016-05-12 15:11:40 -07004401
4402 mutex_unlock(&dev_priv->wm.wm_mutex);
Pradeep Bhat2d41c0b2014-11-04 17:06:42 +00004403}
4404
Ville Syrjäläd8905652016-01-14 14:53:35 +02004405static void ilk_compute_wm_config(struct drm_device *dev,
4406 struct intel_wm_config *config)
4407{
4408 struct intel_crtc *crtc;
4409
4410 /* Compute the currently _active_ config */
4411 for_each_intel_crtc(dev, crtc) {
4412 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
4413
4414 if (!wm->pipe_enabled)
4415 continue;
4416
4417 config->sprites_enabled |= wm->sprites_enabled;
4418 config->sprites_scaled |= wm->sprites_scaled;
4419 config->num_pipes_active++;
4420 }
4421}
4422
Matt Ropered4a6a72016-02-23 17:20:13 -08004423static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
Paulo Zanoni801bcff2013-05-31 10:08:35 -03004424{
Chris Wilson91c8a322016-07-05 10:40:23 +01004425 struct drm_device *dev = &dev_priv->drm;
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004426 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
Imre Deak820c1982013-12-17 14:46:36 +02004427 struct ilk_wm_maximums max;
Ville Syrjäläd8905652016-01-14 14:53:35 +02004428 struct intel_wm_config config = {};
Imre Deak820c1982013-12-17 14:46:36 +02004429 struct ilk_wm_values results = {};
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004430 enum intel_ddb_partitioning partitioning;
Matt Roper261a27d2015-10-08 15:28:25 -07004431
Ville Syrjäläd8905652016-01-14 14:53:35 +02004432 ilk_compute_wm_config(dev, &config);
4433
4434 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
4435 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
Ville Syrjälä0362c782013-10-09 19:17:57 +03004436
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004437 /* 5/6 split only in single pipe config on IVB+ */
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004438 if (INTEL_GEN(dev_priv) >= 7 &&
Ville Syrjäläd8905652016-01-14 14:53:35 +02004439 config.num_pipes_active == 1 && config.sprites_enabled) {
4440 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
4441 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
Ville Syrjäläa485bfb2013-10-09 19:17:59 +03004442
Imre Deak820c1982013-12-17 14:46:36 +02004443 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
Paulo Zanoni861f3382013-05-31 10:19:21 -03004444 } else {
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004445 best_lp_wm = &lp_wm_1_2;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004446 }
4447
Ville Syrjälä198a1e92013-10-09 19:17:58 +03004448 partitioning = (best_lp_wm == &lp_wm_1_2) ?
Ville Syrjälä77c122b2013-08-06 22:24:04 +03004449 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
Paulo Zanoni861f3382013-05-31 10:19:21 -03004450
Imre Deak820c1982013-12-17 14:46:36 +02004451 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
Ville Syrjälä609cede2013-10-09 19:18:03 +03004452
Imre Deak820c1982013-12-17 14:46:36 +02004453 ilk_write_wm_values(dev_priv, &results);
Paulo Zanoni1011d8c2013-05-09 16:55:50 -03004454}
4455
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004456static void ilk_initial_watermarks(struct intel_atomic_state *state,
4457 struct intel_crtc_state *cstate)
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004458{
Matt Ropered4a6a72016-02-23 17:20:13 -08004459 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4460 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004461
Matt Ropered4a6a72016-02-23 17:20:13 -08004462 mutex_lock(&dev_priv->wm.wm_mutex);
Matt Ropere8f1f022016-05-12 07:05:55 -07004463 intel_crtc->wm.active.ilk = cstate->wm.ilk.intermediate;
Matt Ropered4a6a72016-02-23 17:20:13 -08004464 ilk_program_watermarks(dev_priv);
4465 mutex_unlock(&dev_priv->wm.wm_mutex);
4466}
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004467
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01004468static void ilk_optimize_watermarks(struct intel_atomic_state *state,
4469 struct intel_crtc_state *cstate)
Matt Ropered4a6a72016-02-23 17:20:13 -08004470{
4471 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
4472 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
4473
4474 mutex_lock(&dev_priv->wm.wm_mutex);
4475 if (cstate->wm.need_postvbl_update) {
Matt Ropere8f1f022016-05-12 07:05:55 -07004476 intel_crtc->wm.active.ilk = cstate->wm.ilk.optimal;
Matt Ropered4a6a72016-02-23 17:20:13 -08004477 ilk_program_watermarks(dev_priv);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004478 }
Matt Ropered4a6a72016-02-23 17:20:13 -08004479 mutex_unlock(&dev_priv->wm.wm_mutex);
Ville Syrjäläb9d5c832015-09-24 15:53:14 -07004480}
4481
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004482static inline void skl_wm_level_from_reg_val(uint32_t val,
4483 struct skl_wm_level *level)
Pradeep Bhat30789992014-11-04 17:06:45 +00004484{
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004485 level->plane_en = val & PLANE_WM_EN;
4486 level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
4487 level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
4488 PLANE_WM_LINES_MASK;
Pradeep Bhat30789992014-11-04 17:06:45 +00004489}
4490
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004491void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc,
4492 struct skl_pipe_wm *out)
Pradeep Bhat30789992014-11-04 17:06:45 +00004493{
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004494 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Pradeep Bhat30789992014-11-04 17:06:45 +00004495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Pradeep Bhat30789992014-11-04 17:06:45 +00004496 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004497 int level, max_level;
4498 enum plane_id plane_id;
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004499 uint32_t val;
Pradeep Bhat30789992014-11-04 17:06:45 +00004500
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004501 max_level = ilk_wm_max_level(dev_priv);
Pradeep Bhat30789992014-11-04 17:06:45 +00004502
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004503 for_each_plane_id_on_crtc(intel_crtc, plane_id) {
4504 struct skl_plane_wm *wm = &out->planes[plane_id];
Pradeep Bhat30789992014-11-04 17:06:45 +00004505
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004506 for (level = 0; level <= max_level; level++) {
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004507 if (plane_id != PLANE_CURSOR)
4508 val = I915_READ(PLANE_WM(pipe, plane_id, level));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004509 else
4510 val = I915_READ(CUR_WM(pipe, level));
4511
4512 skl_wm_level_from_reg_val(val, &wm->wm[level]);
4513 }
4514
Ville Syrjäläd5cdfdf52016-11-22 18:01:58 +02004515 if (plane_id != PLANE_CURSOR)
4516 val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
cpaul@redhat.comd8c0faf2016-10-18 16:09:49 -02004517 else
4518 val = I915_READ(CUR_WM_TRANS(pipe));
4519
4520 skl_wm_level_from_reg_val(val, &wm->trans_wm);
4521 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004522
Matt Roper3ef00282015-03-09 10:19:24 -07004523 if (!intel_crtc->active)
Pradeep Bhat30789992014-11-04 17:06:45 +00004524 return;
4525
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004526 out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
Pradeep Bhat30789992014-11-04 17:06:45 +00004527}
4528
4529void skl_wm_get_hw_state(struct drm_device *dev)
4530{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004531 struct drm_i915_private *dev_priv = to_i915(dev);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004532 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
Damien Lespiaua269c582014-11-04 17:06:49 +00004533 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
Pradeep Bhat30789992014-11-04 17:06:45 +00004534 struct drm_crtc *crtc;
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004535 struct intel_crtc *intel_crtc;
4536 struct intel_crtc_state *cstate;
Pradeep Bhat30789992014-11-04 17:06:45 +00004537
Damien Lespiaua269c582014-11-04 17:06:49 +00004538 skl_ddb_get_hw_state(dev_priv, ddb);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004539 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
4540 intel_crtc = to_intel_crtc(crtc);
4541 cstate = to_intel_crtc_state(crtc->state);
4542
4543 skl_pipe_wm_get_hw_state(crtc, &cstate->wm.skl.optimal);
4544
Maarten Lankhorst03af79e2016-10-26 15:41:36 +02004545 if (intel_crtc->active)
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004546 hw->dirty_pipes |= drm_crtc_mask(crtc);
cpaul@redhat.combf9d99a2016-10-14 17:31:55 -04004547 }
Matt Ropera1de91e2016-05-12 07:05:57 -07004548
Matt Roper279e99d2016-05-12 07:06:02 -07004549 if (dev_priv->active_crtcs) {
4550 /* Fully recompute DDB on first atomic commit */
4551 dev_priv->wm.distrust_bios_wm = true;
4552 } else {
4553 /* Easy/common case; just sanitize DDB now if everything off */
4554 memset(ddb, 0, sizeof(*ddb));
4555 }
Pradeep Bhat30789992014-11-04 17:06:45 +00004556}
4557
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004558static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
4559{
4560 struct drm_device *dev = crtc->dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01004561 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004562 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper4e0963c2015-09-24 15:53:15 -07004564 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
Matt Ropere8f1f022016-05-12 07:05:55 -07004565 struct intel_pipe_wm *active = &cstate->wm.ilk.optimal;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004566 enum pipe pipe = intel_crtc->pipe;
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02004567 static const i915_reg_t wm0_pipe_reg[] = {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004568 [PIPE_A] = WM0_PIPEA_ILK,
4569 [PIPE_B] = WM0_PIPEB_ILK,
4570 [PIPE_C] = WM0_PIPEC_IVB,
4571 };
4572
4573 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004574 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläce0e0712013-12-05 15:51:36 +02004575 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004576
Ville Syrjälä15606532016-05-13 17:55:17 +03004577 memset(active, 0, sizeof(*active));
4578
Matt Roper3ef00282015-03-09 10:19:24 -07004579 active->pipe_enabled = intel_crtc->active;
Ville Syrjälä2a44b762014-03-07 18:32:09 +02004580
4581 if (active->pipe_enabled) {
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004582 u32 tmp = hw->wm_pipe[pipe];
4583
4584 /*
4585 * For active pipes LP0 watermark is marked as
4586 * enabled, and LP1+ watermaks as disabled since
4587 * we can't really reverse compute them in case
4588 * multiple pipes are active.
4589 */
4590 active->wm[0].enable = true;
4591 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
4592 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
4593 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
4594 active->linetime = hw->wm_linetime[pipe];
4595 } else {
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01004596 int level, max_level = ilk_wm_max_level(dev_priv);
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004597
4598 /*
4599 * For inactive pipes, all watermark levels
4600 * should be marked as enabled but zeroed,
4601 * which is what we'd compute them to.
4602 */
4603 for (level = 0; level <= max_level; level++)
4604 active->wm[level].enable = true;
4605 }
Matt Roper4e0963c2015-09-24 15:53:15 -07004606
4607 intel_crtc->wm.active.ilk = *active;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004608}
4609
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004610#define _FW_WM(value, plane) \
4611 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
4612#define _FW_WM_VLV(value, plane) \
4613 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
4614
4615static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
4616 struct vlv_wm_values *wm)
4617{
4618 enum pipe pipe;
4619 uint32_t tmp;
4620
4621 for_each_pipe(dev_priv, pipe) {
4622 tmp = I915_READ(VLV_DDL(pipe));
4623
Ville Syrjälä1b313892016-11-28 19:37:08 +02004624 wm->ddl[pipe].plane[PLANE_PRIMARY] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004625 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004626 wm->ddl[pipe].plane[PLANE_CURSOR] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004627 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004628 wm->ddl[pipe].plane[PLANE_SPRITE0] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004629 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004630 wm->ddl[pipe].plane[PLANE_SPRITE1] =
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004631 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
4632 }
4633
4634 tmp = I915_READ(DSPFW1);
4635 wm->sr.plane = _FW_WM(tmp, SR);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004636 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
4637 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
4638 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004639
4640 tmp = I915_READ(DSPFW2);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004641 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
4642 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
4643 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004644
4645 tmp = I915_READ(DSPFW3);
4646 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
4647
4648 if (IS_CHERRYVIEW(dev_priv)) {
4649 tmp = I915_READ(DSPFW7_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004650 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4651 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004652
4653 tmp = I915_READ(DSPFW8_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004654 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
4655 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004656
4657 tmp = I915_READ(DSPFW9_CHV);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004658 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
4659 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004660
4661 tmp = I915_READ(DSPHOWM);
4662 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004663 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
4664 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
4665 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
4666 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4667 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4668 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4669 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4670 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4671 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004672 } else {
4673 tmp = I915_READ(DSPFW7);
Ville Syrjälä1b313892016-11-28 19:37:08 +02004674 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
4675 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004676
4677 tmp = I915_READ(DSPHOWM);
4678 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
Ville Syrjälä1b313892016-11-28 19:37:08 +02004679 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4680 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4681 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
4682 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4683 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4684 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004685 }
4686}
4687
4688#undef _FW_WM
4689#undef _FW_WM_VLV
4690
4691void vlv_wm_get_hw_state(struct drm_device *dev)
4692{
4693 struct drm_i915_private *dev_priv = to_i915(dev);
4694 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
Ville Syrjäläf07d43d2017-03-02 19:14:52 +02004695 struct intel_crtc *crtc;
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004696 u32 val;
4697
4698 vlv_read_wm_values(dev_priv, wm);
4699
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004700 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4701 wm->level = VLV_WM_LEVEL_PM2;
4702
4703 if (IS_CHERRYVIEW(dev_priv)) {
4704 mutex_lock(&dev_priv->rps.hw_lock);
4705
4706 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4707 if (val & DSP_MAXFIFO_PM5_ENABLE)
4708 wm->level = VLV_WM_LEVEL_PM5;
4709
Ville Syrjälä58590c12015-09-08 21:05:12 +03004710 /*
4711 * If DDR DVFS is disabled in the BIOS, Punit
4712 * will never ack the request. So if that happens
4713 * assume we don't have to enable/disable DDR DVFS
4714 * dynamically. To test that just set the REQ_ACK
4715 * bit to poke the Punit, but don't change the
4716 * HIGH/LOW bits so that we don't actually change
4717 * the current state.
4718 */
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004719 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
Ville Syrjälä58590c12015-09-08 21:05:12 +03004720 val |= FORCE_DDR_FREQ_REQ_ACK;
4721 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4722
4723 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4724 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4725 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4726 "assuming DDR DVFS is disabled\n");
4727 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4728 } else {
4729 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4730 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4731 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4732 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004733
4734 mutex_unlock(&dev_priv->rps.hw_lock);
4735 }
4736
Ville Syrjäläff32c542017-03-02 19:14:57 +02004737 for_each_intel_crtc(dev, crtc) {
4738 struct intel_crtc_state *crtc_state =
4739 to_intel_crtc_state(crtc->base.state);
4740 struct vlv_wm_state *active = &crtc->wm.active.vlv;
4741 const struct vlv_fifo_state *fifo_state =
4742 &crtc_state->wm.vlv.fifo_state;
4743 enum pipe pipe = crtc->pipe;
4744 enum plane_id plane_id;
4745 int level;
4746
4747 vlv_get_fifo_size(crtc_state);
4748
4749 active->num_levels = wm->level + 1;
4750 active->cxsr = wm->cxsr;
4751
Ville Syrjäläff32c542017-03-02 19:14:57 +02004752 for (level = 0; level < active->num_levels; level++) {
4753 struct vlv_pipe_wm *raw =
4754 &crtc_state->wm.vlv.raw[level];
4755
4756 active->sr[level].plane = wm->sr.plane;
4757 active->sr[level].cursor = wm->sr.cursor;
4758
4759 for_each_plane_id_on_crtc(crtc, plane_id) {
4760 active->wm[level].plane[plane_id] =
4761 wm->pipe[pipe].plane[plane_id];
4762
4763 raw->plane[plane_id] =
4764 vlv_invert_wm_value(active->wm[level].plane[plane_id],
4765 fifo_state->plane[plane_id]);
4766 }
4767 }
4768
4769 for_each_plane_id_on_crtc(crtc, plane_id)
4770 vlv_raw_plane_wm_set(crtc_state, level,
4771 plane_id, USHRT_MAX);
4772 vlv_invalidate_wms(crtc, active, level);
4773
4774 crtc_state->wm.vlv.optimal = *active;
Ville Syrjälä4841da52017-03-02 19:14:59 +02004775 crtc_state->wm.vlv.intermediate = *active;
Ville Syrjäläff32c542017-03-02 19:14:57 +02004776
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004777 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 +02004778 pipe_name(pipe),
4779 wm->pipe[pipe].plane[PLANE_PRIMARY],
4780 wm->pipe[pipe].plane[PLANE_CURSOR],
4781 wm->pipe[pipe].plane[PLANE_SPRITE0],
4782 wm->pipe[pipe].plane[PLANE_SPRITE1]);
Ville Syrjäläff32c542017-03-02 19:14:57 +02004783 }
Ville Syrjälä6eb1a682015-06-24 22:00:03 +03004784
4785 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4786 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4787}
4788
Ville Syrjälä602ae832017-03-02 19:15:02 +02004789void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
4790{
4791 struct intel_plane *plane;
4792 struct intel_crtc *crtc;
4793
4794 mutex_lock(&dev_priv->wm.wm_mutex);
4795
4796 for_each_intel_plane(&dev_priv->drm, plane) {
4797 struct intel_crtc *crtc =
4798 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
4799 struct intel_crtc_state *crtc_state =
4800 to_intel_crtc_state(crtc->base.state);
4801 struct intel_plane_state *plane_state =
4802 to_intel_plane_state(plane->base.state);
4803 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
4804 const struct vlv_fifo_state *fifo_state =
4805 &crtc_state->wm.vlv.fifo_state;
4806 enum plane_id plane_id = plane->id;
4807 int level;
4808
4809 if (plane_state->base.visible)
4810 continue;
4811
4812 for (level = 0; level < wm_state->num_levels; level++) {
4813 struct vlv_pipe_wm *raw =
4814 &crtc_state->wm.vlv.raw[level];
4815
4816 raw->plane[plane_id] = 0;
4817
4818 wm_state->wm[level].plane[plane_id] =
4819 vlv_invert_wm_value(raw->plane[plane_id],
4820 fifo_state->plane[plane_id]);
4821 }
4822 }
4823
4824 for_each_intel_crtc(&dev_priv->drm, crtc) {
4825 struct intel_crtc_state *crtc_state =
4826 to_intel_crtc_state(crtc->base.state);
4827
4828 crtc_state->wm.vlv.intermediate =
4829 crtc_state->wm.vlv.optimal;
4830 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4831 }
4832
4833 vlv_program_watermarks(dev_priv);
4834
4835 mutex_unlock(&dev_priv->wm.wm_mutex);
4836}
4837
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004838void ilk_wm_get_hw_state(struct drm_device *dev)
4839{
Chris Wilsonfac5e232016-07-04 11:34:36 +01004840 struct drm_i915_private *dev_priv = to_i915(dev);
Imre Deak820c1982013-12-17 14:46:36 +02004841 struct ilk_wm_values *hw = &dev_priv->wm.hw;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004842 struct drm_crtc *crtc;
4843
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01004844 for_each_crtc(dev, crtc)
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004845 ilk_pipe_wm_get_hw_state(crtc);
4846
4847 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4848 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4849 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4850
4851 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
Tvrtko Ursulin175fded2016-11-16 08:55:42 +00004852 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjäläcfa76982014-03-07 18:32:08 +02004853 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4854 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4855 }
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004856
Tvrtko Ursulin86527442016-10-13 11:03:00 +01004857 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004858 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4859 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Tvrtko Ursulinfd6b8f42016-10-14 10:13:06 +01004860 else if (IS_IVYBRIDGE(dev_priv))
Ville Syrjäläac9545f2013-12-05 15:51:28 +02004861 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4862 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
Ville Syrjälä243e6a42013-10-14 14:55:24 +03004863
4864 hw->enable_fbc_wm =
4865 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4866}
4867
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004868/**
4869 * intel_update_watermarks - update FIFO watermark values based on current modes
4870 *
4871 * Calculate watermark values for the various WM regs based on current mode
4872 * and plane configuration.
4873 *
4874 * There are several cases to deal with here:
4875 * - normal (i.e. non-self-refresh)
4876 * - self-refresh (SR) mode
4877 * - lines are large relative to FIFO size (buffer can hold up to 2)
4878 * - lines are small relative to FIFO size (buffer can hold more than 2
4879 * lines), so need to account for TLB latency
4880 *
4881 * The normal calculation is:
4882 * watermark = dotclock * bytes per pixel * latency
4883 * where latency is platform & configuration dependent (we assume pessimal
4884 * values here).
4885 *
4886 * The SR calculation is:
4887 * watermark = (trunc(latency/line time)+1) * surface width *
4888 * bytes per pixel
4889 * where
4890 * line time = htotal / dotclock
4891 * surface width = hdisplay for normal plane and 64 for cursor
4892 * and latency is assumed to be high, as above.
4893 *
4894 * The final value programmed to the register should always be rounded up,
4895 * and include an extra 2 entries to account for clock crossings.
4896 *
4897 * We don't use the sprite, so we can ignore that. And on Crestline we have
4898 * to set the non-SR watermarks to 8.
4899 */
Ville Syrjälä432081b2016-10-31 22:37:03 +02004900void intel_update_watermarks(struct intel_crtc *crtc)
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004901{
Ville Syrjälä432081b2016-10-31 22:37:03 +02004902 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004903
4904 if (dev_priv->display.update_wm)
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004905 dev_priv->display.update_wm(crtc);
Eugeni Dodonovb445e3b2012-04-16 22:20:35 -03004906}
4907
Jani Nikulae2828912016-01-18 09:19:47 +02004908/*
Daniel Vetter92703882012-08-09 16:46:01 +02004909 * Lock protecting IPS related data structures
Daniel Vetter92703882012-08-09 16:46:01 +02004910 */
4911DEFINE_SPINLOCK(mchdev_lock);
4912
4913/* Global for IPS driver to get at the current i915 device. Protected by
4914 * mchdev_lock. */
4915static struct drm_i915_private *i915_mch_dev;
4916
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004917bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004918{
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004919 u16 rgvswctl;
4920
Chris Wilson67520412017-03-02 13:28:01 +00004921 lockdep_assert_held(&mchdev_lock);
Daniel Vetter92703882012-08-09 16:46:01 +02004922
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004923 rgvswctl = I915_READ16(MEMSWCTL);
4924 if (rgvswctl & MEMCTL_CMD_STS) {
4925 DRM_DEBUG("gpu busy, RCS change rejected\n");
4926 return false; /* still busy with another command */
4927 }
4928
4929 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4930 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4931 I915_WRITE16(MEMSWCTL, rgvswctl);
4932 POSTING_READ16(MEMSWCTL);
4933
4934 rgvswctl |= MEMCTL_CMD_STS;
4935 I915_WRITE16(MEMSWCTL, rgvswctl);
4936
4937 return true;
4938}
4939
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004940static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004941{
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004942 u32 rgvmodectl;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004943 u8 fmax, fmin, fstart, vstart;
4944
Daniel Vetter92703882012-08-09 16:46:01 +02004945 spin_lock_irq(&mchdev_lock);
4946
Tvrtko Ursulin84f1b202016-02-11 10:27:32 +00004947 rgvmodectl = I915_READ(MEMMODECTL);
4948
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004949 /* Enable temp reporting */
4950 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4951 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4952
4953 /* 100ms RC evaluation intervals */
4954 I915_WRITE(RCUPEI, 100000);
4955 I915_WRITE(RCDNEI, 100000);
4956
4957 /* Set max/min thresholds to 90ms and 80ms respectively */
4958 I915_WRITE(RCBMAXAVG, 90000);
4959 I915_WRITE(RCBMINAVG, 80000);
4960
4961 I915_WRITE(MEMIHYST, 1);
4962
4963 /* Set up min, max, and cur for interrupt handling */
4964 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4965 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4966 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4967 MEMMODE_FSTART_SHIFT;
4968
Ville Syrjälä616847e2015-09-18 20:03:19 +03004969 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004970 PXVFREQ_PX_SHIFT;
4971
Daniel Vetter20e4d402012-08-08 23:35:39 +02004972 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4973 dev_priv->ips.fstart = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004974
Daniel Vetter20e4d402012-08-08 23:35:39 +02004975 dev_priv->ips.max_delay = fstart;
4976 dev_priv->ips.min_delay = fmin;
4977 dev_priv->ips.cur_delay = fstart;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004978
4979 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4980 fmax, fmin, fstart);
4981
4982 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4983
4984 /*
4985 * Interrupts will be enabled in ironlake_irq_postinstall
4986 */
4987
4988 I915_WRITE(VIDSTART, vstart);
4989 POSTING_READ(VIDSTART);
4990
4991 rgvmodectl |= MEMMODE_SWMODE_EN;
4992 I915_WRITE(MEMMODECTL, rgvmodectl);
4993
Daniel Vetter92703882012-08-09 16:46:01 +02004994 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004995 DRM_ERROR("stuck trying to change perf mode\n");
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02004996 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004997
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01004998 ironlake_set_drps(dev_priv, fstart);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03004999
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005000 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
5001 I915_READ(DDREC) + I915_READ(CSIEC);
Daniel Vetter20e4d402012-08-08 23:35:39 +02005002 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
Ville Syrjälä7d81c3e2015-09-18 20:03:20 +03005003 dev_priv->ips.last_count2 = I915_READ(GFXEC);
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00005004 dev_priv->ips.last_time2 = ktime_get_raw_ns();
Daniel Vetter92703882012-08-09 16:46:01 +02005005
5006 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005007}
5008
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005009static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005010{
Daniel Vetter92703882012-08-09 16:46:01 +02005011 u16 rgvswctl;
5012
5013 spin_lock_irq(&mchdev_lock);
5014
5015 rgvswctl = I915_READ16(MEMSWCTL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005016
5017 /* Ack interrupts, disable EFC interrupt */
5018 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
5019 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
5020 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
5021 I915_WRITE(DEIIR, DE_PCU_EVENT);
5022 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
5023
5024 /* Go back to the starting frequency */
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01005025 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005026 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005027 rgvswctl |= MEMCTL_CMD_STS;
5028 I915_WRITE(MEMSWCTL, rgvswctl);
Daniel Vetterdd92d8d2015-07-20 10:58:21 +02005029 mdelay(1);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005030
Daniel Vetter92703882012-08-09 16:46:01 +02005031 spin_unlock_irq(&mchdev_lock);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005032}
5033
Daniel Vetteracbe9472012-07-26 11:50:05 +02005034/* There's a funny hw issue where the hw returns all 0 when reading from
5035 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
5036 * ourselves, instead of doing a rmw cycle (which might result in us clearing
5037 * all limits and the gpu stuck at whatever frequency it is at atm).
5038 */
Akash Goel74ef1172015-03-06 11:07:19 +05305039static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005040{
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005041 u32 limits;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005042
Daniel Vetter20b46e52012-07-26 11:16:14 +02005043 /* Only set the down limit when we've reached the lowest level to avoid
5044 * getting more interrupts, otherwise leave this clear. This prevents a
5045 * race in the hw when coming out of rc6: There's a tiny window where
5046 * the hw runs at the minimal clock before selecting the desired
5047 * frequency, if the down threshold expires in that window we will not
5048 * receive a down interrupt. */
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03005049 if (IS_GEN9(dev_priv)) {
Akash Goel74ef1172015-03-06 11:07:19 +05305050 limits = (dev_priv->rps.max_freq_softlimit) << 23;
5051 if (val <= dev_priv->rps.min_freq_softlimit)
5052 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
5053 } else {
5054 limits = dev_priv->rps.max_freq_softlimit << 24;
5055 if (val <= dev_priv->rps.min_freq_softlimit)
5056 limits |= dev_priv->rps.min_freq_softlimit << 16;
5057 }
Daniel Vetter20b46e52012-07-26 11:16:14 +02005058
5059 return limits;
5060}
5061
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005062static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
5063{
5064 int new_power;
Akash Goel8a586432015-03-06 11:07:18 +05305065 u32 threshold_up = 0, threshold_down = 0; /* in % */
5066 u32 ei_up = 0, ei_down = 0;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005067
5068 new_power = dev_priv->rps.power;
5069 switch (dev_priv->rps.power) {
5070 case LOW_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005071 if (val > dev_priv->rps.efficient_freq + 1 &&
5072 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005073 new_power = BETWEEN;
5074 break;
5075
5076 case BETWEEN:
Chris Wilsona72b5622016-07-02 15:35:59 +01005077 if (val <= dev_priv->rps.efficient_freq &&
5078 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005079 new_power = LOW_POWER;
Chris Wilsona72b5622016-07-02 15:35:59 +01005080 else if (val >= dev_priv->rps.rp0_freq &&
5081 val > dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005082 new_power = HIGH_POWER;
5083 break;
5084
5085 case HIGH_POWER:
Chris Wilsona72b5622016-07-02 15:35:59 +01005086 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 &&
5087 val < dev_priv->rps.cur_freq)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005088 new_power = BETWEEN;
5089 break;
5090 }
5091 /* Max/min bins are special */
Chris Wilsonaed242f2015-03-18 09:48:21 +00005092 if (val <= dev_priv->rps.min_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005093 new_power = LOW_POWER;
Chris Wilsonaed242f2015-03-18 09:48:21 +00005094 if (val >= dev_priv->rps.max_freq_softlimit)
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005095 new_power = HIGH_POWER;
5096 if (new_power == dev_priv->rps.power)
5097 return;
5098
5099 /* Note the units here are not exactly 1us, but 1280ns. */
5100 switch (new_power) {
5101 case LOW_POWER:
5102 /* Upclock if more than 95% busy over 16ms */
Akash Goel8a586432015-03-06 11:07:18 +05305103 ei_up = 16000;
5104 threshold_up = 95;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005105
5106 /* Downclock if less than 85% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305107 ei_down = 32000;
5108 threshold_down = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005109 break;
5110
5111 case BETWEEN:
5112 /* Upclock if more than 90% busy over 13ms */
Akash Goel8a586432015-03-06 11:07:18 +05305113 ei_up = 13000;
5114 threshold_up = 90;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005115
5116 /* Downclock if less than 75% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305117 ei_down = 32000;
5118 threshold_down = 75;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005119 break;
5120
5121 case HIGH_POWER:
5122 /* Upclock if more than 85% busy over 10ms */
Akash Goel8a586432015-03-06 11:07:18 +05305123 ei_up = 10000;
5124 threshold_up = 85;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005125
5126 /* Downclock if less than 60% busy over 32ms */
Akash Goel8a586432015-03-06 11:07:18 +05305127 ei_down = 32000;
5128 threshold_down = 60;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005129 break;
5130 }
5131
Mika Kuoppala6067a272017-02-15 15:52:59 +02005132 /* When byt can survive without system hang with dynamic
5133 * sw freq adjustments, this restriction can be lifted.
5134 */
5135 if (IS_VALLEYVIEW(dev_priv))
5136 goto skip_hw_write;
5137
Akash Goel8a586432015-03-06 11:07:18 +05305138 I915_WRITE(GEN6_RP_UP_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005139 GT_INTERVAL_FROM_US(dev_priv, ei_up));
Akash Goel8a586432015-03-06 11:07:18 +05305140 I915_WRITE(GEN6_RP_UP_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005141 GT_INTERVAL_FROM_US(dev_priv,
5142 ei_up * threshold_up / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305143
5144 I915_WRITE(GEN6_RP_DOWN_EI,
Chris Wilsona72b5622016-07-02 15:35:59 +01005145 GT_INTERVAL_FROM_US(dev_priv, ei_down));
Akash Goel8a586432015-03-06 11:07:18 +05305146 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
Chris Wilsona72b5622016-07-02 15:35:59 +01005147 GT_INTERVAL_FROM_US(dev_priv,
5148 ei_down * threshold_down / 100));
Akash Goel8a586432015-03-06 11:07:18 +05305149
Chris Wilsona72b5622016-07-02 15:35:59 +01005150 I915_WRITE(GEN6_RP_CONTROL,
5151 GEN6_RP_MEDIA_TURBO |
5152 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5153 GEN6_RP_MEDIA_IS_GFX |
5154 GEN6_RP_ENABLE |
5155 GEN6_RP_UP_BUSY_AVG |
5156 GEN6_RP_DOWN_IDLE_AVG);
Akash Goel8a586432015-03-06 11:07:18 +05305157
Mika Kuoppala6067a272017-02-15 15:52:59 +02005158skip_hw_write:
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005159 dev_priv->rps.power = new_power;
Chris Wilson8fb55192015-04-07 16:20:28 +01005160 dev_priv->rps.up_threshold = threshold_up;
5161 dev_priv->rps.down_threshold = threshold_down;
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005162 dev_priv->rps.last_adj = 0;
5163}
5164
Chris Wilson2876ce72014-03-28 08:03:34 +00005165static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
5166{
5167 u32 mask = 0;
5168
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005169 /* We use UP_EI_EXPIRED interupts for both up/down in manual mode */
Chris Wilson2876ce72014-03-28 08:03:34 +00005170 if (val > dev_priv->rps.min_freq_softlimit)
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005171 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
Chris Wilson2876ce72014-03-28 08:03:34 +00005172 if (val < dev_priv->rps.max_freq_softlimit)
Chris Wilson6f4b12f82015-03-18 09:48:23 +00005173 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
Chris Wilson2876ce72014-03-28 08:03:34 +00005174
Chris Wilson7b3c29f2014-07-10 20:31:19 +01005175 mask &= dev_priv->pm_rps_events;
5176
Imre Deak59d02a12014-12-19 19:33:26 +02005177 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
Chris Wilson2876ce72014-03-28 08:03:34 +00005178}
5179
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005180/* gen6_set_rps is called to update the frequency request, but should also be
5181 * called when the range (min_delay and max_delay) is modified so that we can
5182 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005183static int gen6_set_rps(struct drm_i915_private *dev_priv, u8 val)
Daniel Vetter20b46e52012-07-26 11:16:14 +02005184{
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005185 /* min/max delay may still have been modified so be sure to
5186 * write the limits value.
5187 */
5188 if (val != dev_priv->rps.cur_freq) {
5189 gen6_set_rps_thresholds(dev_priv, val);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005190
Chris Wilsondc979972016-05-10 14:10:04 +01005191 if (IS_GEN9(dev_priv))
Akash Goel57041952015-03-06 11:07:17 +05305192 I915_WRITE(GEN6_RPNSWREQ,
5193 GEN9_FREQUENCY(val));
Chris Wilsondc979972016-05-10 14:10:04 +01005194 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
Chris Wilsoneb64cad2014-03-27 08:24:20 +00005195 I915_WRITE(GEN6_RPNSWREQ,
5196 HSW_FREQUENCY(val));
5197 else
5198 I915_WRITE(GEN6_RPNSWREQ,
5199 GEN6_FREQUENCY(val) |
5200 GEN6_OFFSET(0) |
5201 GEN6_AGGRESSIVE_TURBO);
Jeff McGeeb8a5ff82014-02-04 11:37:01 -06005202 }
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005203
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005204 /* Make sure we continue to get interrupts
5205 * until we hit the minimum or maximum frequencies.
5206 */
Akash Goel74ef1172015-03-06 11:07:19 +05305207 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
Chris Wilson2876ce72014-03-28 08:03:34 +00005208 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
Chris Wilson7b9e0ae2012-04-28 08:56:39 +01005209
Ben Widawskyb39fb292014-03-19 18:31:11 -07005210 dev_priv->rps.cur_freq = val;
Mika Kuoppala0f945922015-11-17 18:14:26 +02005211 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005212
5213 return 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005214}
5215
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005216static int valleyview_set_rps(struct drm_i915_private *dev_priv, u8 val)
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005217{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005218 int err;
5219
Chris Wilsondc979972016-05-10 14:10:04 +01005220 if (WARN_ONCE(IS_CHERRYVIEW(dev_priv) && (val & 1),
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005221 "Odd GPU freq value\n"))
5222 val &= ~1;
5223
Deepak Scd25dd52015-07-10 18:31:40 +05305224 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
5225
Chris Wilson8fb55192015-04-07 16:20:28 +01005226 if (val != dev_priv->rps.cur_freq) {
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005227 err = vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
5228 if (err)
5229 return err;
5230
Chris Wilsondb4c5e02017-02-10 15:03:46 +00005231 gen6_set_rps_thresholds(dev_priv, val);
Chris Wilson8fb55192015-04-07 16:20:28 +01005232 }
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005233
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005234 dev_priv->rps.cur_freq = val;
5235 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005236
5237 return 0;
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005238}
5239
Deepak Sa7f6e232015-05-09 18:04:44 +05305240/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
Deepak S76c3552f2014-01-30 23:08:16 +05305241 *
5242 * * If Gfx is Idle, then
Deepak Sa7f6e232015-05-09 18:04:44 +05305243 * 1. Forcewake Media well.
5244 * 2. Request idle freq.
5245 * 3. Release Forcewake of Media well.
Deepak S76c3552f2014-01-30 23:08:16 +05305246*/
5247static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
5248{
Chris Wilsonaed242f2015-03-18 09:48:21 +00005249 u32 val = dev_priv->rps.idle_freq;
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005250 int err;
Deepak S5549d252014-06-28 11:26:11 +05305251
Chris Wilsonaed242f2015-03-18 09:48:21 +00005252 if (dev_priv->rps.cur_freq <= val)
Deepak S76c3552f2014-01-30 23:08:16 +05305253 return;
5254
Chris Wilsonc9efef72017-01-02 15:28:45 +00005255 /* The punit delays the write of the frequency and voltage until it
5256 * determines the GPU is awake. During normal usage we don't want to
5257 * waste power changing the frequency if the GPU is sleeping (rc6).
5258 * However, the GPU and driver is now idle and we do not want to delay
5259 * switching to minimum voltage (reducing power whilst idle) as we do
5260 * not expect to be woken in the near future and so must flush the
5261 * change by waking the device.
5262 *
5263 * We choose to take the media powerwell (either would do to trick the
5264 * punit into committing the voltage change) as that takes a lot less
5265 * power than the render powerwell.
5266 */
Deepak Sa7f6e232015-05-09 18:04:44 +05305267 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005268 err = valleyview_set_rps(dev_priv, val);
Deepak Sa7f6e232015-05-09 18:04:44 +05305269 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005270
5271 if (err)
5272 DRM_ERROR("Failed to set RPS for idle\n");
Deepak S76c3552f2014-01-30 23:08:16 +05305273}
5274
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005275void gen6_rps_busy(struct drm_i915_private *dev_priv)
5276{
5277 mutex_lock(&dev_priv->rps.hw_lock);
5278 if (dev_priv->rps.enabled) {
Chris Wilsonbd648182017-02-10 15:03:48 +00005279 u8 freq;
5280
Chris Wilsone0e8c7c2017-03-09 21:12:30 +00005281 if (dev_priv->pm_rps_events & GEN6_PM_RP_UP_EI_EXPIRED)
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005282 gen6_rps_reset_ei(dev_priv);
5283 I915_WRITE(GEN6_PMINTRMSK,
5284 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
Michał Winiarski2b83c4c2016-06-20 11:58:27 +02005285
Chris Wilsonc33d2472016-07-04 08:08:36 +01005286 gen6_enable_rps_interrupts(dev_priv);
5287
Chris Wilsonbd648182017-02-10 15:03:48 +00005288 /* Use the user's desired frequency as a guide, but for better
5289 * performance, jump directly to RPe as our starting frequency.
5290 */
5291 freq = max(dev_priv->rps.cur_freq,
5292 dev_priv->rps.efficient_freq);
5293
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005294 if (intel_set_rps(dev_priv,
Chris Wilsonbd648182017-02-10 15:03:48 +00005295 clamp(freq,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005296 dev_priv->rps.min_freq_softlimit,
5297 dev_priv->rps.max_freq_softlimit)))
5298 DRM_DEBUG_DRIVER("Failed to set idle frequency\n");
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005299 }
5300 mutex_unlock(&dev_priv->rps.hw_lock);
5301}
5302
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005303void gen6_rps_idle(struct drm_i915_private *dev_priv)
5304{
Chris Wilsonc33d2472016-07-04 08:08:36 +01005305 /* Flush our bottom-half so that it does not race with us
5306 * setting the idle frequency and so that it is bounded by
5307 * our rpm wakeref. And then disable the interrupts to stop any
5308 * futher RPS reclocking whilst we are asleep.
5309 */
5310 gen6_disable_rps_interrupts(dev_priv);
5311
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005312 mutex_lock(&dev_priv->rps.hw_lock);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005313 if (dev_priv->rps.enabled) {
Chris Wilsondc979972016-05-10 14:10:04 +01005314 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Deepak S76c3552f2014-01-30 23:08:16 +05305315 vlv_set_rps_idle(dev_priv);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005316 else
Chris Wilsondc979972016-05-10 14:10:04 +01005317 gen6_set_rps(dev_priv, dev_priv->rps.idle_freq);
Chris Wilsonc0951f02013-10-10 21:58:50 +01005318 dev_priv->rps.last_adj = 0;
Ville Syrjälä12c100b2016-05-23 17:42:48 +03005319 I915_WRITE(GEN6_PMINTRMSK,
5320 gen6_sanitize_rps_pm_mask(dev_priv, ~0));
Chris Wilsonc0951f02013-10-10 21:58:50 +01005321 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005322 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005323
Chris Wilson8d3afd72015-05-21 21:01:47 +01005324 spin_lock(&dev_priv->rps.client_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005325 while (!list_empty(&dev_priv->rps.clients))
5326 list_del_init(dev_priv->rps.clients.next);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005327 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005328}
5329
Chris Wilson1854d5c2015-04-07 16:20:32 +01005330void gen6_rps_boost(struct drm_i915_private *dev_priv,
Chris Wilsone61b9952015-04-27 13:41:24 +01005331 struct intel_rps_client *rps,
5332 unsigned long submitted)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005333{
Chris Wilson8d3afd72015-05-21 21:01:47 +01005334 /* This is intentionally racy! We peek at the state here, then
5335 * validate inside the RPS worker.
5336 */
Chris Wilson67d97da2016-07-04 08:08:31 +01005337 if (!(dev_priv->gt.awake &&
Chris Wilson8d3afd72015-05-21 21:01:47 +01005338 dev_priv->rps.enabled &&
Chris Wilson29ecd78d2016-07-13 09:10:35 +01005339 dev_priv->rps.cur_freq < dev_priv->rps.boost_freq))
Chris Wilson8d3afd72015-05-21 21:01:47 +01005340 return;
Chris Wilson43cf3bf2015-03-18 09:48:22 +00005341
Chris Wilsone61b9952015-04-27 13:41:24 +01005342 /* Force a RPS boost (and don't count it against the client) if
5343 * the GPU is severely congested.
5344 */
Chris Wilsond0bc54f2015-05-21 21:01:48 +01005345 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
Chris Wilsone61b9952015-04-27 13:41:24 +01005346 rps = NULL;
5347
Chris Wilson8d3afd72015-05-21 21:01:47 +01005348 spin_lock(&dev_priv->rps.client_lock);
5349 if (rps == NULL || list_empty(&rps->link)) {
5350 spin_lock_irq(&dev_priv->irq_lock);
5351 if (dev_priv->rps.interrupts_enabled) {
5352 dev_priv->rps.client_boost = true;
Chris Wilsonc33d2472016-07-04 08:08:36 +01005353 schedule_work(&dev_priv->rps.work);
Chris Wilson8d3afd72015-05-21 21:01:47 +01005354 }
5355 spin_unlock_irq(&dev_priv->irq_lock);
Chris Wilson1854d5c2015-04-07 16:20:32 +01005356
Chris Wilson2e1b8732015-04-27 13:41:22 +01005357 if (rps != NULL) {
5358 list_add(&rps->link, &dev_priv->rps.clients);
5359 rps->boosts++;
Chris Wilson1854d5c2015-04-07 16:20:32 +01005360 } else
5361 dev_priv->rps.boosts++;
Chris Wilsonc0951f02013-10-10 21:58:50 +01005362 }
Chris Wilson8d3afd72015-05-21 21:01:47 +01005363 spin_unlock(&dev_priv->rps.client_lock);
Chris Wilsonb29c19b2013-09-25 17:34:56 +01005364}
5365
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005366int intel_set_rps(struct drm_i915_private *dev_priv, u8 val)
Jesse Barnes0a073b82013-04-17 15:54:58 -07005367{
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005368 int err;
5369
Chris Wilsoncfd1c482017-02-20 09:47:07 +00005370 lockdep_assert_held(&dev_priv->rps.hw_lock);
5371 GEM_BUG_ON(val > dev_priv->rps.max_freq);
5372 GEM_BUG_ON(val < dev_priv->rps.min_freq);
5373
Chris Wilson76e4e4b2017-02-20 09:47:08 +00005374 if (!dev_priv->rps.enabled) {
5375 dev_priv->rps.cur_freq = val;
5376 return 0;
5377 }
5378
Chris Wilsondc979972016-05-10 14:10:04 +01005379 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005380 err = valleyview_set_rps(dev_priv, val);
Ville Syrjäläffe02b42015-02-02 19:09:50 +02005381 else
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005382 err = gen6_set_rps(dev_priv, val);
5383
5384 return err;
Jesse Barnes0a073b82013-04-17 15:54:58 -07005385}
5386
Chris Wilsondc979972016-05-10 14:10:04 +01005387static void gen9_disable_rc6(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005388{
Zhe Wang20e49362014-11-04 17:07:05 +00005389 I915_WRITE(GEN6_RC_CONTROL, 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005390 I915_WRITE(GEN9_PG_ENABLE, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005391}
5392
Chris Wilsondc979972016-05-10 14:10:04 +01005393static void gen9_disable_rps(struct drm_i915_private *dev_priv)
Akash Goel2030d682016-04-23 00:05:45 +05305394{
Akash Goel2030d682016-04-23 00:05:45 +05305395 I915_WRITE(GEN6_RP_CONTROL, 0);
5396}
5397
Chris Wilsondc979972016-05-10 14:10:04 +01005398static void gen6_disable_rps(struct drm_i915_private *dev_priv)
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005399{
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005400 I915_WRITE(GEN6_RC_CONTROL, 0);
5401 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
Akash Goel2030d682016-04-23 00:05:45 +05305402 I915_WRITE(GEN6_RP_CONTROL, 0);
Daniel Vetter44fc7d52013-07-12 22:43:27 +02005403}
5404
Chris Wilsondc979972016-05-10 14:10:04 +01005405static void cherryview_disable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05305406{
Deepak S38807742014-05-23 21:00:15 +05305407 I915_WRITE(GEN6_RC_CONTROL, 0);
5408}
5409
Chris Wilsondc979972016-05-10 14:10:04 +01005410static void valleyview_disable_rps(struct drm_i915_private *dev_priv)
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005411{
Deepak S98a2e5f2014-08-18 10:35:27 -07005412 /* we're doing forcewake before Disabling RC6,
5413 * This what the BIOS expects when going into suspend */
Mika Kuoppala59bad942015-01-16 11:34:40 +02005414 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S98a2e5f2014-08-18 10:35:27 -07005415
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005416 I915_WRITE(GEN6_RC_CONTROL, 0);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005417
Mika Kuoppala59bad942015-01-16 11:34:40 +02005418 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnesd20d4f02013-04-23 10:09:28 -07005419}
5420
Chris Wilsondc979972016-05-10 14:10:04 +01005421static void intel_print_rc6_info(struct drm_i915_private *dev_priv, u32 mode)
Ben Widawskydc39fff2013-10-18 12:32:07 -07005422{
Chris Wilsondc979972016-05-10 14:10:04 +01005423 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Imre Deak91ca6892014-04-14 20:24:25 +03005424 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
5425 mode = GEN6_RC_CTL_RC6_ENABLE;
5426 else
5427 mode = 0;
5428 }
Chris Wilsondc979972016-05-10 14:10:04 +01005429 if (HAS_RC6p(dev_priv))
Imre Deakb99d49c2016-06-29 19:13:54 +03005430 DRM_DEBUG_DRIVER("Enabling RC6 states: "
5431 "RC6 %s RC6p %s RC6pp %s\n",
5432 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
5433 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
5434 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
Rodrigo Vivi58abf1d2014-10-07 07:06:50 -07005435
5436 else
Imre Deakb99d49c2016-06-29 19:13:54 +03005437 DRM_DEBUG_DRIVER("Enabling RC6 states: RC6 %s\n",
5438 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
Ben Widawskydc39fff2013-10-18 12:32:07 -07005439}
5440
Chris Wilsondc979972016-05-10 14:10:04 +01005441static bool bxt_check_bios_rc6_setup(struct drm_i915_private *dev_priv)
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305442{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005443 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305444 bool enable_rc6 = true;
5445 unsigned long rc6_ctx_base;
Imre Deakfc619842016-06-29 19:13:55 +03005446 u32 rc_ctl;
5447 int rc_sw_target;
5448
5449 rc_ctl = I915_READ(GEN6_RC_CONTROL);
5450 rc_sw_target = (I915_READ(GEN6_RC_STATE) & RC_SW_TARGET_STATE_MASK) >>
5451 RC_SW_TARGET_STATE_SHIFT;
5452 DRM_DEBUG_DRIVER("BIOS enabled RC states: "
5453 "HW_CTRL %s HW_RC6 %s SW_TARGET_STATE %x\n",
5454 onoff(rc_ctl & GEN6_RC_CTL_HW_ENABLE),
5455 onoff(rc_ctl & GEN6_RC_CTL_RC6_ENABLE),
5456 rc_sw_target);
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305457
5458 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005459 DRM_DEBUG_DRIVER("RC6 Base location not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305460 enable_rc6 = false;
5461 }
5462
5463 /*
5464 * The exact context size is not known for BXT, so assume a page size
5465 * for this check.
5466 */
5467 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03005468 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
5469 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
5470 ggtt->stolen_reserved_size))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005471 DRM_DEBUG_DRIVER("RC6 Base address not as expected.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305472 enable_rc6 = false;
5473 }
5474
5475 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
5476 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
5477 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
5478 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
Imre Deakb99d49c2016-06-29 19:13:54 +03005479 DRM_DEBUG_DRIVER("Engine Idle wait time not set properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305480 enable_rc6 = false;
5481 }
5482
Imre Deakfc619842016-06-29 19:13:55 +03005483 if (!I915_READ(GEN8_PUSHBUS_CONTROL) ||
5484 !I915_READ(GEN8_PUSHBUS_ENABLE) ||
5485 !I915_READ(GEN8_PUSHBUS_SHIFT)) {
5486 DRM_DEBUG_DRIVER("Pushbus not setup properly.\n");
5487 enable_rc6 = false;
5488 }
5489
5490 if (!I915_READ(GEN6_GFXPAUSE)) {
5491 DRM_DEBUG_DRIVER("GFX pause not setup properly.\n");
5492 enable_rc6 = false;
5493 }
5494
5495 if (!I915_READ(GEN8_MISC_CTRL0)) {
5496 DRM_DEBUG_DRIVER("GPM control not setup properly.\n");
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305497 enable_rc6 = false;
5498 }
5499
5500 return enable_rc6;
5501}
5502
Chris Wilsondc979972016-05-10 14:10:04 +01005503int sanitize_rc6_option(struct drm_i915_private *dev_priv, int enable_rc6)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005504{
Daniel Vettere7d66d82015-06-15 23:23:54 +02005505 /* No RC6 before Ironlake and code is gone for ilk. */
Chris Wilsondc979972016-05-10 14:10:04 +01005506 if (INTEL_INFO(dev_priv)->gen < 6)
Imre Deake6069ca2014-04-18 16:01:02 +03005507 return 0;
5508
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305509 if (!enable_rc6)
5510 return 0;
5511
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005512 if (IS_GEN9_LP(dev_priv) && !bxt_check_bios_rc6_setup(dev_priv)) {
Sagar Arun Kamble274008e2016-02-06 00:13:29 +05305513 DRM_INFO("RC6 disabled by BIOS\n");
5514 return 0;
5515 }
5516
Daniel Vetter456470e2012-08-08 23:35:40 +02005517 /* Respect the kernel parameter if it is set */
Imre Deake6069ca2014-04-18 16:01:02 +03005518 if (enable_rc6 >= 0) {
5519 int mask;
5520
Chris Wilsondc979972016-05-10 14:10:04 +01005521 if (HAS_RC6p(dev_priv))
Imre Deake6069ca2014-04-18 16:01:02 +03005522 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
5523 INTEL_RC6pp_ENABLE;
5524 else
5525 mask = INTEL_RC6_ENABLE;
5526
5527 if ((enable_rc6 & mask) != enable_rc6)
Imre Deakb99d49c2016-06-29 19:13:54 +03005528 DRM_DEBUG_DRIVER("Adjusting RC6 mask to %d "
5529 "(requested %d, valid %d)\n",
5530 enable_rc6 & mask, enable_rc6, mask);
Imre Deake6069ca2014-04-18 16:01:02 +03005531
5532 return enable_rc6 & mask;
5533 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005534
Chris Wilsondc979972016-05-10 14:10:04 +01005535 if (IS_IVYBRIDGE(dev_priv))
Ben Widawskycca84a12014-01-28 20:25:38 -08005536 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
Ben Widawsky8bade1a2014-01-28 20:25:39 -08005537
5538 return INTEL_RC6_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005539}
5540
Chris Wilsondc979972016-05-10 14:10:04 +01005541static void gen6_init_rps_frequencies(struct drm_i915_private *dev_priv)
Imre Deake6069ca2014-04-18 16:01:02 +03005542{
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005543 /* All of these values are in units of 50MHz */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005544
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005545 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +02005546 if (IS_GEN9_LP(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005547 u32 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005548 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
5549 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5550 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
5551 } else {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005552 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
Bob Paauwe35040562015-06-25 14:54:07 -07005553 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
5554 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
5555 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
5556 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005557 /* hw_max = RP0 until we check for overclocking */
Chris Wilson773ea9a2016-07-13 09:10:33 +01005558 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005559
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005560 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005561 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005562 IS_GEN9_BC(dev_priv)) {
Chris Wilson773ea9a2016-07-13 09:10:33 +01005563 u32 ddcc_status = 0;
5564
5565 if (sandybridge_pcode_read(dev_priv,
5566 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
5567 &ddcc_status) == 0)
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005568 dev_priv->rps.efficient_freq =
Tom O'Rourke46efa4a2015-02-10 23:06:46 -08005569 clamp_t(u8,
5570 ((ddcc_status >> 8) & 0xff),
5571 dev_priv->rps.min_freq,
5572 dev_priv->rps.max_freq);
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005573 }
5574
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005575 if (IS_GEN9_BC(dev_priv)) {
Akash Goelc5e06882015-06-29 14:50:19 +05305576 /* Store the frequency values in 16.66 MHZ units, which is
Chris Wilson773ea9a2016-07-13 09:10:33 +01005577 * the natural hardware unit for SKL
5578 */
Akash Goelc5e06882015-06-29 14:50:19 +05305579 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
5580 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
5581 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
5582 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
5583 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
5584 }
Ben Widawsky3280e8b2014-03-31 17:16:42 -07005585}
5586
Chris Wilson3a45b052016-07-13 09:10:32 +01005587static void reset_rps(struct drm_i915_private *dev_priv,
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005588 int (*set)(struct drm_i915_private *, u8))
Chris Wilson3a45b052016-07-13 09:10:32 +01005589{
5590 u8 freq = dev_priv->rps.cur_freq;
5591
5592 /* force a reset */
5593 dev_priv->rps.power = -1;
5594 dev_priv->rps.cur_freq = -1;
5595
Chris Wilson9fcee2f2017-01-26 10:19:19 +00005596 if (set(dev_priv, freq))
5597 DRM_ERROR("Failed to reset RPS to initial values\n");
Chris Wilson3a45b052016-07-13 09:10:32 +01005598}
5599
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005600/* See the Gen9_GT_PM_Programming_Guide doc for the below */
Chris Wilsondc979972016-05-10 14:10:04 +01005601static void gen9_enable_rps(struct drm_i915_private *dev_priv)
Zhe Wang20e49362014-11-04 17:07:05 +00005602{
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005603 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5604
Akash Goel0beb0592015-03-06 11:07:20 +05305605 /* Program defaults and thresholds for RPS*/
5606 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5607 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005608
Akash Goel0beb0592015-03-06 11:07:20 +05305609 /* 1 second timeout*/
5610 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
5611 GT_INTERVAL_FROM_US(dev_priv, 1000000));
5612
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005613 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005614
Akash Goel0beb0592015-03-06 11:07:20 +05305615 /* Leaning on the below call to gen6_set_rps to program/setup the
5616 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
5617 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
Chris Wilson3a45b052016-07-13 09:10:32 +01005618 reset_rps(dev_priv, gen6_set_rps);
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005619
5620 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5621}
5622
Chris Wilsondc979972016-05-10 14:10:04 +01005623static void gen9_enable_rc6(struct drm_i915_private *dev_priv)
Jesse Barnesb6fef0e2015-01-16 18:07:25 +00005624{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005625 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305626 enum intel_engine_id id;
Zhe Wang20e49362014-11-04 17:07:05 +00005627 uint32_t rc6_mask = 0;
Zhe Wang20e49362014-11-04 17:07:05 +00005628
5629 /* 1a: Software RC state - RC0 */
5630 I915_WRITE(GEN6_RC_STATE, 0);
5631
5632 /* 1b: Get forcewake during program sequence. Although the driver
5633 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005634 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005635
5636 /* 2a: Disable RC states. */
5637 I915_WRITE(GEN6_RC_CONTROL, 0);
5638
5639 /* 2b: Program RC6 thresholds.*/
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305640
5641 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
Chris Wilsondc979972016-05-10 14:10:04 +01005642 if (IS_SKYLAKE(dev_priv))
Sagar Arun Kamble63a4dec2015-09-12 10:17:53 +05305643 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
5644 else
5645 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
Zhe Wang20e49362014-11-04 17:07:05 +00005646 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5647 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305648 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005649 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305650
Dave Gordon1a3d1892016-05-13 15:36:30 +01005651 if (HAS_GUC(dev_priv))
Sagar Arun Kamble97c322e2015-09-12 10:17:54 +05305652 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
5653
Zhe Wang20e49362014-11-04 17:07:05 +00005654 I915_WRITE(GEN6_RC_SLEEP, 0);
Zhe Wang20e49362014-11-04 17:07:05 +00005655
Zhe Wang38c23522015-01-20 12:23:04 +00005656 /* 2c: Program Coarse Power Gating Policies. */
5657 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
5658 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
5659
Zhe Wang20e49362014-11-04 17:07:05 +00005660 /* 3a: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005661 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Zhe Wang20e49362014-11-04 17:07:05 +00005662 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Jani Nikula87ad3212016-01-14 12:53:34 +02005663 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
Chris Wilson1c044f92017-01-25 17:26:01 +00005664 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
5665 I915_WRITE(GEN6_RC_CONTROL,
5666 GEN6_RC_CTL_HW_ENABLE | GEN6_RC_CTL_EI_MODE(1) | rc6_mask);
Zhe Wang20e49362014-11-04 17:07:05 +00005667
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305668 /*
5669 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305670 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
Sagar Kamblecb07bae2015-04-12 11:28:14 +05305671 */
Chris Wilsondc979972016-05-10 14:10:04 +01005672 if (NEEDS_WaRsDisableCoarsePowerGating(dev_priv))
Sagar Arun Kamblef2d2fe92015-09-12 10:17:51 +05305673 I915_WRITE(GEN9_PG_ENABLE, 0);
5674 else
5675 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
5676 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
Zhe Wang38c23522015-01-20 12:23:04 +00005677
Mika Kuoppala59bad942015-01-16 11:34:40 +02005678 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Zhe Wang20e49362014-11-04 17:07:05 +00005679}
5680
Chris Wilsondc979972016-05-10 14:10:04 +01005681static void gen8_enable_rps(struct drm_i915_private *dev_priv)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005682{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005683 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305684 enum intel_engine_id id;
Tom O'Rourke93ee2922014-11-19 14:21:52 -08005685 uint32_t rc6_mask = 0;
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005686
5687 /* 1a: Software RC state - RC0 */
5688 I915_WRITE(GEN6_RC_STATE, 0);
5689
5690 /* 1c & 1d: Get forcewake during program sequence. Although the driver
5691 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02005692 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005693
5694 /* 2a: Disable RC states. */
5695 I915_WRITE(GEN6_RC_CONTROL, 0);
5696
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005697 /* 2b: Program RC6 thresholds.*/
5698 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5699 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5700 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
Akash Goel3b3f1652016-10-13 22:44:48 +05305701 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005702 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005703 I915_WRITE(GEN6_RC_SLEEP, 0);
Chris Wilsondc979972016-05-10 14:10:04 +01005704 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005705 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
5706 else
5707 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005708
5709 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005710 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005711 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
Chris Wilsondc979972016-05-10 14:10:04 +01005712 intel_print_rc6_info(dev_priv, rc6_mask);
5713 if (IS_BROADWELL(dev_priv))
Tom O'Rourke0d68b252014-04-09 11:44:06 -07005714 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5715 GEN7_RC_CTL_TO_MODE |
5716 rc6_mask);
5717 else
5718 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
5719 GEN6_RC_CTL_EI_MODE(1) |
5720 rc6_mask);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005721
5722 /* 4 Program defaults and thresholds for RPS*/
Ben Widawskyf9bdc582014-03-31 17:16:41 -07005723 I915_WRITE(GEN6_RPNSWREQ,
5724 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
5725 I915_WRITE(GEN6_RC_VIDEO_FREQ,
5726 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
Daniel Vetter7526ed72014-09-29 15:07:19 +02005727 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
5728 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005729
Daniel Vetter7526ed72014-09-29 15:07:19 +02005730 /* Docs recommend 900MHz, and 300 MHz respectively */
5731 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
5732 dev_priv->rps.max_freq_softlimit << 24 |
5733 dev_priv->rps.min_freq_softlimit << 16);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005734
Daniel Vetter7526ed72014-09-29 15:07:19 +02005735 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
5736 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
5737 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
5738 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005739
Daniel Vetter7526ed72014-09-29 15:07:19 +02005740 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005741
5742 /* 5: Enable RPS */
Daniel Vetter7526ed72014-09-29 15:07:19 +02005743 I915_WRITE(GEN6_RP_CONTROL,
5744 GEN6_RP_MEDIA_TURBO |
5745 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5746 GEN6_RP_MEDIA_IS_GFX |
5747 GEN6_RP_ENABLE |
5748 GEN6_RP_UP_BUSY_AVG |
5749 GEN6_RP_DOWN_IDLE_AVG);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005750
Daniel Vetter7526ed72014-09-29 15:07:19 +02005751 /* 6: Ring frequency + overclocking (our driver does this later */
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005752
Chris Wilson3a45b052016-07-13 09:10:32 +01005753 reset_rps(dev_priv, gen6_set_rps);
Daniel Vetter7526ed72014-09-29 15:07:19 +02005754
Mika Kuoppala59bad942015-01-16 11:34:40 +02005755 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ben Widawsky6edee7f2013-11-02 21:07:52 -07005756}
5757
Chris Wilsondc979972016-05-10 14:10:04 +01005758static void gen6_enable_rps(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005759{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005760 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05305761 enum intel_engine_id id;
Chris Wilson99ac9612016-07-13 09:10:34 +01005762 u32 rc6vids, rc6_mask = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005763 u32 gtfifodbg;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005764 int rc6_mode;
Dave Gordonb4ac5af2016-03-24 11:20:38 +00005765 int ret;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005766
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005767 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005768
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005769 /* Here begins a magic sequence of register writes to enable
5770 * auto-downclocking.
5771 *
5772 * Perhaps there might be some value in exposing these to
5773 * userspace...
5774 */
5775 I915_WRITE(GEN6_RC_STATE, 0);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005776
5777 /* Clear the DBG now so we don't confuse earlier errors */
Ville Syrjälä297b32e2016-04-13 21:09:30 +03005778 gtfifodbg = I915_READ(GTFIFODBG);
5779 if (gtfifodbg) {
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005780 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5781 I915_WRITE(GTFIFODBG, gtfifodbg);
5782 }
5783
Mika Kuoppala59bad942015-01-16 11:34:40 +02005784 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005785
5786 /* disable the counters and set deterministic thresholds */
5787 I915_WRITE(GEN6_RC_CONTROL, 0);
5788
5789 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5790 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5791 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5792 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5793 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5794
Akash Goel3b3f1652016-10-13 22:44:48 +05305795 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00005796 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005797
5798 I915_WRITE(GEN6_RC_SLEEP, 0);
5799 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
Chris Wilsondc979972016-05-10 14:10:04 +01005800 if (IS_IVYBRIDGE(dev_priv))
Stéphane Marchesin351aa562013-08-13 11:55:17 -07005801 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5802 else
5803 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
Stéphane Marchesin0920a482013-01-29 19:41:59 -08005804 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005805 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5806
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005807 /* Check if we are enabling RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01005808 rc6_mode = intel_enable_rc6();
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005809 if (rc6_mode & INTEL_RC6_ENABLE)
5810 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5811
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005812 /* We don't use those on Haswell */
Chris Wilsondc979972016-05-10 14:10:04 +01005813 if (!IS_HASWELL(dev_priv)) {
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005814 if (rc6_mode & INTEL_RC6p_ENABLE)
5815 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005816
Eugeni Dodonov5a7dc922012-07-02 11:51:05 -03005817 if (rc6_mode & INTEL_RC6pp_ENABLE)
5818 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5819 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005820
Chris Wilsondc979972016-05-10 14:10:04 +01005821 intel_print_rc6_info(dev_priv, rc6_mask);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005822
5823 I915_WRITE(GEN6_RC_CONTROL,
5824 rc6_mask |
5825 GEN6_RC_CTL_EI_MODE(1) |
5826 GEN6_RC_CTL_HW_ENABLE);
5827
Chris Wilsondd75fdc2013-09-25 17:34:57 +01005828 /* Power down if completely idle for over 50ms */
5829 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005830 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005831
Chris Wilson3a45b052016-07-13 09:10:32 +01005832 reset_rps(dev_priv, gen6_set_rps);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005833
Ben Widawsky31643d52012-09-26 10:34:01 -07005834 rc6vids = 0;
5835 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
Chris Wilsondc979972016-05-10 14:10:04 +01005836 if (IS_GEN6(dev_priv) && ret) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005837 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
Chris Wilsondc979972016-05-10 14:10:04 +01005838 } else if (IS_GEN6(dev_priv) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
Ben Widawsky31643d52012-09-26 10:34:01 -07005839 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5840 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5841 rc6vids &= 0xffff00;
5842 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5843 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5844 if (ret)
5845 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5846 }
5847
Mika Kuoppala59bad942015-01-16 11:34:40 +02005848 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005849}
5850
Chris Wilsonfb7404e2016-07-13 09:10:38 +01005851static void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005852{
5853 int min_freq = 15;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005854 unsigned int gpu_freq;
5855 unsigned int max_ia_freq, min_ring_freq;
Akash Goel4c8c7742015-06-29 14:50:20 +05305856 unsigned int max_gpu_freq, min_gpu_freq;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005857 int scaling_factor = 180;
Ben Widawskyeda79642013-10-07 17:15:48 -03005858 struct cpufreq_policy *policy;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005859
Jesse Barnes4fc688c2012-11-02 11:14:01 -07005860 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02005861
Ben Widawskyeda79642013-10-07 17:15:48 -03005862 policy = cpufreq_cpu_get(0);
5863 if (policy) {
5864 max_ia_freq = policy->cpuinfo.max_freq;
5865 cpufreq_cpu_put(policy);
5866 } else {
5867 /*
5868 * Default to measured freq if none found, PCU will ensure we
5869 * don't go over
5870 */
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005871 max_ia_freq = tsc_khz;
Ben Widawskyeda79642013-10-07 17:15:48 -03005872 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005873
5874 /* Convert from kHz to MHz */
5875 max_ia_freq /= 1000;
5876
Ben Widawsky153b4b952013-10-22 22:05:09 -07005877 min_ring_freq = I915_READ(DCLK) & 0xf;
Ben Widawskyf6aca452013-10-02 09:25:02 -07005878 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5879 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005880
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005881 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305882 /* Convert GT frequency to 50 HZ units */
5883 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5884 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5885 } else {
5886 min_gpu_freq = dev_priv->rps.min_freq;
5887 max_gpu_freq = dev_priv->rps.max_freq;
5888 }
5889
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005890 /*
5891 * For each potential GPU frequency, load a ring frequency we'd like
5892 * to use for memory access. We do this by specifying the IA frequency
5893 * the PCU should use as a reference to determine the ring frequency.
5894 */
Akash Goel4c8c7742015-06-29 14:50:20 +05305895 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5896 int diff = max_gpu_freq - gpu_freq;
Chris Wilson3ebecd02013-04-12 19:10:13 +01005897 unsigned int ia_freq = 0, ring_freq = 0;
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005898
Rodrigo Vivib976dc52017-01-23 10:32:37 -08005899 if (IS_GEN9_BC(dev_priv)) {
Akash Goel4c8c7742015-06-29 14:50:20 +05305900 /*
5901 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5902 * No floor required for ring frequency on SKL.
5903 */
5904 ring_freq = gpu_freq;
Chris Wilsondc979972016-05-10 14:10:04 +01005905 } else if (INTEL_INFO(dev_priv)->gen >= 8) {
Ben Widawsky46c764d2013-11-02 21:07:49 -07005906 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5907 ring_freq = max(min_ring_freq, gpu_freq);
Chris Wilsondc979972016-05-10 14:10:04 +01005908 } else if (IS_HASWELL(dev_priv)) {
Ben Widawskyf6aca452013-10-02 09:25:02 -07005909 ring_freq = mult_frac(gpu_freq, 5, 4);
Chris Wilson3ebecd02013-04-12 19:10:13 +01005910 ring_freq = max(min_ring_freq, ring_freq);
5911 /* leave ia_freq as the default, chosen by cpufreq */
5912 } else {
5913 /* On older processors, there is no separate ring
5914 * clock domain, so in order to boost the bandwidth
5915 * of the ring, we need to upclock the CPU (ia_freq).
5916 *
5917 * For GPU frequencies less than 750MHz,
5918 * just use the lowest ring freq.
5919 */
5920 if (gpu_freq < min_freq)
5921 ia_freq = 800;
5922 else
5923 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5924 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5925 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005926
Ben Widawsky42c05262012-09-26 10:34:00 -07005927 sandybridge_pcode_write(dev_priv,
5928 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
Chris Wilson3ebecd02013-04-12 19:10:13 +01005929 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5930 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5931 gpu_freq);
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005932 }
Eugeni Dodonov2b4e57b2012-04-18 15:29:23 -03005933}
5934
Ville Syrjälä03af2042014-06-28 02:03:53 +03005935static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
Deepak S2b6b3a02014-05-27 15:59:30 +05305936{
5937 u32 val, rp0;
5938
Jani Nikula5b5929c2015-10-07 11:17:46 +03005939 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
Deepak S2b6b3a02014-05-27 15:59:30 +05305940
Imre Deak43b67992016-08-31 19:13:02 +03005941 switch (INTEL_INFO(dev_priv)->sseu.eu_total) {
Jani Nikula5b5929c2015-10-07 11:17:46 +03005942 case 8:
5943 /* (2 * 4) config */
5944 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5945 break;
5946 case 12:
5947 /* (2 * 6) config */
5948 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5949 break;
5950 case 16:
5951 /* (2 * 8) config */
5952 default:
5953 /* Setting (2 * 8) Min RP0 for any other combination */
5954 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5955 break;
Deepak S095acd52015-01-17 11:05:59 +05305956 }
Jani Nikula5b5929c2015-10-07 11:17:46 +03005957
5958 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5959
Deepak S2b6b3a02014-05-27 15:59:30 +05305960 return rp0;
5961}
5962
5963static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5964{
5965 u32 val, rpe;
5966
5967 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5968 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5969
5970 return rpe;
5971}
5972
Deepak S7707df42014-07-12 18:46:14 +05305973static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5974{
5975 u32 val, rp1;
5976
Jani Nikula5b5929c2015-10-07 11:17:46 +03005977 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5978 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5979
Deepak S7707df42014-07-12 18:46:14 +05305980 return rp1;
5981}
5982
Deepak S96676fe2016-08-12 18:46:41 +05305983static u32 cherryview_rps_min_freq(struct drm_i915_private *dev_priv)
5984{
5985 u32 val, rpn;
5986
5987 val = vlv_punit_read(dev_priv, FB_GFX_FMIN_AT_VMIN_FUSE);
5988 rpn = ((val >> FB_GFX_FMIN_AT_VMIN_FUSE_SHIFT) &
5989 FB_GFX_FREQ_FUSE_MASK);
5990
5991 return rpn;
5992}
5993
Deepak Sf8f2b002014-07-10 13:16:21 +05305994static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5995{
5996 u32 val, rp1;
5997
5998 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5999
6000 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
6001
6002 return rp1;
6003}
6004
Ville Syrjälä03af2042014-06-28 02:03:53 +03006005static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006006{
6007 u32 val, rp0;
6008
Jani Nikula64936252013-05-22 15:36:20 +03006009 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006010
6011 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
6012 /* Clamp to max */
6013 rp0 = min_t(u32, rp0, 0xea);
6014
6015 return rp0;
6016}
6017
6018static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
6019{
6020 u32 val, rpe;
6021
Jani Nikula64936252013-05-22 15:36:20 +03006022 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006023 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
Jani Nikula64936252013-05-22 15:36:20 +03006024 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006025 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
6026
6027 return rpe;
6028}
6029
Ville Syrjälä03af2042014-06-28 02:03:53 +03006030static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006031{
Imre Deak36146032014-12-04 18:39:35 +02006032 u32 val;
6033
6034 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
6035 /*
6036 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
6037 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
6038 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
6039 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
6040 * to make sure it matches what Punit accepts.
6041 */
6042 return max_t(u32, val, 0xc0);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006043}
6044
Imre Deakae484342014-03-31 15:10:44 +03006045/* Check that the pctx buffer wasn't move under us. */
6046static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
6047{
6048 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6049
6050 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
6051 dev_priv->vlv_pctx->stolen->start);
6052}
6053
Deepak S38807742014-05-23 21:00:15 +05306054
6055/* Check that the pcbr address is not empty. */
6056static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
6057{
6058 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
6059
6060 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
6061}
6062
Chris Wilsondc979972016-05-10 14:10:04 +01006063static void cherryview_setup_pctx(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306064{
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006065 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03006066 unsigned long pctx_paddr, paddr;
Deepak S38807742014-05-23 21:00:15 +05306067 u32 pcbr;
6068 int pctx_size = 32*1024;
6069
Deepak S38807742014-05-23 21:00:15 +05306070 pcbr = I915_READ(VLV_PCBR);
6071 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006072 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
Deepak S38807742014-05-23 21:00:15 +05306073 paddr = (dev_priv->mm.stolen_base +
Joonas Lahtinen62106b42016-03-18 10:42:57 +02006074 (ggtt->stolen_size - pctx_size));
Deepak S38807742014-05-23 21:00:15 +05306075
6076 pctx_paddr = (paddr & (~4095));
6077 I915_WRITE(VLV_PCBR, pctx_paddr);
6078 }
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006079
6080 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Deepak S38807742014-05-23 21:00:15 +05306081}
6082
Chris Wilsondc979972016-05-10 14:10:04 +01006083static void valleyview_setup_pctx(struct drm_i915_private *dev_priv)
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006084{
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006085 struct drm_i915_gem_object *pctx;
6086 unsigned long pctx_paddr;
6087 u32 pcbr;
6088 int pctx_size = 24*1024;
6089
6090 pcbr = I915_READ(VLV_PCBR);
6091 if (pcbr) {
6092 /* BIOS set it up already, grab the pre-alloc'd space */
6093 int pcbr_offset;
6094
6095 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006096 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006097 pcbr_offset,
Daniel Vetter190d6cd2013-07-04 13:06:28 +02006098 I915_GTT_OFFSET_NONE,
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006099 pctx_size);
6100 goto out;
6101 }
6102
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006103 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
6104
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006105 /*
6106 * From the Gunit register HAS:
6107 * The Gfx driver is expected to program this register and ensure
6108 * proper allocation within Gfx stolen memory. For example, this
6109 * register should be programmed such than the PCBR range does not
6110 * overlap with other ranges, such as the frame buffer, protected
6111 * memory, or any other relevant ranges.
6112 */
Tvrtko Ursulin187685c2016-12-01 14:16:36 +00006113 pctx = i915_gem_object_create_stolen(dev_priv, pctx_size);
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006114 if (!pctx) {
6115 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
Tvrtko Ursulinee504892016-02-11 10:27:30 +00006116 goto out;
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006117 }
6118
6119 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
6120 I915_WRITE(VLV_PCBR, pctx_paddr);
6121
6122out:
Ville Syrjäläce611ef2014-11-07 21:33:46 +02006123 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
Jesse Barnesc9cddff2013-05-08 10:45:13 -07006124 dev_priv->vlv_pctx = pctx;
6125}
6126
Chris Wilsondc979972016-05-10 14:10:04 +01006127static void valleyview_cleanup_pctx(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006128{
Imre Deakae484342014-03-31 15:10:44 +03006129 if (WARN_ON(!dev_priv->vlv_pctx))
6130 return;
6131
Chris Wilsonf0cd5182016-10-28 13:58:43 +01006132 i915_gem_object_put(dev_priv->vlv_pctx);
Imre Deakae484342014-03-31 15:10:44 +03006133 dev_priv->vlv_pctx = NULL;
6134}
6135
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006136static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
6137{
6138 dev_priv->rps.gpll_ref_freq =
6139 vlv_get_cck_clock(dev_priv, "GPLL ref",
6140 CCK_GPLL_CLOCK_CONTROL,
6141 dev_priv->czclk_freq);
6142
6143 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
6144 dev_priv->rps.gpll_ref_freq);
6145}
6146
Chris Wilsondc979972016-05-10 14:10:04 +01006147static void valleyview_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006148{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006149 u32 val;
Imre Deak4e805192014-04-14 20:24:41 +03006150
Chris Wilsondc979972016-05-10 14:10:04 +01006151 valleyview_setup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006152
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006153 vlv_init_gpll_ref_freq(dev_priv);
6154
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006155 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6156 switch ((val >> 6) & 3) {
6157 case 0:
6158 case 1:
6159 dev_priv->mem_freq = 800;
6160 break;
6161 case 2:
6162 dev_priv->mem_freq = 1066;
6163 break;
6164 case 3:
6165 dev_priv->mem_freq = 1333;
6166 break;
6167 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006168 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006169
Imre Deak4e805192014-04-14 20:24:41 +03006170 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
6171 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6172 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006173 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006174 dev_priv->rps.max_freq);
6175
6176 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
6177 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006178 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006179 dev_priv->rps.efficient_freq);
6180
Deepak Sf8f2b002014-07-10 13:16:21 +05306181 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
6182 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006183 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak Sf8f2b002014-07-10 13:16:21 +05306184 dev_priv->rps.rp1_freq);
6185
Imre Deak4e805192014-04-14 20:24:41 +03006186 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
6187 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006188 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Imre Deak4e805192014-04-14 20:24:41 +03006189 dev_priv->rps.min_freq);
Imre Deak4e805192014-04-14 20:24:41 +03006190}
6191
Chris Wilsondc979972016-05-10 14:10:04 +01006192static void cherryview_init_gt_powersave(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306193{
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006194 u32 val;
Deepak S2b6b3a02014-05-27 15:59:30 +05306195
Chris Wilsondc979972016-05-10 14:10:04 +01006196 cherryview_setup_pctx(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306197
Ville Syrjäläc30fec62016-03-04 21:43:02 +02006198 vlv_init_gpll_ref_freq(dev_priv);
6199
Ville Syrjäläa5805162015-05-26 20:42:30 +03006200 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006201 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006202 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläc6e8f392014-11-07 21:33:43 +02006203
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006204 switch ((val >> 2) & 0x7) {
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006205 case 3:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006206 dev_priv->mem_freq = 2000;
6207 break;
Ville Syrjäläbfa7df02015-09-24 23:29:18 +03006208 default:
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006209 dev_priv->mem_freq = 1600;
6210 break;
6211 }
Ville Syrjälä80b83b62014-11-10 22:55:14 +02006212 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
Ville Syrjälä2bb25c12014-08-18 14:42:44 +03006213
Deepak S2b6b3a02014-05-27 15:59:30 +05306214 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
6215 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
6216 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006217 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306218 dev_priv->rps.max_freq);
6219
6220 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
6221 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006222 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306223 dev_priv->rps.efficient_freq);
6224
Deepak S7707df42014-07-12 18:46:14 +05306225 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
6226 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006227 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
Deepak S7707df42014-07-12 18:46:14 +05306228 dev_priv->rps.rp1_freq);
6229
Deepak S96676fe2016-08-12 18:46:41 +05306230 dev_priv->rps.min_freq = cherryview_rps_min_freq(dev_priv);
Deepak S2b6b3a02014-05-27 15:59:30 +05306231 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
Ville Syrjälä7c59a9c12015-01-23 21:04:26 +02006232 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
Deepak S2b6b3a02014-05-27 15:59:30 +05306233 dev_priv->rps.min_freq);
6234
Ville Syrjälä1c147622014-08-18 14:42:43 +03006235 WARN_ONCE((dev_priv->rps.max_freq |
6236 dev_priv->rps.efficient_freq |
6237 dev_priv->rps.rp1_freq |
6238 dev_priv->rps.min_freq) & 1,
6239 "Odd GPU freq values\n");
Deepak S38807742014-05-23 21:00:15 +05306240}
6241
Chris Wilsondc979972016-05-10 14:10:04 +01006242static void valleyview_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deak4e805192014-04-14 20:24:41 +03006243{
Chris Wilsondc979972016-05-10 14:10:04 +01006244 valleyview_cleanup_pctx(dev_priv);
Imre Deak4e805192014-04-14 20:24:41 +03006245}
6246
Chris Wilsondc979972016-05-10 14:10:04 +01006247static void cherryview_enable_rps(struct drm_i915_private *dev_priv)
Deepak S38807742014-05-23 21:00:15 +05306248{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006249 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306250 enum intel_engine_id id;
Deepak S2b6b3a02014-05-27 15:59:30 +05306251 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
Deepak S38807742014-05-23 21:00:15 +05306252
6253 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6254
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006255 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
6256 GT_FIFO_FREE_ENTRIES_CHV);
Deepak S38807742014-05-23 21:00:15 +05306257 if (gtfifodbg) {
6258 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6259 gtfifodbg);
6260 I915_WRITE(GTFIFODBG, gtfifodbg);
6261 }
6262
6263 cherryview_check_pctx(dev_priv);
6264
6265 /* 1a & 1b: Get forcewake during program sequence. Although the driver
6266 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
Mika Kuoppala59bad942015-01-16 11:34:40 +02006267 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306268
Ville Syrjälä160614a2015-01-19 13:50:47 +02006269 /* Disable RC states. */
6270 I915_WRITE(GEN6_RC_CONTROL, 0);
6271
Deepak S38807742014-05-23 21:00:15 +05306272 /* 2a: Program RC6 thresholds.*/
6273 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
6274 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
6275 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
6276
Akash Goel3b3f1652016-10-13 22:44:48 +05306277 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006278 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Deepak S38807742014-05-23 21:00:15 +05306279 I915_WRITE(GEN6_RC_SLEEP, 0);
6280
Deepak Sf4f71c72015-03-28 15:23:35 +05306281 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
6282 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
Deepak S38807742014-05-23 21:00:15 +05306283
6284 /* allows RC6 residency counter to work */
6285 I915_WRITE(VLV_COUNTER_CONTROL,
6286 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
6287 VLV_MEDIA_RC6_COUNT_EN |
6288 VLV_RENDER_RC6_COUNT_EN));
6289
6290 /* For now we assume BIOS is allocating and populating the PCBR */
6291 pcbr = I915_READ(VLV_PCBR);
6292
Deepak S38807742014-05-23 21:00:15 +05306293 /* 3: Enable RC6 */
Chris Wilsondc979972016-05-10 14:10:04 +01006294 if ((intel_enable_rc6() & INTEL_RC6_ENABLE) &&
6295 (pcbr >> VLV_PCBR_ADDR_SHIFT))
Ville Syrjäläaf5a75a2015-01-19 13:50:50 +02006296 rc6_mode = GEN7_RC_CTL_TO_MODE;
Deepak S38807742014-05-23 21:00:15 +05306297
6298 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
6299
Deepak S2b6b3a02014-05-27 15:59:30 +05306300 /* 4 Program defaults and thresholds for RPS*/
Ville Syrjälä3cbdb482015-01-19 13:50:49 +02006301 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Deepak S2b6b3a02014-05-27 15:59:30 +05306302 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6303 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6304 I915_WRITE(GEN6_RP_UP_EI, 66000);
6305 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6306
6307 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6308
6309 /* 5: Enable RPS */
6310 I915_WRITE(GEN6_RP_CONTROL,
6311 GEN6_RP_MEDIA_HW_NORMAL_MODE |
Ville Syrjäläeb973a52015-01-21 19:37:59 +02006312 GEN6_RP_MEDIA_IS_GFX |
Deepak S2b6b3a02014-05-27 15:59:30 +05306313 GEN6_RP_ENABLE |
6314 GEN6_RP_UP_BUSY_AVG |
6315 GEN6_RP_DOWN_IDLE_AVG);
6316
Deepak S3ef62342015-04-29 08:36:24 +05306317 /* Setting Fixed Bias */
6318 val = VLV_OVERRIDE_EN |
6319 VLV_SOC_TDP_EN |
6320 CHV_BIAS_CPU_50_SOC_50;
6321 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6322
Deepak S2b6b3a02014-05-27 15:59:30 +05306323 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
6324
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006325 /* RPS code assumes GPLL is used */
6326 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6327
Jani Nikula742f4912015-09-03 11:16:09 +03006328 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Deepak S2b6b3a02014-05-27 15:59:30 +05306329 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6330
Chris Wilson3a45b052016-07-13 09:10:32 +01006331 reset_rps(dev_priv, valleyview_set_rps);
Deepak S2b6b3a02014-05-27 15:59:30 +05306332
Mika Kuoppala59bad942015-01-16 11:34:40 +02006333 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Deepak S38807742014-05-23 21:00:15 +05306334}
6335
Chris Wilsondc979972016-05-10 14:10:04 +01006336static void valleyview_enable_rps(struct drm_i915_private *dev_priv)
Jesse Barnes0a073b82013-04-17 15:54:58 -07006337{
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006338 struct intel_engine_cs *engine;
Akash Goel3b3f1652016-10-13 22:44:48 +05306339 enum intel_engine_id id;
Ben Widawsky2a5913a2014-03-19 18:31:13 -07006340 u32 gtfifodbg, val, rc6_mode = 0;
Jesse Barnes0a073b82013-04-17 15:54:58 -07006341
6342 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
6343
Imre Deakae484342014-03-31 15:10:44 +03006344 valleyview_check_pctx(dev_priv);
6345
Ville Syrjälä297b32e2016-04-13 21:09:30 +03006346 gtfifodbg = I915_READ(GTFIFODBG);
6347 if (gtfifodbg) {
Jesse Barnesf7d85c12013-09-27 10:40:54 -07006348 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
6349 gtfifodbg);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006350 I915_WRITE(GTFIFODBG, gtfifodbg);
6351 }
6352
Deepak Sc8d9a592013-11-23 14:55:42 +05306353 /* If VLV, Forcewake all wells, else re-direct to regular path */
Mika Kuoppala59bad942015-01-16 11:34:40 +02006354 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006355
Ville Syrjälä160614a2015-01-19 13:50:47 +02006356 /* Disable RC states. */
6357 I915_WRITE(GEN6_RC_CONTROL, 0);
6358
Ville Syrjäläcad725f2015-01-19 13:50:48 +02006359 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006360 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
6361 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
6362 I915_WRITE(GEN6_RP_UP_EI, 66000);
6363 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
6364
6365 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6366
6367 I915_WRITE(GEN6_RP_CONTROL,
6368 GEN6_RP_MEDIA_TURBO |
6369 GEN6_RP_MEDIA_HW_NORMAL_MODE |
6370 GEN6_RP_MEDIA_IS_GFX |
6371 GEN6_RP_ENABLE |
6372 GEN6_RP_UP_BUSY_AVG |
6373 GEN6_RP_DOWN_IDLE_CONT);
6374
6375 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
6376 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
6377 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
6378
Akash Goel3b3f1652016-10-13 22:44:48 +05306379 for_each_engine(engine, dev_priv, id)
Tvrtko Ursuline2f80392016-03-16 11:00:36 +00006380 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006381
Jesse Barnes2f0aa3042013-11-15 09:32:11 -08006382 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006383
6384 /* allows RC6 residency counter to work */
Jesse Barnes49798eb2013-09-26 17:55:57 -07006385 I915_WRITE(VLV_COUNTER_CONTROL,
Deepak S31685c22014-07-03 17:33:01 -04006386 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
6387 VLV_RENDER_RC0_COUNT_EN |
Jesse Barnes49798eb2013-09-26 17:55:57 -07006388 VLV_MEDIA_RC6_COUNT_EN |
6389 VLV_RENDER_RC6_COUNT_EN));
Deepak S31685c22014-07-03 17:33:01 -04006390
Chris Wilsondc979972016-05-10 14:10:04 +01006391 if (intel_enable_rc6() & INTEL_RC6_ENABLE)
Jesse Barnes6b88f292013-11-15 09:32:12 -08006392 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
Ben Widawskydc39fff2013-10-18 12:32:07 -07006393
Chris Wilsondc979972016-05-10 14:10:04 +01006394 intel_print_rc6_info(dev_priv, rc6_mode);
Ben Widawskydc39fff2013-10-18 12:32:07 -07006395
Jesse Barnesa2b23fe2013-09-19 09:33:13 -07006396 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006397
Deepak S3ef62342015-04-29 08:36:24 +05306398 /* Setting Fixed Bias */
6399 val = VLV_OVERRIDE_EN |
6400 VLV_SOC_TDP_EN |
6401 VLV_BIAS_CPU_125_SOC_875;
6402 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
6403
Jani Nikula64936252013-05-22 15:36:20 +03006404 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006405
Ville Syrjälä8d40c3a2014-11-07 21:33:45 +02006406 /* RPS code assumes GPLL is used */
6407 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
6408
Jani Nikula742f4912015-09-03 11:16:09 +03006409 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
Jesse Barnes0a073b82013-04-17 15:54:58 -07006410 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
6411
Chris Wilson3a45b052016-07-13 09:10:32 +01006412 reset_rps(dev_priv, valleyview_set_rps);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006413
Mika Kuoppala59bad942015-01-16 11:34:40 +02006414 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006415}
6416
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006417static unsigned long intel_pxfreq(u32 vidfreq)
6418{
6419 unsigned long freq;
6420 int div = (vidfreq & 0x3f0000) >> 16;
6421 int post = (vidfreq & 0x3000) >> 12;
6422 int pre = (vidfreq & 0x7);
6423
6424 if (!pre)
6425 return 0;
6426
6427 freq = ((div * 133333) / ((1<<post) * pre));
6428
6429 return freq;
6430}
6431
Daniel Vettereb48eb02012-04-26 23:28:12 +02006432static const struct cparams {
6433 u16 i;
6434 u16 t;
6435 u16 m;
6436 u16 c;
6437} cparams[] = {
6438 { 1, 1333, 301, 28664 },
6439 { 1, 1066, 294, 24460 },
6440 { 1, 800, 294, 25192 },
6441 { 0, 1333, 276, 27605 },
6442 { 0, 1066, 276, 27605 },
6443 { 0, 800, 231, 23784 },
6444};
6445
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006446static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006447{
6448 u64 total_count, diff, ret;
6449 u32 count1, count2, count3, m = 0, c = 0;
6450 unsigned long now = jiffies_to_msecs(jiffies), diff1;
6451 int i;
6452
Chris Wilson67520412017-03-02 13:28:01 +00006453 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006454
Daniel Vetter20e4d402012-08-08 23:35:39 +02006455 diff1 = now - dev_priv->ips.last_time1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006456
6457 /* Prevent division-by-zero if we are asking too fast.
6458 * Also, we don't get interesting results if we are polling
6459 * faster than once in 10ms, so just return the saved value
6460 * in such cases.
6461 */
6462 if (diff1 <= 10)
Daniel Vetter20e4d402012-08-08 23:35:39 +02006463 return dev_priv->ips.chipset_power;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006464
6465 count1 = I915_READ(DMIEC);
6466 count2 = I915_READ(DDREC);
6467 count3 = I915_READ(CSIEC);
6468
6469 total_count = count1 + count2 + count3;
6470
6471 /* FIXME: handle per-counter overflow */
Daniel Vetter20e4d402012-08-08 23:35:39 +02006472 if (total_count < dev_priv->ips.last_count1) {
6473 diff = ~0UL - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006474 diff += total_count;
6475 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006476 diff = total_count - dev_priv->ips.last_count1;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006477 }
6478
6479 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006480 if (cparams[i].i == dev_priv->ips.c_m &&
6481 cparams[i].t == dev_priv->ips.r_t) {
Daniel Vettereb48eb02012-04-26 23:28:12 +02006482 m = cparams[i].m;
6483 c = cparams[i].c;
6484 break;
6485 }
6486 }
6487
6488 diff = div_u64(diff, diff1);
6489 ret = ((m * diff) + c);
6490 ret = div_u64(ret, 10);
6491
Daniel Vetter20e4d402012-08-08 23:35:39 +02006492 dev_priv->ips.last_count1 = total_count;
6493 dev_priv->ips.last_time1 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006494
Daniel Vetter20e4d402012-08-08 23:35:39 +02006495 dev_priv->ips.chipset_power = ret;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006496
6497 return ret;
6498}
6499
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006500unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
6501{
6502 unsigned long val;
6503
Chris Wilsondc979972016-05-10 14:10:04 +01006504 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006505 return 0;
6506
6507 spin_lock_irq(&mchdev_lock);
6508
6509 val = __i915_chipset_val(dev_priv);
6510
6511 spin_unlock_irq(&mchdev_lock);
6512
6513 return val;
6514}
6515
Daniel Vettereb48eb02012-04-26 23:28:12 +02006516unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
6517{
6518 unsigned long m, x, b;
6519 u32 tsfs;
6520
6521 tsfs = I915_READ(TSFS);
6522
6523 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
6524 x = I915_READ8(TR1);
6525
6526 b = tsfs & TSFS_INTR_MASK;
6527
6528 return ((m * x) / 127) - b;
6529}
6530
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006531static int _pxvid_to_vd(u8 pxvid)
6532{
6533 if (pxvid == 0)
6534 return 0;
6535
6536 if (pxvid >= 8 && pxvid < 31)
6537 pxvid = 31;
6538
6539 return (pxvid + 2) * 125;
6540}
6541
6542static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006543{
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006544 const int vd = _pxvid_to_vd(pxvid);
6545 const int vm = vd - 1125;
6546
Chris Wilsondc979972016-05-10 14:10:04 +01006547 if (INTEL_INFO(dev_priv)->is_mobile)
Mika Kuoppalad972d6e2014-12-01 18:01:05 +02006548 return vm > 0 ? vm : 0;
6549
6550 return vd;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006551}
6552
Daniel Vetter02d71952012-08-09 16:44:54 +02006553static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006554{
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006555 u64 now, diff, diffms;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006556 u32 count;
6557
Chris Wilson67520412017-03-02 13:28:01 +00006558 lockdep_assert_held(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006559
Thomas Gleixner5ed0bdf2014-07-16 21:05:06 +00006560 now = ktime_get_raw_ns();
6561 diffms = now - dev_priv->ips.last_time2;
6562 do_div(diffms, NSEC_PER_MSEC);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006563
6564 /* Don't divide by 0 */
Daniel Vettereb48eb02012-04-26 23:28:12 +02006565 if (!diffms)
6566 return;
6567
6568 count = I915_READ(GFXEC);
6569
Daniel Vetter20e4d402012-08-08 23:35:39 +02006570 if (count < dev_priv->ips.last_count2) {
6571 diff = ~0UL - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006572 diff += count;
6573 } else {
Daniel Vetter20e4d402012-08-08 23:35:39 +02006574 diff = count - dev_priv->ips.last_count2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006575 }
6576
Daniel Vetter20e4d402012-08-08 23:35:39 +02006577 dev_priv->ips.last_count2 = count;
6578 dev_priv->ips.last_time2 = now;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006579
6580 /* More magic constants... */
6581 diff = diff * 1181;
6582 diff = div_u64(diff, diffms * 10);
Daniel Vetter20e4d402012-08-08 23:35:39 +02006583 dev_priv->ips.gfx_power = diff;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006584}
6585
Daniel Vetter02d71952012-08-09 16:44:54 +02006586void i915_update_gfx_val(struct drm_i915_private *dev_priv)
6587{
Chris Wilsondc979972016-05-10 14:10:04 +01006588 if (INTEL_INFO(dev_priv)->gen != 5)
Daniel Vetter02d71952012-08-09 16:44:54 +02006589 return;
6590
Daniel Vetter92703882012-08-09 16:46:01 +02006591 spin_lock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006592
6593 __i915_update_gfx_val(dev_priv);
6594
Daniel Vetter92703882012-08-09 16:46:01 +02006595 spin_unlock_irq(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006596}
6597
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006598static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
Daniel Vettereb48eb02012-04-26 23:28:12 +02006599{
6600 unsigned long t, corr, state1, corr2, state2;
6601 u32 pxvid, ext_v;
6602
Chris Wilson67520412017-03-02 13:28:01 +00006603 lockdep_assert_held(&mchdev_lock);
Daniel Vetter02d71952012-08-09 16:44:54 +02006604
Ville Syrjälä616847e2015-09-18 20:03:19 +03006605 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
Daniel Vettereb48eb02012-04-26 23:28:12 +02006606 pxvid = (pxvid >> 24) & 0x7f;
6607 ext_v = pvid_to_extvid(dev_priv, pxvid);
6608
6609 state1 = ext_v;
6610
6611 t = i915_mch_val(dev_priv);
6612
6613 /* Revel in the empirically derived constants */
6614
6615 /* Correction factor in 1/100000 units */
6616 if (t > 80)
6617 corr = ((t * 2349) + 135940);
6618 else if (t >= 50)
6619 corr = ((t * 964) + 29317);
6620 else /* < 50 */
6621 corr = ((t * 301) + 1004);
6622
6623 corr = corr * ((150142 * state1) / 10000 - 78642);
6624 corr /= 100000;
Daniel Vetter20e4d402012-08-08 23:35:39 +02006625 corr2 = (corr * dev_priv->ips.corr);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006626
6627 state2 = (corr2 * state1) / 10000;
6628 state2 /= 100; /* convert to mW */
6629
Daniel Vetter02d71952012-08-09 16:44:54 +02006630 __i915_update_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006631
Daniel Vetter20e4d402012-08-08 23:35:39 +02006632 return dev_priv->ips.gfx_power + state2;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006633}
6634
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006635unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
6636{
6637 unsigned long val;
6638
Chris Wilsondc979972016-05-10 14:10:04 +01006639 if (INTEL_INFO(dev_priv)->gen != 5)
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006640 return 0;
6641
6642 spin_lock_irq(&mchdev_lock);
6643
6644 val = __i915_gfx_val(dev_priv);
6645
6646 spin_unlock_irq(&mchdev_lock);
6647
6648 return val;
6649}
6650
Daniel Vettereb48eb02012-04-26 23:28:12 +02006651/**
6652 * i915_read_mch_val - return value for IPS use
6653 *
6654 * Calculate and return a value for the IPS driver to use when deciding whether
6655 * we have thermal and power headroom to increase CPU or GPU power budget.
6656 */
6657unsigned long i915_read_mch_val(void)
6658{
6659 struct drm_i915_private *dev_priv;
6660 unsigned long chipset_val, graphics_val, ret = 0;
6661
Daniel Vetter92703882012-08-09 16:46:01 +02006662 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006663 if (!i915_mch_dev)
6664 goto out_unlock;
6665 dev_priv = i915_mch_dev;
6666
Chris Wilsonf531dcb2012-09-25 10:16:12 +01006667 chipset_val = __i915_chipset_val(dev_priv);
6668 graphics_val = __i915_gfx_val(dev_priv);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006669
6670 ret = chipset_val + graphics_val;
6671
6672out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006673 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006674
6675 return ret;
6676}
6677EXPORT_SYMBOL_GPL(i915_read_mch_val);
6678
6679/**
6680 * i915_gpu_raise - raise GPU frequency limit
6681 *
6682 * Raise the limit; IPS indicates we have thermal headroom.
6683 */
6684bool i915_gpu_raise(void)
6685{
6686 struct drm_i915_private *dev_priv;
6687 bool ret = true;
6688
Daniel Vetter92703882012-08-09 16:46:01 +02006689 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006690 if (!i915_mch_dev) {
6691 ret = false;
6692 goto out_unlock;
6693 }
6694 dev_priv = i915_mch_dev;
6695
Daniel Vetter20e4d402012-08-08 23:35:39 +02006696 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6697 dev_priv->ips.max_delay--;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006698
6699out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006700 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006701
6702 return ret;
6703}
6704EXPORT_SYMBOL_GPL(i915_gpu_raise);
6705
6706/**
6707 * i915_gpu_lower - lower GPU frequency limit
6708 *
6709 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6710 * frequency maximum.
6711 */
6712bool i915_gpu_lower(void)
6713{
6714 struct drm_i915_private *dev_priv;
6715 bool ret = true;
6716
Daniel Vetter92703882012-08-09 16:46:01 +02006717 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006718 if (!i915_mch_dev) {
6719 ret = false;
6720 goto out_unlock;
6721 }
6722 dev_priv = i915_mch_dev;
6723
Daniel Vetter20e4d402012-08-08 23:35:39 +02006724 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6725 dev_priv->ips.max_delay++;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006726
6727out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006728 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006729
6730 return ret;
6731}
6732EXPORT_SYMBOL_GPL(i915_gpu_lower);
6733
6734/**
6735 * i915_gpu_busy - indicate GPU business to IPS
6736 *
6737 * Tell the IPS driver whether or not the GPU is busy.
6738 */
6739bool i915_gpu_busy(void)
6740{
Daniel Vettereb48eb02012-04-26 23:28:12 +02006741 bool ret = false;
6742
Daniel Vetter92703882012-08-09 16:46:01 +02006743 spin_lock_irq(&mchdev_lock);
Chris Wilsondcff85c2016-08-05 10:14:11 +01006744 if (i915_mch_dev)
6745 ret = i915_mch_dev->gt.awake;
Daniel Vetter92703882012-08-09 16:46:01 +02006746 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006747
6748 return ret;
6749}
6750EXPORT_SYMBOL_GPL(i915_gpu_busy);
6751
6752/**
6753 * i915_gpu_turbo_disable - disable graphics turbo
6754 *
6755 * Disable graphics turbo by resetting the max frequency and setting the
6756 * current frequency to the default.
6757 */
6758bool i915_gpu_turbo_disable(void)
6759{
6760 struct drm_i915_private *dev_priv;
6761 bool ret = true;
6762
Daniel Vetter92703882012-08-09 16:46:01 +02006763 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006764 if (!i915_mch_dev) {
6765 ret = false;
6766 goto out_unlock;
6767 }
6768 dev_priv = i915_mch_dev;
6769
Daniel Vetter20e4d402012-08-08 23:35:39 +02006770 dev_priv->ips.max_delay = dev_priv->ips.fstart;
Daniel Vettereb48eb02012-04-26 23:28:12 +02006771
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01006772 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
Daniel Vettereb48eb02012-04-26 23:28:12 +02006773 ret = false;
6774
6775out_unlock:
Daniel Vetter92703882012-08-09 16:46:01 +02006776 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006777
6778 return ret;
6779}
6780EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6781
6782/**
6783 * Tells the intel_ips driver that the i915 driver is now loaded, if
6784 * IPS got loaded first.
6785 *
6786 * This awkward dance is so that neither module has to depend on the
6787 * other in order for IPS to do the appropriate communication of
6788 * GPU turbo limits to i915.
6789 */
6790static void
6791ips_ping_for_i915_load(void)
6792{
6793 void (*link)(void);
6794
6795 link = symbol_get(ips_link_to_i915_driver);
6796 if (link) {
6797 link();
6798 symbol_put(ips_link_to_i915_driver);
6799 }
6800}
6801
6802void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6803{
Daniel Vetter02d71952012-08-09 16:44:54 +02006804 /* We only register the i915 ips part with intel-ips once everything is
6805 * set up, to avoid intel-ips sneaking in and reading bogus values. */
Daniel Vetter92703882012-08-09 16:46:01 +02006806 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006807 i915_mch_dev = dev_priv;
Daniel Vetter92703882012-08-09 16:46:01 +02006808 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006809
6810 ips_ping_for_i915_load();
6811}
6812
6813void intel_gpu_ips_teardown(void)
6814{
Daniel Vetter92703882012-08-09 16:46:01 +02006815 spin_lock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006816 i915_mch_dev = NULL;
Daniel Vetter92703882012-08-09 16:46:01 +02006817 spin_unlock_irq(&mchdev_lock);
Daniel Vettereb48eb02012-04-26 23:28:12 +02006818}
Deepak S76c3552f2014-01-30 23:08:16 +05306819
Chris Wilsondc979972016-05-10 14:10:04 +01006820static void intel_init_emon(struct drm_i915_private *dev_priv)
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006821{
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006822 u32 lcfuse;
6823 u8 pxw[16];
6824 int i;
6825
6826 /* Disable to program */
6827 I915_WRITE(ECR, 0);
6828 POSTING_READ(ECR);
6829
6830 /* Program energy weights for various events */
6831 I915_WRITE(SDEW, 0x15040d00);
6832 I915_WRITE(CSIEW0, 0x007f0000);
6833 I915_WRITE(CSIEW1, 0x1e220004);
6834 I915_WRITE(CSIEW2, 0x04000004);
6835
6836 for (i = 0; i < 5; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006837 I915_WRITE(PEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006838 for (i = 0; i < 3; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006839 I915_WRITE(DEW(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006840
6841 /* Program P-state weights to account for frequency power adjustment */
6842 for (i = 0; i < 16; i++) {
Ville Syrjälä616847e2015-09-18 20:03:19 +03006843 u32 pxvidfreq = I915_READ(PXVFREQ(i));
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006844 unsigned long freq = intel_pxfreq(pxvidfreq);
6845 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6846 PXVFREQ_PX_SHIFT;
6847 unsigned long val;
6848
6849 val = vid * vid;
6850 val *= (freq / 1000);
6851 val *= 255;
6852 val /= (127*127*900);
6853 if (val > 0xff)
6854 DRM_ERROR("bad pxval: %ld\n", val);
6855 pxw[i] = val;
6856 }
6857 /* Render standby states get 0 weight */
6858 pxw[14] = 0;
6859 pxw[15] = 0;
6860
6861 for (i = 0; i < 4; i++) {
6862 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6863 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
Ville Syrjälä616847e2015-09-18 20:03:19 +03006864 I915_WRITE(PXW(i), val);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006865 }
6866
6867 /* Adjust magic regs to magic values (more experimental results) */
6868 I915_WRITE(OGW0, 0);
6869 I915_WRITE(OGW1, 0);
6870 I915_WRITE(EG0, 0x00007f00);
6871 I915_WRITE(EG1, 0x0000000e);
6872 I915_WRITE(EG2, 0x000e0000);
6873 I915_WRITE(EG3, 0x68000300);
6874 I915_WRITE(EG4, 0x42000000);
6875 I915_WRITE(EG5, 0x00140031);
6876 I915_WRITE(EG6, 0);
6877 I915_WRITE(EG7, 0);
6878
6879 for (i = 0; i < 8; i++)
Ville Syrjälä616847e2015-09-18 20:03:19 +03006880 I915_WRITE(PXWL(i), 0);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006881
6882 /* Enable PMON + select events */
6883 I915_WRITE(ECR, 0x80000019);
6884
6885 lcfuse = I915_READ(LCFUSE02);
6886
Daniel Vetter20e4d402012-08-08 23:35:39 +02006887 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
Eugeni Dodonovdde18882012-04-18 15:29:24 -03006888}
6889
Chris Wilsondc979972016-05-10 14:10:04 +01006890void intel_init_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006891{
Imre Deakb268c692015-12-15 20:10:31 +02006892 /*
6893 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6894 * requirement.
6895 */
6896 if (!i915.enable_rc6) {
6897 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6898 intel_runtime_pm_get(dev_priv);
6899 }
Imre Deake6069ca2014-04-18 16:01:02 +03006900
Chris Wilsonb5163db2016-08-10 13:58:24 +01006901 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson773ea9a2016-07-13 09:10:33 +01006902 mutex_lock(&dev_priv->rps.hw_lock);
6903
6904 /* Initialize RPS limits (for userspace) */
Chris Wilsondc979972016-05-10 14:10:04 +01006905 if (IS_CHERRYVIEW(dev_priv))
6906 cherryview_init_gt_powersave(dev_priv);
6907 else if (IS_VALLEYVIEW(dev_priv))
6908 valleyview_init_gt_powersave(dev_priv);
Chris Wilson2a13ae72016-08-02 11:15:27 +01006909 else if (INTEL_GEN(dev_priv) >= 6)
Chris Wilson773ea9a2016-07-13 09:10:33 +01006910 gen6_init_rps_frequencies(dev_priv);
6911
6912 /* Derive initial user preferences/limits from the hardware limits */
6913 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
6914 dev_priv->rps.cur_freq = dev_priv->rps.idle_freq;
6915
6916 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
6917 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
6918
6919 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
6920 dev_priv->rps.min_freq_softlimit =
6921 max_t(int,
6922 dev_priv->rps.efficient_freq,
6923 intel_freq_opcode(dev_priv, 450));
6924
Chris Wilson99ac9612016-07-13 09:10:34 +01006925 /* After setting max-softlimit, find the overclock max freq */
6926 if (IS_GEN6(dev_priv) ||
6927 IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
6928 u32 params = 0;
6929
6930 sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &params);
6931 if (params & BIT(31)) { /* OC supported */
6932 DRM_DEBUG_DRIVER("Overclocking supported, max: %dMHz, overclock: %dMHz\n",
6933 (dev_priv->rps.max_freq & 0xff) * 50,
6934 (params & 0xff) * 50);
6935 dev_priv->rps.max_freq = params & 0xff;
6936 }
6937 }
6938
Chris Wilson29ecd78d2016-07-13 09:10:35 +01006939 /* Finally allow us to boost to max by default */
6940 dev_priv->rps.boost_freq = dev_priv->rps.max_freq;
6941
Chris Wilson773ea9a2016-07-13 09:10:33 +01006942 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb5163db2016-08-10 13:58:24 +01006943 mutex_unlock(&dev_priv->drm.struct_mutex);
Chris Wilson54b4f682016-07-21 21:16:19 +01006944
6945 intel_autoenable_gt_powersave(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006946}
6947
Chris Wilsondc979972016-05-10 14:10:04 +01006948void intel_cleanup_gt_powersave(struct drm_i915_private *dev_priv)
Imre Deakae484342014-03-31 15:10:44 +03006949{
Ville Syrjälä8dac1e12016-08-02 14:07:33 +03006950 if (IS_VALLEYVIEW(dev_priv))
Chris Wilsondc979972016-05-10 14:10:04 +01006951 valleyview_cleanup_gt_powersave(dev_priv);
Imre Deakb268c692015-12-15 20:10:31 +02006952
6953 if (!i915.enable_rc6)
6954 intel_runtime_pm_put(dev_priv);
Imre Deakae484342014-03-31 15:10:44 +03006955}
6956
Chris Wilson54b4f682016-07-21 21:16:19 +01006957/**
6958 * intel_suspend_gt_powersave - suspend PM work and helper threads
6959 * @dev_priv: i915 device
6960 *
6961 * We don't want to disable RC6 or other features here, we just want
6962 * to make sure any work we've queued has finished and won't bother
6963 * us while we're suspended.
6964 */
6965void intel_suspend_gt_powersave(struct drm_i915_private *dev_priv)
6966{
6967 if (INTEL_GEN(dev_priv) < 6)
6968 return;
6969
6970 if (cancel_delayed_work_sync(&dev_priv->rps.autoenable_work))
6971 intel_runtime_pm_put(dev_priv);
6972
6973 /* gen6_rps_idle() will be called later to disable interrupts */
6974}
6975
Chris Wilsonb7137e02016-07-13 09:10:37 +01006976void intel_sanitize_gt_powersave(struct drm_i915_private *dev_priv)
6977{
6978 dev_priv->rps.enabled = true; /* force disabling */
6979 intel_disable_gt_powersave(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01006980
6981 gen6_reset_rps_interrupts(dev_priv);
Jesse Barnes156c7ca2014-06-12 08:35:45 -07006982}
6983
Chris Wilsondc979972016-05-10 14:10:04 +01006984void intel_disable_gt_powersave(struct drm_i915_private *dev_priv)
Daniel Vetter8090c6b2012-06-24 16:42:32 +02006985{
Chris Wilsonb7137e02016-07-13 09:10:37 +01006986 if (!READ_ONCE(dev_priv->rps.enabled))
6987 return;
Jesse Barnes1a01ab32012-11-02 11:14:00 -07006988
Jesse Barnes4fc688c2012-11-02 11:14:01 -07006989 mutex_lock(&dev_priv->rps.hw_lock);
Jesse Barnes0a073b82013-04-17 15:54:58 -07006990
Chris Wilsonb7137e02016-07-13 09:10:37 +01006991 if (INTEL_GEN(dev_priv) >= 9) {
6992 gen9_disable_rc6(dev_priv);
6993 gen9_disable_rps(dev_priv);
6994 } else if (IS_CHERRYVIEW(dev_priv)) {
6995 cherryview_disable_rps(dev_priv);
6996 } else if (IS_VALLEYVIEW(dev_priv)) {
6997 valleyview_disable_rps(dev_priv);
6998 } else if (INTEL_GEN(dev_priv) >= 6) {
6999 gen6_disable_rps(dev_priv);
7000 } else if (IS_IRONLAKE_M(dev_priv)) {
7001 ironlake_disable_drps(dev_priv);
7002 }
7003
7004 dev_priv->rps.enabled = false;
7005 mutex_unlock(&dev_priv->rps.hw_lock);
7006}
7007
7008void intel_enable_gt_powersave(struct drm_i915_private *dev_priv)
7009{
Chris Wilson54b4f682016-07-21 21:16:19 +01007010 /* We shouldn't be disabling as we submit, so this should be less
7011 * racy than it appears!
7012 */
Chris Wilsonb7137e02016-07-13 09:10:37 +01007013 if (READ_ONCE(dev_priv->rps.enabled))
7014 return;
7015
7016 /* Powersaving is controlled by the host when inside a VM */
7017 if (intel_vgpu_active(dev_priv))
7018 return;
7019
7020 mutex_lock(&dev_priv->rps.hw_lock);
Imre Deak3cc134e2014-11-19 15:30:03 +02007021
Chris Wilsondc979972016-05-10 14:10:04 +01007022 if (IS_CHERRYVIEW(dev_priv)) {
7023 cherryview_enable_rps(dev_priv);
7024 } else if (IS_VALLEYVIEW(dev_priv)) {
7025 valleyview_enable_rps(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007026 } else if (INTEL_GEN(dev_priv) >= 9) {
Chris Wilsondc979972016-05-10 14:10:04 +01007027 gen9_enable_rc6(dev_priv);
7028 gen9_enable_rps(dev_priv);
Rodrigo Vivib976dc52017-01-23 10:32:37 -08007029 if (IS_GEN9_BC(dev_priv))
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007030 gen6_update_ring_freq(dev_priv);
Chris Wilsondc979972016-05-10 14:10:04 +01007031 } else if (IS_BROADWELL(dev_priv)) {
7032 gen8_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007033 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007034 } else if (INTEL_GEN(dev_priv) >= 6) {
Chris Wilsondc979972016-05-10 14:10:04 +01007035 gen6_enable_rps(dev_priv);
Chris Wilsonfb7404e2016-07-13 09:10:38 +01007036 gen6_update_ring_freq(dev_priv);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007037 } else if (IS_IRONLAKE_M(dev_priv)) {
7038 ironlake_enable_drps(dev_priv);
7039 intel_init_emon(dev_priv);
Jesse Barnes0a073b82013-04-17 15:54:58 -07007040 }
Chris Wilsonaed242f2015-03-18 09:48:21 +00007041
7042 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
7043 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
7044
7045 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
7046 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
7047
Chris Wilson54b4f682016-07-21 21:16:19 +01007048 dev_priv->rps.enabled = true;
Jesse Barnes4fc688c2012-11-02 11:14:01 -07007049 mutex_unlock(&dev_priv->rps.hw_lock);
Chris Wilsonb7137e02016-07-13 09:10:37 +01007050}
Imre Deakc6df39b2014-04-14 20:24:29 +03007051
Chris Wilson54b4f682016-07-21 21:16:19 +01007052static void __intel_autoenable_gt_powersave(struct work_struct *work)
7053{
7054 struct drm_i915_private *dev_priv =
7055 container_of(work, typeof(*dev_priv), rps.autoenable_work.work);
7056 struct intel_engine_cs *rcs;
7057 struct drm_i915_gem_request *req;
7058
7059 if (READ_ONCE(dev_priv->rps.enabled))
7060 goto out;
7061
Akash Goel3b3f1652016-10-13 22:44:48 +05307062 rcs = dev_priv->engine[RCS];
Chris Wilsone8a9c582016-12-18 15:37:20 +00007063 if (rcs->last_retired_context)
Chris Wilson54b4f682016-07-21 21:16:19 +01007064 goto out;
7065
7066 if (!rcs->init_context)
7067 goto out;
7068
7069 mutex_lock(&dev_priv->drm.struct_mutex);
7070
7071 req = i915_gem_request_alloc(rcs, dev_priv->kernel_context);
7072 if (IS_ERR(req))
7073 goto unlock;
7074
7075 if (!i915.enable_execlists && i915_switch_context(req) == 0)
7076 rcs->init_context(req);
7077
7078 /* Mark the device busy, calling intel_enable_gt_powersave() */
7079 i915_add_request_no_flush(req);
7080
7081unlock:
7082 mutex_unlock(&dev_priv->drm.struct_mutex);
7083out:
7084 intel_runtime_pm_put(dev_priv);
7085}
7086
7087void intel_autoenable_gt_powersave(struct drm_i915_private *dev_priv)
7088{
7089 if (READ_ONCE(dev_priv->rps.enabled))
7090 return;
7091
7092 if (IS_IRONLAKE_M(dev_priv)) {
7093 ironlake_enable_drps(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007094 intel_init_emon(dev_priv);
Chris Wilson54b4f682016-07-21 21:16:19 +01007095 } else if (INTEL_INFO(dev_priv)->gen >= 6) {
7096 /*
7097 * PCU communication is slow and this doesn't need to be
7098 * done at any specific time, so do this out of our fast path
7099 * to make resume and init faster.
7100 *
7101 * We depend on the HW RC6 power context save/restore
7102 * mechanism when entering D3 through runtime PM suspend. So
7103 * disable RPM until RPS/RC6 is properly setup. We can only
7104 * get here via the driver load/system resume/runtime resume
7105 * paths, so the _noresume version is enough (and in case of
7106 * runtime resume it's necessary).
7107 */
7108 if (queue_delayed_work(dev_priv->wq,
7109 &dev_priv->rps.autoenable_work,
7110 round_jiffies_up_relative(HZ)))
7111 intel_runtime_pm_get_noresume(dev_priv);
7112 }
7113}
7114
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007115static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007116{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007117 /*
7118 * On Ibex Peak and Cougar Point, we need to disable clock
7119 * gating for the panel power sequencer or it will fail to
7120 * start up when no ports are active.
7121 */
7122 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7123}
7124
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007125static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007126{
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007127 enum pipe pipe;
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007128
Damien Lespiau055e3932014-08-18 13:49:10 +01007129 for_each_pipe(dev_priv, pipe) {
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007130 I915_WRITE(DSPCNTR(pipe),
7131 I915_READ(DSPCNTR(pipe)) |
7132 DISPPLANE_TRICKLE_FEED_DISABLE);
Ville Syrjäläb12ce1d2015-05-26 20:27:23 +03007133
7134 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
7135 POSTING_READ(DSPSURF(pipe));
Ville Syrjälä0e088b82013-06-07 10:47:04 +03007136 }
7137}
7138
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007139static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
Ville Syrjälä017636c2013-12-05 15:51:37 +02007140{
Ville Syrjälä017636c2013-12-05 15:51:37 +02007141 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
7142 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
7143 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
7144
7145 /*
7146 * Don't touch WM1S_LP_EN here.
7147 * Doing so could cause underruns.
7148 */
7149}
7150
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007151static void ironlake_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007152{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007153 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007154
Damien Lespiauf1e8fa52013-06-07 17:41:09 +01007155 /*
7156 * Required for FBC
7157 * WaFbcDisableDpfcClockGating:ilk
7158 */
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007159 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7160 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7161 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007162
7163 I915_WRITE(PCH_3DCGDIS0,
7164 MARIUNIT_CLOCK_GATE_DISABLE |
7165 SVSMUNIT_CLOCK_GATE_DISABLE);
7166 I915_WRITE(PCH_3DCGDIS1,
7167 VFMUNIT_CLOCK_GATE_DISABLE);
7168
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007169 /*
7170 * According to the spec the following bits should be set in
7171 * order to enable memory self-refresh
7172 * The bit 22/21 of 0x42004
7173 * The bit 5 of 0x42020
7174 * The bit 15 of 0x45000
7175 */
7176 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7177 (I915_READ(ILK_DISPLAY_CHICKEN2) |
7178 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007179 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007180 I915_WRITE(DISP_ARB_CTL,
7181 (I915_READ(DISP_ARB_CTL) |
7182 DISP_FBC_WM_DIS));
Ville Syrjälä017636c2013-12-05 15:51:37 +02007183
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007184 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007185
7186 /*
7187 * Based on the document from hardware guys the following bits
7188 * should be set unconditionally in order to enable FBC.
7189 * The bit 22 of 0x42000
7190 * The bit 22 of 0x42004
7191 * The bit 7,8,9 of 0x42020.
7192 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007193 if (IS_IRONLAKE_M(dev_priv)) {
Damien Lespiau4bb35332013-06-14 15:23:24 +01007194 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007195 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7196 I915_READ(ILK_DISPLAY_CHICKEN1) |
7197 ILK_FBCQ_DIS);
7198 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7199 I915_READ(ILK_DISPLAY_CHICKEN2) |
7200 ILK_DPARB_GATE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007201 }
7202
Damien Lespiau4d47e4f2012-10-19 17:55:42 +01007203 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
7204
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007205 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7206 I915_READ(ILK_DISPLAY_CHICKEN2) |
7207 ILK_ELPIN_409_SELECT);
7208 I915_WRITE(_3D_CHICKEN2,
7209 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
7210 _3D_CHICKEN2_WM_READ_PIPELINED);
Daniel Vetter4358a372012-10-18 11:49:51 +02007211
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007212 /* WaDisableRenderCachePipelinedFlush:ilk */
Daniel Vetter4358a372012-10-18 11:49:51 +02007213 I915_WRITE(CACHE_MODE_0,
7214 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Daniel Vetter3107bd42012-10-31 22:52:31 +01007215
Akash Goel4e046322014-04-04 17:14:38 +05307216 /* WaDisable_RenderCache_OperationalFlush:ilk */
7217 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7218
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007219 g4x_disable_trickle_feed(dev_priv);
Ville Syrjäläbdad2b22013-06-07 10:47:03 +03007220
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007221 ibx_init_clock_gating(dev_priv);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007222}
7223
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007224static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetter3107bd42012-10-31 22:52:31 +01007225{
Daniel Vetter3107bd42012-10-31 22:52:31 +01007226 int pipe;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007227 uint32_t val;
Daniel Vetter3107bd42012-10-31 22:52:31 +01007228
7229 /*
7230 * On Ibex Peak and Cougar Point, we need to disable clock
7231 * gating for the panel power sequencer or it will fail to
7232 * start up when no ports are active.
7233 */
Jesse Barnescd664072013-10-02 10:34:19 -07007234 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7235 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7236 PCH_CPUNIT_CLOCK_GATE_DISABLE);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007237 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
7238 DPLS_EDP_PPS_FIX_DIS);
Takashi Iwai335c07b2012-12-11 11:46:29 +01007239 /* The below fixes the weird display corruption, a few pixels shifted
7240 * downward, on (only) LVDS of some HP laptops with IVY.
7241 */
Damien Lespiau055e3932014-08-18 13:49:10 +01007242 for_each_pipe(dev_priv, pipe) {
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007243 val = I915_READ(TRANS_CHICKEN2(pipe));
7244 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7245 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007246 if (dev_priv->vbt.fdi_rx_polarity_inverted)
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007247 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
Paulo Zanonidc4bd2d2013-04-08 15:48:08 -03007248 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
7249 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7250 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
Paulo Zanoni3f704fa2013-04-08 15:48:07 -03007251 I915_WRITE(TRANS_CHICKEN2(pipe), val);
7252 }
Daniel Vetter3107bd42012-10-31 22:52:31 +01007253 /* WADP0ClockGatingDisable */
Damien Lespiau055e3932014-08-18 13:49:10 +01007254 for_each_pipe(dev_priv, pipe) {
Daniel Vetter3107bd42012-10-31 22:52:31 +01007255 I915_WRITE(TRANS_CHICKEN1(pipe),
7256 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7257 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007258}
7259
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007260static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007261{
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007262 uint32_t tmp;
7263
7264 tmp = I915_READ(MCH_SSKPD);
Daniel Vetterdf662a22014-08-04 11:17:25 +02007265 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7266 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7267 tmp);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007268}
7269
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007270static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007271{
Damien Lespiau231e54f2012-10-19 17:55:41 +01007272 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007273
Damien Lespiau231e54f2012-10-19 17:55:41 +01007274 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007275
7276 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7277 I915_READ(ILK_DISPLAY_CHICKEN2) |
7278 ILK_ELPIN_409_SELECT);
7279
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007280 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
Daniel Vetter42839082012-12-14 23:38:28 +01007281 I915_WRITE(_3D_CHICKEN,
7282 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
7283
Akash Goel4e046322014-04-04 17:14:38 +05307284 /* WaDisable_RenderCache_OperationalFlush:snb */
7285 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7286
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007287 /*
7288 * BSpec recoomends 8x4 when MSAA is used,
7289 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007290 *
7291 * Note that PS/WM thread counts depend on the WIZ hashing
7292 * disable bit, which we don't touch here, but it's good
7293 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007294 */
7295 I915_WRITE(GEN6_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007296 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjälä8d85d272014-02-04 21:59:15 +02007297
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007298 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007299
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007300 I915_WRITE(CACHE_MODE_0,
Daniel Vetter50743292012-04-26 22:02:54 +02007301 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007302
7303 I915_WRITE(GEN6_UCGCTL1,
7304 I915_READ(GEN6_UCGCTL1) |
7305 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7306 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7307
7308 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7309 * gating disable must be set. Failure to set it results in
7310 * flickering pixels due to Z write ordering failures after
7311 * some amount of runtime in the Mesa "fire" demo, and Unigine
7312 * Sanctuary and Tropics, and apparently anything else with
7313 * alpha test or pixel discard.
7314 *
7315 * According to the spec, bit 11 (RCCUNIT) must also be set,
7316 * but we didn't debug actual testcases to find it out.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007317 *
Ville Syrjäläef593182014-01-22 21:32:47 +02007318 * WaDisableRCCUnitClockGating:snb
7319 * WaDisableRCPBUnitClockGating:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007320 */
7321 I915_WRITE(GEN6_UCGCTL2,
7322 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7323 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7324
Ville Syrjälä5eb146d2014-02-04 21:59:16 +02007325 /* WaStripsFansDisableFastClipPerformanceFix:snb */
Ville Syrjälä743b57d2014-02-04 21:59:17 +02007326 I915_WRITE(_3D_CHICKEN3,
7327 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007328
7329 /*
Ville Syrjäläe927ecd2014-02-04 21:59:18 +02007330 * Bspec says:
7331 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
7332 * 3DSTATE_SF number of SF output attributes is more than 16."
7333 */
7334 I915_WRITE(_3D_CHICKEN3,
7335 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
7336
7337 /*
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007338 * According to the spec the following bits should be
7339 * set in order to enable memory self-refresh and fbc:
7340 * The bit21 and bit22 of 0x42000
7341 * The bit21 and bit22 of 0x42004
7342 * The bit5 and bit7 of 0x42020
7343 * The bit14 of 0x70180
7344 * The bit14 of 0x71180
Damien Lespiau4bb35332013-06-14 15:23:24 +01007345 *
7346 * WaFbcAsynchFlipDisableFbcQueue:snb
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007347 */
7348 I915_WRITE(ILK_DISPLAY_CHICKEN1,
7349 I915_READ(ILK_DISPLAY_CHICKEN1) |
7350 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7351 I915_WRITE(ILK_DISPLAY_CHICKEN2,
7352 I915_READ(ILK_DISPLAY_CHICKEN2) |
7353 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
Damien Lespiau231e54f2012-10-19 17:55:41 +01007354 I915_WRITE(ILK_DSPCLK_GATE_D,
7355 I915_READ(ILK_DSPCLK_GATE_D) |
7356 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7357 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007358
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007359 g4x_disable_trickle_feed(dev_priv);
Ben Widawskyf8f2ac92012-10-03 19:34:24 -07007360
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007361 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007362
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007363 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007364}
7365
7366static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
7367{
7368 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
7369
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007370 /*
Ville Syrjälä46680e02014-01-22 21:33:01 +02007371 * WaVSThreadDispatchOverride:ivb,vlv
Ville Syrjälä3aad9052014-01-22 21:32:59 +02007372 *
7373 * This actually overrides the dispatch
7374 * mode for all thread types.
7375 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007376 reg &= ~GEN7_FF_SCHED_MASK;
7377 reg |= GEN7_FF_TS_SCHED_HW;
7378 reg |= GEN7_FF_VS_SCHED_HW;
7379 reg |= GEN7_FF_DS_SCHED_HW;
7380
7381 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
7382}
7383
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007384static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007385{
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007386 /*
7387 * TODO: this bit should only be enabled when really needed, then
7388 * disabled when not needed anymore in order to save power.
7389 */
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007390 if (HAS_PCH_LPT_LP(dev_priv))
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007391 I915_WRITE(SOUTH_DSPCLK_GATE_D,
7392 I915_READ(SOUTH_DSPCLK_GATE_D) |
7393 PCH_LP_PARTITION_LEVEL_DISABLE);
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007394
7395 /* WADPOClockGatingDisable:hsw */
Ville Syrjälä36c0d0c2015-09-18 20:03:31 +03007396 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
7397 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
Paulo Zanoni0a790cd2013-04-17 18:15:49 -03007398 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
Paulo Zanoni17a303e2012-11-20 15:12:07 -02007399}
7400
Ville Syrjälä712bf362016-10-31 22:37:23 +02007401static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007402{
Tvrtko Ursulin4f8036a2016-10-13 11:02:52 +01007403 if (HAS_PCH_LPT_LP(dev_priv)) {
Imre Deak7d708ee2013-04-17 14:04:50 +03007404 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
7405
7406 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7407 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7408 }
7409}
7410
Imre Deak450174f2016-05-03 15:54:21 +03007411static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7412 int general_prio_credits,
7413 int high_prio_credits)
7414{
7415 u32 misccpctl;
7416
7417 /* WaTempDisableDOPClkGating:bdw */
7418 misccpctl = I915_READ(GEN7_MISCCPCTL);
7419 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7420
7421 I915_WRITE(GEN8_L3SQCREG1,
7422 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
7423 L3_HIGH_PRIO_CREDITS(high_prio_credits));
7424
7425 /*
7426 * Wait at least 100 clocks before re-enabling clock gating.
7427 * See the definition of L3SQCREG1 in BSpec.
7428 */
7429 POSTING_READ(GEN8_L3SQCREG1);
7430 udelay(1);
7431 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
7432}
7433
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007434static void kabylake_init_clock_gating(struct drm_i915_private *dev_priv)
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007435{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007436 gen9_init_clock_gating(dev_priv);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007437
7438 /* WaDisableSDEUnitClockGating:kbl */
7439 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7440 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7441 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala8aeb7f62016-06-07 17:19:05 +03007442
7443 /* WaDisableGamClockGating:kbl */
7444 if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
7445 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7446 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007447
7448 /* WaFbcNukeOnHostModify:kbl */
7449 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7450 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007451}
7452
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007453static void skylake_init_clock_gating(struct drm_i915_private *dev_priv)
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007454{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007455 gen9_init_clock_gating(dev_priv);
Mika Kuoppala44fff992016-06-07 17:19:09 +03007456
7457 /* WAC6entrylatency:skl */
7458 I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
7459 FBC_LLC_FULLY_OPEN);
Mika Kuoppala031cd8c2016-06-07 17:19:18 +03007460
7461 /* WaFbcNukeOnHostModify:skl */
7462 I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
7463 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007464}
7465
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007466static void broadwell_init_clock_gating(struct drm_i915_private *dev_priv)
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007467{
Damien Lespiau07d27e22014-03-03 17:31:46 +00007468 enum pipe pipe;
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007469
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007470 ilk_init_lp_watermarks(dev_priv);
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007471
Ben Widawskyab57fff2013-12-12 15:28:04 -08007472 /* WaSwitchSolVfFArbitrationPriority:bdw */
Ben Widawsky50ed5fb2013-11-02 21:07:40 -07007473 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007474
Ben Widawskyab57fff2013-12-12 15:28:04 -08007475 /* WaPsrDPAMaskVBlankInSRD:bdw */
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007476 I915_WRITE(CHICKEN_PAR1_1,
7477 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7478
Ben Widawskyab57fff2013-12-12 15:28:04 -08007479 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
Damien Lespiau055e3932014-08-18 13:49:10 +01007480 for_each_pipe(dev_priv, pipe) {
Damien Lespiau07d27e22014-03-03 17:31:46 +00007481 I915_WRITE(CHICKEN_PIPESL_1(pipe),
Ville Syrjäläc7c65622014-03-05 13:05:45 +02007482 I915_READ(CHICKEN_PIPESL_1(pipe)) |
Ville Syrjälä8f670bb2014-03-05 13:05:47 +02007483 BDW_DPRS_MASK_VBLANK_SRD);
Ben Widawskyfe4ab3c2013-11-02 21:07:54 -07007484 }
Ben Widawsky63801f22013-12-12 17:26:03 -08007485
Ben Widawskyab57fff2013-12-12 15:28:04 -08007486 /* WaVSRefCountFullforceMissDisable:bdw */
7487 /* WaDSRefCountFullforceMissDisable:bdw */
7488 I915_WRITE(GEN7_FF_THREAD_MODE,
7489 I915_READ(GEN7_FF_THREAD_MODE) &
7490 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjälä36075a42014-02-04 21:59:21 +02007491
Ville Syrjälä295e8bb2014-02-27 21:59:01 +02007492 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7493 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä4f1ca9e2014-02-27 21:59:02 +02007494
7495 /* WaDisableSDEUnitClockGating:bdw */
7496 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7497 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Damien Lespiau5d708682014-03-26 18:41:51 +00007498
Imre Deak450174f2016-05-03 15:54:21 +03007499 /* WaProgramL3SqcReg1Default:bdw */
7500 gen8_set_l3sqc_credits(dev_priv, 30, 2);
Ville Syrjälä4d487cf2015-05-19 20:32:56 +03007501
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007502 /*
7503 * WaGttCachingOffByDefault:bdw
7504 * GTT cache may not work with big pages, so if those
7505 * are ever enabled GTT cache may need to be disabled.
7506 */
7507 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
7508
Mika Kuoppala17e0adf2016-06-07 17:19:02 +03007509 /* WaKVMNotificationOnConfigChange:bdw */
7510 I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
7511 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7512
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007513 lpt_init_clock_gating(dev_priv);
Robert Bragg9cc19732017-02-12 13:32:52 +00007514
7515 /* WaDisableDopClockGating:bdw
7516 *
7517 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7518 * clock gating.
7519 */
7520 I915_WRITE(GEN6_UCGCTL1,
7521 I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
Ben Widawsky1020a5c2013-11-02 21:07:06 -07007522}
7523
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007524static void haswell_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007525{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007526 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007527
Francisco Jerezf3fc4882013-10-02 15:53:16 -07007528 /* L3 caching of data atomics doesn't work -- disable it. */
7529 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
7530 I915_WRITE(HSW_ROW_CHICKEN3,
7531 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
7532
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007533 /* This is required by WaCatErrorRejectionIssue:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007534 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7535 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7536 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7537
Ville Syrjäläe36ea7f2014-01-22 21:33:00 +02007538 /* WaVSRefCountFullforceMissDisable:hsw */
7539 I915_WRITE(GEN7_FF_THREAD_MODE,
7540 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007541
Akash Goel4e046322014-04-04 17:14:38 +05307542 /* WaDisable_RenderCache_OperationalFlush:hsw */
7543 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7544
Chia-I Wufe27c602014-01-28 13:29:33 +08007545 /* enable HiZ Raw Stall Optimization */
7546 I915_WRITE(CACHE_MODE_0_GEN7,
7547 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7548
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007549 /* WaDisable4x2SubspanOptimization:hsw */
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007550 I915_WRITE(CACHE_MODE_1,
7551 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007552
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007553 /*
7554 * BSpec recommends 8x4 when MSAA is used,
7555 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007556 *
7557 * Note that PS/WM thread counts depend on the WIZ hashing
7558 * disable bit, which we don't touch here, but it's good
7559 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007560 */
7561 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007562 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa12c4962014-02-04 21:59:20 +02007563
Kenneth Graunke94411592014-12-31 16:23:00 -08007564 /* WaSampleCChickenBitEnable:hsw */
7565 I915_WRITE(HALF_SLICE_CHICKEN3,
7566 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
7567
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007568 /* WaSwitchSolVfFArbitrationPriority:hsw */
Ben Widawskye3dff582013-03-20 14:49:14 -07007569 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7570
Paulo Zanoni90a88642013-05-03 17:23:45 -03007571 /* WaRsPkgCStateDisplayPMReq:hsw */
7572 I915_WRITE(CHICKEN_PAR1_1,
7573 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
Eugeni Dodonov1544d9d2012-07-02 11:51:10 -03007574
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007575 lpt_init_clock_gating(dev_priv);
Eugeni Dodonovcad2a2d2012-07-02 11:51:09 -03007576}
7577
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007578static void ivybridge_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007579{
Ben Widawsky20848222012-05-04 18:58:59 -07007580 uint32_t snpcr;
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007581
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007582 ilk_init_lp_watermarks(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007583
Damien Lespiau231e54f2012-10-19 17:55:41 +01007584 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007585
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007586 /* WaDisableEarlyCull:ivb */
Jesse Barnes87f80202012-10-02 17:43:41 -05007587 I915_WRITE(_3D_CHICKEN3,
7588 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7589
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007590 /* WaDisableBackToBackFlipFix:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007591 I915_WRITE(IVB_CHICKEN3,
7592 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7593 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7594
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007595 /* WaDisablePSDDualDispatchEnable:ivb */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007596 if (IS_IVB_GT1(dev_priv))
Jesse Barnes12f33822012-10-25 12:15:45 -07007597 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
7598 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007599
Akash Goel4e046322014-04-04 17:14:38 +05307600 /* WaDisable_RenderCache_OperationalFlush:ivb */
7601 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7602
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007603 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007604 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
7605 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
7606
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007607 /* WaApplyL3ControlAndL3ChickenMode:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007608 I915_WRITE(GEN7_L3CNTLREG1,
7609 GEN7_WA_FOR_GEN7_L3_CONTROL);
7610 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
Jesse Barnes8ab43972012-10-25 12:15:42 -07007611 GEN7_WA_L3_CHICKEN_MODE);
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007612 if (IS_IVB_GT1(dev_priv))
Jesse Barnes8ab43972012-10-25 12:15:42 -07007613 I915_WRITE(GEN7_ROW_CHICKEN2,
7614 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007615 else {
7616 /* must write both registers */
7617 I915_WRITE(GEN7_ROW_CHICKEN2,
7618 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Jesse Barnes8ab43972012-10-25 12:15:42 -07007619 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
7620 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
Ville Syrjälä412236c2014-01-22 21:32:44 +02007621 }
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007622
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007623 /* WaForceL3Serialization:ivb */
Jesse Barnes61939d92012-10-02 17:43:38 -05007624 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7625 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7626
Ville Syrjälä1b80a19a2014-01-22 21:32:53 +02007627 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007628 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007629 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007630 */
7631 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä28acf3b2014-01-22 21:32:48 +02007632 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007633
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007634 /* This is required by WaCatErrorRejectionIssue:ivb */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007635 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7636 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7637 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7638
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007639 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007640
7641 gen7_setup_fixed_func_scheduler(dev_priv);
Daniel Vetter97e19302012-04-24 16:00:21 +02007642
Chris Wilson22721342014-03-04 09:41:43 +00007643 if (0) { /* causes HiZ corruption on ivb:gt1 */
7644 /* enable HiZ Raw Stall Optimization */
7645 I915_WRITE(CACHE_MODE_0_GEN7,
7646 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
7647 }
Chia-I Wu116f2b62014-01-28 13:29:34 +08007648
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007649 /* WaDisable4x2SubspanOptimization:ivb */
Daniel Vetter97e19302012-04-24 16:00:21 +02007650 I915_WRITE(CACHE_MODE_1,
7651 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Ben Widawsky20848222012-05-04 18:58:59 -07007652
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007653 /*
7654 * BSpec recommends 8x4 when MSAA is used,
7655 * however in practice 16x4 seems fastest.
Ville Syrjäläc5c98a52014-02-05 12:43:47 +02007656 *
7657 * Note that PS/WM thread counts depend on the WIZ hashing
7658 * disable bit, which we don't touch here, but it's good
7659 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007660 */
7661 I915_WRITE(GEN7_GT_MODE,
Damien Lespiau98533252014-12-08 17:33:51 +00007662 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
Ville Syrjäläa607c1a2014-02-04 21:59:19 +02007663
Ben Widawsky20848222012-05-04 18:58:59 -07007664 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
7665 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7666 snpcr |= GEN6_MBC_SNPCR_MED;
7667 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
Daniel Vetter3107bd42012-10-31 22:52:31 +01007668
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007669 if (!HAS_PCH_NOP(dev_priv))
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007670 cpt_init_clock_gating(dev_priv);
Daniel Vetter1d7aaa02013-02-09 21:03:42 +01007671
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007672 gen6_check_mch_setup(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007673}
7674
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007675static void valleyview_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007676{
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007677 /* WaDisableEarlyCull:vlv */
Jesse Barnes87f80202012-10-02 17:43:41 -05007678 I915_WRITE(_3D_CHICKEN3,
7679 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
7680
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007681 /* WaDisableBackToBackFlipFix:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007682 I915_WRITE(IVB_CHICKEN3,
7683 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7684 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7685
Ville Syrjäläfad7d362014-01-22 21:32:39 +02007686 /* WaPsdDispatchEnable:vlv */
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007687 /* WaDisablePSDDualDispatchEnable:vlv */
Jesse Barnes12f33822012-10-25 12:15:45 -07007688 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
Jesse Barnesd3bc0302013-03-08 10:45:51 -08007689 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
7690 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
Jesse Barnes12f33822012-10-25 12:15:45 -07007691
Akash Goel4e046322014-04-04 17:14:38 +05307692 /* WaDisable_RenderCache_OperationalFlush:vlv */
7693 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7694
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007695 /* WaForceL3Serialization:vlv */
Jesse Barnes61939d92012-10-02 17:43:38 -05007696 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
7697 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
7698
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007699 /* WaDisableDopClockGating:vlv */
Jesse Barnes8ab43972012-10-25 12:15:42 -07007700 I915_WRITE(GEN7_ROW_CHICKEN2,
7701 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7702
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007703 /* This is required by WaCatErrorRejectionIssue:vlv */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007704 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7705 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7706 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7707
Ville Syrjälä46680e02014-01-22 21:33:01 +02007708 gen7_setup_fixed_func_scheduler(dev_priv);
7709
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007710 /*
Jesse Barnes0f846f82012-06-14 11:04:47 -07007711 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007712 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
Jesse Barnes0f846f82012-06-14 11:04:47 -07007713 */
7714 I915_WRITE(GEN6_UCGCTL2,
Ville Syrjälä3c0edae2014-01-22 21:32:56 +02007715 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes0f846f82012-06-14 11:04:47 -07007716
Akash Goelc98f5062014-03-24 23:00:07 +05307717 /* WaDisableL3Bank2xClockGate:vlv
7718 * Disabling L3 clock gating- MMIO 940c[25] = 1
7719 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7720 I915_WRITE(GEN7_UCGCTL4,
7721 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
Jesse Barnese3f33d42012-06-14 11:04:50 -07007722
Ville Syrjäläafd58e72014-01-22 21:33:03 +02007723 /*
7724 * BSpec says this must be set, even though
7725 * WaDisable4x2SubspanOptimization isn't listed for VLV.
7726 */
Daniel Vetter6b26c862012-04-24 14:04:12 +02007727 I915_WRITE(CACHE_MODE_1,
7728 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
Jesse Barnes79831172012-06-20 10:53:12 -07007729
7730 /*
Ville Syrjäläda2518f2015-01-21 19:38:01 +02007731 * BSpec recommends 8x4 when MSAA is used,
7732 * however in practice 16x4 seems fastest.
7733 *
7734 * Note that PS/WM thread counts depend on the WIZ hashing
7735 * disable bit, which we don't touch here, but it's good
7736 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
7737 */
7738 I915_WRITE(GEN7_GT_MODE,
7739 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
7740
7741 /*
Ville Syrjälä031994e2014-01-22 21:32:46 +02007742 * WaIncreaseL3CreditsForVLVB0:vlv
7743 * This is the hardware default actually.
7744 */
7745 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
7746
7747 /*
Damien Lespiauecdb4eb72013-05-03 18:48:10 +01007748 * WaDisableVLVClockGating_VBIIssue:vlv
Jesse Barnes2d809572012-10-25 12:15:44 -07007749 * Disable clock gating on th GCFG unit to prevent a delay
7750 * in the reporting of vblank events.
7751 */
Ville Syrjälä7a0d1ee2014-01-22 21:33:04 +02007752 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007753}
7754
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007755static void cherryview_init_clock_gating(struct drm_i915_private *dev_priv)
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007756{
Ville Syrjälä232ce332014-04-09 13:28:35 +03007757 /* WaVSRefCountFullforceMissDisable:chv */
7758 /* WaDSRefCountFullforceMissDisable:chv */
7759 I915_WRITE(GEN7_FF_THREAD_MODE,
7760 I915_READ(GEN7_FF_THREAD_MODE) &
7761 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
Ville Syrjäläacea6f92014-04-09 13:28:36 +03007762
7763 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7764 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7765 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
Ville Syrjälä08466972014-04-09 13:28:37 +03007766
7767 /* WaDisableCSUnitClockGating:chv */
7768 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7769 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
Ville Syrjäläc6317802014-04-09 13:28:38 +03007770
7771 /* WaDisableSDEUnitClockGating:chv */
7772 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7773 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007774
7775 /*
Imre Deak450174f2016-05-03 15:54:21 +03007776 * WaProgramL3SqcReg1Default:chv
7777 * See gfxspecs/Related Documents/Performance Guide/
7778 * LSQC Setting Recommendations.
7779 */
7780 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7781
7782 /*
Ville Syrjälä6d50b062015-05-19 20:32:57 +03007783 * GTT cache may not work with big pages, so if those
7784 * are ever enabled GTT cache may need to be disabled.
7785 */
7786 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
Ville Syrjäläa4565da2014-04-09 13:28:10 +03007787}
7788
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007789static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007790{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007791 uint32_t dspclk_gate;
7792
7793 I915_WRITE(RENCLK_GATE_D1, 0);
7794 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7795 GS_UNIT_CLOCK_GATE_DISABLE |
7796 CL_UNIT_CLOCK_GATE_DISABLE);
7797 I915_WRITE(RAMCLK_GATE_D, 0);
7798 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7799 OVRUNIT_CLOCK_GATE_DISABLE |
7800 OVCUNIT_CLOCK_GATE_DISABLE;
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007801 if (IS_GM45(dev_priv))
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007802 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7803 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
Daniel Vetter4358a372012-10-18 11:49:51 +02007804
7805 /* WaDisableRenderCachePipelinedFlush */
7806 I915_WRITE(CACHE_MODE_0,
7807 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
Ville Syrjäläde1aa622013-06-07 10:47:01 +03007808
Akash Goel4e046322014-04-04 17:14:38 +05307809 /* WaDisable_RenderCache_OperationalFlush:g4x */
7810 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7811
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007812 g4x_disable_trickle_feed(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007813}
7814
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007815static void crestline_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007816{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007817 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7818 I915_WRITE(RENCLK_GATE_D2, 0);
7819 I915_WRITE(DSPCLK_GATE_D, 0);
7820 I915_WRITE(RAMCLK_GATE_D, 0);
7821 I915_WRITE16(DEUC, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007822 I915_WRITE(MI_ARB_STATE,
7823 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307824
7825 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7826 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007827}
7828
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007829static void broadwater_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007830{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007831 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7832 I965_RCC_CLOCK_GATE_DISABLE |
7833 I965_RCPB_CLOCK_GATE_DISABLE |
7834 I965_ISC_CLOCK_GATE_DISABLE |
7835 I965_FBC_CLOCK_GATE_DISABLE);
7836 I915_WRITE(RENCLK_GATE_D2, 0);
Ville Syrjälä20f94962013-06-07 10:47:02 +03007837 I915_WRITE(MI_ARB_STATE,
7838 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Akash Goel4e046322014-04-04 17:14:38 +05307839
7840 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7841 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007842}
7843
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007844static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007845{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007846 u32 dstate = I915_READ(D_STATE);
7847
7848 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7849 DSTATE_DOT_CLOCK_GATING;
7850 I915_WRITE(D_STATE, dstate);
Chris Wilson13a86b82012-04-24 14:51:43 +01007851
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007852 if (IS_PINEVIEW(dev_priv))
Chris Wilson13a86b82012-04-24 14:51:43 +01007853 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
Daniel Vetter974a3b02012-09-09 11:54:16 +02007854
7855 /* IIR "flip pending" means done if this bit is set */
7856 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
Ville Syrjälä12fabbcb92014-02-25 15:13:38 +02007857
7858 /* interrupts should cause a wake up from C3 */
Ville Syrjälä32992542014-02-25 15:13:39 +02007859 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
Ville Syrjälädbb42742014-02-25 15:13:41 +02007860
7861 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7862 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007863
7864 I915_WRITE(MI_ARB_STATE,
7865 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007866}
7867
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007868static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007869{
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007870 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
Ville Syrjälä54e472a2014-02-25 15:13:40 +02007871
7872 /* interrupts should cause a wake up from C3 */
7873 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7874 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
Ville Syrjälä10383922014-08-15 01:21:54 +03007875
7876 I915_WRITE(MEM_MODE,
7877 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007878}
7879
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007880static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007881{
Ville Syrjälä10383922014-08-15 01:21:54 +03007882 I915_WRITE(MEM_MODE,
7883 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7884 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007885}
7886
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007887void intel_init_clock_gating(struct drm_i915_private *dev_priv)
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007888{
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007889 dev_priv->display.init_clock_gating(dev_priv);
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03007890}
7891
Ville Syrjälä712bf362016-10-31 22:37:23 +02007892void intel_suspend_hw(struct drm_i915_private *dev_priv)
Imre Deak7d708ee2013-04-17 14:04:50 +03007893{
Ville Syrjälä712bf362016-10-31 22:37:23 +02007894 if (HAS_PCH_LPT(dev_priv))
7895 lpt_suspend_hw(dev_priv);
Imre Deak7d708ee2013-04-17 14:04:50 +03007896}
7897
Ville Syrjälä46f16e62016-10-31 22:37:22 +02007898static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
Imre Deakbb400da2016-03-16 13:38:54 +02007899{
7900 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7901}
7902
7903/**
7904 * intel_init_clock_gating_hooks - setup the clock gating hooks
7905 * @dev_priv: device private
7906 *
7907 * Setup the hooks that configure which clocks of a given platform can be
7908 * gated and also apply various GT and display specific workarounds for these
7909 * platforms. Note that some GT specific workarounds are applied separately
7910 * when GPU contexts or batchbuffers start their execution.
7911 */
7912void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7913{
7914 if (IS_SKYLAKE(dev_priv))
Daniel Vetterdc00b6a2016-05-19 09:14:20 +02007915 dev_priv->display.init_clock_gating = skylake_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007916 else if (IS_KABYLAKE(dev_priv))
Mika Kuoppala9498dba2016-06-07 17:19:01 +03007917 dev_priv->display.init_clock_gating = kabylake_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007918 else if (IS_BROXTON(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007919 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
Ander Conselvan de Oliveira9fb50262017-01-26 11:16:58 +02007920 else if (IS_GEMINILAKE(dev_priv))
7921 dev_priv->display.init_clock_gating = glk_init_clock_gating;
Imre Deakbb400da2016-03-16 13:38:54 +02007922 else if (IS_BROADWELL(dev_priv))
7923 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7924 else if (IS_CHERRYVIEW(dev_priv))
7925 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7926 else if (IS_HASWELL(dev_priv))
7927 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7928 else if (IS_IVYBRIDGE(dev_priv))
7929 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7930 else if (IS_VALLEYVIEW(dev_priv))
7931 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7932 else if (IS_GEN6(dev_priv))
7933 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7934 else if (IS_GEN5(dev_priv))
7935 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7936 else if (IS_G4X(dev_priv))
7937 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007938 else if (IS_I965GM(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007939 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
Jani Nikulac0f86832016-12-07 12:13:04 +02007940 else if (IS_I965G(dev_priv))
Imre Deakbb400da2016-03-16 13:38:54 +02007941 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7942 else if (IS_GEN3(dev_priv))
7943 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7944 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7945 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7946 else if (IS_GEN2(dev_priv))
7947 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7948 else {
7949 MISSING_CASE(INTEL_DEVID(dev_priv));
7950 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7951 }
7952}
7953
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007954/* Set up chip specific power management-related functions */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007955void intel_init_pm(struct drm_i915_private *dev_priv)
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007956{
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02007957 intel_fbc_init(dev_priv);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007958
Daniel Vetterc921aba2012-04-26 23:28:17 +02007959 /* For cxsr */
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007960 if (IS_PINEVIEW(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007961 i915_pineview_get_mem_freq(dev_priv);
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007962 else if (IS_GEN5(dev_priv))
Ville Syrjälä148ac1f2016-10-31 22:37:16 +02007963 i915_ironlake_get_mem_freq(dev_priv);
Daniel Vetterc921aba2012-04-26 23:28:17 +02007964
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007965 /* For FIFO watermark updates */
Ville Syrjälä62d75df2016-10-31 22:37:25 +02007966 if (INTEL_GEN(dev_priv) >= 9) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007967 skl_setup_wm_latency(dev_priv);
Maarten Lankhorste62929b2016-11-08 13:55:33 +01007968 dev_priv->display.initial_watermarks = skl_initial_wm;
Maarten Lankhorstccf010f2016-11-08 13:55:32 +01007969 dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
Matt Roper98d39492016-05-12 07:06:03 -07007970 dev_priv->display.compute_global_watermarks = skl_compute_wm;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01007971 } else if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007972 ilk_setup_wm_latency(dev_priv);
Ville Syrjälä53615a52013-08-01 16:18:50 +03007973
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007974 if ((IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[1] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007975 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01007976 (!IS_GEN5(dev_priv) && dev_priv->wm.pri_latency[0] &&
Ville Syrjäläbd602542014-01-07 16:14:10 +02007977 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
Matt Roper86c8bbb2015-09-24 15:53:16 -07007978 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
Matt Ropered4a6a72016-02-23 17:20:13 -08007979 dev_priv->display.compute_intermediate_wm =
7980 ilk_compute_intermediate_wm;
7981 dev_priv->display.initial_watermarks =
7982 ilk_initial_watermarks;
7983 dev_priv->display.optimize_watermarks =
7984 ilk_optimize_watermarks;
Ville Syrjäläbd602542014-01-07 16:14:10 +02007985 } else {
7986 DRM_DEBUG_KMS("Failed to read display plane latency. "
7987 "Disable CxSR\n");
7988 }
Ville Syrjälä6b6b3ee2016-11-28 19:37:07 +02007989 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
Ville Syrjäläbb726512016-10-31 22:37:24 +02007990 vlv_setup_wm_latency(dev_priv);
Ville Syrjäläff32c542017-03-02 19:14:57 +02007991 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007992 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007993 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
Ville Syrjälä4841da52017-03-02 19:14:59 +02007994 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
Ville Syrjäläff32c542017-03-02 19:14:57 +02007995 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
Ville Syrjälä9b1e14f2016-10-31 22:37:15 +02007996 } else if (IS_PINEVIEW(dev_priv)) {
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01007997 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev_priv),
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03007998 dev_priv->is_ddr3,
7999 dev_priv->fsb_freq,
8000 dev_priv->mem_freq)) {
8001 DRM_INFO("failed to find known CxSR latency "
8002 "(found ddr%s fsb freq %d, mem freq %d), "
8003 "disabling CxSR\n",
8004 (dev_priv->is_ddr3 == 1) ? "3" : "2",
8005 dev_priv->fsb_freq, dev_priv->mem_freq);
8006 /* Disable CxSR and never update its watermark again */
Imre Deak5209b1f2014-07-01 12:36:17 +03008007 intel_set_memory_cxsr(dev_priv, false);
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008008 dev_priv->display.update_wm = NULL;
8009 } else
8010 dev_priv->display.update_wm = pineview_update_wm;
Tvrtko Ursulin9beb5fe2016-10-13 11:03:06 +01008011 } else if (IS_G4X(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008012 dev_priv->display.update_wm = g4x_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008013 } else if (IS_GEN4(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008014 dev_priv->display.update_wm = i965_update_wm;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008015 } else if (IS_GEN3(dev_priv)) {
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008016 dev_priv->display.update_wm = i9xx_update_wm;
8017 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Tvrtko Ursulin5db94012016-10-13 11:03:10 +01008018 } else if (IS_GEN2(dev_priv)) {
Ville Syrjälä62d75df2016-10-31 22:37:25 +02008019 if (INTEL_INFO(dev_priv)->num_pipes == 1) {
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008020 dev_priv->display.update_wm = i845_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008021 dev_priv->display.get_fifo_size = i845_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008022 } else {
8023 dev_priv->display.update_wm = i9xx_update_wm;
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008024 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008025 }
Daniel Vetterfeb56b92013-12-14 20:38:30 -02008026 } else {
8027 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008028 }
8029}
8030
Lyude87660502016-08-17 15:55:53 -04008031static inline int gen6_check_mailbox_status(struct drm_i915_private *dev_priv)
8032{
8033 uint32_t flags =
8034 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8035
8036 switch (flags) {
8037 case GEN6_PCODE_SUCCESS:
8038 return 0;
8039 case GEN6_PCODE_UNIMPLEMENTED_CMD:
8040 case GEN6_PCODE_ILLEGAL_CMD:
8041 return -ENXIO;
8042 case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Chris Wilson7850d1c2016-08-26 11:59:26 +01008043 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
Lyude87660502016-08-17 15:55:53 -04008044 return -EOVERFLOW;
8045 case GEN6_PCODE_TIMEOUT:
8046 return -ETIMEDOUT;
8047 default:
8048 MISSING_CASE(flags)
8049 return 0;
8050 }
8051}
8052
8053static inline int gen7_check_mailbox_status(struct drm_i915_private *dev_priv)
8054{
8055 uint32_t flags =
8056 I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_ERROR_MASK;
8057
8058 switch (flags) {
8059 case GEN6_PCODE_SUCCESS:
8060 return 0;
8061 case GEN6_PCODE_ILLEGAL_CMD:
8062 return -ENXIO;
8063 case GEN7_PCODE_TIMEOUT:
8064 return -ETIMEDOUT;
8065 case GEN7_PCODE_ILLEGAL_DATA:
8066 return -EINVAL;
8067 case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
8068 return -EOVERFLOW;
8069 default:
8070 MISSING_CASE(flags);
8071 return 0;
8072 }
8073}
8074
Tom O'Rourke151a49d2014-11-13 18:50:10 -08008075int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008076{
Lyude87660502016-08-17 15:55:53 -04008077 int status;
8078
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008079 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008080
Chris Wilson3f5582d2016-06-30 15:32:45 +01008081 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8082 * use te fw I915_READ variants to reduce the amount of work
8083 * required when reading/writing.
8084 */
8085
8086 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008087 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
8088 return -EAGAIN;
8089 }
8090
Chris Wilson3f5582d2016-06-30 15:32:45 +01008091 I915_WRITE_FW(GEN6_PCODE_DATA, *val);
8092 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
8093 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008094
Chris Wilson3f5582d2016-06-30 15:32:45 +01008095 if (intel_wait_for_register_fw(dev_priv,
8096 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8097 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008098 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
8099 return -ETIMEDOUT;
8100 }
8101
Chris Wilson3f5582d2016-06-30 15:32:45 +01008102 *val = I915_READ_FW(GEN6_PCODE_DATA);
8103 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008104
Lyude87660502016-08-17 15:55:53 -04008105 if (INTEL_GEN(dev_priv) > 6)
8106 status = gen7_check_mailbox_status(dev_priv);
8107 else
8108 status = gen6_check_mailbox_status(dev_priv);
8109
8110 if (status) {
8111 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed: %d\n",
8112 status);
8113 return status;
8114 }
8115
Ben Widawsky42c05262012-09-26 10:34:00 -07008116 return 0;
8117}
8118
Chris Wilson3f5582d2016-06-30 15:32:45 +01008119int sandybridge_pcode_write(struct drm_i915_private *dev_priv,
Lyude87660502016-08-17 15:55:53 -04008120 u32 mbox, u32 val)
Ben Widawsky42c05262012-09-26 10:34:00 -07008121{
Lyude87660502016-08-17 15:55:53 -04008122 int status;
8123
Jesse Barnes4fc688c2012-11-02 11:14:01 -07008124 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
Ben Widawsky42c05262012-09-26 10:34:00 -07008125
Chris Wilson3f5582d2016-06-30 15:32:45 +01008126 /* GEN6_PCODE_* are outside of the forcewake domain, we can
8127 * use te fw I915_READ variants to reduce the amount of work
8128 * required when reading/writing.
8129 */
8130
8131 if (I915_READ_FW(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008132 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
8133 return -EAGAIN;
8134 }
8135
Chris Wilson3f5582d2016-06-30 15:32:45 +01008136 I915_WRITE_FW(GEN6_PCODE_DATA, val);
Imre Deak8bf41b72016-11-28 17:29:27 +02008137 I915_WRITE_FW(GEN6_PCODE_DATA1, 0);
Chris Wilson3f5582d2016-06-30 15:32:45 +01008138 I915_WRITE_FW(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
Ben Widawsky42c05262012-09-26 10:34:00 -07008139
Chris Wilson3f5582d2016-06-30 15:32:45 +01008140 if (intel_wait_for_register_fw(dev_priv,
8141 GEN6_PCODE_MAILBOX, GEN6_PCODE_READY, 0,
8142 500)) {
Ben Widawsky42c05262012-09-26 10:34:00 -07008143 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
8144 return -ETIMEDOUT;
8145 }
8146
Chris Wilson3f5582d2016-06-30 15:32:45 +01008147 I915_WRITE_FW(GEN6_PCODE_DATA, 0);
Ben Widawsky42c05262012-09-26 10:34:00 -07008148
Lyude87660502016-08-17 15:55:53 -04008149 if (INTEL_GEN(dev_priv) > 6)
8150 status = gen7_check_mailbox_status(dev_priv);
8151 else
8152 status = gen6_check_mailbox_status(dev_priv);
8153
8154 if (status) {
8155 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed: %d\n",
8156 status);
8157 return status;
8158 }
8159
Ben Widawsky42c05262012-09-26 10:34:00 -07008160 return 0;
8161}
Jesse Barnesa0e4e192013-04-02 11:23:05 -07008162
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008163static bool skl_pcode_try_request(struct drm_i915_private *dev_priv, u32 mbox,
8164 u32 request, u32 reply_mask, u32 reply,
8165 u32 *status)
8166{
8167 u32 val = request;
8168
8169 *status = sandybridge_pcode_read(dev_priv, mbox, &val);
8170
8171 return *status || ((val & reply_mask) == reply);
8172}
8173
8174/**
8175 * skl_pcode_request - send PCODE request until acknowledgment
8176 * @dev_priv: device private
8177 * @mbox: PCODE mailbox ID the request is targeted for
8178 * @request: request ID
8179 * @reply_mask: mask used to check for request acknowledgment
8180 * @reply: value used to check for request acknowledgment
8181 * @timeout_base_ms: timeout for polling with preemption enabled
8182 *
8183 * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
Imre Deak01299362017-02-24 16:32:10 +02008184 * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008185 * The request is acknowledged once the PCODE reply dword equals @reply after
8186 * applying @reply_mask. Polling is first attempted with preemption enabled
Imre Deak01299362017-02-24 16:32:10 +02008187 * for @timeout_base_ms and if this times out for another 50 ms with
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008188 * preemption disabled.
8189 *
8190 * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
8191 * other error as reported by PCODE.
8192 */
8193int skl_pcode_request(struct drm_i915_private *dev_priv, u32 mbox, u32 request,
8194 u32 reply_mask, u32 reply, int timeout_base_ms)
8195{
8196 u32 status;
8197 int ret;
8198
8199 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
8200
8201#define COND skl_pcode_try_request(dev_priv, mbox, request, reply_mask, reply, \
8202 &status)
8203
8204 /*
8205 * Prime the PCODE by doing a request first. Normally it guarantees
8206 * that a subsequent request, at most @timeout_base_ms later, succeeds.
8207 * _wait_for() doesn't guarantee when its passed condition is evaluated
8208 * first, so send the first request explicitly.
8209 */
8210 if (COND) {
8211 ret = 0;
8212 goto out;
8213 }
8214 ret = _wait_for(COND, timeout_base_ms * 1000, 10);
8215 if (!ret)
8216 goto out;
8217
8218 /*
8219 * The above can time out if the number of requests was low (2 in the
8220 * worst case) _and_ PCODE was busy for some reason even after a
8221 * (queued) request and @timeout_base_ms delay. As a workaround retry
8222 * the poll with preemption disabled to maximize the number of
Imre Deak01299362017-02-24 16:32:10 +02008223 * requests. Increase the timeout from @timeout_base_ms to 50ms to
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008224 * account for interrupts that could reduce the number of these
Imre Deak01299362017-02-24 16:32:10 +02008225 * requests, and for any quirks of the PCODE firmware that delays
8226 * the request completion.
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008227 */
8228 DRM_DEBUG_KMS("PCODE timeout, retrying with preemption disabled\n");
8229 WARN_ON_ONCE(timeout_base_ms > 3);
8230 preempt_disable();
Imre Deak01299362017-02-24 16:32:10 +02008231 ret = wait_for_atomic(COND, 50);
Imre Deaka0b8a1f2016-12-05 18:27:37 +02008232 preempt_enable();
8233
8234out:
8235 return ret ? ret : status;
8236#undef COND
8237}
8238
Ville Syrjälädd06f882014-11-10 22:55:12 +02008239static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
8240{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008241 /*
8242 * N = val - 0xb7
8243 * Slow = Fast = GPLL ref * N
8244 */
8245 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008246}
8247
Fengguang Wub55dd642014-07-12 11:21:39 +02008248static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008249{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008250 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
Jesse Barnes855ba3b2013-04-17 15:54:57 -07008251}
8252
Fengguang Wub55dd642014-07-12 11:21:39 +02008253static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308254{
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008255 /*
8256 * N = val / 2
8257 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
8258 */
8259 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
Deepak S22b1b2f2014-07-12 14:54:33 +05308260}
8261
Fengguang Wub55dd642014-07-12 11:21:39 +02008262static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
Deepak S22b1b2f2014-07-12 14:54:33 +05308263{
Ville Syrjälä1c147622014-08-18 14:42:43 +03008264 /* CHV needs even values */
Ville Syrjäläc30fec62016-03-04 21:43:02 +02008265 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
Deepak S22b1b2f2014-07-12 14:54:33 +05308266}
8267
Ville Syrjälä616bc822015-01-23 21:04:25 +02008268int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
8269{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008270 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008271 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
8272 GEN9_FREQ_SCALER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008273 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008274 return chv_gpu_freq(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008275 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008276 return byt_gpu_freq(dev_priv, val);
8277 else
8278 return val * GT_FREQUENCY_MULTIPLIER;
8279}
8280
Ville Syrjälä616bc822015-01-23 21:04:25 +02008281int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
8282{
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008283 if (IS_GEN9(dev_priv))
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008284 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
8285 GT_FREQUENCY_MULTIPLIER);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008286 else if (IS_CHERRYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008287 return chv_freq_opcode(dev_priv, val);
Joonas Lahtinen2d1fe072016-04-07 11:08:05 +03008288 else if (IS_VALLEYVIEW(dev_priv))
Ville Syrjälä616bc822015-01-23 21:04:25 +02008289 return byt_freq_opcode(dev_priv, val);
8290 else
Mika Kuoppala500a3d22015-11-13 19:29:41 +02008291 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
Deepak S22b1b2f2014-07-12 14:54:33 +05308292}
8293
Chris Wilson6ad790c2015-04-07 16:20:31 +01008294struct request_boost {
8295 struct work_struct work;
Daniel Vettereed29a52015-05-21 14:21:25 +02008296 struct drm_i915_gem_request *req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008297};
8298
8299static void __intel_rps_boost_work(struct work_struct *work)
8300{
8301 struct request_boost *boost = container_of(work, struct request_boost, work);
Chris Wilsone61b9952015-04-27 13:41:24 +01008302 struct drm_i915_gem_request *req = boost->req;
Chris Wilson6ad790c2015-04-07 16:20:31 +01008303
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008304 if (!i915_gem_request_completed(req))
Chris Wilsonc0336662016-05-06 15:40:21 +01008305 gen6_rps_boost(req->i915, NULL, req->emitted_jiffies);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008306
Chris Wilsone8a261e2016-07-20 13:31:49 +01008307 i915_gem_request_put(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008308 kfree(boost);
8309}
8310
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008311void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008312{
8313 struct request_boost *boost;
8314
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008315 if (req == NULL || INTEL_GEN(req->i915) < 6)
Chris Wilson6ad790c2015-04-07 16:20:31 +01008316 return;
8317
Chris Wilsonf69a02c2016-07-01 17:23:16 +01008318 if (i915_gem_request_completed(req))
Chris Wilsone61b9952015-04-27 13:41:24 +01008319 return;
8320
Chris Wilson6ad790c2015-04-07 16:20:31 +01008321 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
8322 if (boost == NULL)
8323 return;
8324
Chris Wilsone8a261e2016-07-20 13:31:49 +01008325 boost->req = i915_gem_request_get(req);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008326
8327 INIT_WORK(&boost->work, __intel_rps_boost_work);
Tvrtko Ursulin91d14252016-05-06 14:48:28 +01008328 queue_work(req->i915->wq, &boost->work);
Chris Wilson6ad790c2015-04-07 16:20:31 +01008329}
8330
Tvrtko Ursulin192aa182016-12-01 14:16:45 +00008331void intel_pm_setup(struct drm_i915_private *dev_priv)
Chris Wilson907b28c2013-07-19 20:36:52 +01008332{
Daniel Vetterf742a552013-12-06 10:17:53 +01008333 mutex_init(&dev_priv->rps.hw_lock);
Chris Wilson8d3afd72015-05-21 21:01:47 +01008334 spin_lock_init(&dev_priv->rps.client_lock);
Daniel Vetterf742a552013-12-06 10:17:53 +01008335
Chris Wilson54b4f682016-07-21 21:16:19 +01008336 INIT_DELAYED_WORK(&dev_priv->rps.autoenable_work,
8337 __intel_autoenable_gt_powersave);
Chris Wilson1854d5c2015-04-07 16:20:32 +01008338 INIT_LIST_HEAD(&dev_priv->rps.clients);
Paulo Zanoni5d584b22014-03-07 20:08:15 -03008339
Paulo Zanoni33688d92014-03-07 20:08:19 -03008340 dev_priv->pm.suspended = false;
Imre Deak1f814da2015-12-16 02:52:19 +02008341 atomic_set(&dev_priv->pm.wakeref_count, 0);
Chris Wilson907b28c2013-07-19 20:36:52 +01008342}